diff options
Diffstat (limited to 'internal/repositories/migration_db_repo_test.go')
| -rw-r--r-- | internal/repositories/migration_db_repo_test.go | 264 |
1 files changed, 188 insertions, 76 deletions
diff --git a/internal/repositories/migration_db_repo_test.go b/internal/repositories/migration_db_repo_test.go index 55b8eb2..2775faa 100644 --- a/internal/repositories/migration_db_repo_test.go +++ b/internal/repositories/migration_db_repo_test.go @@ -2,16 +2,17 @@ package repositories_test import ( "database/sql" + "errors" "testing" "time" "github.com/eugenetriguba/bolt/internal/bolttest" "github.com/eugenetriguba/bolt/internal/models" "github.com/eugenetriguba/bolt/internal/repositories" - "gotest.tools/v3/assert" + "github.com/eugenetriguba/checkmate/assert" ) -func TestMigrationDBRepo_NewMigrationDBRepoCreatesTable(t *testing.T) { +func TestNewMigrationDBRepo_CreatesTable(t *testing.T) { db := bolttest.NewTestDB(t, "postgres") row := db.QueryRow(` SELECT 1 @@ -24,7 +25,7 @@ func TestMigrationDBRepo_NewMigrationDBRepoCreatesTable(t *testing.T) { assert.ErrorIs(t, err, sql.ErrNoRows) _, err = repositories.NewMigrationDBRepo(db) - assert.NilError(t, err) + assert.Nil(t, err) row = db.QueryRow(` SELECT 1 @@ -33,47 +34,75 @@ func TestMigrationDBRepo_NewMigrationDBRepoCreatesTable(t *testing.T) { AND TABLE_NAME = 'bolt_migrations' `) err = row.Scan(&scanResult) - assert.NilError(t, err) + assert.Nil(t, err) } -func TestMigrationDBRepo_NewMigrationDBRepoLeavesCurrentTable(t *testing.T) { +func TestNewMigrationDBRepo_TableAlreadyExists(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) + assert.Nil(t, err) _, err = db.Exec(`INSERT INTO bolt_migrations(id) VALUES (1);`) - assert.NilError(t, err) + assert.Nil(t, err) _, err = repositories.NewMigrationDBRepo(db) - assert.NilError(t, err) + assert.Nil(t, err) row := db.QueryRow(`SELECT id FROM bolt_migrations`) var scanResult int err = row.Scan(&scanResult) - assert.NilError(t, err) + assert.Nil(t, err) assert.Equal(t, scanResult, 1) } -func TestMigrationDBRepo_ListWithEmptyTable(t *testing.T) { +func TestNewMigrationDBRepo_TableCreationErr(t *testing.T) { + expectedErr := errors.New("error!") + db := bolttest.MockSqlDb{ + ExecReturnValue: bolttest.ExecReturnValue{ + Err: expectedErr, + }, + } + + _, err := repositories.NewMigrationDBRepo(&db) + + assert.ErrorIs(t, err, expectedErr) +} + +func TestList_EmptyTable(t *testing.T) { db := bolttest.NewTestDB(t, "postgres") repo, err := repositories.NewMigrationDBRepo(db) - assert.NilError(t, err) + assert.Nil(t, err) migrations, err := repo.List() - assert.NilError(t, err) + assert.Nil(t, err) assert.Equal(t, len(migrations), 0) } -func TestMigrationDBRepo_ListWithSingleResult(t *testing.T) { +func TestList_QueryErr(t *testing.T) { + expectedErr := errors.New("error!") + db := bolttest.MockSqlDb{ + QueryReturnValue: bolttest.QueryReturnValue{ + Err: expectedErr, + }, + } + repo, err := repositories.NewMigrationDBRepo(&db) + assert.Nil(t, err) + + _, err = repo.List() + + assert.ErrorIs(t, err, expectedErr) +} + +func TestList_SingleResult(t *testing.T) { db := bolttest.NewTestDB(t, "postgres") repo, err := repositories.NewMigrationDBRepo(db) - assert.NilError(t, err) + assert.Nil(t, err) version := "20230101000000" _, err = db.Exec(`INSERT INTO bolt_migrations(version) VALUES ($1);`, version) - assert.NilError(t, err) + assert.Nil(t, err) migrations, err := repo.List() - assert.NilError(t, err) + assert.Nil(t, err) assert.Equal(t, len(migrations), 1) assert.DeepEqual( t, @@ -82,17 +111,17 @@ func TestMigrationDBRepo_ListWithSingleResult(t *testing.T) { ) } -func TestMigrationDBRepo_ListWithShortVersion(t *testing.T) { +func TestList_ShortVersion(t *testing.T) { db := bolttest.NewTestDB(t, "postgres") repo, err := repositories.NewMigrationDBRepo(db) - assert.NilError(t, err) + assert.Nil(t, err) version := "20230101" _, err = db.Exec(`INSERT INTO bolt_migrations(version) VALUES ($1);`, version) - assert.NilError(t, err) + assert.Nil(t, err) migrations, err := repo.List() - assert.NilError(t, err) + assert.Nil(t, err) assert.Equal(t, len(migrations), 1) assert.DeepEqual( t, @@ -101,39 +130,101 @@ func TestMigrationDBRepo_ListWithShortVersion(t *testing.T) { ) } -func TestMigrationDBRepo_IsAppliedWithNotApplied(t *testing.T) { +func TestIsApplied_WithNotApplied(t *testing.T) { db := bolttest.NewTestDB(t, "postgres") repo, err := repositories.NewMigrationDBRepo(db) - assert.NilError(t, err) + assert.Nil(t, err) version := "20230101010101" applied, err := repo.IsApplied(version) - assert.NilError(t, err) + assert.Nil(t, err) assert.Equal(t, applied, false) } -func TestMigrationDBRepo_IsAppliedWithApplied(t *testing.T) { +func TestIsApplied_WithApplied(t *testing.T) { db := bolttest.NewTestDB(t, "postgres") repo, err := repositories.NewMigrationDBRepo(db) - assert.NilError(t, err) + assert.Nil(t, err) version := "20230101010101" _, err = db.Exec(`INSERT INTO bolt_migrations(version) VALUES ($1);`, version) - assert.NilError(t, err) + assert.Nil(t, err) applied, err := repo.IsApplied(version) - assert.NilError(t, err) + assert.Nil(t, err) assert.Equal(t, applied, true) } -func TestMigrationDBRepo_Apply(t *testing.T) { +func TestApply(t *testing.T) { db := bolttest.NewTestDB(t, "postgres") repo, err := repositories.NewMigrationDBRepo(db) - assert.NilError(t, err) + assert.Nil(t, err) - migration := models.NewMigration(time.Now(), "test") + migration := models.NewTimestampMigration(time.Now(), "test") err = repo.Apply(`CREATE TABLE tmp(id INT NOT NULL PRIMARY KEY)`, migration) - assert.NilError(t, err) + assert.Nil(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.Nil(t, err) + applied, err := repo.IsApplied(migration.Version) + assert.Nil(t, err) + assert.Equal(t, applied, true) +} + +func TestApply_MalformedSql(t *testing.T) { + db := bolttest.NewTestDB(t, "postgres") + repo, err := repositories.NewMigrationDBRepo(db) + assert.Nil(t, err) + migration := models.NewTimestampMigration(time.Now(), "test") + + err = repo.Apply("this is not SQL", migration) + assert.ErrorContains(t, err, "syntax error") + assert.Equal(t, migration.Applied, false) +} + +func TestApplyWithTx_BeginErr(t *testing.T) { + expectedErr := errors.New("error!") + db := bolttest.MockSqlDb{ + BeginReturnValue: bolttest.BeginReturnValue{ + Err: expectedErr, + }, + } + repo, err := repositories.NewMigrationDBRepo(&db) + assert.Nil(t, err) + migration := models.NewTimestampMigration(time.Now(), "test") + + err = repo.ApplyWithTx("SELECT 1 FROM bolt_migrations;", migration) + + assert.ErrorIs(t, err, expectedErr) +} + +func TestApplyWithTx_ExecErr(t *testing.T) { + db := bolttest.NewTestDB(t, "postgres") + repo, err := repositories.NewMigrationDBRepo(db) + assert.Nil(t, err) + migration := models.NewTimestampMigration(time.Now(), "test") + + err = repo.ApplyWithTx("SELECT 1 FROM abc123donotexist;", migration) + + assert.ErrorContains(t, err, `relation "abc123donotexist" does not exist`) +} + +func TestApplyWithTx_SuccessfullyApplied(t *testing.T) { + db := bolttest.NewTestDB(t, "postgres") + repo, err := repositories.NewMigrationDBRepo(db) + assert.Nil(t, err) + + migration := models.NewTimestampMigration(time.Now(), "test") + err = repo.ApplyWithTx(`CREATE TABLE tmp(id INT NOT NULL PRIMARY KEY)`, migration) + assert.Nil(t, err) assert.Equal(t, migration.Applied, true) row := db.QueryRow(` @@ -144,30 +235,30 @@ func TestMigrationDBRepo_Apply(t *testing.T) { `) var scanResult int err = row.Scan(&scanResult) - assert.NilError(t, err) + assert.Nil(t, err) applied, err := repo.IsApplied(migration.Version) - assert.NilError(t, err) + assert.Nil(t, err) assert.Equal(t, applied, true) } -func TestMigrationDBRepo_Revert(t *testing.T) { +func TestRevert(t *testing.T) { db := bolttest.NewTestDB(t, "postgres") repo, err := repositories.NewMigrationDBRepo(db) - assert.NilError(t, err) + assert.Nil(t, err) _, err = db.Exec(`CREATE TABLE tmp(id INT NOT NULL PRIMARY KEY)`) - assert.NilError(t, err) + assert.Nil(t, err) - migration := models.NewMigration(time.Now(), "test") + migration := models.NewTimestampMigration(time.Now(), "test") _, err = db.Exec( `INSERT INTO bolt_migrations(version) VALUES ($1);`, migration.Version, ) - assert.NilError(t, err) + assert.Nil(t, err) migration.Applied = true err = repo.Revert(`DROP TABLE tmp;`, migration) - assert.NilError(t, err) + assert.Nil(t, err) assert.Equal(t, migration.Applied, false) row := db.QueryRow(` @@ -183,61 +274,82 @@ func TestMigrationDBRepo_Revert(t *testing.T) { row = db.QueryRow(`SELECT count(*) FROM bolt_migrations`) var count int err = row.Scan(&count) - assert.NilError(t, err) + assert.Nil(t, err) assert.Equal(t, count, 0) } -func TestMigrationDBRepo_ApplyAndRevertWithoutTransaction(t *testing.T) { +func TestRevert_MalformedSql(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 PRIMARY KEY, id2 INT, id3 INT)") - assert.NilError(t, err) - - migration := models.NewMigration(time.Now(), "test") - // CREATE INDEX CONCURRENTLY cannot run inside a transaction. - err = repo.Apply( - `-- bolt: no-transaction\CREATE INDEX CONCURRENTLY i1 ON tmp(id2);`, - migration, - ) - assert.NilError(t, err) + assert.Nil(t, err) + migration := models.NewTimestampMigration(time.Now(), "test") + migration.Applied = true + + err = repo.Revert("this is not SQL", migration) + assert.ErrorContains(t, err, "syntax error") assert.Equal(t, migration.Applied, true) +} - err = repo.Revert( - "-- bolt: no-transaction\nCREATE INDEX CONCURRENTLY i2 ON tmp(id3);", - migration, - ) - assert.NilError(t, err) - assert.Equal(t, migration.Applied, false) +func TestRevertWithTx_BeginErr(t *testing.T) { + expectedErr := errors.New("error!") + db := bolttest.MockSqlDb{ + BeginReturnValue: bolttest.BeginReturnValue{ + Err: expectedErr, + }, + } + repo, err := repositories.NewMigrationDBRepo(&db) + assert.Nil(t, err) + migration := models.NewTimestampMigration(time.Now(), "test") + + err = repo.RevertWithTx("DROP TABLE bolt_migrations;", migration) + + assert.ErrorIs(t, err, expectedErr) } -func TestMigrationDBRepo_ApplyMalformedSql(t *testing.T) { +func TestRevertWithTx_ExecErr(t *testing.T) { db := bolttest.NewTestDB(t, "postgres") repo, err := repositories.NewMigrationDBRepo(db) - assert.NilError(t, err) - migration := models.NewMigration(time.Now(), "test") + assert.Nil(t, err) + migration := models.NewTimestampMigration(time.Now(), "test") - err = repo.Apply("this is not SQL", migration) - assert.ErrorContains(t, err, "syntax error") - assert.Equal(t, migration.Applied, false) + err = repo.RevertWithTx("DROP TABLE abc123donotexist;", migration) - err = repo.Apply("-- bolt: no-transaction\nthis is not SQL", migration) - assert.ErrorContains(t, err, "syntax error") - assert.Equal(t, migration.Applied, false) + assert.ErrorContains(t, err, `table "abc123donotexist" does not exist`) } -func TestMigrationDBRepo_RevertMalformedSql(t *testing.T) { +func TestRevertWithTx_SuccessfullyReverted(t *testing.T) { db := bolttest.NewTestDB(t, "postgres") repo, err := repositories.NewMigrationDBRepo(db) - assert.NilError(t, err) - migration := models.NewMigration(time.Now(), "test") + assert.Nil(t, err) + + _, err = db.Exec(`CREATE TABLE tmp(id INT NOT NULL PRIMARY KEY)`) + assert.Nil(t, err) + + migration := models.NewTimestampMigration(time.Now(), "test") + _, err = db.Exec( + `INSERT INTO bolt_migrations(version) VALUES ($1);`, + migration.Version, + ) + assert.Nil(t, err) migration.Applied = true - err = repo.Revert("this is not SQL", migration) - assert.ErrorContains(t, err, "syntax error") - assert.Equal(t, migration.Applied, true) + err = repo.RevertWithTx(`DROP TABLE tmp;`, migration) + assert.Nil(t, err) + assert.Equal(t, migration.Applied, false) - err = repo.Revert("-- bolt: no-transaction\nthis is not SQL", migration) - assert.ErrorContains(t, err, "syntax error") - 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.ErrorIs(t, err, sql.ErrNoRows) + + row = db.QueryRow(`SELECT count(*) FROM bolt_migrations`) + var count int + err = row.Scan(&count) + assert.Nil(t, err) + assert.Equal(t, count, 0) } |