-
Notifications
You must be signed in to change notification settings - Fork 0
/
operations_test.go
279 lines (245 loc) · 7.02 KB
/
operations_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
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
// Package structql implements the Database structure.
// This file contains tests for parse.go.
package structql
import (
"fmt"
"reflect"
"testing"
)
// TestSelectFromWhere tests the (*Connection).SelectFromWhere() method.
func TestSelectFromWhere(t *testing.T) {
type Person struct {
ID int32 `sql:"id" typ:"SERIAL"`
Name string `sql:"name"`
}
personA := Person{1, "A"}
personB := Person{2, "B"}
personC := Person{3, "C"}
tests := []struct {
cond string
args []interface{}
wantPeople []Person
}{
{
"",
[]interface{}{},
[]Person{personA, personB, personC},
}, {
"name = 'C'",
[]interface{}{},
[]Person{personC},
}, {
"id <= 2",
[]interface{}{},
[]Person{personA, personB},
}, {
"id <= 2 AND name = 'A'",
[]interface{}{},
[]Person{personA},
}, {
"id = %d",
[]interface{}{3},
[]Person{personC},
},
}
// Create a suitable table in the test database.
conn := createTableUnsafe("People", Person{})
defer conn.Close()
defer conn.DropTable("People")
for _, person := range []Person{personA, personB, personC} {
if _, err := conn.InsertObject("People", person); err != nil {
t.Fatalf("Failed to insert Person %v: %v.", person, err)
}
}
for i, test := range tests {
// Execute the SELECT FROM WHERE query.
people, err := conn.SelectFromWhere(Person{}, "People", test.cond, test.args...)
if err != nil {
t.Errorf("TestSelectFromWhere()[%d] - failed to execute query: %v.", i, err)
continue
}
// Cast the []interface{} slice into a []Person{} slice.
havePeople := make([]Person, 0, len(people))
for _, personI := range people {
person := personI.(Person)
havePeople = append(havePeople, person)
}
// Compare the retrieved and expected Person slices.
if !reflect.DeepEqual(havePeople, test.wantPeople) {
t.Errorf("TestSelectFromWhere()[%d] = %v, want people %v.", i, havePeople, test.wantPeople)
}
}
}
// TestInsertObject tests the (*Connection).InsertObject() method.
func TestInsertObject(t *testing.T) {
type Person struct {
ID int16 `sql:"id" typ:"SMALLSERIAL"`
Name string `sql:"name"`
Age int32 `sql:"age"`
DNA []byte `sql:"dna"`
}
tests := []struct {
person Person
}{
{
Person{
ID: 1,
Name: "",
Age: 0,
DNA: []byte{},
},
}, {
Person{
ID: 2,
Name: "John Cena",
Age: 42,
DNA: []byte{1, 2, 3},
},
},
}
conn := createTableUnsafe("People", Person{})
defer conn.Close()
defer conn.DropTable("People")
for i, test := range tests {
// Insert the Person into the database.
haveID, err := conn.InsertObject("People", test.person)
if err != nil {
t.Errorf("TestInsertObject()[%d] - failed to insert object: %v.", i, err)
continue
}
// Verify that returned record ID scales with the test index.
if wantID := i + 1; haveID != wantID {
t.Errorf("TestInsertObject()[%d] = %d, want record ID %v.", i, haveID, wantID)
}
// Retrieve the Person from the database.
query := fmt.Sprintf(`SELECT * FROM People WHERE name = '%s'`, test.person.Name)
rows, err := conn.query(query)
if err != nil {
t.Errorf("TestInsertObject()[%d] - failed to execute query: %v.", i, err)
continue
}
people, err := parseResponse(rows, Person{})
if err != nil {
t.Errorf("TestInsertObject()[%d] - failed to parse response: %v.", i, err)
continue
}
if len(people) != 1 {
t.Errorf("TestInsertObject()[%d] = %d, want 1 Person.", i, len(people))
continue
}
// Verify that the retrieved Person is the same Person that was inserted.
person := people[0]
if !reflect.DeepEqual(test.person, person) {
t.Errorf("TestInsertObject()[%d] = %v, want Person %v.", i, person, test.person)
}
}
}
// TestUpdateObject tests the (*Connection).UpdateObject() method.
func TestUpdateObject(t *testing.T) {
type Person struct {
ID int32 `sql:"id" opt:"PRIMARY KEY"`
Name string `sql:"name"`
}
tests := []struct {
person Person
}{
{
Person{
ID: 1,
Name: "Joseph",
},
}, {
Person{
ID: 1,
Name: "Faith",
},
},
}
conn := createTableUnsafe("People", Person{})
defer conn.Close()
defer conn.DropTable("People")
base := Person{ID: 1, Name: "Rook"}
if _, err := conn.InsertObject("People", base); err != nil {
t.Fatalf("Failed to insert %#v into table: %v.", base, err)
}
for i, test := range tests {
if err := conn.UpdateObject("People", test.person); err != nil {
t.Errorf("TestUpdateObject()[%d] - failed to update object: %v.", i, err)
continue
}
people, err := conn.SelectFrom(Person{}, "People")
if err != nil {
t.Errorf("TestUpdateObject()[%d] - failed to query database: %v.", i, err)
} else if len(people) != 1 {
t.Errorf("TestUpdateObject()[%d] = %d, want 1 Person.", i, len(people))
} else if !reflect.DeepEqual(test.person, people[0]) {
t.Errorf("TestUpdateObject()[%d] = %v, want Person %v.", i, people[0], test.person)
}
}
}
// TestDeleteObject tests the (*Connection).DeleteObject() method.
func TestDeleteObject(t *testing.T) {
type Pizza struct {
ID int32 `sql:"id" opt:"PRIMARY KEY"`
Topping string `sql:"topping"`
}
cheese := Pizza{1, "Cheese"}
deluxe := Pizza{2, "Deluxe"}
tests := []struct {
pizza Pizza
wantPizzas []Pizza
}{
{
Pizza{3, "Pepperoni"},
[]Pizza{cheese, deluxe},
}, {
cheese,
[]Pizza{deluxe},
}, {
Pizza{3, "Pepperoni"},
[]Pizza{deluxe},
}, {
deluxe,
[]Pizza{},
},
}
conn := createTableUnsafe("Pizza", Pizza{})
defer conn.Close()
defer conn.DropTable("Pizza")
for _, pizza := range []Pizza{cheese, deluxe} {
if _, err := conn.InsertObject("Pizza", pizza); err != nil {
t.Fatalf("Failed to insert Pizza %v: %v.", pizza, err)
}
}
for i, test := range tests {
if err := conn.DeleteObject("Pizza", test.pizza); err != nil {
t.Errorf("TestDeleteObject()[%d] - failed to delete Pizza: %v.", i, err)
continue
}
rows, err := conn.SelectFrom(Pizza{}, "Pizza")
if err != nil {
t.Errorf("TestDeleteObject()[%d] - failed to select Pizza: %v.", i, err)
continue
}
havePizzas := make([]Pizza, len(rows))
for i, row := range rows {
havePizzas[i] = row.(Pizza)
}
if !reflect.DeepEqual(havePizzas, test.wantPizzas) {
t.Errorf("TestDeleteObject()[%d] = %v, want Pizza %v.", i, havePizzas, test.wantPizzas)
}
}
}
// createTableUnsafe constructs a database Connection and creates a table with
// the given name from the provided object. Failure to do so results in a panic.
func createTableUnsafe(table string, object interface{}) *Connection {
creds := GetTestCreds()
conn, err := Connect(creds)
if err != nil {
panic(fmt.Sprintf("Failed to construct Connection: %v.", err))
}
if err := conn.CreateTableFromObject(table, object); err != nil {
panic(fmt.Sprintf("Failed to create table %q: %v.", table, err))
}
return conn
}