-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathexport_test.go
More file actions
401 lines (354 loc) · 12.4 KB
/
Copy pathexport_test.go
File metadata and controls
401 lines (354 loc) · 12.4 KB
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
package ripoff
import (
"context"
"fmt"
"os"
"path"
"runtime"
"strings"
"testing"
"github.com/jackc/pgx/v5"
"github.com/lib/pq"
"github.com/stretchr/testify/require"
"gopkg.in/yaml.v3"
)
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, []string{}, []string{})
require.NoError(t, err)
// Ensure ripoff file matches expected output.
// The marshal/unmashal dance here lets us ensure everything is an interface{}
newRipoffFile := &RipoffFile{}
newRipoffBytes, err := yaml.Marshal(ripoffFile)
require.NoError(t, err)
err = yaml.Unmarshal(newRipoffBytes, newRipoffFile)
require.NoError(t, err)
expectedRipoffYaml, err := os.ReadFile(path.Join(testDir, "ripoff.yml"))
require.NoError(t, err)
expectedRipoffFile := &RipoffFile{}
err = yaml.Unmarshal(expectedRipoffYaml, expectedRipoffFile)
require.NoError(t, err)
expectedRipoffFile.Plugins = nil
newRipoffFile.Plugins = nil
require.Equal(t, expectedRipoffFile, newRipoffFile)
// 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, DEFAULT_MAX_CONCURRENCY)
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)
require.NoError(t, err)
defer func() {
err := conn.Close(ctx)
require.NoError(t, err)
}()
_, 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)
}
}
// TestExcludeFlag tests that the exclude flag properly excludes tables from export
func TestExcludeFlag(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)
require.NoError(t, err)
defer func() {
err := conn.Close(ctx)
require.NoError(t, err)
}()
// Start a transaction that we'll roll back at the end
tx, err := conn.Begin(ctx)
require.NoError(t, err)
defer func() {
err := tx.Rollback(ctx)
require.NoError(t, err)
}()
// Create three tables - one we'll include and two we'll exclude
_, err = tx.Exec(ctx, `
CREATE TABLE include_me (
id SERIAL PRIMARY KEY,
name TEXT
);
CREATE TABLE exclude_me (
id SERIAL PRIMARY KEY,
description TEXT
);
CREATE TABLE also_exclude_me (
id SERIAL PRIMARY KEY,
data TEXT
);
INSERT INTO include_me (name) VALUES ('test data 1'), ('test data 2');
INSERT INTO exclude_me (description) VALUES ('should not appear'), ('also should not appear');
INSERT INTO also_exclude_me (data) VALUES ('should not appear'), ('also should not appear'), ('third row');
`)
require.NoError(t, err)
// Test 1: Exclude a single table
t.Run("Single exclude", func(t *testing.T) {
ripoffFile, err := ExportToRipoff(ctx, tx, []string{"exclude_me"}, []string{})
require.NoError(t, err)
// Verify that ripoffFile.Rows contains rows from include_me but not exclude_me
hasIncludeMe := false
hasExcludeMe := false
hasAlsoExcludeMe := false
for rowId := range ripoffFile.Rows {
tableName := strings.Split(rowId, ":")
if len(tableName) > 0 {
if tableName[0] == "include_me" {
hasIncludeMe = true
}
if tableName[0] == "exclude_me" {
hasExcludeMe = true
}
if tableName[0] == "also_exclude_me" {
hasAlsoExcludeMe = true
}
}
}
// We should have rows from include_me
require.True(t, hasIncludeMe, "Expected to find rows from include_me table")
// We should NOT have rows from exclude_me
require.False(t, hasExcludeMe, "Found rows from exclude_me table even though it was excluded")
// We should have rows from also_exclude_me (since it wasn't excluded in this test)
require.True(t, hasAlsoExcludeMe, "Expected to find rows from also_exclude_me table")
// Count rows to make sure we have the right number
includeCount := 0
excludeCount := 0
alsoExcludeCount := 0
for rowId := range ripoffFile.Rows {
tableName := strings.Split(rowId, ":")
if len(tableName) > 0 {
if tableName[0] == "include_me" {
includeCount++
}
if tableName[0] == "exclude_me" {
excludeCount++
}
if tableName[0] == "also_exclude_me" {
alsoExcludeCount++
}
}
}
require.Equal(t, 2, includeCount, "Expected 2 rows from include_me table")
require.Equal(t, 0, excludeCount, "Expected 0 rows from exclude_me table")
require.Equal(t, 3, alsoExcludeCount, "Expected 3 rows from also_exclude_me table")
})
// Test 2: Exclude multiple tables
t.Run("Multiple excludes", func(t *testing.T) {
ripoffFile, err := ExportToRipoff(ctx, tx, []string{"exclude_me", "also_exclude_me"}, []string{})
require.NoError(t, err)
// Verify that ripoffFile.Rows contains rows from include_me but not from the excluded tables
hasIncludeMe := false
hasExcludeMe := false
hasAlsoExcludeMe := false
for rowId := range ripoffFile.Rows {
tableName := strings.Split(rowId, ":")
if len(tableName) > 0 {
if tableName[0] == "include_me" {
hasIncludeMe = true
}
if tableName[0] == "exclude_me" {
hasExcludeMe = true
}
if tableName[0] == "also_exclude_me" {
hasAlsoExcludeMe = true
}
}
}
// We should have rows from include_me
require.True(t, hasIncludeMe, "Expected to find rows from include_me table")
// We should NOT have rows from exclude_me
require.False(t, hasExcludeMe, "Found rows from exclude_me table even though it was excluded")
// We should NOT have rows from also_exclude_me
require.False(t, hasAlsoExcludeMe, "Found rows from also_exclude_me table even though it was excluded")
// Count rows to make sure we have the right number
includeCount := 0
excludeCount := 0
alsoExcludeCount := 0
for rowId := range ripoffFile.Rows {
tableName := strings.Split(rowId, ":")
if len(tableName) > 0 {
if tableName[0] == "include_me" {
includeCount++
}
if tableName[0] == "exclude_me" {
excludeCount++
}
if tableName[0] == "also_exclude_me" {
alsoExcludeCount++
}
}
}
require.Equal(t, 2, includeCount, "Expected 2 rows from include_me table")
require.Equal(t, 0, excludeCount, "Expected 0 rows from exclude_me table")
require.Equal(t, 0, alsoExcludeCount, "Expected 0 rows from also_exclude_me table")
})
}
// TestExcludeColumnsFlag tests that the exclude-columns flag properly excludes columns from export
func TestExcludeColumnsFlag(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)
require.NoError(t, err)
defer func() {
err := conn.Close(ctx)
require.NoError(t, err)
}()
// Start a transaction that we'll roll back at the end
tx, err := conn.Begin(ctx)
require.NoError(t, err)
defer func() {
err := tx.Rollback(ctx)
require.NoError(t, err)
}()
// Create test tables with timestamped columns
_, err = tx.Exec(ctx, `
CREATE TABLE users (
id SERIAL PRIMARY KEY,
name TEXT,
email TEXT,
created_at TIMESTAMP DEFAULT NOW(),
updated_at TIMESTAMP DEFAULT NOW()
);
CREATE TABLE posts (
id SERIAL PRIMARY KEY,
title TEXT,
content TEXT,
user_id INTEGER REFERENCES users(id),
created_at TIMESTAMP DEFAULT NOW(),
updated_at TIMESTAMP DEFAULT NOW()
);
INSERT INTO users (name, email) VALUES ('Alice', 'alice@example.com'), ('Bob', 'bob@example.com');
INSERT INTO posts (title, content, user_id) VALUES
('Post 1', 'Content 1', 1),
('Post 2', 'Content 2', 1),
('Post 3', 'Content 3', 2);
`)
require.NoError(t, err)
// Test 1: Exclude global columns (created_at, updated_at)
t.Run("Global column exclusion", func(t *testing.T) {
ripoffFile, err := ExportToRipoff(ctx, tx, []string{}, []string{"created_at", "updated_at"})
require.NoError(t, err)
// Verify that no row contains created_at or updated_at columns
for rowId, row := range ripoffFile.Rows {
_, hasCreatedAt := row["created_at"]
_, hasUpdatedAt := row["updated_at"]
require.False(t, hasCreatedAt, "Row %s should not have created_at column", rowId)
require.False(t, hasUpdatedAt, "Row %s should not have updated_at column", rowId)
// But should still have other columns
tableName := strings.Split(rowId, ":")[0]
switch tableName {
case "users":
_, hasName := row["name"]
_, hasEmail := row["email"]
require.True(t, hasName, "Row %s should have name column", rowId)
require.True(t, hasEmail, "Row %s should have email column", rowId)
case "posts":
_, hasTitle := row["title"]
_, hasContent := row["content"]
require.True(t, hasTitle, "Row %s should have title column", rowId)
require.True(t, hasContent, "Row %s should have content column", rowId)
}
}
})
// Test 2: Exclude table-specific column (users.created_at) - shared column name
t.Run("Table-specific column exclusion", func(t *testing.T) {
ripoffFile, err := ExportToRipoff(ctx, tx, []string{}, []string{"users.created_at"})
require.NoError(t, err)
// Verify that user rows don't have created_at but post rows still have created_at
for rowId, row := range ripoffFile.Rows {
tableName := strings.Split(rowId, ":")[0]
switch tableName {
case "users":
_, hasCreatedAt := row["created_at"]
require.False(t, hasCreatedAt, "User row %s should not have created_at column", rowId)
// Should still have other columns
_, hasName := row["name"]
_, hasEmail := row["email"]
require.True(t, hasName, "User row %s should have name column", rowId)
require.True(t, hasEmail, "User row %s should have email column", rowId)
case "posts":
// Posts should have created_at since only users.created_at was excluded
_, hasTitle := row["title"]
_, hasCreatedAt := row["created_at"]
require.True(t, hasTitle, "Post row %s should have title column", rowId)
require.True(t, hasCreatedAt, "Post row %s should have created_at column", rowId)
}
}
})
// Test 3: Combine both exclusion types
t.Run("Combined exclusions", func(t *testing.T) {
ripoffFile, err := ExportToRipoff(ctx, tx, []string{}, []string{"created_at", "users.email"})
require.NoError(t, err)
// Verify exclusions are applied correctly
for rowId, row := range ripoffFile.Rows {
tableName := strings.Split(rowId, ":")[0]
// No row should have created_at (global exclusion)
_, hasCreatedAt := row["created_at"]
require.False(t, hasCreatedAt, "Row %s should not have created_at column", rowId)
switch tableName {
case "users":
// Users should not have email (table-specific exclusion)
_, hasEmail := row["email"]
require.False(t, hasEmail, "User row %s should not have email column", rowId)
// But should have name and updated_at
_, hasName := row["name"]
_, hasUpdatedAt := row["updated_at"]
require.True(t, hasName, "User row %s should have name column", rowId)
require.True(t, hasUpdatedAt, "User row %s should have updated_at column", rowId)
case "posts":
// Posts should have all columns except created_at
_, hasTitle := row["title"]
_, hasUpdatedAt := row["updated_at"]
require.True(t, hasTitle, "Post row %s should have title column", rowId)
require.True(t, hasUpdatedAt, "Post row %s should have updated_at column", rowId)
}
}
})
}