-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmotorsdb_test.go
311 lines (292 loc) · 7.24 KB
/
motorsdb_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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
package main
import (
"database/sql"
"fmt"
"log"
"sort"
"testing"
_ "github.com/mattn/go-sqlite3"
)
// SetupTestDB sets up an in-memory SQLite database for testing.
func SetupTestDB(t *testing.T) *sql.DB {
// Open an in-memory SQLite database
db, err := sql.Open("sqlite3", ":memory:")
if err != nil {
t.Fatalf("Failed to open in-memory SQLite database: %v", err)
}
// Create tables
createTableQuery := `
CREATE TABLE IF NOT EXISTS ScanIds (
scan_id INTEGER PRIMARY KEY AUTOINCREMENT,
sid VARCHAR(50) NOT NULL UNIQUE
);
CREATE TABLE IF NOT EXISTS MotorMnes (
motor_id INTEGER PRIMARY KEY AUTOINCREMENT,
scan_id INTEGER NOT NULL,
motor_mne VARCHAR(255) NOT NULL
);
CREATE TABLE IF NOT EXISTS MotorPositions (
motor_id INTEGER NOT NULL,
motor_position FLOAT
);`
_, err = db.Exec(createTableQuery)
if err != nil {
t.Fatalf("Failed to create table: %v", err)
}
return db
}
// TestInsertMotors tests the InsertMotors function using an in-memory database
func TestInsertMotors(t *testing.T) {
// Set up the in-memory database
db := SetupTestDB(t)
defer db.Close()
// Set up table of test records to submit
tests := []struct {
motor_record MotorRecord
expect_success bool
}{
{
MotorRecord{
ScanId: "sid_1",
Motors: map[string]float64{
"mne0": 1.23,
"mne1": 4.56,
},
},
true,
},
{
MotorRecord{
ScanId: "sid_1", // violates unique sid constraint
Motors: map[string]float64{},
},
false,
},
{
MotorRecord{
ScanId: "sid_2",
Motors: map[string]float64{
"mne0": 1.23,
"mne1": 4.56,
},
},
true,
},
}
// Run tests
for _, tt := range tests {
t.Run("", func(t *testing.T) {
scan_id, err := InsertMotors(tt.motor_record, db)
if err != nil && tt.expect_success {
t.Errorf("InsertMotors(%v, db) failed", tt.motor_record)
}
if tt.expect_success {
err := validateMotorsDbRowCounts(tt.motor_record, scan_id, db)
if err != nil {
t.Error(err)
}
}
})
}
}
// Helper to validate the number of rows belonging to a given record in each table of the motors db.
func validateMotorsDbRowCounts(motor_record MotorRecord, scan_id int64, db *sql.DB) error {
var count int
err := db.QueryRow("SELECT COUNT(*) FROM ScanIds WHERE sid = ?", motor_record.ScanId).Scan(&count)
if err != nil {
return fmt.Errorf("Failed to query ScanIds table: %v", err)
}
if count != 1 {
return fmt.Errorf("Expected 1 record in ScanIds, but got %d", count)
}
err = db.QueryRow("SELECT COUNT(*) FROM MotorMnes WHERE scan_id = ?", scan_id).Scan(&count)
if err != nil {
return fmt.Errorf("Failed to query MotorMnes table: %v", err)
}
if count != len(motor_record.Motors) {
return fmt.Errorf("Expected %d records in MotorMnes, but got %d", len(motor_record.Motors), count)
}
//err = db.QueryRow("SELECT COUNT(*) FROM MotorPositions").Scan(&count)
err = db.QueryRow("SELECT COUNT(*) FROM MotorPositions mp JOIN MotorMnes mm ON mp.motor_id = mm.motor_id WHERE mm.scan_id = ?", scan_id).Scan(&count)
if err != nil {
return fmt.Errorf("Failed to query MotorPositions table: %v", err)
}
if count != len(motor_record.Motors) {
return fmt.Errorf("Expected %d records in MotorPositions, but got %d", len(motor_record.Motors), count)
}
return nil
}
// Test for translating "mototrs" portions of user queries to
// MotorsDbQuery structs
func TestTranslateQuery(t *testing.T) {
tests := []struct {
query map[string]any
expected MotorsDbQuery
}{
{
query: map[string]any{"motors": map[string]any{"mne": 1.23}},
expected: MotorsDbQuery{
MotorPositionQueries: []MotorPositionQuery{
MotorPositionQuery{
Mne: "mne",
Exact: []float64{1.23},
},
},
},
},
{
query: map[string]any{"motors": map[string]any{"mne0": 1.23}, "motors.mne1": 4.56},
expected: MotorsDbQuery{
MotorPositionQueries: []MotorPositionQuery{
MotorPositionQuery{
Mne: "mne0",
Exact: []float64{1.23},
},
MotorPositionQuery{
Mne: "mne1",
Exact: []float64{4.56},
},
},
},
},
{
query: map[string]any{
"motors.mne": 1.23,
},
expected: MotorsDbQuery{
MotorPositionQueries: []MotorPositionQuery{
MotorPositionQuery{
Mne: "mne",
Exact: []float64{1.23},
},
},
},
},
{
query: map[string]any{
"motors.mne": map[string]any{"$lt": 1.23},
},
expected: MotorsDbQuery{
MotorPositionQueries: []MotorPositionQuery{
MotorPositionQuery{
Mne: "mne",
Max: 1.23,
},
},
},
},
{
query: map[string]any{
"motors.mne": map[string]any{"$gt": 1.23},
},
expected: MotorsDbQuery{
MotorPositionQueries: []MotorPositionQuery{
MotorPositionQuery{
Mne: "mne",
Min: 1.23,
},
},
},
},
{
query: map[string]any{
"motors.mne": map[string]any{"$eq": 1.23},
},
expected: MotorsDbQuery{
MotorPositionQueries: []MotorPositionQuery{
MotorPositionQuery{
Mne: "mne",
Exact: []float64{1.23},
},
},
},
},
{
query: map[string]any{
"motors.mne": map[string]any{"$in": []any{1.23, 4.56}},
},
expected: MotorsDbQuery{
MotorPositionQueries: []MotorPositionQuery{
MotorPositionQuery{
Mne: "mne",
Exact: []float64{1.23, 4.56},
},
},
},
},
{
query: map[string]any{
"motors": map[string]any{
"mne": map[string]any{"$gt": -1.23, "$lt": 4.56, "$in": []any{0.0, 1.23}},
},
},
expected: MotorsDbQuery{
MotorPositionQueries: []MotorPositionQuery{
MotorPositionQuery{
Mne: "mne",
Exact: []float64{0, 1.23},
Min: -1.23,
Max: 4.56,
},
},
},
},
}
for _, tt := range tests {
t.Run("", func(t *testing.T) {
got := translateQuery(tt.query)
if !equalMotorDbQuery(got, tt.expected) {
t.Errorf("translateQuery(%v) = %v; want %v", tt.query, got, tt.expected)
} else {
log.Printf("Translated query: %+v; got %+v\n", tt.query, got)
}
})
}
}
// equalMotorDbQuery checks if two MotorsDbQuery instances are identical.
func equalMotorDbQuery(a, b MotorsDbQuery) bool {
// Compare Sids slices
if len(a.Sids) != len(b.Sids) {
return false
}
for i := range a.Sids {
if a.Sids[i] != b.Sids[i] {
return false
}
}
// Sort MotorPositionQueries slices by the Mne field to ensure consistent ordering
sort.Slice(a.MotorPositionQueries, func(i, j int) bool {
return a.MotorPositionQueries[i].Mne < a.MotorPositionQueries[j].Mne
})
sort.Slice(b.MotorPositionQueries, func(i, j int) bool {
return b.MotorPositionQueries[i].Mne < b.MotorPositionQueries[j].Mne
})
// Compare MotorPositionQueries slices
if len(a.MotorPositionQueries) != len(b.MotorPositionQueries) {
return false
}
for i := range a.MotorPositionQueries {
if !equalMotorPositionQuery(a.MotorPositionQueries[i], b.MotorPositionQueries[i]) {
return false
}
}
return true
}
// equalMotorPositionQuery checks if two MotorPositionQuery instances are identical.
func equalMotorPositionQuery(a, b MotorPositionQuery) bool {
// Compare Mne
if a.Mne != b.Mne {
return false
}
// Compare Exact slice
if len(a.Exact) != len(b.Exact) {
return false
}
for i := range a.Exact {
if a.Exact[i] != b.Exact[i] {
return false
}
}
// Compare Min and Max
return a.Min == b.Min && a.Max == b.Max
}