Comments (2)
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.
Implemented in PR #24
from neverl8.
Related Issues (20)
- Team Working Agreement HOT 1
- Meeting Host Prefrences HOT 1
- Create Meeeting Types
- Configure Linter/Style checker HOT 1
- See Host details
- Document Personas
- Schedule Meeting HOT 3
- Present available hosts time blocks HOT 1
- Greenhouse Integration
- Meeting Re schedule
- Reschedule as host
- Architecture diagrams
- CI/CD
- conventional commits HOT 4
- Get Access to Google services HOT 2
- Add OAuth2
- DB Migration Management HOT 1
- SPIKE: Api Docs
- SPIKE Add Frontend to mono repo HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from neverl8.