Skip to content

Commit

Permalink
Introduce Gin web framework in codebase
Browse files Browse the repository at this point in the history
  • Loading branch information
hthunberg committed Oct 28, 2023
1 parent ed6a996 commit 63c30a1
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 27 deletions.
11 changes: 5 additions & 6 deletions dbtest/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"errors"
"fmt"
"log"
"os"
"time"

"github.com/docker/go-connections/nat"
Expand Down Expand Up @@ -38,7 +37,7 @@ type TestDatabase struct {
Container testcontainers.Container
}

func SetupTestDatabase(ctx context.Context, testDatabaseContainerRequest testcontainers.GenericContainerRequest) (*TestDatabase, error) {
func SetupTestDatabase(ctx context.Context, testDatabaseContainerRequest testcontainers.GenericContainerRequest, absoluteMigrationsPath string) (*TestDatabase, error) {
// setup db container
container, dbInstance, err := createContainer(ctx, testDatabaseContainerRequest)
if err != nil {
Expand All @@ -58,7 +57,7 @@ func SetupTestDatabase(ctx context.Context, testDatabaseContainerRequest testcon
dbAddr := fmt.Sprintf("%s:%s", dbHost, dbPort.Port())

// migrate db schema
err = migrateDb(dbAddr)
err = migrateDb(dbAddr, absoluteMigrationsPath)
if err != nil {
return nil, fmt.Errorf("setup test bank:migrate db: %v", err)
}
Expand Down Expand Up @@ -152,11 +151,11 @@ func createContainer(ctx context.Context, req testcontainers.GenericContainerReq
return container, db, nil
}

func migrateDb(dbAddr string) error {
func migrateDb(dbAddr, absoluteMigrationsPath string) error {
databaseURL := fmt.Sprintf("postgres://%s:%s@%s/%s?sslmode=disable", DBUser, DBPass, dbAddr, DBName)

// file:./<path> to be relative to working directory
migrationsURL := os.Getenv("MIGRATION_URL")
// URL file:./<path> needed fir golang-migrate
migrationsURL := fmt.Sprintf("file:%s", absoluteMigrationsPath)

if len(migrationsURL) < 1 {
return fmt.Errorf("migrate db:missing env migration_url: %s", migrationsURL)
Expand Down
14 changes: 9 additions & 5 deletions internal/app/bank/integration/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,31 @@ package integration

import (
"context"
"fmt"
"log"
"os"
"path/filepath"
"testing"

"github.com/hthunberg/course-golang-postgres-grpc-api/dbtest"
"github.com/hthunberg/course-golang-postgres-grpc-api/internal/app/bank"
"go.uber.org/zap"
)

var testee bank.Bank

func TestMain(m *testing.M) {
os.Setenv("MIGRATION_URL", fmt.Sprintf("file:.%s", "/../../../../build/db/migrations"))

ctx := context.Background()

absoluteMigrationsPath, err := filepath.Abs("../../../../build/db/migrations")
if err != nil {
log.Fatal("failed to calculate absolute path to db migrations", zap.Error(err))
}

// Set up a postgres DB
testDBRequest := dbtest.TestDatabaseContainerRequest()
testDB, err := dbtest.SetupTestDatabase(ctx, testDBRequest)
testDB, err := dbtest.SetupTestDatabase(ctx, testDBRequest, absoluteMigrationsPath)
if err != nil {
log.Fatal("failed to setup postgres db", err)
log.Fatal("failed to setup postgres db", zap.Error(err))
}
defer testDB.TearDown()
testee = bank.NewBank(testDB.DbInstance)
Expand Down
1 change: 0 additions & 1 deletion test/integration/logconsumer.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ func newTestLogConsumer(msgs []string, done chan bool) TestLogConsumer {
}

func (g *TestLogConsumer) Accept(l tc.Log) {
// fmt.Println(string(l.Content))
s := string(l.Content)
if s == fmt.Sprintf("echo %s\n", lastMessage) {
g.Done <- true
Expand Down
17 changes: 11 additions & 6 deletions test/integration/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@ import (
"fmt"
"log"
"os"
"path/filepath"
"testing"
"time"

"github.com/hthunberg/course-golang-postgres-grpc-api/dbtest"
"github.com/jackc/pgx/v5/pgxpool"
"go.uber.org/zap"
)

var (
Expand All @@ -20,8 +22,6 @@ var (
)

func TestMain(m *testing.M) {
os.Setenv("MIGRATION_URL", fmt.Sprintf("file:.%s", "/../../build/db/migrations"))

ctx := context.Background()

// Docker provides the ability for us to create custom networks and place containers on one or more networks.
Expand All @@ -36,23 +36,28 @@ func TestMain(m *testing.M) {
// Alias for postgres db when running inside a custom network,
testDBAlias := "testdb"

absoluteMigrationsPath, err := filepath.Abs("../../build/db/migrations")
if err != nil {
log.Fatal("failed to calculate absolute path to db migrations", zap.Error(err))
}

// Set up a postgres DB
testDBRequest := dbtest.TestDatabaseContainerRequest()
network.ApplyNetworkAlias(&testDBRequest, testDBAlias)
testDB, err := dbtest.SetupTestDatabase(ctx, testDBRequest)
testDB, err := dbtest.SetupTestDatabase(ctx, testDBRequest, absoluteMigrationsPath)
if err != nil {
log.Fatal("failed to setup postgres db", err)
log.Fatal("failed to setup postgres db", zap.Error(err))
}
defer testDB.TearDown()
testDbInstance = testDB.DbInstance

// Set up a test bank
dbAddr := fmt.Sprintf("%s:%s", testDBAlias, dbtest.DBPort)
testBankRequest := TestBankContainerRequest(dbAddr)
testBankRequest := TestBankContainerRequest(dbAddr, absoluteMigrationsPath)
network.ApplyNetworkAlias(&testBankRequest, "testbank")
testBank, err := setupTestBank(ctx, testBankRequest)
if err != nil {
log.Fatal("failed to setup test bank", err)
log.Fatal("failed to setup test bank", zap.Error(err))
}
defer testBank.TearDown()

Expand Down
13 changes: 4 additions & 9 deletions test/integration/testbank.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,32 +49,27 @@ func (tdb *TestBank) TearDown() {
_ = tdb.container.Terminate(context.Background())
}

func TestBankContainerRequest(dbAddr string) tc.GenericContainerRequest {
func TestBankContainerRequest(dbAddr, absoluteMigrationsPath string) tc.GenericContainerRequest {
dbSource := fmt.Sprintf("postgres://%s:%s@%s/%s?sslmode=disable", dbtest.DBUser, dbtest.DBPass, dbAddr, dbtest.DBName)

// TODO: Hans fixa
// file:./<path> to be relative to working directory
// hostPathMigrationsURL := os.Getenv("MIGRATION_URL")
hostPathMigrationsURL := "/Users/hansthunberg/git-views/golang/course-golang-postgres-grpc-api/build/db/migrations"

env := map[string]string{
"ENVIRONMENT": "integrationtest",
"DB_SOURCE": dbSource,
"MIGRATION_URL": "file://migrations",
"MIGRATION_URL": "file://migrations", // Relative to /app/bin/migrations
"LOG_LEVEL": "DEBUG",
}
port := "8080/tcp"

req := tc.GenericContainerRequest{
ContainerRequest: tc.ContainerRequest{
Name: "testbank",
Image: "bank:latest",
Labels: map[string]string{"app": "testbank"},
ExposedPorts: []string{port},
Env: env,
Mounts: tc.ContainerMounts{
tc.ContainerMount{
Source: tc.GenericBindMountSource{
HostPath: hostPathMigrationsURL,
HostPath: absoluteMigrationsPath,
},
Target: tc.ContainerMountTarget("/app/bin/migrations"),
},
Expand Down

0 comments on commit 63c30a1

Please sign in to comment.