-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
migrations: add a simple test that runs the migrations (for mariadb)
- Loading branch information
Showing
1 changed file
with
53 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package migrations | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
"github.com/R-a-dio/valkyrie/config" | ||
"github.com/golang-migrate/migrate/v4" | ||
"github.com/stretchr/testify/require" | ||
"github.com/testcontainers/testcontainers-go" | ||
"github.com/testcontainers/testcontainers-go/modules/mariadb" | ||
) | ||
|
||
func TestCheckVersion(t *testing.T) { | ||
ctx := context.Background() | ||
|
||
testcontainers.Logger = testcontainers.TestLogger(t) | ||
|
||
// setup a container to test in | ||
container, err := mariadb.RunContainer(ctx, | ||
testcontainers.WithImage("mariadb:latest"), | ||
mariadb.WithDatabase("test"), | ||
mariadb.WithUsername("root"), | ||
mariadb.WithPassword(""), | ||
) | ||
require.NoError(t, err, "failed setting up container") | ||
|
||
cfg, err := config.LoadFile() | ||
require.NoError(t, err) | ||
|
||
dsn, err := container.ConnectionString(ctx) | ||
require.NoError(t, err) | ||
|
||
c := cfg.Conf() | ||
c.Database.DSN = dsn | ||
cfg.StoreConf(c) | ||
|
||
// first CheckVersion, should fail because no migrations have been ran yet | ||
err = CheckVersion(ctx, cfg) | ||
require.Error(t, err) | ||
require.ErrorIs(t, err, migrate.ErrNilVersion) | ||
|
||
migr, err := New(ctx, cfg) | ||
require.NoError(t, err) | ||
|
||
// now run all the migrations | ||
err = migr.Up() | ||
require.NoError(t, err) | ||
|
||
// then CheckVersion again that should succeed | ||
err = CheckVersion(ctx, cfg) | ||
require.NoError(t, err) | ||
} |