diff options
| author | Eugene Triguba <eugenetriguba@gmail.com> | 2024-01-10 22:01:56 -0500 |
|---|---|---|
| committer | Eugene Triguba <eugenetriguba@gmail.com> | 2024-01-10 22:01:56 -0500 |
| commit | 0a0b911a15202214ee0421a6d71ed6a84f1461c9 (patch) | |
| tree | 4e7d7e534ecd3ddff13bfee5c702dc6c1703d7db | |
| parent | Fix fs repo create and tweak tests (diff) | |
| download | cli-0a0b911a15202214ee0421a6d71ed6a84f1461c9.zip | |
Tweak tests
| -rw-r--r-- | internal/bolttest/db.go | 35 | ||||
| -rw-r--r-- | internal/repositories/migration_db_repo.go | 8 | ||||
| -rw-r--r-- | internal/repositories/migration_db_repo_test.go | 185 | ||||
| -rw-r--r-- | internal/repositories/migration_fs_repo_test.go | 12 | ||||
| -rw-r--r-- | internal/storage/db_test.go | 8 |
5 files changed, 233 insertions, 15 deletions
diff --git a/internal/bolttest/db.go b/internal/bolttest/db.go index 191605b..de2dcfa 100644 --- a/internal/bolttest/db.go +++ b/internal/bolttest/db.go @@ -1,17 +1,19 @@ package bolttest import ( + "database/sql" "os" "strconv" + "testing" "github.com/eugenetriguba/bolt/internal/configloader" + "github.com/eugenetriguba/bolt/internal/storage" + "gotest.tools/v3/assert" ) -func NewTestConnectionConfig(driver string) (*configloader.ConnectionConfig, error) { +func NewTestConnectionConfig(t *testing.T, driver string) *configloader.ConnectionConfig { port, err := strconv.Atoi(os.Getenv("DB_PORT")) - if err != nil { - return nil, err - } + assert.NilError(t, err) return &configloader.ConnectionConfig{ Driver: driver, @@ -20,5 +22,28 @@ func NewTestConnectionConfig(driver string) (*configloader.ConnectionConfig, err Port: port, User: os.Getenv("DB_USERNAME"), Password: os.Getenv("DB_PASSWORD"), - }, nil + } +} + +func NewTestDB(t *testing.T, driver string) *sql.DB { + connectionConfig := NewTestConnectionConfig(t, driver) + connInfo := storage.DBConnectionString(connectionConfig) + db, err := storage.DBConnect(driver, connInfo) + assert.NilError(t, err) + t.Cleanup(func() { + _, err = db.Exec(` + DO $$ DECLARE rec RECORD; + BEGIN FOR rec IN ( + SELECT table_name + FROM information_schema.tables + WHERE table_schema = 'public' + ) LOOP EXECUTE 'DROP TABLE IF EXISTS ' || rec.table_name || ' CASCADE'; + END LOOP; + END $$; + `) + assert.NilError(t, err) + err = db.Close() + assert.NilError(t, err) + }) + return db } diff --git a/internal/repositories/migration_db_repo.go b/internal/repositories/migration_db_repo.go index 5571ab3..7f8fc10 100644 --- a/internal/repositories/migration_db_repo.go +++ b/internal/repositories/migration_db_repo.go @@ -47,8 +47,9 @@ func (mr *MigrationDBRepo) List() (map[string]*models.Migration, error) { if err != nil { return nil, err } - migrations[version] = &models.Migration{ - Version: strings.TrimSpace(version), + trimmedVersion := strings.TrimSpace(version) + migrations[trimmedVersion] = &models.Migration{ + Version: trimmedVersion, // Note: We don't store the user-friendly message for // the migration in the database. It's purely for the // user to understand what the migration was locally. @@ -67,7 +68,8 @@ func (mr *MigrationDBRepo) List() (map[string]*models.Migration, error) { // Check err first before looking at whether the version is applied. func (mr *MigrationDBRepo) IsApplied(version string) (applied bool, err error) { row := mr.db.QueryRow(`SELECT 1 FROM bolt_migrations WHERE version = $1`, version) - err = row.Scan() + var scanResult int + err = row.Scan(&scanResult) if errors.Is(err, sql.ErrNoRows) { return false, nil } diff --git a/internal/repositories/migration_db_repo_test.go b/internal/repositories/migration_db_repo_test.go new file mode 100644 index 0000000..264b3be --- /dev/null +++ b/internal/repositories/migration_db_repo_test.go @@ -0,0 +1,185 @@ +package repositories_test + +import ( + "database/sql" + "testing" + "time" + + "github.com/eugenetriguba/bolt/internal/bolttest" + "github.com/eugenetriguba/bolt/internal/models" + "github.com/eugenetriguba/bolt/internal/repositories" + "gotest.tools/v3/assert" +) + +func TestMigrationDBRepo_NewMigrationDBRepoCreatesTable(t *testing.T) { + db := bolttest.NewTestDB(t, "postgres") + row := db.QueryRow(` + SELECT 1 + FROM INFORMATION_SCHEMA.TABLES + WHERE TABLE_SCHEMA = 'public' + AND TABLE_NAME = 'bolt_migrations' + `) + var scanResult int + err := row.Scan(&scanResult) + assert.ErrorIs(t, err, sql.ErrNoRows) + + _, err = repositories.NewMigrationDBRepo(db) + assert.NilError(t, err) + + row = db.QueryRow(` + SELECT 1 + FROM INFORMATION_SCHEMA.TABLES + WHERE TABLE_SCHEMA = 'public' + AND TABLE_NAME = 'bolt_migrations' + `) + err = row.Scan(&scanResult) + assert.NilError(t, err) +} + +func TestMigrationDBRepo_NewMigrationDBRepoLeavesCurrentTable(t *testing.T) { + db := bolttest.NewTestDB(t, "postgres") + _, err := db.Exec(`CREATE TABLE bolt_migrations(id INT NOT NULL PRIMARY KEY)`) + assert.NilError(t, err) + _, err = db.Exec(`INSERT INTO bolt_migrations(id) VALUES (1);`) + assert.NilError(t, err) + + _, err = repositories.NewMigrationDBRepo(db) + assert.NilError(t, err) + + row := db.QueryRow(`SELECT id FROM bolt_migrations`) + var scanResult int + err = row.Scan(&scanResult) + assert.NilError(t, err) + assert.Equal(t, scanResult, 1) +} + +func TestMigrationDBRepo_ListWithEmptyTable(t *testing.T) { + db := bolttest.NewTestDB(t, "postgres") + repo, err := repositories.NewMigrationDBRepo(db) + assert.NilError(t, err) + + migrations, err := repo.List() + assert.NilError(t, err) + assert.Equal(t, len(migrations), 0) +} + +func TestMigrationDBRepo_ListWithSingleResult(t *testing.T) { + db := bolttest.NewTestDB(t, "postgres") + repo, err := repositories.NewMigrationDBRepo(db) + assert.NilError(t, err) + + version := "20230101000000" + _, err = db.Exec(`INSERT INTO bolt_migrations(version) VALUES ($1);`, version) + assert.NilError(t, err) + + migrations, err := repo.List() + assert.NilError(t, err) + assert.Equal(t, len(migrations), 1) + assert.DeepEqual( + t, + migrations[version], + &models.Migration{Version: version, Message: "", Applied: true}, + ) +} + +func TestMigrationDBRepo_ListWithShortVersion(t *testing.T) { + db := bolttest.NewTestDB(t, "postgres") + repo, err := repositories.NewMigrationDBRepo(db) + assert.NilError(t, err) + + version := "20230101" + _, err = db.Exec(`INSERT INTO bolt_migrations(version) VALUES ($1);`, version) + assert.NilError(t, err) + + migrations, err := repo.List() + assert.NilError(t, err) + assert.Equal(t, len(migrations), 1) + assert.DeepEqual( + t, + migrations[version], + &models.Migration{Version: version, Message: "", Applied: true}, + ) +} + +func TestMigrationDBRepo_IsAppliedWithNotApplied(t *testing.T) { + db := bolttest.NewTestDB(t, "postgres") + repo, err := repositories.NewMigrationDBRepo(db) + assert.NilError(t, err) + + version := "20230101010101" + applied, err := repo.IsApplied(version) + assert.NilError(t, err) + assert.Equal(t, applied, false) +} + +func TestMigrationDBRepo_IsAppliedWithApplied(t *testing.T) { + db := bolttest.NewTestDB(t, "postgres") + repo, err := repositories.NewMigrationDBRepo(db) + assert.NilError(t, err) + + version := "20230101010101" + _, err = db.Exec(`INSERT INTO bolt_migrations(version) VALUES ($1);`, version) + assert.NilError(t, err) + + applied, err := repo.IsApplied(version) + assert.NilError(t, err) + assert.Equal(t, applied, true) +} + +func TestMigrationDBRepo_Apply(t *testing.T) { + db := bolttest.NewTestDB(t, "postgres") + repo, err := repositories.NewMigrationDBRepo(db) + assert.NilError(t, err) + + migration := models.NewMigration(time.Now(), "test") + err = repo.Apply(`CREATE TABLE tmp(id INT NOT NULL PRIMARY KEY)`, migration) + assert.NilError(t, err) + assert.Equal(t, migration.Applied, true) + + row := db.QueryRow(` + SELECT 1 + FROM INFORMATION_SCHEMA.TABLES + WHERE TABLE_SCHEMA = 'public' + AND TABLE_NAME = 'tmp' + `) + var scanResult int + err = row.Scan(&scanResult) + assert.NilError(t, err) + applied, err := repo.IsApplied(migration.Version) + assert.NilError(t, err) + assert.Equal(t, applied, true) +} + +func TestMigrationDBRepo_Revert(t *testing.T) { + db := bolttest.NewTestDB(t, "postgres") + repo, err := repositories.NewMigrationDBRepo(db) + assert.NilError(t, err) + + _, err = db.Exec(`CREATE TABLE tmp(id INT NOT NULL PRIMARY KEY)`) + assert.NilError(t, err) + + migration := models.NewMigration(time.Now(), "test") + _, err = db.Exec(`INSERT INTO bolt_migrations(version) VALUES ($1);`, migration.Version) + assert.NilError(t, err) + migration.Applied = true + + err = repo.Revert(`DROP TABLE tmp;`, migration) + assert.NilError(t, err) + assert.Equal(t, migration.Applied, false) + + row := db.QueryRow(` + SELECT 1 + FROM INFORMATION_SCHEMA.TABLES + WHERE TABLE_SCHEMA = 'public' + AND TABLE_NAME = 'tmp' + `) + var scanResult int + err = row.Scan(&scanResult) + assert.ErrorIs(t, err, sql.ErrNoRows) + + row = db.QueryRow(`SELECT count(*) FROM bolt_migrations`) + var count int + err = row.Scan(&count) + assert.NilError(t, err) + assert.Equal(t, count, 0) +} diff --git a/internal/repositories/migration_fs_repo_test.go b/internal/repositories/migration_fs_repo_test.go index b4b03e6..a0448a5 100644 --- a/internal/repositories/migration_fs_repo_test.go +++ b/internal/repositories/migration_fs_repo_test.go @@ -63,9 +63,17 @@ func TestMigrationFsRepoReadUpgradeAndDowngradeScript(t *testing.T) { migrationDirName := fmt.Sprintf("%s_add_users_table", migration.Version) expectedUpgradeScriptContents := []byte("CREATE TABLE users(id int PRIMARY KEY);") - os.WriteFile(filepath.Join(tempDir, migrationDirName, "upgrade.sql"), expectedUpgradeScriptContents, 0755) + os.WriteFile( + filepath.Join(tempDir, migrationDirName, "upgrade.sql"), + expectedUpgradeScriptContents, + 0755, + ) expectedDowngradeScriptContents := []byte("DROP TABLE users;") - os.WriteFile(filepath.Join(tempDir, migrationDirName, "downgrade.sql"), expectedDowngradeScriptContents, 0755) + os.WriteFile( + filepath.Join(tempDir, migrationDirName, "downgrade.sql"), + expectedDowngradeScriptContents, + 0755, + ) upgradeScriptContents, err := repo.ReadUpgradeScript(migration) assert.NilError(t, err) diff --git a/internal/storage/db_test.go b/internal/storage/db_test.go index 377c8c8..d9e22d0 100644 --- a/internal/storage/db_test.go +++ b/internal/storage/db_test.go @@ -11,8 +11,7 @@ import ( ) func TestDBConnectWithPostgres(t *testing.T) { - cfg, err := bolttest.NewTestConnectionConfig("postgres") - assert.NilError(t, err) + cfg := bolttest.NewTestConnectionConfig(t, "postgres") conn, err := storage.DBConnect(cfg.Driver, storage.DBConnectionString(cfg)) assert.NilError(t, err) @@ -28,10 +27,9 @@ func TestDBConnectMalformedConnectionString(t *testing.T) { } func TestDBConnectUnsupportedDriver(t *testing.T) { - cfg, err := bolttest.NewTestConnectionConfig("redis") - assert.NilError(t, err) + cfg := bolttest.NewTestConnectionConfig(t, "redis") - _, err = storage.DBConnect(cfg.Driver, storage.DBConnectionString(cfg)) + _, err := storage.DBConnect(cfg.Driver, storage.DBConnectionString(cfg)) assert.ErrorContains(t, err, `unknown driver "redis"`) } |