From db7de4b31b1836abed822077840665639f0d66da Mon Sep 17 00:00:00 2001 From: Eugene Triguba Date: Fri, 19 Jan 2024 23:27:27 -0500 Subject: Add ability to use sequential or timestamp-based versions --- internal/repositories/migration_db_repo_test.go | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) (limited to 'internal/repositories/migration_db_repo_test.go') diff --git a/internal/repositories/migration_db_repo_test.go b/internal/repositories/migration_db_repo_test.go index 55b8eb2..dc635b0 100644 --- a/internal/repositories/migration_db_repo_test.go +++ b/internal/repositories/migration_db_repo_test.go @@ -131,7 +131,7 @@ func TestMigrationDBRepo_Apply(t *testing.T) { repo, err := repositories.NewMigrationDBRepo(db) assert.NilError(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.Equal(t, migration.Applied, true) @@ -158,7 +158,7 @@ func TestMigrationDBRepo_Revert(t *testing.T) { _, err = db.Exec(`CREATE TABLE tmp(id INT NOT NULL PRIMARY KEY)`) assert.NilError(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, @@ -194,7 +194,7 @@ func TestMigrationDBRepo_ApplyAndRevertWithoutTransaction(t *testing.T) { _, err = db.Exec("CREATE TABLE tmp(id INT PRIMARY KEY, id2 INT, id3 INT)") assert.NilError(t, err) - migration := models.NewMigration(time.Now(), "test") + migration := models.NewTimestampMigration(time.Now(), "test") // CREATE INDEX CONCURRENTLY cannot run inside a transaction. err = repo.Apply( `-- bolt: no-transaction\CREATE INDEX CONCURRENTLY i1 ON tmp(id2);`, @@ -215,7 +215,7 @@ func TestMigrationDBRepo_ApplyMalformedSql(t *testing.T) { db := bolttest.NewTestDB(t, "postgres") repo, err := repositories.NewMigrationDBRepo(db) assert.NilError(t, err) - migration := models.NewMigration(time.Now(), "test") + migration := models.NewTimestampMigration(time.Now(), "test") err = repo.Apply("this is not SQL", migration) assert.ErrorContains(t, err, "syntax error") @@ -230,7 +230,7 @@ func TestMigrationDBRepo_RevertMalformedSql(t *testing.T) { db := bolttest.NewTestDB(t, "postgres") repo, err := repositories.NewMigrationDBRepo(db) assert.NilError(t, err) - migration := models.NewMigration(time.Now(), "test") + migration := models.NewTimestampMigration(time.Now(), "test") migration.Applied = true err = repo.Revert("this is not SQL", migration) -- cgit v1.2.3 From b06eba3df09a8fcd777b8fe350c2b184758963d1 Mon Sep 17 00:00:00 2001 From: Eugene Triguba Date: Sun, 28 Jan 2024 16:10:14 -0500 Subject: Use checkmate for tests --- internal/repositories/migration_db_repo_test.go | 118 ++++++++++++------------ 1 file changed, 59 insertions(+), 59 deletions(-) (limited to 'internal/repositories/migration_db_repo_test.go') diff --git a/internal/repositories/migration_db_repo_test.go b/internal/repositories/migration_db_repo_test.go index dc635b0..5d1a9f1 100644 --- a/internal/repositories/migration_db_repo_test.go +++ b/internal/repositories/migration_db_repo_test.go @@ -8,7 +8,7 @@ import ( "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" ) func TestMigrationDBRepo_NewMigrationDBRepoCreatesTable(t *testing.T) { @@ -21,10 +21,10 @@ func TestMigrationDBRepo_NewMigrationDBRepoCreatesTable(t *testing.T) { `) var scanResult int err := row.Scan(&scanResult) - assert.ErrorIs(t, err, sql.ErrNoRows) + checkmate.AssertErrorIs(t, err, sql.ErrNoRows) _, err = repositories.NewMigrationDBRepo(db) - assert.NilError(t, err) + checkmate.AssertNil(t, err) row = db.QueryRow(` SELECT 1 @@ -33,49 +33,49 @@ func TestMigrationDBRepo_NewMigrationDBRepoCreatesTable(t *testing.T) { AND TABLE_NAME = 'bolt_migrations' `) err = row.Scan(&scanResult) - assert.NilError(t, err) + checkmate.AssertNil(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) + checkmate.AssertNil(t, err) _, err = db.Exec(`INSERT INTO bolt_migrations(id) VALUES (1);`) - assert.NilError(t, err) + checkmate.AssertNil(t, err) _, err = repositories.NewMigrationDBRepo(db) - assert.NilError(t, err) + checkmate.AssertNil(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) + checkmate.AssertNil(t, err) + checkmate.AssertEqual(t, scanResult, 1) } func TestMigrationDBRepo_ListWithEmptyTable(t *testing.T) { db := bolttest.NewTestDB(t, "postgres") repo, err := repositories.NewMigrationDBRepo(db) - assert.NilError(t, err) + checkmate.AssertNil(t, err) migrations, err := repo.List() - assert.NilError(t, err) - assert.Equal(t, len(migrations), 0) + checkmate.AssertNil(t, err) + checkmate.AssertEqual(t, len(migrations), 0) } func TestMigrationDBRepo_ListWithSingleResult(t *testing.T) { db := bolttest.NewTestDB(t, "postgres") repo, err := repositories.NewMigrationDBRepo(db) - assert.NilError(t, err) + checkmate.AssertNil(t, err) version := "20230101000000" _, err = db.Exec(`INSERT INTO bolt_migrations(version) VALUES ($1);`, version) - assert.NilError(t, err) + checkmate.AssertNil(t, err) migrations, err := repo.List() - assert.NilError(t, err) - assert.Equal(t, len(migrations), 1) - assert.DeepEqual( + checkmate.AssertNil(t, err) + checkmate.AssertEqual(t, len(migrations), 1) + checkmate.AssertDeepEqual( t, migrations[version], &models.Migration{Version: version, Message: "", Applied: true}, @@ -85,16 +85,16 @@ func TestMigrationDBRepo_ListWithSingleResult(t *testing.T) { func TestMigrationDBRepo_ListWithShortVersion(t *testing.T) { db := bolttest.NewTestDB(t, "postgres") repo, err := repositories.NewMigrationDBRepo(db) - assert.NilError(t, err) + checkmate.AssertNil(t, err) version := "20230101" _, err = db.Exec(`INSERT INTO bolt_migrations(version) VALUES ($1);`, version) - assert.NilError(t, err) + checkmate.AssertNil(t, err) migrations, err := repo.List() - assert.NilError(t, err) - assert.Equal(t, len(migrations), 1) - assert.DeepEqual( + checkmate.AssertNil(t, err) + checkmate.AssertEqual(t, len(migrations), 1) + checkmate.AssertDeepEqual( t, migrations[version], &models.Migration{Version: version, Message: "", Applied: true}, @@ -104,37 +104,37 @@ func TestMigrationDBRepo_ListWithShortVersion(t *testing.T) { func TestMigrationDBRepo_IsAppliedWithNotApplied(t *testing.T) { db := bolttest.NewTestDB(t, "postgres") repo, err := repositories.NewMigrationDBRepo(db) - assert.NilError(t, err) + checkmate.AssertNil(t, err) version := "20230101010101" applied, err := repo.IsApplied(version) - assert.NilError(t, err) - assert.Equal(t, applied, false) + checkmate.AssertNil(t, err) + checkmate.AssertEqual(t, applied, false) } func TestMigrationDBRepo_IsAppliedWithApplied(t *testing.T) { db := bolttest.NewTestDB(t, "postgres") repo, err := repositories.NewMigrationDBRepo(db) - assert.NilError(t, err) + checkmate.AssertNil(t, err) version := "20230101010101" _, err = db.Exec(`INSERT INTO bolt_migrations(version) VALUES ($1);`, version) - assert.NilError(t, err) + checkmate.AssertNil(t, err) applied, err := repo.IsApplied(version) - assert.NilError(t, err) - assert.Equal(t, applied, true) + checkmate.AssertNil(t, err) + checkmate.AssertEqual(t, applied, true) } func TestMigrationDBRepo_Apply(t *testing.T) { db := bolttest.NewTestDB(t, "postgres") repo, err := repositories.NewMigrationDBRepo(db) - assert.NilError(t, err) + checkmate.AssertNil(t, err) migration := models.NewTimestampMigration(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) + checkmate.AssertNil(t, err) + checkmate.AssertEqual(t, migration.Applied, true) row := db.QueryRow(` SELECT 1 @@ -144,31 +144,31 @@ func TestMigrationDBRepo_Apply(t *testing.T) { `) var scanResult int err = row.Scan(&scanResult) - assert.NilError(t, err) + checkmate.AssertNil(t, err) applied, err := repo.IsApplied(migration.Version) - assert.NilError(t, err) - assert.Equal(t, applied, true) + checkmate.AssertNil(t, err) + checkmate.AssertEqual(t, applied, true) } func TestMigrationDBRepo_Revert(t *testing.T) { db := bolttest.NewTestDB(t, "postgres") repo, err := repositories.NewMigrationDBRepo(db) - assert.NilError(t, err) + checkmate.AssertNil(t, err) _, err = db.Exec(`CREATE TABLE tmp(id INT NOT NULL PRIMARY KEY)`) - assert.NilError(t, err) + checkmate.AssertNil(t, err) migration := models.NewTimestampMigration(time.Now(), "test") _, err = db.Exec( `INSERT INTO bolt_migrations(version) VALUES ($1);`, migration.Version, ) - assert.NilError(t, err) + checkmate.AssertNil(t, err) migration.Applied = true err = repo.Revert(`DROP TABLE tmp;`, migration) - assert.NilError(t, err) - assert.Equal(t, migration.Applied, false) + checkmate.AssertNil(t, err) + checkmate.AssertEqual(t, migration.Applied, false) row := db.QueryRow(` SELECT 1 @@ -178,21 +178,21 @@ func TestMigrationDBRepo_Revert(t *testing.T) { `) var scanResult int err = row.Scan(&scanResult) - assert.ErrorIs(t, err, sql.ErrNoRows) + checkmate.AssertErrorIs(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) + checkmate.AssertNil(t, err) + checkmate.AssertEqual(t, count, 0) } func TestMigrationDBRepo_ApplyAndRevertWithoutTransaction(t *testing.T) { db := bolttest.NewTestDB(t, "postgres") repo, err := repositories.NewMigrationDBRepo(db) - assert.NilError(t, err) + checkmate.AssertNil(t, err) _, err = db.Exec("CREATE TABLE tmp(id INT PRIMARY KEY, id2 INT, id3 INT)") - assert.NilError(t, err) + checkmate.AssertNil(t, err) migration := models.NewTimestampMigration(time.Now(), "test") // CREATE INDEX CONCURRENTLY cannot run inside a transaction. @@ -200,44 +200,44 @@ func TestMigrationDBRepo_ApplyAndRevertWithoutTransaction(t *testing.T) { `-- bolt: no-transaction\CREATE INDEX CONCURRENTLY i1 ON tmp(id2);`, migration, ) - assert.NilError(t, err) - assert.Equal(t, migration.Applied, true) + checkmate.AssertNil(t, err) + checkmate.AssertEqual(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) + checkmate.AssertNil(t, err) + checkmate.AssertEqual(t, migration.Applied, false) } func TestMigrationDBRepo_ApplyMalformedSql(t *testing.T) { db := bolttest.NewTestDB(t, "postgres") repo, err := repositories.NewMigrationDBRepo(db) - assert.NilError(t, err) + checkmate.AssertNil(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) + checkmate.AssertErrorContains(t, err, "syntax error") + checkmate.AssertEqual(t, migration.Applied, false) err = repo.Apply("-- bolt: no-transaction\nthis is not SQL", migration) - assert.ErrorContains(t, err, "syntax error") - assert.Equal(t, migration.Applied, false) + checkmate.AssertErrorContains(t, err, "syntax error") + checkmate.AssertEqual(t, migration.Applied, false) } func TestMigrationDBRepo_RevertMalformedSql(t *testing.T) { db := bolttest.NewTestDB(t, "postgres") repo, err := repositories.NewMigrationDBRepo(db) - assert.NilError(t, err) + checkmate.AssertNil(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) + checkmate.AssertErrorContains(t, err, "syntax error") + checkmate.AssertEqual(t, migration.Applied, true) err = repo.Revert("-- bolt: no-transaction\nthis is not SQL", migration) - assert.ErrorContains(t, err, "syntax error") - assert.Equal(t, migration.Applied, true) + checkmate.AssertErrorContains(t, err, "syntax error") + checkmate.AssertEqual(t, migration.Applied, true) } -- cgit v1.2.3 From 345e28bd44ba6a38bf7ee9bfe2066f333bbbe894 Mon Sep 17 00:00:00 2001 From: Eugene Triguba Date: Sun, 28 Jan 2024 16:29:00 -0500 Subject: Update to checkmate v0.2.0 --- internal/repositories/migration_db_repo_test.go | 118 ++++++++++++------------ 1 file changed, 59 insertions(+), 59 deletions(-) (limited to 'internal/repositories/migration_db_repo_test.go') diff --git a/internal/repositories/migration_db_repo_test.go b/internal/repositories/migration_db_repo_test.go index 5d1a9f1..4e41b36 100644 --- a/internal/repositories/migration_db_repo_test.go +++ b/internal/repositories/migration_db_repo_test.go @@ -8,7 +8,7 @@ import ( "github.com/eugenetriguba/bolt/internal/bolttest" "github.com/eugenetriguba/bolt/internal/models" "github.com/eugenetriguba/bolt/internal/repositories" - "github.com/eugenetriguba/checkmate" + "github.com/eugenetriguba/checkmate/assert" ) func TestMigrationDBRepo_NewMigrationDBRepoCreatesTable(t *testing.T) { @@ -21,10 +21,10 @@ func TestMigrationDBRepo_NewMigrationDBRepoCreatesTable(t *testing.T) { `) var scanResult int err := row.Scan(&scanResult) - checkmate.AssertErrorIs(t, err, sql.ErrNoRows) + assert.ErrorIs(t, err, sql.ErrNoRows) _, err = repositories.NewMigrationDBRepo(db) - checkmate.AssertNil(t, err) + assert.Nil(t, err) row = db.QueryRow(` SELECT 1 @@ -33,49 +33,49 @@ func TestMigrationDBRepo_NewMigrationDBRepoCreatesTable(t *testing.T) { AND TABLE_NAME = 'bolt_migrations' `) err = row.Scan(&scanResult) - checkmate.AssertNil(t, err) + assert.Nil(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)`) - checkmate.AssertNil(t, err) + assert.Nil(t, err) _, err = db.Exec(`INSERT INTO bolt_migrations(id) VALUES (1);`) - checkmate.AssertNil(t, err) + assert.Nil(t, err) _, err = repositories.NewMigrationDBRepo(db) - checkmate.AssertNil(t, err) + assert.Nil(t, err) row := db.QueryRow(`SELECT id FROM bolt_migrations`) var scanResult int err = row.Scan(&scanResult) - checkmate.AssertNil(t, err) - checkmate.AssertEqual(t, scanResult, 1) + assert.Nil(t, err) + assert.Equal(t, scanResult, 1) } func TestMigrationDBRepo_ListWithEmptyTable(t *testing.T) { db := bolttest.NewTestDB(t, "postgres") repo, err := repositories.NewMigrationDBRepo(db) - checkmate.AssertNil(t, err) + assert.Nil(t, err) migrations, err := repo.List() - checkmate.AssertNil(t, err) - checkmate.AssertEqual(t, len(migrations), 0) + assert.Nil(t, err) + assert.Equal(t, len(migrations), 0) } func TestMigrationDBRepo_ListWithSingleResult(t *testing.T) { db := bolttest.NewTestDB(t, "postgres") repo, err := repositories.NewMigrationDBRepo(db) - checkmate.AssertNil(t, err) + assert.Nil(t, err) version := "20230101000000" _, err = db.Exec(`INSERT INTO bolt_migrations(version) VALUES ($1);`, version) - checkmate.AssertNil(t, err) + assert.Nil(t, err) migrations, err := repo.List() - checkmate.AssertNil(t, err) - checkmate.AssertEqual(t, len(migrations), 1) - checkmate.AssertDeepEqual( + assert.Nil(t, err) + assert.Equal(t, len(migrations), 1) + assert.DeepEqual( t, migrations[version], &models.Migration{Version: version, Message: "", Applied: true}, @@ -85,16 +85,16 @@ func TestMigrationDBRepo_ListWithSingleResult(t *testing.T) { func TestMigrationDBRepo_ListWithShortVersion(t *testing.T) { db := bolttest.NewTestDB(t, "postgres") repo, err := repositories.NewMigrationDBRepo(db) - checkmate.AssertNil(t, err) + assert.Nil(t, err) version := "20230101" _, err = db.Exec(`INSERT INTO bolt_migrations(version) VALUES ($1);`, version) - checkmate.AssertNil(t, err) + assert.Nil(t, err) migrations, err := repo.List() - checkmate.AssertNil(t, err) - checkmate.AssertEqual(t, len(migrations), 1) - checkmate.AssertDeepEqual( + assert.Nil(t, err) + assert.Equal(t, len(migrations), 1) + assert.DeepEqual( t, migrations[version], &models.Migration{Version: version, Message: "", Applied: true}, @@ -104,37 +104,37 @@ func TestMigrationDBRepo_ListWithShortVersion(t *testing.T) { func TestMigrationDBRepo_IsAppliedWithNotApplied(t *testing.T) { db := bolttest.NewTestDB(t, "postgres") repo, err := repositories.NewMigrationDBRepo(db) - checkmate.AssertNil(t, err) + assert.Nil(t, err) version := "20230101010101" applied, err := repo.IsApplied(version) - checkmate.AssertNil(t, err) - checkmate.AssertEqual(t, applied, false) + assert.Nil(t, err) + assert.Equal(t, applied, false) } func TestMigrationDBRepo_IsAppliedWithApplied(t *testing.T) { db := bolttest.NewTestDB(t, "postgres") repo, err := repositories.NewMigrationDBRepo(db) - checkmate.AssertNil(t, err) + assert.Nil(t, err) version := "20230101010101" _, err = db.Exec(`INSERT INTO bolt_migrations(version) VALUES ($1);`, version) - checkmate.AssertNil(t, err) + assert.Nil(t, err) applied, err := repo.IsApplied(version) - checkmate.AssertNil(t, err) - checkmate.AssertEqual(t, applied, true) + assert.Nil(t, err) + assert.Equal(t, applied, true) } func TestMigrationDBRepo_Apply(t *testing.T) { db := bolttest.NewTestDB(t, "postgres") repo, err := repositories.NewMigrationDBRepo(db) - checkmate.AssertNil(t, err) + assert.Nil(t, err) migration := models.NewTimestampMigration(time.Now(), "test") err = repo.Apply(`CREATE TABLE tmp(id INT NOT NULL PRIMARY KEY)`, migration) - checkmate.AssertNil(t, err) - checkmate.AssertEqual(t, migration.Applied, true) + assert.Nil(t, err) + assert.Equal(t, migration.Applied, true) row := db.QueryRow(` SELECT 1 @@ -144,31 +144,31 @@ func TestMigrationDBRepo_Apply(t *testing.T) { `) var scanResult int err = row.Scan(&scanResult) - checkmate.AssertNil(t, err) + assert.Nil(t, err) applied, err := repo.IsApplied(migration.Version) - checkmate.AssertNil(t, err) - checkmate.AssertEqual(t, applied, true) + assert.Nil(t, err) + assert.Equal(t, applied, true) } func TestMigrationDBRepo_Revert(t *testing.T) { db := bolttest.NewTestDB(t, "postgres") repo, err := repositories.NewMigrationDBRepo(db) - checkmate.AssertNil(t, err) + assert.Nil(t, err) _, err = db.Exec(`CREATE TABLE tmp(id INT NOT NULL PRIMARY KEY)`) - checkmate.AssertNil(t, err) + assert.Nil(t, err) migration := models.NewTimestampMigration(time.Now(), "test") _, err = db.Exec( `INSERT INTO bolt_migrations(version) VALUES ($1);`, migration.Version, ) - checkmate.AssertNil(t, err) + assert.Nil(t, err) migration.Applied = true err = repo.Revert(`DROP TABLE tmp;`, migration) - checkmate.AssertNil(t, err) - checkmate.AssertEqual(t, migration.Applied, false) + assert.Nil(t, err) + assert.Equal(t, migration.Applied, false) row := db.QueryRow(` SELECT 1 @@ -178,21 +178,21 @@ func TestMigrationDBRepo_Revert(t *testing.T) { `) var scanResult int err = row.Scan(&scanResult) - checkmate.AssertErrorIs(t, err, sql.ErrNoRows) + assert.ErrorIs(t, err, sql.ErrNoRows) row = db.QueryRow(`SELECT count(*) FROM bolt_migrations`) var count int err = row.Scan(&count) - checkmate.AssertNil(t, err) - checkmate.AssertEqual(t, count, 0) + assert.Nil(t, err) + assert.Equal(t, count, 0) } func TestMigrationDBRepo_ApplyAndRevertWithoutTransaction(t *testing.T) { db := bolttest.NewTestDB(t, "postgres") repo, err := repositories.NewMigrationDBRepo(db) - checkmate.AssertNil(t, err) + assert.Nil(t, err) _, err = db.Exec("CREATE TABLE tmp(id INT PRIMARY KEY, id2 INT, id3 INT)") - checkmate.AssertNil(t, err) + assert.Nil(t, err) migration := models.NewTimestampMigration(time.Now(), "test") // CREATE INDEX CONCURRENTLY cannot run inside a transaction. @@ -200,44 +200,44 @@ func TestMigrationDBRepo_ApplyAndRevertWithoutTransaction(t *testing.T) { `-- bolt: no-transaction\CREATE INDEX CONCURRENTLY i1 ON tmp(id2);`, migration, ) - checkmate.AssertNil(t, err) - checkmate.AssertEqual(t, migration.Applied, true) + assert.Nil(t, err) + assert.Equal(t, migration.Applied, true) err = repo.Revert( "-- bolt: no-transaction\nCREATE INDEX CONCURRENTLY i2 ON tmp(id3);", migration, ) - checkmate.AssertNil(t, err) - checkmate.AssertEqual(t, migration.Applied, false) + assert.Nil(t, err) + assert.Equal(t, migration.Applied, false) } func TestMigrationDBRepo_ApplyMalformedSql(t *testing.T) { db := bolttest.NewTestDB(t, "postgres") repo, err := repositories.NewMigrationDBRepo(db) - checkmate.AssertNil(t, err) + assert.Nil(t, err) migration := models.NewTimestampMigration(time.Now(), "test") err = repo.Apply("this is not SQL", migration) - checkmate.AssertErrorContains(t, err, "syntax error") - checkmate.AssertEqual(t, migration.Applied, false) + assert.ErrorContains(t, err, "syntax error") + assert.Equal(t, migration.Applied, false) err = repo.Apply("-- bolt: no-transaction\nthis is not SQL", migration) - checkmate.AssertErrorContains(t, err, "syntax error") - checkmate.AssertEqual(t, migration.Applied, false) + assert.ErrorContains(t, err, "syntax error") + assert.Equal(t, migration.Applied, false) } func TestMigrationDBRepo_RevertMalformedSql(t *testing.T) { db := bolttest.NewTestDB(t, "postgres") repo, err := repositories.NewMigrationDBRepo(db) - checkmate.AssertNil(t, err) + assert.Nil(t, err) migration := models.NewTimestampMigration(time.Now(), "test") migration.Applied = true err = repo.Revert("this is not SQL", migration) - checkmate.AssertErrorContains(t, err, "syntax error") - checkmate.AssertEqual(t, migration.Applied, true) + assert.ErrorContains(t, err, "syntax error") + assert.Equal(t, migration.Applied, true) err = repo.Revert("-- bolt: no-transaction\nthis is not SQL", migration) - checkmate.AssertErrorContains(t, err, "syntax error") - checkmate.AssertEqual(t, migration.Applied, true) + assert.ErrorContains(t, err, "syntax error") + assert.Equal(t, migration.Applied, true) } -- cgit v1.2.3 From bf1d3b3e1fc42132678b244e479839416a03a6ce Mon Sep 17 00:00:00 2001 From: Eugene Triguba Date: Thu, 29 Feb 2024 19:53:20 -0500 Subject: Add service tests --- internal/repositories/migration_db_repo_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'internal/repositories/migration_db_repo_test.go') diff --git a/internal/repositories/migration_db_repo_test.go b/internal/repositories/migration_db_repo_test.go index 4e41b36..9f4f9e8 100644 --- a/internal/repositories/migration_db_repo_test.go +++ b/internal/repositories/migration_db_repo_test.go @@ -197,7 +197,7 @@ func TestMigrationDBRepo_ApplyAndRevertWithoutTransaction(t *testing.T) { migration := models.NewTimestampMigration(time.Now(), "test") // CREATE INDEX CONCURRENTLY cannot run inside a transaction. err = repo.Apply( - `-- bolt: no-transaction\CREATE INDEX CONCURRENTLY i1 ON tmp(id2);`, + `-- bolt: no-transaction\nCREATE INDEX CONCURRENTLY i1 ON tmp(id2);`, migration, ) assert.Nil(t, err) -- cgit v1.2.3 From 6760ae82d62449f41c68950264c02c40be81bfb3 Mon Sep 17 00:00:00 2001 From: Eugene Triguba Date: Sat, 2 Mar 2024 09:29:41 -0500 Subject: Improve coverage for separate Tx methods --- internal/repositories/migration_db_repo_test.go | 188 +++++++++++++++++++----- 1 file changed, 150 insertions(+), 38 deletions(-) (limited to 'internal/repositories/migration_db_repo_test.go') diff --git a/internal/repositories/migration_db_repo_test.go b/internal/repositories/migration_db_repo_test.go index 9f4f9e8..2775faa 100644 --- a/internal/repositories/migration_db_repo_test.go +++ b/internal/repositories/migration_db_repo_test.go @@ -2,6 +2,7 @@ package repositories_test import ( "database/sql" + "errors" "testing" "time" @@ -11,7 +12,7 @@ import ( "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 @@ -36,7 +37,7 @@ func TestMigrationDBRepo_NewMigrationDBRepoCreatesTable(t *testing.T) { 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.Nil(t, err) @@ -53,7 +54,20 @@ func TestMigrationDBRepo_NewMigrationDBRepoLeavesCurrentTable(t *testing.T) { 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.Nil(t, err) @@ -63,7 +77,22 @@ func TestMigrationDBRepo_ListWithEmptyTable(t *testing.T) { 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.Nil(t, err) @@ -82,7 +111,7 @@ 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.Nil(t, err) @@ -101,7 +130,7 @@ 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.Nil(t, err) @@ -112,7 +141,7 @@ func TestMigrationDBRepo_IsAppliedWithNotApplied(t *testing.T) { 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.Nil(t, err) @@ -126,7 +155,7 @@ func TestMigrationDBRepo_IsAppliedWithApplied(t *testing.T) { 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.Nil(t, err) @@ -150,7 +179,69 @@ func TestMigrationDBRepo_Apply(t *testing.T) { assert.Equal(t, applied, true) } -func TestMigrationDBRepo_Revert(t *testing.T) { +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(` + 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 TestRevert(t *testing.T) { db := bolttest.NewTestDB(t, "postgres") repo, err := repositories.NewMigrationDBRepo(db) assert.Nil(t, err) @@ -187,57 +278,78 @@ func TestMigrationDBRepo_Revert(t *testing.T) { 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.Nil(t, err) - _, err = db.Exec("CREATE TABLE tmp(id INT PRIMARY KEY, id2 INT, id3 INT)") - assert.Nil(t, err) - migration := models.NewTimestampMigration(time.Now(), "test") - // CREATE INDEX CONCURRENTLY cannot run inside a transaction. - err = repo.Apply( - `-- bolt: no-transaction\nCREATE INDEX CONCURRENTLY i1 ON tmp(id2);`, - migration, - ) - 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.Revert( - "-- bolt: no-transaction\nCREATE INDEX CONCURRENTLY i2 ON tmp(id3);", - migration, - ) +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) - assert.Equal(t, migration.Applied, false) + 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.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.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) } -- cgit v1.2.3