Git Product home page Git Product logo

Comments (2)

jwills-r8 avatar jwills-r8 commented on August 22, 2024 1

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")
}

from neverl8.

drewfugate avatar drewfugate commented on August 22, 2024

Implemented in PR #24

from neverl8.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.