-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5 from programme-lv/dev
database integration testing
- Loading branch information
Showing
10 changed files
with
643 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package database | ||
|
||
import ( | ||
"log" | ||
"os" | ||
"sync" | ||
"testing" | ||
|
||
"github.com/programme-lv/backend/internal/database/testdb" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
var ( | ||
once sync.Once | ||
dbProvider testdb.TestDBProvider | ||
) | ||
|
||
func TestMain(m *testing.M) { | ||
var err error | ||
dbProvider, err = testdb.NewPostgresTestcontainerProvider() | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
exitCode := m.Run() | ||
dbProvider.Close() | ||
os.Exit(exitCode) | ||
} | ||
|
||
func TestCreateUser(t *testing.T) { | ||
db := dbProvider.GetTestDB() | ||
assert.NotNil(t, db) | ||
|
||
username := "username" | ||
password := "password" | ||
email := "email@gmail.com" | ||
firstName := "firstName" | ||
lastName := "lastName" | ||
|
||
assert.Nil(t, CreateUser(db, username, hashPassword(password), email, firstName, lastName)) | ||
|
||
user, err := SelectUserByUsername(db, username) | ||
assert.Nil(t, err) | ||
|
||
assert.Equal(t, user.Username, username) | ||
assert.Equal(t, user.Email, email) | ||
assert.Equal(t, user.FirstName, firstName) | ||
assert.Equal(t, user.LastName, lastName) | ||
assert.Equal(t, user.IsAdmin, false) | ||
|
||
assert.True(t, passwordsMatch(user.HashedPassword, password)) | ||
|
||
assert.Nil(t, DeleteUserById(db, user.ID)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package testdb | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"io" | ||
"log" | ||
|
||
"github.com/testcontainers/testcontainers-go" | ||
"github.com/testcontainers/testcontainers-go/wait" | ||
) | ||
|
||
type flywayLogConsumer struct{} | ||
|
||
func (consumer *flywayLogConsumer) Accept(l testcontainers.Log) { | ||
fmt.Print(string(l.Content)) | ||
} | ||
|
||
// exectues flyway:21 on host network with migrationsDir mounted to /flyway/flyway-migrations | ||
func execFlywayContainer(networkName string, networkAlias string, migrationDir string, dbHost string, dbPort string, dbName string, dbUser string, dbPassword string) error { | ||
args := []string{ | ||
fmt.Sprintf("-url=jdbc:postgresql://%s:%s/%s", dbHost, dbPort, dbName), | ||
fmt.Sprintf("-user=%s", dbUser), | ||
fmt.Sprintf("-password=%s", dbPassword), | ||
"-connectRetries=5", | ||
"-locations=filesystem:/flyway/flyway-migrations", | ||
"migrate", | ||
} | ||
|
||
req := testcontainers.ContainerRequest{ | ||
Image: "flyway/flyway:9.21", | ||
Mounts: testcontainers.ContainerMounts{ | ||
testcontainers.ContainerMount{ | ||
Source: testcontainers.GenericBindMountSource{ | ||
HostPath: migrationDir, | ||
}, | ||
Target: "/flyway/flyway-migrations", | ||
}, | ||
}, | ||
WaitingFor: wait.ForAll(wait.ForExit()), | ||
Cmd: args, | ||
Networks: []string{networkName}, | ||
NetworkAliases: map[string][]string{ | ||
networkName: {networkAlias}, | ||
}, | ||
} | ||
|
||
c, err := testcontainers.GenericContainer(context.Background(), | ||
testcontainers.GenericContainerRequest{ | ||
ContainerRequest: req, | ||
Started: false, | ||
}) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
err = c.Start(context.Background()) | ||
if err != nil { | ||
log.Printf("Error starting flyway container: %v", err) | ||
} | ||
cLogs, err := c.Logs(context.Background()) | ||
if err != nil { | ||
log.Printf("Error getting flyway container logs: %v", err) | ||
} | ||
logs, err := io.ReadAll(cLogs) | ||
if err != nil { | ||
log.Printf("Error reading flyway container logs: %v", err) | ||
} | ||
log.Println(string(logs)) | ||
return err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package testdb | ||
|
||
import ( | ||
"log" | ||
"os" | ||
"path/filepath" | ||
|
||
git "github.com/go-git/go-git/v5" | ||
) | ||
|
||
type DBMigrations struct { | ||
rootDir string | ||
} | ||
|
||
func cloneGitDBMigrations() (*DBMigrations, error) { | ||
tmpDir, err := os.MkdirTemp("", "proglv-db-migrations") | ||
if err != nil { | ||
return nil, err | ||
} | ||
repoUrl := "https://github.com/programme-lv/database" | ||
|
||
_, err = git.PlainClone(tmpDir, false, &git.CloneOptions{ | ||
URL: repoUrl, | ||
Progress: os.Stdout, | ||
}) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
res := &DBMigrations{ | ||
rootDir: tmpDir, | ||
} | ||
return res, nil | ||
} | ||
|
||
func (dbm *DBMigrations) getFlywayMigrationsDir() string { | ||
return filepath.Join(dbm.rootDir, "flyway-migrations") | ||
} | ||
|
||
func (dbm *DBMigrations) erase() { | ||
err := os.RemoveAll(dbm.rootDir) | ||
if err != nil { | ||
log.Printf("Failed to remove tmp dir: %v", err) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package testdb | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/testcontainers/testcontainers-go" | ||
"github.com/testcontainers/testcontainers-go/wait" | ||
) | ||
|
||
type postgresContainer struct { | ||
container testcontainers.Container | ||
user string | ||
password string | ||
database string | ||
} | ||
|
||
func startPostgresContainer(networkName string, networkAlias string, user string, pass string, dbName string) (*postgresContainer, error) { | ||
req := testcontainers.ContainerRequest{ | ||
Image: "postgres:latest", | ||
ExposedPorts: []string{"5432/tcp"}, | ||
Env: map[string]string{ | ||
"POSTGRES_USER": user, | ||
"POSTGRES_PASSWORD": pass, | ||
"POSTGRES_DB": dbName, | ||
}, | ||
WaitingFor: wait.ForAll( | ||
wait.ForLog("database system is ready to accept connections"), | ||
wait.ForExposedPort(), | ||
), | ||
Networks: []string{networkName}, | ||
NetworkAliases: map[string][]string{ | ||
networkName: {networkAlias}, | ||
}, | ||
} | ||
|
||
container, err := testcontainers.GenericContainer(context.Background(), | ||
testcontainers.GenericContainerRequest{ | ||
ContainerRequest: req, | ||
Started: true, | ||
}) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
res := &postgresContainer{ | ||
container: container, | ||
user: user, | ||
password: pass, | ||
database: dbName, | ||
} | ||
return res, nil | ||
} |
Oops, something went wrong.