-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathlocation_test.go
104 lines (100 loc) · 2.01 KB
/
location_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
// Copyright © 2021 The Things Network
// Use of this source code is governed by the MIT license that can be found in the LICENSE file.
package apppayload_test
import (
"math"
"strconv"
"testing"
apppayload "go.thethings.network/lorawan-application-payload"
)
func TestInferLocation(t *testing.T) {
for i, tc := range []struct {
m map[string]interface{}
loc apppayload.Location
ok bool
}{
{
m: map[string]interface{}{
"gps_5": map[string]interface{}{
"latitude": float64(1),
"longitude": float64(2),
"altitude": float64(3),
},
},
loc: apppayload.Location{
Latitude: 1,
Longitude: 2,
Altitude: 3,
},
ok: true,
},
{
m: map[string]interface{}{
"lat": float64(1),
"longitudeDeg": float64(2),
},
ok: false, // invalid pair
},
{
m: map[string]interface{}{
"lat": 1,
"lon": 2,
},
ok: false, // invalid numeric type
},
{
m: map[string]interface{}{
"lat": float64(1),
"lon": float64(2),
},
loc: apppayload.Location{
Latitude: 1,
Longitude: 2,
},
ok: true,
},
{
m: map[string]interface{}{
"latitude": float64(1),
"longitude": float64(2),
"altitude": float64(3),
"accuracy": float64(4),
},
loc: apppayload.Location{
Latitude: 1,
Longitude: 2,
Altitude: 3,
Accuracy: 4,
},
ok: true,
},
{
m: map[string]interface{}{
"latitude": math.NaN(),
"longitude": float64(2),
"altitude": float64(3),
"accuracy": float64(4),
},
ok: false,
},
{
m: map[string]interface{}{
"latitude": float64(1),
"longitude": float64(200),
"altitude": float64(3),
"accuracy": float64(4),
},
ok: false,
},
} {
t.Run(strconv.Itoa(i), func(t *testing.T) {
loc, ok := apppayload.InferLocation(tc.m)
if ok != tc.ok {
t.Fatalf("Expected location to return `%v` but it was `%v`", tc.ok, ok)
}
if loc != tc.loc {
t.Fatalf("Expected location to be `%v` but it was `%v`", tc.loc, loc)
}
})
}
}