forked from schollz/sqlite3dump
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdump_test.go
92 lines (80 loc) · 2.17 KB
/
dump_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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package sqlite3dump
import (
"bufio"
"bytes"
"database/sql"
"io/ioutil"
"path/filepath"
"strings"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func assertEqualIgnoreLineSeparators(t *testing.T, expected []byte, actual []byte) {
expectedStr := strings.ReplaceAll(string(expected), "\r\n", "\n")
actualStr := strings.ReplaceAll(string(actual), "\r\n", "\n")
assert.Equal(t, expectedStr, actualStr)
}
func TestCars(t *testing.T) {
var b bytes.Buffer
out := bufio.NewWriter(&b)
err := Dump("testdata/cars.db", out)
assert.Nil(t, err)
out.Flush()
pythonOutput, _ := ioutil.ReadFile("testdata/python.sql")
assertEqualIgnoreLineSeparators(t, pythonOutput, b.Bytes())
ioutil.WriteFile("out.sql", b.Bytes(), 0644)
}
func TestMigrate(t *testing.T) {
var b bytes.Buffer
out := bufio.NewWriter(&b)
db, err := sql.Open("sqlite3", "testdata/cars.db")
assert.Nil(t, err)
defer db.Close()
err = DumpMigration(db, out)
assert.Nil(t, err)
out.Flush()
pythonOutput, _ := ioutil.ReadFile("testdata/migrate.sql")
assertEqualIgnoreLineSeparators(t, pythonOutput, b.Bytes())
}
func TestDump(t *testing.T) {
cases := map[string]struct {
dbFile string
expectFile string
options []Option
}{
"No Options": {
dbFile: "cars.db",
expectFile: "python.sql",
},
"WithMigration": {
dbFile: "cars.db",
expectFile: "migrate.sql",
options: []Option{WithMigration()},
},
"WithDropIfExists": {
dbFile: "cars.db",
expectFile: "drop_if_exists.sql",
options: []Option{WithDropIfExists(true)},
},
"WithTransaction - false": {
dbFile: "cars.db",
expectFile: "without_tx.sql",
options: []Option{WithTransaction(false)},
},
}
for name, c := range cases {
t.Run(name, func(t *testing.T) {
expect, err := ioutil.ReadFile(filepath.Join("testdata", c.expectFile))
require.NoError(t, err, "failed to open expect file")
var b bytes.Buffer
out := bufio.NewWriter(&b)
dbFilePath := filepath.Join("testdata", c.dbFile)
err = Dump(dbFilePath, out, c.options...)
require.NoError(t, err)
out.Flush()
got := b.Bytes()
assertEqualIgnoreLineSeparators(t, expect, got)
})
}
}