-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathquery_execute_test.go
181 lines (160 loc) · 4.94 KB
/
query_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
package lore
import (
"fmt"
"testing"
sqlmock "gopkg.in/DATA-DOG/go-sqlmock.v1"
"github.com/jmoiron/sqlx"
)
/*
getTestSqlxDb returns a sqlx.DB for testing, along with a mock handle.
*/
func getTestSqlxDb(t *testing.T) (*sqlx.DB, sqlmock.Sqlmock) {
mockDb, mock, err := sqlmock.New()
if err != nil {
t.Fatalf("Error getting test mock sqlx db: %s", err.Error())
}
sqlxDb := sqlx.NewDb(mockDb, "sqlmock")
return sqlxDb, mock
}
/*
TestExecuteUnexported tests the (unexported) Query execute method.
*/
func TestExecuteUnexported(t *testing.T) {
db, _ := getTestSqlxDb(t)
// Build test query.
q := NewQuery(newTestModelEmpty())
q.SetSqlBuilder(
q.BuildSqlSelectStar(),
)
// Test nil db resilience.
numRowsAffected, err := q.execute(nil, nil, _EXECUTE_MODE_NO_PARSE)
if numRowsAffected != 0 || err == nil {
t.Errorf("Expected 0 rows affected and non-empty err since nil db")
return
}
// Test invalid mode resilience.
numRowsAffected, err = q.execute(db, nil, 42)
if numRowsAffected != 0 || err == nil {
t.Errorf("Expected 0 rows affected and non-empty err since invalid mode")
return
}
// Build test/mock db.
db, dbMock := getTestSqlxDb(t)
// Test single-row query.
rows := sqlmock.NewRows([]string{_TEST_DB_FIELDNAME_ID, _TEST_DB_FIELDNAME_FIELD}).
AddRow(_TEST_MODEL_ID, _TEST_MODEL_FIELD)
dbMock.ExpectQuery(fmt.Sprintf("^SELECT \\* FROM %s", _TEST_DB_TABLENAME)).
WillReturnRows(rows)
numRowsAffected, err = q.execute(db, newTestModelEmpty(), _EXECUTE_MODE_PARSE_SINGLE)
if err != nil {
t.Error(err)
return
}
if numRowsAffected != 1 {
t.Errorf("Expect numRowsAffected equal to 1, but got: %d", numRowsAffected)
return
}
// Add another row and test multi-row query.
rows = sqlmock.NewRows([]string{_TEST_DB_FIELDNAME_ID, _TEST_DB_FIELDNAME_FIELD}).
AddRow(_TEST_MODEL_ID, _TEST_MODEL_FIELD).
AddRow(_TEST_MODEL_ID+1, _TEST_MODEL_FIELD+1)
dbMock.ExpectQuery(fmt.Sprintf("^SELECT \\* FROM %s", _TEST_DB_TABLENAME)).
WillReturnRows(rows)
numRowsAffected, err = q.execute(db, newTestModelEmptyList(), _EXECUTE_MODE_PARSE_LIST)
if err != nil {
t.Error(err)
return
}
if numRowsAffected != 2 {
t.Errorf("Expect numRowsAffected equal to 2, but got: %d", numRowsAffected)
return
}
}
/*
TestExecute tests the Query Execute method using a mock db.
*/
func TestExecute(t *testing.T) {
// Build test query.
q := NewQuery(newTestModelEmpty())
q.SetSqlBuilder(
q.BuildSqlSelectStar(),
)
// Build test/mock db.
db, dbMock := getTestSqlxDb(t)
dbMock.ExpectExec(fmt.Sprintf("^SELECT \\* FROM %s", _TEST_DB_TABLENAME)).WillReturnResult(sqlmock.NewResult(1, 1))
numRowsAffected, err := q.Execute(db)
if err != nil {
t.Errorf("Error in Execute: %s", err.Error())
return
}
if numRowsAffected != 1 {
t.Errorf("Unexpected numRowsAffected (%d) != 1", numRowsAffected)
return
}
err = dbMock.ExpectationsWereMet()
if err != nil {
t.Error(err)
return
}
}
/*
TestExecuteThenParseSingle tests the Query ExecuteThenParseSingle method using a mock db.
TODO: Better testing of return result.
TODO: Test invalid model interface.
*/
func TestExecuteThenParseSingle(t *testing.T) {
// Build test query.
q := NewQuery(newTestModelEmpty())
q.SetSqlBuilder(
q.BuildSqlSelectStar().Limit(1).Suffix(RETURNING_STAR),
)
// Build test/mock db.
db, dbMock := getTestSqlxDb(t)
dbMock.ExpectQuery(fmt.Sprintf("^SELECT \\* FROM %s LIMIT 1 RETURNING \\*", _TEST_DB_TABLENAME)).
WillReturnRows(sqlmock.NewRows([]string{}))
found, err := q.ExecuteThenParseSingle(db, newTestModelEmpty())
if err != nil {
t.Errorf("Error in ExecuteThenParseList: %s", err.Error())
return
}
if found {
t.Errorf("Unexpected found = %t", found)
return
}
// Should get error if pass in empty list to ExecuteThenParseSingle.
_, err = q.ExecuteThenParseSingle(db, newTestModelEmptyList())
if err == nil {
t.Error("Expected error from passing in list instead of single to ExecuteThenParseSingle, but got no such error")
return
}
}
/*
TestExecuteThenParseList tests the Query ExecuteThenParseList method using a mock db.
TODO: Better testing of return result.
*/
func TestExecuteThenParseList(t *testing.T) {
// Build test query.
q := NewQuery(newTestModelEmpty())
q.SetSqlBuilder(
q.BuildSqlSelectStar().Suffix(RETURNING_STAR),
)
// Build test/mock db.
db, dbMock := getTestSqlxDb(t)
dbMock.ExpectQuery(fmt.Sprintf("^SELECT \\* FROM %s RETURNING \\*", _TEST_DB_TABLENAME)).
WillReturnRows(sqlmock.NewRows([]string{}))
numRowsAffected, err := q.ExecuteThenParseList(db, newTestModelEmptyList())
if err != nil {
t.Errorf("Error in ExecuteThenParseList: %s", err.Error())
return
}
if numRowsAffected != 0 {
t.Errorf("Unexpected numRowsAffected (%d) != 0", numRowsAffected)
return
}
// Should get error if pass in empty-but-not-list to ExecuteThenParseList.
_, err = q.ExecuteThenParseList(db, newTestModelEmpty())
if err == nil {
t.Error("Expected error from passing in single instead of list to ExecuteThenParseList, but got no such error")
return
}
}