Skip to content

Commit

Permalink
Improve DB tests (#5508)
Browse files Browse the repository at this point in the history
## Motivation

This PR improves two tests in the `sql` package that are currently required to be updated every time a new migration is added. Instead the tests are now self contained and don't fail after adding a migration.
  • Loading branch information
fasmat committed Jan 29, 2024
1 parent e2ff6f5 commit bb31280
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 4 deletions.
18 changes: 15 additions & 3 deletions sql/database_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,14 +128,26 @@ func TestDatabaseSkipMigrations(t *testing.T) {

func TestDatabaseVacuumState(t *testing.T) {
dir := t.TempDir()

ctrl := gomock.NewController(t)
migration1 := NewMockMigration(ctrl)
migration1.EXPECT().Order().Return(1).AnyTimes()
migration1.EXPECT().Apply(gomock.Any()).Return(nil).Times(1)

migration2 := NewMockMigration(ctrl)
migration2.EXPECT().Order().Return(2).AnyTimes()
migration2.EXPECT().Apply(gomock.Any()).Return(nil).Times(1)

dbFile := filepath.Join(dir, "test.sql")
db, err := Open("file:" + dbFile)
db, err := Open("file:"+dbFile,
WithMigrations([]Migration{migration1}),
)
require.NoError(t, err)
require.NoError(t, db.Close())

db, err = Open("file:"+dbFile,
WithMigrations([]Migration{}),
WithVacuumState(10),
WithMigrations([]Migration{migration1, migration2}),
WithVacuumState(2),
)
require.NoError(t, err)
require.NoError(t, db.Close())
Expand Down
9 changes: 8 additions & 1 deletion sql/migrations_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package sql

import (
"slices"
"testing"

"github.com/stretchr/testify/require"
Expand All @@ -15,5 +16,11 @@ func TestMigrationsAppliedOnce(t *testing.T) {
return true
})
require.NoError(t, err)
require.Equal(t, version, 10)

migrations, err := StateMigrations()
require.NoError(t, err)
expectedVersion := slices.MaxFunc(migrations, func(a, b Migration) int {
return a.Order() - b.Order()
})
require.Equal(t, expectedVersion.Order(), version)
}

0 comments on commit bb31280

Please sign in to comment.