diff options
| author | Eugene Triguba <eugenetriguba@gmail.com> | 2024-03-11 20:42:20 -0400 |
|---|---|---|
| committer | Eugene Triguba <eugenetriguba@gmail.com> | 2024-03-11 20:42:20 -0400 |
| commit | 95423212626f490b8c6434b420a4cd567110f41a (patch) | |
| tree | dcaf53d9ec06e1a3ca8f3ff37787d9feb4ccba4b | |
| parent | Update readme config reference (diff) | |
| download | cli-95423212626f490b8c6434b420a4cd567110f41a.zip | |
Tweak table cleanup logic for tests
| -rw-r--r-- | internal/bolttest/db.go | 5 | ||||
| -rw-r--r-- | internal/repositories/migration_db_repo_test.go | 14 | ||||
| -rw-r--r-- | internal/storage/db_test.go | 8 |
3 files changed, 9 insertions, 18 deletions
diff --git a/internal/bolttest/db.go b/internal/bolttest/db.go index 398418f..0e1e740 100644 --- a/internal/bolttest/db.go +++ b/internal/bolttest/db.go @@ -14,10 +14,15 @@ func NewTestDB(t *testing.T) storage.DB { connectionConfig := NewTestConnectionConfig() db, err := storage.NewDB(connectionConfig) assert.Nil(t, err) + + DropTable(t, db, "bolt_migrations") + DropTable(t, db, "tmp") t.Cleanup(func() { DropTable(t, db, "bolt_migrations") + DropTable(t, db, "tmp") assert.Nil(t, db.Close()) }) + return db } diff --git a/internal/repositories/migration_db_repo_test.go b/internal/repositories/migration_db_repo_test.go index 9c5d824..37efe37 100644 --- a/internal/repositories/migration_db_repo_test.go +++ b/internal/repositories/migration_db_repo_test.go @@ -121,10 +121,6 @@ func TestApply(t *testing.T) { testdb := bolttest.NewTestDB(t) repo, err := repositories.NewMigrationDBRepo(testdb) assert.Nil(t, err) - t.Cleanup(func() { - bolttest.DropTable(t, testdb, "tmp") - }) - bolttest.DropTable(t, testdb, "tmp") migration := models.NewTimestampMigration(time.Now(), "test") err = repo.Apply(`CREATE TABLE tmp(id INT NOT NULL PRIMARY KEY)`, migration) @@ -166,9 +162,6 @@ func TestApplyWithTx_SuccessfullyApplied(t *testing.T) { testdb := bolttest.NewTestDB(t) repo, err := repositories.NewMigrationDBRepo(testdb) assert.Nil(t, err) - t.Cleanup(func() { - bolttest.DropTable(t, testdb, "tmp") - }) migration := models.NewTimestampMigration(time.Now(), "test") err = repo.ApplyWithTx(`CREATE TABLE tmp(id INT NOT NULL PRIMARY KEY)`, migration) @@ -187,9 +180,6 @@ func TestRevert(t *testing.T) { testdb := bolttest.NewTestDB(t) repo, err := repositories.NewMigrationDBRepo(testdb) assert.Nil(t, err) - t.Cleanup(func() { - bolttest.DropTable(t, testdb, "tmp") - }) _, err = testdb.Exec(`CREATE TABLE tmp(id INT NOT NULL PRIMARY KEY)`) assert.Nil(t, err) @@ -242,10 +232,6 @@ func TestRevertWithTx_SuccessfullyReverted(t *testing.T) { testdb := bolttest.NewTestDB(t) repo, err := repositories.NewMigrationDBRepo(testdb) assert.Nil(t, err) - t.Cleanup(func() { - bolttest.DropTable(t, testdb, "tmp") - }) - bolttest.DropTable(t, testdb, "tmp") _, err = testdb.Exec(`CREATE TABLE tmp(id INT NOT NULL PRIMARY KEY)`) assert.Nil(t, err) diff --git a/internal/storage/db_test.go b/internal/storage/db_test.go index f3386f5..02b134d 100644 --- a/internal/storage/db_test.go +++ b/internal/storage/db_test.go @@ -51,11 +51,11 @@ func TestClose_IsClosed(t *testing.T) { func TestTableExists_DoesExist(t *testing.T) { db, err := storage.NewDB(bolttest.NewTestConnectionConfig()) assert.Nil(t, err) + bolttest.DropTable(t, db, "tmp") t.Cleanup(func() { bolttest.DropTable(t, db, "tmp") assert.Nil(t, db.Close()) }) - bolttest.DropTable(t, db, "tmp") _, err = db.Exec("CREATE TABLE tmp(id int primary key);") assert.Nil(t, err) @@ -83,11 +83,11 @@ func TestQueryPlaceholders(t *testing.T) { cfg := bolttest.NewTestConnectionConfig() db, err := storage.NewDB(cfg) assert.Nil(t, err) + bolttest.DropTable(t, db, "tmp") t.Cleanup(func() { bolttest.DropTable(t, db, "tmp") assert.Nil(t, db.Close()) }) - bolttest.DropTable(t, db, "tmp") _, err = db.Exec(`CREATE TABLE tmp(id int primary key);`) assert.Nil(t, err) _, err = db.Exec(`INSERT INTO tmp(id) VALUES(1);`) @@ -114,11 +114,11 @@ func TestTx_Commit(t *testing.T) { cfg := bolttest.NewTestConnectionConfig() db, err := storage.NewDB(cfg) assert.Nil(t, err) + bolttest.DropTable(t, db, "tmp") t.Cleanup(func() { bolttest.DropTable(t, db, "tmp") assert.Nil(t, db.Close()) }) - bolttest.DropTable(t, db, "tmp") err = db.Tx(func(db storage.DB) error { _, err = db.Exec(`CREATE TABLE tmp(id int primary key);`) @@ -136,11 +136,11 @@ func TestTx_Rollback(t *testing.T) { cfg := bolttest.NewTestConnectionConfig() db, err := storage.NewDB(cfg) assert.Nil(t, err) + bolttest.DropTable(t, db, "tmp") t.Cleanup(func() { bolttest.DropTable(t, db, "tmp") assert.Nil(t, db.Close()) }) - bolttest.DropTable(t, db, "tmp") _, err = db.Exec(`CREATE TABLE tmp(id INT PRIMARY KEY);`) assert.Nil(t, err) expectedErr := errors.New("error!") |