rise8-us / neverl8

0 stars 0 forks source link

Test Containers #21

Closed jwills-r8 closed 7 months ago

jwills-r8 commented 7 months ago

As a Developer I want persistence layer tests to have their own DB instance for tests So that the db state is isolated and always known, so I can more easily identify problems

See:
https://golang.testcontainers.org/ https://golang.testcontainers.org/modules/postgres/

jwills-r8 commented 7 months ago

I set this up in a test repo, Including it here for reference

package test_util

import (
    "context"
    "fmt"
    "github.com/docker/go-connections/nat"
    "github.com/jinzhu/gorm"
    "github.com/testcontainers/testcontainers-go"
    "github.com/testcontainers/testcontainers-go/modules/postgres"
    "github.com/testcontainers/testcontainers-go/wait"
    "log"
    "time"
)

type TestDb struct {
    Db        *gorm.DB
    container testcontainers.Container
}

func SetupTestDb() *TestDb {
    ctx := context.Background()
    container, port, _ := createContainer(ctx)
    db, err := gorm.Open("postgres", fmt.Sprintf("host=localhost user=user password=password dbname=testdb sslmode=disable port=%s", port.Port()))
    if err != nil {
        log.Fatalf("####Failed to connect to the database: %s", err)
    }
    return &TestDb{Db: db, container: container}
}

func (tdb *TestDb) TearDown() {
    tdb.Db.Close()
    // remove test container
    _ = tdb.container.Terminate(context.Background())
}

func createContainer(ctx context.Context) (testcontainers.Container, nat.Port, error) {
    dbName := "testdb"
    dbUser := "user"
    dbPassword := "password"

    postgresContainer, err := postgres.RunContainer(ctx,
        testcontainers.WithImage("docker.io/postgres:15.2-alpine"),
        postgres.WithDatabase(dbName),
        postgres.WithUsername(dbUser),
        postgres.WithPassword(dbPassword),
        testcontainers.WithWaitStrategy(
            wait.ForLog("database system is ready to accept connections").
                WithOccurrence(2).
                WithStartupTimeout(5*time.Second)),
    )

    // Define the container request

    if err != nil {
        log.Fatalf("Failed to start container: %s", err)
    }

    // Get the container's mapped port
    mappedPort, err := postgresContainer.MappedPort(ctx, "5432")
    if err != nil {
        log.Fatalf("Failed to get mapped port: %s", err)
    }

    return postgresContainer, mappedPort, nil
}

In use

package repository_test

import (
    "github.com/jinzhu/gorm"
    _ "github.com/jinzhu/gorm/dialects/postgres"
    "github.com/rise8-us/activ8/model"
    "github.com/rise8-us/activ8/repository"
    "github.com/rise8-us/activ8/test_util"
    "github.com/stretchr/testify/assert"
    "os"
    "testing"
    "time"
)

var db *gorm.DB

func TestMain(m *testing.M) {
    testDB := test_util.SetupTestDb()
    db = testDB.Db
    //TODO: Look at moving to go-migrate an running migrations as part of SetupTestDb()
    //setup DB
    db.AutoMigrate(&model.Person{})
    defer testDB.TearDown()
    os.Exit(m.Run())
}

func TestCreatePerson(t *testing.T) {

    repo := repository.NewPersonRepository(db)

    // Mock test data
    person := &model.Person{
        Email:       "a.b@com",
        FirstName:   "Jeff",
        LastName:    "Wills",
        DisplayName: "Zoji",
    }

    // Call the CreatePerson function
    createdPerson, err := repo.CreatePerson(person)

    // Assert that no error occurred
    assert.NoError(t, err, "expected no error")
    // Assert that the created person matches the input person
    assert.NotNil(t, createdPerson, "expected person to be created")
    assert.Equal(t, createdPerson.Email, person.Email, "expected calendar to match")
    assert.Equal(t, uint(1), person.ID, "expected user id to be 1")
    assert.Equal(t, time.Now().Minute(), person.CreatedAt.Minute(), "expected created at to be set")
}
drewfugate commented 7 months ago

Implemented in PR #24