-
Notifications
You must be signed in to change notification settings - Fork 1
/
sql_device_locations_test.go
92 lines (74 loc) · 1.91 KB
/
sql_device_locations_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
package main
import (
"encoding/json"
"strconv"
"testing"
"time"
)
func TestAddOTLocation(t *testing.T) {
ctx, s := setupDB(t)
om := owntracksMessage{}
if err := json.Unmarshal([]byte(egOwntracksLocation), &om); err != nil {
t.Fatal(err)
}
if err := s.AddOTLocation(ctx, om); err != nil {
t.Fatal(err)
}
var count int
if err := s.db.QueryRowContext(ctx, `select count(*) from device_locations`).Scan(&count); err != nil {
t.Fatal(err)
}
if count != 1 {
t.Errorf("want one row, got: %d", count)
}
}
func TestAddTakeoutLocation(t *testing.T) {
ctx, s := setupDB(t)
locs := []takeoutLocation{
{LatitudeE7: 10 * 1e7, LongitudeE7: -10 * 10e7, TimestampMS: strconv.Itoa(int(time.Now().Unix() * 1000)), Accuracy: 100, Raw: json.RawMessage([]byte(`{}`))},
{LatitudeE7: 10 * 1e7, LongitudeE7: -10 * 10e7, TimestampMS: strconv.Itoa(int(time.Now().Unix() * 1000)), Accuracy: 100, Raw: json.RawMessage([]byte(`{}`))},
}
if err := s.AddGoogleTakeoutLocations(ctx, locs); err != nil {
t.Fatal(err)
}
var count int
if err := s.db.QueryRowContext(ctx, `select count(*) from device_locations`).Scan(&count); err != nil {
t.Fatal(err)
}
if count != 2 {
t.Errorf("want 2 rows, got: %d", count)
}
}
func TestLatestLocationTimestamp(t *testing.T) {
ctx, s := setupDB(t)
now := time.Now()
earlier := now.Add(-5 * time.Minute)
for _, msg := range []otLocation{
{
TimestampUnix: int(now.Unix()),
},
{
TimestampUnix: int(earlier.Unix()),
},
} {
jb, err := json.Marshal(msg)
if err != nil {
t.Fatal(err)
}
om := owntracksMessage{
Type: "location",
Data: jb,
}
if err := s.AddOTLocation(ctx, om); err != nil {
t.Fatal(err)
}
}
l, err := s.LatestLocationTimestamp(ctx)
if err != nil {
t.Fatal(err)
}
// round off, db storage is unix time in seconds
if !l.Equal(now.Truncate(1 * time.Second)) {
t.Errorf("want latest time %s, got: %s", now.String(), l.String())
}
}