-
Notifications
You must be signed in to change notification settings - Fork 0
/
db_test.go
325 lines (283 loc) · 7.61 KB
/
db_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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
package gotelem
import (
"bufio"
"context"
"encoding/json"
"errors"
"fmt"
"math/rand"
"reflect"
"strings"
"testing"
"time"
"github.com/kschamplin/gotelem/internal/logparsers"
"github.com/kschamplin/gotelem/skylab"
)
// helper func to get a random bus event with random data.
func GetRandomBusEvent() skylab.BusEvent {
data := skylab.WsrVelocity{
MotorVelocity: 1.0,
VehicleVelocity: 4.0,
}
ev := skylab.BusEvent{
Timestamp: time.Now(),
Data: &data,
}
return ev
}
// exampleData is a telemetry log data snippet that
// we use to seed the database.
const exampleData = `1698013005.164 1455ED8FDBDFF4FC3BD
1698013005.168 1460000000000000000
1698013005.170 1470000000000000000
1698013005.172 1610000000000000000
1698013005.175 1210000000000000000
1698013005.177 157FFFFC74200000000
1698013005.181 1030000000000000000
1698013005.184 1430000000000000000
1698013005.187 04020D281405EA8FB41
1698013005.210 0413BDF81406AF70042
1698013005.212 042569F81408EF0FF41
1698013005.215 04358A8814041060242
1698013005.219 04481958140D2A40342
1698013005.221 0452DB2814042990442
1698013005.224 047AF948140C031FD41
1698013005.226 04B27A081401ACD0B42
1698013005.229 04DCEAA81403C8C0A42
1698013005.283 04E0378814024580142
1698013005.286 04F97908140BFBC0142
1698013005.289 050098A81402F0F0A42
1698013005.293 051E6AE81402AF20842
1698013005.297 0521AC081403A970742
1698013005.300 0535BB181403CEB0542
1698013005.304 054ECC0814088FE0142
1698013005.307 0554ED181401F44F341
1698013005.309 05726E48140D42BEB41
1698013005.312 059EFC98140EC400142
`
// MakeMockDatabase creates a new dummy database.
func MakeMockDatabase(name string) *TelemDb {
fstring := fmt.Sprintf("file:%s?mode=memory&cache=shared", name)
tdb, err := OpenRawDb(fstring)
if err != nil {
panic(err)
}
return tdb
}
func SeedMockDatabase(tdb *TelemDb) {
// seed the database now.
scanner := bufio.NewScanner(strings.NewReader(exampleData))
for scanner.Scan() {
str := scanner.Text()
bev, err := logparsers.ParsersMap["telem"](str)
if err != nil {
panic(err)
}
_, err = tdb.AddEvents(bev)
if err != nil {
panic(err)
}
}
}
func GetSeedEvents() []skylab.BusEvent {
evs := make([]skylab.BusEvent, 0)
scanner := bufio.NewScanner(strings.NewReader(exampleData))
for scanner.Scan() {
str := scanner.Text()
bev, err := logparsers.ParsersMap["telem"](str)
if err != nil {
panic(err)
}
evs = append(evs, bev)
}
return evs
}
func TestTelemDb(t *testing.T) {
t.Run("test opening database", func(t *testing.T) {
// create our mock
tdb := MakeMockDatabase(t.Name())
tdb.db.Ping()
})
t.Run("test inserting bus event", func(t *testing.T) {
tdb := MakeMockDatabase(t.Name())
type args struct {
events []skylab.BusEvent
}
tests := []struct {
name string
args args
wantErr bool
}{
{
name: "add no packet",
args: args{
events: []skylab.BusEvent{},
},
wantErr: false,
},
{
name: "add single packet",
args: args{
events: []skylab.BusEvent{GetRandomBusEvent()},
},
wantErr: false,
},
{
name: "add multiple packet",
args: args{
events: []skylab.BusEvent{GetRandomBusEvent(), GetRandomBusEvent()},
},
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if _, err := tdb.AddEvents(tt.args.events...); (err != nil) != tt.wantErr {
t.Errorf("TelemDb.AddEvents() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
})
t.Run("test getting packets", func(t *testing.T) {
tdb := MakeMockDatabase(t.Name())
SeedMockDatabase(tdb)
ctx := context.Background()
f := BusEventFilter{}
limitMod := &LimitOffsetModifier{Limit: 1}
pkt, err := tdb.GetPackets(ctx, f, limitMod)
if err != nil {
t.Fatalf("error getting packets: %v", err)
}
if len(pkt) != 1 {
t.Fatalf("expected exactly one response, got %d", len(pkt))
}
// todo - validate what this should be.
})
t.Run("test read-write packet", func(t *testing.T) {
})
}
func MockDocument(key string) json.RawMessage {
var v = make(map[string]interface{})
v["identifier"] = map[string]string{"key": key}
v["randomdata"] = rand.Int()
res, err := json.Marshal(v)
if err != nil {
panic(err)
}
return res
}
func TestDbDocuments(t *testing.T) {
t.Run("test inserting a document", func(t *testing.T) {
tdb := MakeMockDatabase(t.Name())
tdb.db.Ping()
ctx := context.Background()
err := tdb.AddDocument(ctx, MockDocument("hi"))
if err != nil {
t.Fatalf("AddDocument expected no error, got err=%v", err)
}
})
t.Run("test inserting duplicate documents", func(t *testing.T) {
tdb := MakeMockDatabase(t.Name())
tdb.db.Ping()
ctx := context.Background()
doc := MockDocument("hi")
err := tdb.AddDocument(ctx, doc)
if err != nil {
t.Fatalf("AddDocument expected no error, got err=%v", err)
}
err = tdb.AddDocument(ctx, doc)
if err == nil {
t.Fatalf("AddDocument expected duplicate key error, got nil")
}
})
t.Run("test inserting bad document", func(t *testing.T) {
tdb := MakeMockDatabase(t.Name())
tdb.db.Ping()
ctx := context.Background()
var badDoc = map[string]string{"bad": "duh"}
msg, err := json.Marshal(badDoc)
if err != nil {
panic(err)
}
err = tdb.AddDocument(ctx, msg)
if err == nil {
t.Fatalf("AddDocument expected error, got nil")
}
})
t.Run("test getting document", func(t *testing.T) {
tdb := MakeMockDatabase(t.Name())
tdb.db.Ping()
ctx := context.Background()
doc := MockDocument("hi")
err := tdb.AddDocument(ctx, doc)
if err != nil {
t.Fatalf("AddDocument expected no error, got err=%v", err)
}
res, err := tdb.GetDocument(ctx, "hi")
if err != nil {
t.Fatalf("GetDocument expected no error, got err=%v", err)
}
if !reflect.DeepEqual(res, doc) {
t.Fatalf("GetDocument did not return identical document")
}
})
t.Run("test getting nonexistent document", func(t *testing.T) {
tdb := MakeMockDatabase(t.Name())
tdb.db.Ping()
ctx := context.Background()
res, err := tdb.GetDocument(ctx, "hi")
if err == nil || !errors.Is(err, DocumentNotFoundError("hi")) {
t.Fatalf("GetDocument expected DocumentNotFoundError, got %v", err)
}
if res != nil {
t.Fatalf("GetDocument expected nil result, got %v", res)
}
})
t.Run("test update document", func(t *testing.T) {
tdb := MakeMockDatabase(t.Name())
tdb.db.Ping()
ctx := context.Background()
doc1 := MockDocument("hi")
doc2 := MockDocument("hi") // same key, we want to update.
tdb.AddDocument(ctx, doc1)
err := tdb.UpdateDocument(ctx, "hi", doc2)
if err != nil {
t.Fatalf("UpdateDocument expected no error, got err=%v", err)
}
// compare.
res, _ := tdb.GetDocument(ctx, "hi")
if !reflect.DeepEqual(res, doc2) {
t.Fatalf("UpdateDocument did not return new doc, got %s", res)
}
})
t.Run("test update nonexistent document", func(t *testing.T) {
tdb := MakeMockDatabase(t.Name())
tdb.db.Ping()
ctx := context.Background()
doc := MockDocument("hi")
err := tdb.UpdateDocument(ctx, "badKey", doc)
if err == nil {
t.Fatalf("UpdateDocument expected error, got nil")
}
})
t.Run("test delete document", func(t *testing.T) {
tdb := MakeMockDatabase(t.Name())
tdb.db.Ping()
ctx := context.Background()
doc := MockDocument("hi")
tdb.AddDocument(ctx, doc)
err := tdb.DeleteDocument(ctx, "hi")
if err != nil {
t.Fatalf("DeleteDocument expected no error, got err=%v", err)
}
})
t.Run("test delete nonexistent document", func(t *testing.T) {
tdb := MakeMockDatabase(t.Name())
tdb.db.Ping()
ctx := context.Background()
err := tdb.DeleteDocument(ctx, "hi")
if !errors.Is(err, DocumentNotFoundError("hi")) {
t.Fatalf("DeleteDocument expected not found, got err=%v", err)
}
})
}