Skip to content

Commit

Permalink
migrations: add a simple test that runs the migrations (for mariadb)
Browse files Browse the repository at this point in the history
  • Loading branch information
Wessie committed May 1, 2024
1 parent f2c3948 commit 2d146ff
Showing 1 changed file with 53 additions and 0 deletions.
53 changes: 53 additions & 0 deletions migrations/migrate_test.go
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)
}

0 comments on commit 2d146ff

Please sign in to comment.