-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathquery_sql_and_execute_test.go
185 lines (170 loc) · 4.87 KB
/
query_sql_and_execute_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
package lore
import (
"fmt"
"testing"
"github.com/Masterminds/squirrel"
)
/*
TestSelectModelByPrimaryKey tests the SelectModelByPrimaryKey function.
*/
func TestSelectModelByPrimaryKey(t *testing.T) {
// Test invalid ModelInterface instance.
tmi := createTestModelInvalidInstance()
// Test empty primary key resilience.
pk := tmi.DbPrimaryFieldKey()
if pk != "" {
t.Error("Expected empty primary field key for invalid instance")
return
}
found, err := SelectModelByPrimaryKey(tmi, nil, nil)
if found != false || err == nil {
t.Error("Expected not found and non-nil err for invalid instance with empty primary key")
return
}
tm := createTestModelInstance()
db, dbMock := getTestSqlxDb(t)
dbMock.ExpectQuery(fmt.Sprintf(
"^SELECT \\* FROM %s WHERE %s =", _TEST_DB_TABLENAME, _TEST_DB_FIELDNAME_ID,
)).WithArgs(_TEST_MODEL_ID)
SelectModelByPrimaryKey(tm, db, newTestModelEmpty())
err = dbMock.ExpectationsWereMet()
if err != nil {
t.Error(err)
return
}
}
/*
TestSelectModelWhere tests the SelectModelWhere function.
*/
func TestSelectModelWhere(t *testing.T) {
tm := createTestModelInstance()
db, dbMock := getTestSqlxDb(t)
dbMock.ExpectQuery(fmt.Sprintf(
"^SELECT \\* FROM %s WHERE %s", _TEST_DB_TABLENAME, _TEST_DB_FIELDNAME_FIELD,
)).WithArgs(_TEST_MODEL_FIELD)
SelectModelWhere(tm, db, newSqlPart(squirrel.Eq{
_TEST_DB_FIELDNAME_FIELD: _TEST_MODEL_FIELD,
}), newTestModelEmpty())
err := dbMock.ExpectationsWereMet()
if err != nil {
t.Error(err)
return
}
}
/*
TestSelectModelsWhere tests the SelectModelsWhere function.
*/
func TestSelectModelsWhere(t *testing.T) {
tm := createTestModelInstance()
db, dbMock := getTestSqlxDb(t)
dbMock.ExpectQuery(fmt.Sprintf(
"^SELECT \\* FROM %s WHERE %s.*LIMIT 3", _TEST_DB_TABLENAME, _TEST_DB_FIELDNAME_FIELD,
)).WithArgs(_TEST_MODEL_FIELD)
var limit uint64
limit = 3
SelectModelsWhere(tm, db, newSqlPart(squirrel.Eq{
_TEST_DB_FIELDNAME_FIELD: _TEST_MODEL_FIELD,
}), &limit, newTestModelEmptyList())
err := dbMock.ExpectationsWereMet()
if err != nil {
t.Error(err)
return
}
}
/*
TestInsertNewModel tests the InsertNewModel function.
TODO: Find way to test args in a way that is robust to random changes in argument order injected via
Squirrel.
*/
func TestInsertNewModel(t *testing.T) {
tm := createTestModelInstance()
db, dbMock := getTestSqlxDb(t)
dbMock.ExpectQuery(fmt.Sprintf(
"^INSERT INTO %s.*RETURNING \\*", _TEST_DB_TABLENAME,
))
InsertNewModel(tm, db, newTestModelEmpty())
err := dbMock.ExpectationsWereMet()
if err != nil {
t.Error(err)
return
}
}
/*
TestUpdateModelByPrimaryKey tests the UpdateModelByPrimaryKey function.
TODO: Find way to test args in a way that is robust to random changes in argument order injected via
Squirrel.
*/
func TestUpdateModelByPrimaryKey(t *testing.T) {
tm := createTestModelInstance()
db, dbMock := getTestSqlxDb(t)
dbMock.ExpectQuery(fmt.Sprintf(
"^UPDATE %s SET .* WHERE %s.*RETURNING \\*", _TEST_DB_TABLENAME, _TEST_DB_FIELDNAME_ID,
))
UpdateModelByPrimaryKey(tm, db, newTestModelEmpty())
err := dbMock.ExpectationsWereMet()
if err != nil {
t.Error(err)
return
}
}
/*
TestUpdateSetMapWhere tests the UpdateSetMapWhere function.
*/
func TestUpdateSetMapWhere(t *testing.T) {
tm := createTestModelInstance()
db, dbMock := getTestSqlxDb(t)
dbMock.ExpectQuery(fmt.Sprintf(
"^UPDATE %s SET %s =.* WHERE %s =.*RETURNING \\*",
_TEST_DB_TABLENAME,
_TEST_DB_FIELDNAME_FIELD,
_TEST_DB_FIELDNAME_ID,
)).WithArgs(_TEST_MODEL_FIELD+1, _TEST_MODEL_ID)
UpdateSetMapWhere(tm, db, map[string]interface{}{
_TEST_DB_FIELDNAME_FIELD: _TEST_MODEL_FIELD + 1,
}, newSqlPart(squirrel.Eq{
_TEST_DB_FIELDNAME_ID: _TEST_MODEL_ID,
}), newTestModelEmptyList())
err := dbMock.ExpectationsWereMet()
if err != nil {
t.Error(err)
return
}
}
/*
TestDeleteModelByPrimaryKey tests the DeleteModelByPrimaryKey function.
*/
func TestDeleteModelByPrimaryKey(t *testing.T) {
tm := createTestModelInstance()
db, dbMock := getTestSqlxDb(t)
dbMock.ExpectQuery(fmt.Sprintf(
"^DELETE FROM %s WHERE %s =.*RETURNING \\*", _TEST_DB_TABLENAME, _TEST_DB_FIELDNAME_ID,
)).WithArgs(_TEST_MODEL_ID)
DeleteModelByPrimaryKey(tm, db, newTestModelEmpty())
err := dbMock.ExpectationsWereMet()
if err != nil {
t.Error(err)
return
}
}
/*
TestDeleteModelsWhere tests the DeleteModelsWhere function.
TODO: Find way to test multiple WHERE args in a way that is robust to random changes in argument
order injected via Squirrel.
*/
func TestDeleteModelsWhere(t *testing.T) {
tm := createTestModelInstance()
db, dbMock := getTestSqlxDb(t)
dbMock.ExpectQuery(fmt.Sprintf(
"^DELETE FROM %s WHERE %s = .* RETURNING \\*",
_TEST_DB_TABLENAME,
_TEST_DB_FIELDNAME_FIELD,
)).WithArgs(_TEST_MODEL_FIELD)
DeleteModelsWhere(tm, db, newSqlPart(squirrel.Eq{
_TEST_DB_FIELDNAME_FIELD: _TEST_MODEL_FIELD,
}), newTestModelEmptyList())
err := dbMock.ExpectationsWereMet()
if err != nil {
t.Error(err)
return
}
}