-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsqlite_syntax_test.go
109 lines (103 loc) · 2.53 KB
/
sqlite_syntax_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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
package gobase
import (
"testing"
)
func TestSqLiteCreateTable(t *testing.T) {
fileName := "./testdata/create_table.go"
expectedUpQuery := "CREATE TABLE users (\n\tid INTEGER,\n\tname TEXT,\n\tcreated_at DATETIME,\n\tupdated_at DATETIME,\n\tis_member BOOLEAN\n);"
expectedDownQuery := "DROP TABLE users;"
schema := Parse(fileName)
outputUpQuery, outputDownQuery := SqLiteCreateTable(schema)
if expectedUpQuery != outputUpQuery {
t.Errorf("Up query err. expected=%s. got=%s", expectedUpQuery, outputUpQuery)
}
if expectedDownQuery != outputDownQuery {
t.Errorf("Down query err. expected=%s. got=%s", expectedDownQuery, outputDownQuery)
}
}
func TestSqliteMigration(t *testing.T) {
tests := []struct {
name string
change ChangeLog
expUp string
expDown string
}{
{
name: "No Change",
change: ChangeLog{},
expUp: "",
expDown: "",
},
{
name: "Field Creation",
change: ChangeLog{
Creations: []Create{
{
CreationType: FIELD,
ON: ONTABLE,
TableName: "users",
CreationData: "id:int",
},
},
},
expUp: "ALTER TABLE users\nADD COLUMN id INTEGER;\n",
expDown: "ALTER TABLE users\nDROP COLUMN id;\n",
},
{
name: "Field Deletion",
change: ChangeLog{
Deletions: []Delete{
{
DeletionType: FIELD,
ON: ONTABLE,
TableName: "users",
DeletionData: "id:int",
},
},
},
expUp: "ALTER TABLE users\nDROP COLUMN id;\n",
expDown: "ALTER TABLE users\nADD COLUMN id INTEGER;\n",
},
{
name: "Table Rename",
change: ChangeLog{
Updates: []Update{
{
UpdateType: NAMEUPDATE,
ON: ONTABLE,
TableName: "users",
UpdateData: "accounts",
},
},
},
expUp: "ALTER TABLE users\nRENAME TO accounts;\n",
expDown: "ALTER TABLE accounts\nRENAME TO users;\n",
},
{
name: "Field Rename",
change: ChangeLog{
Updates: []Update{
{
UpdateType: NAMEUPDATE,
ON: ONFIELD,
TableName: "users",
UpdateData: "id:user_id",
},
},
},
expUp: "ALTER TABLE users\nRENAME COLUMN id to user_id;\n",
expDown: "ALTER TABLE users\nRENAME COLUMN user_id to id;\n",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
gotUp, gotDown := SqliteMigration(tt.change)
if gotUp != tt.expUp {
t.Errorf("Up Mig Not Same\nExpected: %s\nGot: %s", tt.expUp, gotUp)
}
if gotDown != tt.expDown {
t.Errorf("Down Mig Not Same\nExpected: %s\nGot: %s", tt.expDown, gotDown)
}
})
}
}