-
Notifications
You must be signed in to change notification settings - Fork 1
/
airgradient_test.go
102 lines (95 loc) · 2.04 KB
/
airgradient_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
package main
import (
"errors"
"net/http"
"net/http/httptest"
"testing"
"github.com/stretchr/testify/assert"
)
func TestGetAirGradientAPIURL(t *testing.T) {
var testCases = []struct {
name string
locationID int
expectedURL string
}{
{
"location-id-0",
0,
"https://api.airgradient.com/public/api/v1/locations/measures/current",
},
{
"location-id-12345",
12345,
"https://api.airgradient.com/public/api/v1/locations/12345/measures/current",
},
}
for _, tC := range testCases {
t.Run(tC.name, func(t *testing.T) {
assert.Equal(t, tC.expectedURL, getAirGradientAPIURL(tC.locationID))
})
}
}
func TestConvertTemp(t *testing.T) {
var testCases = []struct {
name string
temp float64
tempUnit string
expected float64
}{
{
"convert-celsius-to-fahrenheit",
20,
"F",
68,
},
{
"no-conversion",
20,
"C",
20,
},
}
for _, tC := range testCases {
t.Run(tC.name, func(t *testing.T) {
assert.Equal(t, tC.expected, convertTemperature(tC.temp, tC.tempUnit))
})
}
}
func TestGetAirGradientMeasures(t *testing.T) {
var testCases = []struct {
name string
payloadFile string
err error
}{
{
"correct-api-v1-locations-measures-current",
"testdata/api-v1-locations-measures-current.json",
nil,
},
{
"correct-api-v1-locations-measures-current-with-more-float64",
"testdata/api-v1-locations-measures-current-with-more-float64.json",
nil,
},
{
"correct-api-v1-locations-12345-measures-current",
"testdata/api-v1-locations-12345-measures-current.json",
nil,
},
{
"incorrect-response-404",
"testdata/incorrect-response-404.json",
errors.New("Error unmarshalling JSON"),
},
}
for _, tC := range testCases {
t.Run(tC.name, func(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
http.ServeFile(w, r, tC.payloadFile)
}))
defer server.Close()
_, err := getAirGradientMeasures(server.URL, "SECRET-TOKEN")
assert.Equal(t, tC.err, err)
})
}
}