-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathpumphistory_test.go
110 lines (103 loc) · 2.42 KB
/
pumphistory_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
package medtronic
import (
"encoding/base64"
"testing"
"time"
)
func TestPumpHistory(t *testing.T) {
records := setupPumpHistory()
cases := []struct {
cutoff string
index int
}{
{"2020-02-25T11:00", 0},
{"2020-02-25T09:00", 8},
{"2020-02-24T23:00", 54},
{"2020-02-24T01:00", 297},
{"2000-01-01T00:00", 297},
}
for _, c := range cases {
t.Run("", func(t *testing.T) {
results := findSince(records, parseTime(c.cutoff))
if len(results) != c.index {
t.Errorf("findSince(%s) returned %d records, want %d", c.cutoff, len(results), c.index)
}
})
}
}
func TestPumpHistoryFrom(t *testing.T) {
records := setupPumpHistory()
cases := []struct {
id string
index int
}{
{"ewgAgAsZFBYjAA==", 0},
{"AwAAAAAytygZFA==", 8},
{"ewAyuwEYFAAYAA==", 296},
{"xyzzy", -1},
}
for _, c := range cases {
t.Run("", func(t *testing.T) {
recordID, err := base64.StdEncoding.DecodeString(c.id)
if err != nil {
// Allow illegal base64 strings for nonexistent record IDs.
recordID = nil
}
results, found := findID(records, recordID)
if !found {
if c.index != -1 {
t.Errorf("findID(%s) not found, want %d", c.id, c.index)
return
} else if len(results) != len(records) {
t.Errorf("findID(%s) returned %d records when not found, want %d", c.id, len(results), len(records))
}
return
}
if len(results) != c.index {
t.Errorf("findID(%s) returned %d records, want %d", c.id, len(results), c.index)
}
})
}
}
func setupPumpHistory() History {
tc := testCase{"pump-records", 523, 0}
file := testFileName(tc) + ".json"
family := testPumpFamily(tc)
records, err := decodeFromData(file, family)
if err != nil {
panic(err)
}
return records
}
// Mimic the behavior of Pump.findRecords.
func findRecords(records History, check func(HistoryRecord) bool) (History, bool) {
results := records
for i, r := range records {
if check(r) {
results = records[:i+1]
break
}
}
n := len(results)
if n == 0 {
return nil, false
}
r := results[n-1]
if check(r) {
return results[:n-1], true
}
return results, false
}
func findSince(records History, since time.Time) History {
check := func(r HistoryRecord) bool {
return checkBefore(r, since)
}
results, _ := findRecords(records, check)
return results
}
func findID(records History, id []byte) (History, bool) {
check := func(r HistoryRecord) bool {
return checkID(r, id)
}
return findRecords(records, check)
}