-
Notifications
You must be signed in to change notification settings - Fork 0
/
export_test.go
78 lines (72 loc) · 1.99 KB
/
export_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package ripoff
import (
"context"
"fmt"
"os"
"path"
"runtime"
"strings"
"testing"
"github.com/jackc/pgx/v5"
"github.com/lib/pq"
"github.com/stretchr/testify/require"
)
func runExportTestData(t *testing.T, ctx context.Context, tx pgx.Tx, testDir string) {
// Set up schema and initial rows.
setupFile, err := os.ReadFile(path.Join(testDir, "setup.sql"))
require.NoError(t, err)
_, err = tx.Exec(ctx, string(setupFile))
require.NoError(t, err)
// Generate new ripoff file.
ripoffFile, err := ExportToRipoff(ctx, tx)
require.NoError(t, err)
// Wipe database.
truncateFile, err := os.ReadFile(path.Join(testDir, "truncate.sql"))
require.NoError(t, err)
_, err = tx.Exec(ctx, string(truncateFile))
require.NoError(t, err)
// Run generated ripoff.
err = RunRipoff(ctx, tx, ripoffFile)
require.NoError(t, err)
// Try to verify that the number of generated rows matches the ripoff.
tableCount := map[string]int{}
for rowId := range ripoffFile.Rows {
tableName := strings.Split(rowId, ":")
if len(tableName) > 0 {
tableCount[tableName[0]]++
}
}
for tableName, expectedCount := range tableCount {
row := tx.QueryRow(ctx, fmt.Sprintf("SELECT COUNT(*) FROM %s;", pq.QuoteIdentifier(tableName)))
var realCount int
err := row.Scan(&realCount)
require.NoError(t, err)
require.Equal(t, expectedCount, realCount)
}
}
func TestRipoffExport(t *testing.T) {
envUrl := os.Getenv("RIPOFF_TEST_DATABASE_URL")
if envUrl == "" {
envUrl = "postgres:///ripoff-test-db"
}
ctx := context.Background()
conn, err := pgx.Connect(ctx, envUrl)
if err != nil {
require.NoError(t, err)
}
defer conn.Close(ctx)
_, filename, _, _ := runtime.Caller(0)
dir := path.Join(path.Dir(filename), "testdata", "export")
dirEntry, err := os.ReadDir(dir)
require.NoError(t, err)
for _, e := range dirEntry {
if !e.IsDir() {
continue
}
tx, err := conn.Begin(ctx)
require.NoError(t, err)
runExportTestData(t, ctx, tx, path.Join(dir, e.Name()))
err = tx.Rollback(ctx)
require.NoError(t, err)
}
}