Skip to content

Commit

Permalink
chore: fix unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
sweatybridge committed Oct 4, 2024
1 parent 89ffc9e commit 64ec6c4
Show file tree
Hide file tree
Showing 8 changed files with 32 additions and 22 deletions.
15 changes: 11 additions & 4 deletions internal/db/push/push_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package push

import (
"context"
"crypto/sha256"
"encoding/hex"
"os"
"path/filepath"
"testing"
Expand Down Expand Up @@ -161,24 +163,29 @@ func TestPushAll(t *testing.T) {
})

t.Run("throws error on seed failure", func(t *testing.T) {
digest := hex.EncodeToString(sha256.New().Sum(nil))
seedPath := filepath.Join(utils.SupabaseDirPath, "seed.sql")
utils.Config.Db.Seed.SqlPaths = []string{seedPath}
// Setup in-memory fs
fsys := &fstest.OpenErrorFs{DenyPath: seedPath}
_, _ = fsys.Create(seedPath)
fsys := afero.NewMemMapFs()
require.NoError(t, afero.WriteFile(fsys, seedPath, []byte{}, 0644))
path := filepath.Join(utils.MigrationsDir, "0_test.sql")
require.NoError(t, afero.WriteFile(fsys, path, []byte{}, 0644))
// Setup mock postgres
conn := pgtest.NewConn()
defer conn.Close(t)
conn.Query(migration.LIST_MIGRATION_VERSION).
Reply("SELECT 0").
Query(migration.SELECT_SEED_TABLE).
Reply("SELECT 0")
helper.MockMigrationHistory(conn).
Query(migration.INSERT_MIGRATION_VERSION, "0", "test", nil).
Reply("INSERT 0 1")
Reply("INSERT 0 1").
Query(migration.UPSERT_SEED_FILE, seedPath, digest).
ReplyError(pgerrcode.NotNullViolation, `null value in column "hash" of relation "seed_files"`)
// Run test
err := Run(context.Background(), false, false, false, true, dbConfig, fsys, conn.Intercept)
// Check error
assert.ErrorIs(t, err, os.ErrPermission)
assert.ErrorContains(t, err, `ERROR: null value in column "hash" of relation "seed_files" (SQLSTATE 23502)`)
})
}
10 changes: 2 additions & 8 deletions internal/db/start/start_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"io"
"net/http"
"os"
"path/filepath"
"testing"

"github.com/docker/docker/api/types"
Expand Down Expand Up @@ -52,8 +51,6 @@ func TestInitBranch(t *testing.T) {

func TestStartDatabase(t *testing.T) {
t.Run("initialize main branch", func(t *testing.T) {
seedPath := filepath.Join(utils.SupabaseDirPath, "seed.sql")
utils.Config.Db.Seed.SqlPaths = []string{seedPath}
utils.Config.Db.MajorVersion = 15
utils.DbId = "supabase_db_test"
utils.ConfigId = "supabase_config_test"
Expand All @@ -62,8 +59,6 @@ func TestStartDatabase(t *testing.T) {
fsys := afero.NewMemMapFs()
roles := "create role test"
require.NoError(t, afero.WriteFile(fsys, utils.CustomRolesPath, []byte(roles), 0644))
seed := "INSERT INTO employees(name) VALUES ('Alice')"
require.NoError(t, afero.WriteFile(fsys, seedPath, []byte(seed), 0644))
// Setup mock docker
require.NoError(t, apitest.MockDocker(utils.Docker))
defer gock.OffAll()
Expand Down Expand Up @@ -91,9 +86,7 @@ func TestStartDatabase(t *testing.T) {
conn := pgtest.NewConn()
defer conn.Close(t)
conn.Query(roles).
Reply("CREATE ROLE").
Query(seed).
Reply("INSERT 0 1")
Reply("CREATE ROLE")
// Run test
err := StartDatabase(context.Background(), fsys, io.Discard, conn.Intercept)
// Check error
Expand Down Expand Up @@ -274,6 +267,7 @@ func TestSetupDatabase(t *testing.T) {
})

t.Run("throws error on init failure", func(t *testing.T) {
utils.Config.Realtime.Enabled = true
utils.Config.Db.Port = 5432
// Setup mock docker
require.NoError(t, apitest.MockDocker(utils.Docker))
Expand Down
3 changes: 2 additions & 1 deletion internal/link/link_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,8 @@ func TestLinkDatabase(t *testing.T) {
Query(migration.CREATE_VERSION_TABLE).
ReplyError(pgerrcode.InsufficientPrivilege, "permission denied for relation supabase_migrations").
Query(migration.ADD_STATEMENTS_COLUMN).
Query(migration.ADD_NAME_COLUMN)
Query(migration.ADD_NAME_COLUMN).
Query(migration.CREATE_SEED_TABLE)
// Run test
err := linkDatabase(context.Background(), dbConfig, conn.Intercept)
// Check error
Expand Down
4 changes: 2 additions & 2 deletions internal/migration/apply/apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ func MigrateAndSeed(ctx context.Context, version string, conn *pgx.Conn, fsys af
if err := migration.ApplyMigrations(ctx, migrations, conn, afero.NewIOFS(fsys)); err != nil {
return err
}
return SeedDatabase(ctx, conn, fsys)
return applySeedFiles(ctx, conn, fsys)
}

func SeedDatabase(ctx context.Context, conn *pgx.Conn, fsys afero.Fs) error {
func applySeedFiles(ctx context.Context, conn *pgx.Conn, fsys afero.Fs) error {
if !utils.Config.Db.Seed.Enabled {
return nil
}
Expand Down
4 changes: 3 additions & 1 deletion internal/testing/helper/history.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ func MockMigrationHistory(conn *pgtest.MockConn) *pgtest.MockConn {
Query(migration.ADD_STATEMENTS_COLUMN).
Reply("ALTER TABLE").
Query(migration.ADD_NAME_COLUMN).
Reply("ALTER TABLE")
Reply("ALTER TABLE").
Query(migration.CREATE_SEED_TABLE).
Reply("CREATE TABLE")
return conn
}
7 changes: 5 additions & 2 deletions pkg/migration/apply_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,8 @@ func TestApplyMigrations(t *testing.T) {
Query(CREATE_VERSION_TABLE).
ReplyError(pgerrcode.InsufficientPrivilege, "permission denied for relation supabase_migrations").
Query(ADD_STATEMENTS_COLUMN).
Query(ADD_NAME_COLUMN)
Query(ADD_NAME_COLUMN).
Query(CREATE_SEED_TABLE)
// Run test
err := ApplyMigrations(context.Background(), pending, conn.MockClient(t), fsys)
// Check error
Expand Down Expand Up @@ -175,6 +176,8 @@ func mockMigrationHistory(conn *pgtest.MockConn) *pgtest.MockConn {
Query(ADD_STATEMENTS_COLUMN).
Reply("ALTER TABLE").
Query(ADD_NAME_COLUMN).
Reply("ALTER TABLE")
Reply("ALTER TABLE").
Query(CREATE_SEED_TABLE).
Reply("CREATE TABLE")
return conn
}
7 changes: 5 additions & 2 deletions pkg/migration/seed.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ func getRemoteSeeds(ctx context.Context, conn *pgx.Conn) (map[string]string, err
var pgErr *pgconn.PgError
if errors.As(err, &pgErr) && pgErr.Code == pgerrcode.UndefinedTable {
// If seed table is undefined, the remote project has no migrations
return map[string]string{}, nil
return nil, nil
}
return map[string]string{}, err
return nil, err
}
applied := make(map[string]string, len(remotes))
for _, seed := range remotes {
Expand All @@ -31,6 +31,9 @@ func getRemoteSeeds(ctx context.Context, conn *pgx.Conn) (map[string]string, err
}

func GetPendingSeeds(ctx context.Context, locals []string, conn *pgx.Conn, fsys fs.FS) ([]SeedFile, error) {
if len(locals) == 0 {
return nil, nil
}
applied, err := getRemoteSeeds(ctx, conn)
if err != nil {
return nil, err
Expand Down
4 changes: 2 additions & 2 deletions pkg/migration/seed_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func TestPendingSeeds(t *testing.T) {
require.Empty(t, seeds)
})

t.Run("throws error on missing seed table", func(t *testing.T) {
t.Run("ignores missing seed table", func(t *testing.T) {
// Setup mock postgres
conn := pgtest.NewConn()
defer conn.Close(t)
Expand All @@ -74,7 +74,7 @@ func TestPendingSeeds(t *testing.T) {
// Run test
_, err := GetPendingSeeds(context.Background(), pending, conn.MockClient(t), testMigrations)
// Check error
assert.ErrorContains(t, err, `ERROR: relation "seed_files" does not exist (SQLSTATE 42P01)`)
assert.NoError(t, err)
})

t.Run("throws error on missing file", func(t *testing.T) {
Expand Down

0 comments on commit 64ec6c4

Please sign in to comment.