-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb_sqlite_test.go
244 lines (195 loc) · 5.22 KB
/
db_sqlite_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
package mqttGather
import (
"database/sql"
"fmt"
"math"
"testing"
"time"
)
func getTestDB(t *testing.T) *SqliteDB {
db_, err := NewDatabase("file::memory:?cache=private")
if err != nil {
t.Fatal(err)
}
db, _ := db_.(*SqliteDB)
db.db.SetMaxOpenConns(1) // <- this is necessary to avoid some extremly tedious racy conditions
// which may or may not be a driver bug for in-memory dbs during testing.
return db
}
const TEST_SIGNIFIER = "aa:bb:cc:dd:ee:ff"
func getTestDBWithDevice(t *testing.T) (*SqliteDB, int64) {
db := getTestDB(t)
id, _ := db.lookupDevice(TEST_SIGNIFIER)
return db, id
}
func getTestDBWithDeviceInfo(t *testing.T) (*SqliteDB, int64) {
db, id := getTestDBWithDevice(t)
_, err := db.db.Exec(`INSERT INTO
device_info (
device_id, description, latitude, longitude
) VALUES (
:ID, 'bla', 1.0, 2.0);`, id)
if err != nil {
t.Fatal(err)
}
return db, id
}
func TestInsert(t *testing.T) {
db := getTestDB(t)
defer db.Close()
stats := RandomDBAStats()
i, err := db.SaveNow(&stats)
if err != nil {
t.Fatal(err)
}
if i == -1 {
t.Fatal("i==-1")
}
i, err = db.Save(&stats, time.Now())
if err != nil {
t.Fatal(err)
}
if i == -1 {
t.Fatal("i==-1")
}
}
func TestDeviceId(t *testing.T) {
db := getTestDB(t)
defer db.Close()
var doesntexist = "doesntexist"
id, err := db.LoadDeviceId(doesntexist)
if id != -1 || err != sql.ErrNoRows {
t.Fatalf("can't happen, sanity check 1. id(%d) err(%v)", id, err)
}
if db.deviceCache[doesntexist] != 0 {
t.Fatal("can't happen, sanity check 1")
}
id, err = db.lookupDevice(doesntexist)
if err != nil {
t.Fatalf("failed to create deviceid: %v", err)
}
if id != db.deviceCache[doesntexist] || id == 0 {
t.Fatalf("something weird: %d", id)
}
id, err = db.lookupDevice(doesntexist)
if err != nil {
t.Fatalf("failed cached lookup: %v", err)
}
// simulate prexisting device in db
// and not yet in cache
delete(db.deviceCache, doesntexist)
id, err = db.lookupDevice(doesntexist)
if err != nil {
t.Fatalf("failed db/notcache lookup: %v", err)
}
}
func TestLoadDeviceInfo(t *testing.T) {
db, id := getTestDBWithDevice(t)
defer db.Close()
info, err := db.LoadDeviceInfo(TEST_SIGNIFIER)
if info != nil {
t.Fatalf("loaded nonexisting device info: %v", info)
}
if err == nil {
t.Fatalf("expected err!")
}
_, err = db.db.Exec("INSERT INTO device_info (device_id, description, latitude, longitude) VALUES (:ID, 'bla', 1.0, 2.0);", id)
if err != nil {
t.Fatalf("%v", err)
}
info, err = db.LoadDeviceInfo(TEST_SIGNIFIER)
if err != nil {
t.Fatalf("err: %v err:%v", info, err.Error())
}
if info.Latitude != 1.0 || info.Longitude != 2.0 || info.Description != "bla" {
t.Fatalf("Loading Info Failed")
}
}
func TestLoadDeviceInfoFail(t *testing.T) {}
func TestLoadAlert(t *testing.T) {
db, id := getTestDBWithDevice(t)
defer db.Close()
alert, err := db.LoadLastAlert(TEST_SIGNIFIER)
if err != nil || alert.Timestamp != 0 {
t.Fatalf("loaded non existant alert (err=%v): %#v", err, alert)
}
for i := 0; i != 3; i += 1 {
_, err := db.db.Exec("INSERT INTO alert (device_id, ts, alert_phone, message, status) VALUES (:ID, :ts, '123', 'MSG', 'ok' )", id, fmt.Sprintf("%d", i))
if err != nil {
t.Fatalf("sanity: %v", err)
}
}
alert, err = db.LoadLastAlert(TEST_SIGNIFIER)
if err != nil {
t.Fatalf("could not load last alert: %v", err)
}
if alert.Timestamp != 2 {
t.Fatalf("did not load final alert: %#v", alert)
}
}
func testThresholdExceeded(t *testing.T, db *SqliteDB, windowsSeconds int64, threshold float64, countShould int64) {
cnt, err := db.getCountThresholdExceeded(TEST_SIGNIFIER, windowsSeconds, threshold)
if err != nil {
t.Fatalf("%v", err)
}
if cnt != countShould { // first entry doesn't exceed
t.Fatalf("incorrect count for (w: %v thresh: %v): %v (should: %v)", windowsSeconds, threshold, cnt, countShould)
}
}
func TestThresholdExceeded(t *testing.T) {
db, id := getTestDBWithDevice(t)
defer db.Close()
sql := `
INSERT INTO dba_stats
(device_id, ts, max)
VALUES
(:ID, :TS, :MAX)
`
for i := 0; i != 10; i += 1 {
_, err := db.db.Exec(sql, id, i, i)
if err != nil {
t.Fatalf("%v", err)
}
}
var should = [][]int64{
{9, 8, 7, 6, 5, 4, 3, 2, 1, 0},
{8, 8, 7, 6, 5, 4, 3, 2, 1, 0},
{7, 7, 7, 6, 5, 4, 3, 2, 1, 0},
{6, 6, 6, 6, 5, 4, 3, 2, 1, 0},
{5, 5, 5, 5, 5, 4, 3, 2, 1, 0},
{4, 4, 4, 4, 4, 4, 3, 2, 1, 0},
{3, 3, 3, 3, 3, 3, 3, 2, 1, 0},
{2, 2, 2, 2, 2, 2, 2, 2, 1, 0},
{1, 1, 1, 1, 1, 1, 1, 1, 1, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
}
for window := 0; window <= 9; window++ {
for threshold := 0.0; threshold <= 9.5; threshold += 0.5 {
idx2 := int(math.Floor(threshold))
shouldCount := should[window][idx2]
testThresholdExceeded(t, db, int64(window), threshold, shouldCount)
}
}
}
func TestSaveAlert(t *testing.T) {
msg := "TestMsg"
alert := &Alert{
TEST_SIGNIFIER,
0,
"123",
msg,
"ok",
}
db, _ := getTestDBWithDevice(t)
db.SaveAlert(alert)
alert, err := db.LoadLastAlert(TEST_SIGNIFIER)
if err != nil {
t.Fatalf("failed to load: %v", err)
}
if alert.DeviceSignifier != TEST_SIGNIFIER {
t.Fatalf("0 is: %s, should %s", alert.DeviceSignifier, TEST_SIGNIFIER)
}
if alert.Message != msg {
t.Fatalf("1 is: %s, should %s", alert.Message, msg)
}
}