forked from Glucloser/carelink_tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
carelink_test.go
121 lines (107 loc) · 2.26 KB
/
carelink_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
package main
import (
"os"
"testing"
"time"
)
func login(t *testing.T) (CarelinkSession, error) {
sess, err := NewCarelinkSession()
if err != nil {
t.Fatalf("Couldn't create session: %v", err)
}
username := os.Getenv("CARELINK_USERNAME")
if username == "" {
t.Fatal("CARELINK_USERNAME not set")
}
password := os.Getenv("CARELINK_PASSWORD")
if password == "" {
t.Fatal("CARELINK_PASSWORD not set")
}
err = sess.Login(username, password)
if err != nil {
t.Fatalf("Login failed: %v", err)
}
return sess, err
}
func TestLogin(t *testing.T) {
_, _ = login(t)
}
func TestCSVExport(t *testing.T) {
sess, _ := login(t)
reader, err := sess.CSVExport("09/16/2017", "09/17/2017")
if err != nil {
t.Fatalf("CSVExport failed: %v", err)
}
b := make([]byte, 100)
n, err := reader.Read(b)
if err != nil {
t.Logf("Error reading CSVExport response: %v", err)
t.Fail()
}
if n == 0 {
t.Log("Read zero bytes from CSVExport response")
t.Fail()
}
}
func TestCGMExport(t *testing.T) {
sess, _ := login(t)
reader, err := sess.CGMExport()
if err != nil {
t.Fatalf("CGMExport failed: %v", err)
}
b := make([]byte, 100)
n, err := reader.Read(b)
if err != nil {
t.Logf("Error reading CGMExport response: %v", err)
t.Fail()
}
if n == 0 {
t.Log("Read zero bytes from CGMExport response")
t.Fail()
}
}
func TestParseCSV(t *testing.T) {
sess, _ := login(t)
reader, err := sess.CSVExport("09/16/2017", "09/17/2017")
if err != nil {
t.Fatalf("CSVExport failed %v", err)
}
items, err := ParseCSVExport(reader)
if err != nil {
t.Fatalf("ParseCSVExport failed: %v", err)
}
select {
case item := <-items:
if item.RawType == "" {
t.Logf("Item has no RawType: %v\n", item)
t.Fail()
}
case _ = <-time.NewTicker(time.Second).C:
t.Log("No items")
t.Fail()
}
}
func TestParseCGM(t *testing.T) {
sess, _ := login(t)
reader, err := sess.CGMExport()
if err != nil {
t.Fatalf("CGMExport failed: %v", err)
}
items, err := ParseCGMExport(reader)
if err != nil {
t.Fatalf("ParseCGMExport failed: %v", err)
}
if len(items) == 0 {
t.Fatal("No sugars parsed")
}
sugar := items[0]
if sugar.Value == 0 {
t.Log("No sugar value")
t.Fail()
}
var emptyTime time.Time
if sugar.OccurredAt == emptyTime {
t.Log("No occurredAt time")
t.Fail()
}
}