This repository has been archived by the owner on Apr 21, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
device_test.go
97 lines (86 loc) · 2.05 KB
/
device_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
package nbiot
import (
"math/rand"
"net/http"
"strconv"
"testing"
"time"
)
func init() {
rand.Seed(time.Now().UnixNano())
}
func TestDevice(t *testing.T) {
client, err := New()
if err != nil {
t.Fatal(err)
}
collection, err := client.CreateCollection(Collection{})
if err != nil {
t.Fatal(err)
}
defer func() {
if err := client.DeleteCollection(collection.ID); err != nil {
t.Fatal(err)
}
}()
devices, err := client.Devices(collection.ID)
if err != nil {
t.Fatal(err)
}
if len(devices) != 0 {
t.Fatalf("expected zero device, got %#v", devices)
}
device, err := client.CreateDevice(collection.ID, Device{
IMSI: strconv.Itoa(rand.Intn(1e15)),
IMEI: strconv.Itoa(rand.Intn(1e15)),
})
if err != nil {
t.Fatal(err)
}
defer client.DeleteDevice(collection.ID, device.ID)
tagKey := "test key"
tagValue := "test value"
imei := strconv.Itoa(rand.Intn(1e15))
imsi := strconv.Itoa(rand.Intn(1e15))
device.Tags = map[string]string{tagKey: tagValue}
device.IMEI = imei
device.IMSI = imsi
device, err = client.UpdateDevice(collection.ID, device)
if err != nil {
t.Fatal(err)
}
if len(device.Tags) != 1 || device.Tags[tagKey] != tagValue {
t.Fatal("unexpected tags:", device.Tags)
}
if device.IMEI != imei || device.IMSI != imsi {
t.Fatal("unexpected IMEI or IMSI:", device.IMEI, device.IMSI)
}
devices, err = client.Devices(collection.ID)
if err != nil {
t.Fatal(err)
}
found := false
for _, d := range devices {
if d.ID == device.ID {
found = true
break
}
}
if !found {
t.Fatalf("device %v not found in %v", device, devices)
}
if _, err := client.Device(collection.ID, device.ID); err != nil {
t.Fatal(err)
}
data, err := client.DeviceData(collection.ID, devices[0].ID, time.Time{}, time.Time{}, 0)
if err != nil || len(data) != 0 {
t.Fatal(err, data)
}
if err := client.DeleteDevice(collection.ID, device.ID); err != nil {
t.Fatal(err)
}
err = client.DeleteDevice(collection.ID, device.ID)
if cerr, ok := err.(ClientError); !ok || cerr.HTTPStatusCode != http.StatusNotFound {
t.Fatal(err)
}
}