Skip to content

Commit

Permalink
tests
Browse files Browse the repository at this point in the history
  • Loading branch information
reinkrul committed Nov 9, 2023
1 parent 951a32d commit d68ad42
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 4 deletions.
9 changes: 7 additions & 2 deletions storage/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,11 +183,16 @@ func (e *engine) initSQLDatabase() error {
return err
}
migrations, err := migrate.NewWithInstance("iofs", sourceDriver, e.sqlDB.Name(), databaseDriver)
migrations.Log = sqlMigrationLogger{}
if err != nil {
return err
}
return migrations.Up()
migrations.Log = sqlMigrationLogger{}
err = migrations.Up()
if errors.Is(err, migrate.ErrNoChange) {
// There was nothing to migrate
return nil
}
return err
}

type provider struct {
Expand Down
24 changes: 22 additions & 2 deletions storage/engine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.uber.org/mock/gomock"
"os"
"path"
"testing"
)
Expand Down Expand Up @@ -103,14 +104,32 @@ func Test_engine_sqlDatabase(t *testing.T) {
t.Run("defaults to SQLite in data directory", func(t *testing.T) {
e := New()
dataDir := io.TestDirectory(t)
err := e.Configure(core.ServerConfig{Datadir: dataDir})
require.NoError(t, e.Configure(core.ServerConfig{Datadir: dataDir}))
require.NoError(t, e.Start())
t.Cleanup(func() {
_ = e.Shutdown()
})
require.NoError(t, err)
assert.FileExists(t, path.Join(dataDir, "sqlite.db"))
})
t.Run("unable to open SQLite database", func(t *testing.T) {
dataDir := io.TestDirectory(t)
require.NoError(t, os.Remove(dataDir))
e := New()
require.NoError(t, e.Configure(core.ServerConfig{Datadir: dataDir}))
err := e.Start()
assert.EqualError(t, err, "failed to initialize SQL database: unable to open database file")
})
t.Run("nothing to migrate (already migrated)", func(t *testing.T) {
dataDir := io.TestDirectory(t)
e := New()
require.NoError(t, e.Configure(core.ServerConfig{Datadir: dataDir}))
require.NoError(t, e.Start())
require.NoError(t, e.Shutdown())
e = New()
require.NoError(t, e.Configure(core.ServerConfig{Datadir: dataDir}))
require.NoError(t, e.Start())
require.NoError(t, e.Shutdown())
})
t.Run("runs migrations", func(t *testing.T) {
e := New().(*engine)
e.config.SQL.ConnectionString = SQLiteInMemoryConnectionString
Expand All @@ -128,4 +147,5 @@ func Test_engine_sqlDatabase(t *testing.T) {
assert.NoError(t, row.Scan(&count))
assert.Equal(t, 1, count)
})

}

0 comments on commit d68ad42

Please sign in to comment.