-
Notifications
You must be signed in to change notification settings - Fork 6
/
structs_test.go
164 lines (154 loc) · 5.52 KB
/
structs_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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
package nest
import (
"encoding/json"
. "github.com/smartystreets/goconvey/convey"
"reflect"
"testing"
"time"
)
func TestCombined(t *testing.T) {
Convey("Given a JSON object with combined", t, func() {
combined := &Combined{}
err := json.Unmarshal(combinedJSON(), combined)
So(err, ShouldBeNil)
Convey("We should get thermostats", func() {
So(len(combined.Devices.Thermostats), ShouldEqual, 1)
for key, value := range combined.Devices.Thermostats {
So(key, ShouldEqual, combined.Devices.Thermostats["peyiJNo0IldT2YlIVtYaGQ"].DeviceID)
checkFields(value)
}
})
Convey("We should get smokecoalarms", func() {
So(len(combined.Devices.SmokeCoAlarms), ShouldEqual, 1)
for key, value := range combined.Devices.SmokeCoAlarms {
So(key, ShouldEqual, combined.Devices.SmokeCoAlarms["RTMTKxsQTCxzVcsySOHPxKoF4OyCifrs"].DeviceID)
checkFields(value)
}
})
Convey("We should get structures", func() {
So(len(combined.Structures), ShouldEqual, 1)
for key, value := range combined.Structures {
So(key, ShouldEqual, combined.Structures["VqFabWH21nwVyd4RWgJgNb292wa7hG_dUwo2i2SG7j3-BOLY0BA4sw"].StructureID)
checkFields(value)
}
})
Convey("Should get an eta", func() {
So(combined.Structures["VqFabWH21nwVyd4RWgJgNb292wa7hG_dUwo2i2SG7j3-BOLY0BA4sw"].ETA.TripID, ShouldEqual, "myTripHome1024")
checkFields(combined.Structures["VqFabWH21nwVyd4RWgJgNb292wa7hG_dUwo2i2SG7j3-BOLY0BA4sw"].ETA)
})
})
}
func TestAccess(t *testing.T) {
Convey("Given a JSON object with an access token", t, func() {
access := &Access{}
err := json.Unmarshal(accessJSON(), access)
So(err, ShouldBeNil)
})
}
func checkFields(value interface{}) {
s := reflect.ValueOf(value).Elem()
for i := 0; i < s.NumField(); i++ {
f := s.Field(i)
value := f.Interface()
switch value.(type) {
case string:
So(value.(string), ShouldNotBeEmpty)
case bool:
So(value.(bool), ShouldBeTrue)
case int:
So(value.(int), ShouldNotBeNil)
case float32:
So(value.(float32), ShouldNotBeNil)
case time.Time:
So(value.(time.Time), ShouldNotBeNil)
}
}
}
func combinedJSON() []byte {
return []byte(`
{
"devices": {
"thermostats": {
"peyiJNo0IldT2YlIVtYaGQ": {
"device_id": "peyiJNo0IldT2YlIVtYaGQ",
"locale": "en-US",
"software_version": "4.0",
"structure_id": "VqFabWH21nwVyd4RWgJgNb292wa7hG_dUwo2i2SG7j3-BOLY0BA4sw",
"name": "Hcombinedway (upstairs)",
"name_long": "Hcombinedway Thermostat (upstairs)",
"last_connection": "2014-03-02T23:20:19+00:00",
"is_online": true,
"can_cool": true,
"can_heat": true,
"is_using_emergency_heat": true,
"has_fan": true,
"fan_timer_active": true,
"fan_timer_timeout": "2014-03-02T23:20:19+00:00",
"has_leaf": true,
"temperature_scale": "C",
"target_temperature_f": 72,
"target_temperature_c": 21.5,
"target_temperature_high_f": 72,
"target_temperature_high_c": 21.5,
"target_temperature_low_f": 64,
"target_temperature_low_c": 17.5,
"away_temperature_high_f": 72,
"away_temperature_high_c": 21.5,
"away_temperature_low_f": 64,
"away_temperature_low_c": 17.5,
"hvac_mode": "heat",
"ambient_temperature_f": 72,
"ambient_temperature_c": 21.5,
"humidity": 35,
"hvac_state": "heating",
"where_id": "d6reb_OZTM..."
}
},
"smoke_co_alarms": {
"RTMTKxsQTCxzVcsySOHPxKoF4OyCifrs": {
"device_id": "RTMTKxsQTCxzVcsySOHPxKoF4OyCifrs",
"locale": "en-US",
"software_version": "1.01",
"structure_id": "VqFabWH21nwVyd4RWgJgNb292wa7hG_dUwo2i2SG7j3-BOLY0BA4sw",
"name": "Hcombinedway (upstairs)",
"name_long": "Hcombinedway Protect (upstairs)",
"last_connection": "2014-03-02T23:20:19+00:00",
"is_online": true,
"battery_health": "ok",
"co_alarm_state": "ok",
"smoke_alarm_state": "ok",
"ui_color_state": "gray"
}
}
},
"structures": {
"VqFabWH21nwVyd4RWgJgNb292wa7hG_dUwo2i2SG7j3-BOLY0BA4sw": {
"structure_id": "VqFabWH21nwVyd4RWgJgNb292wa7hG_dUwo2i2SG7j3-BOLY0BA4sw",
"thermostats": [
"peyiJNo0IldT2YlIVtYaGQ"
],
"smoke_co_alarms": [
"RTMTKxsQTCxzVcsySOHPxKoF4OyCifrs"
],
"away": "home",
"name": "Home",
"country_code": "US",
"peak_period_start_time": "2014-03-10T23:10:12+00:00",
"peak_period_end_time": "2014-03-10T23:14:19+00:00",
"time_zone": "America/Los_Angeles",
"eta": {
"trip_id": "myTripHome1024",
"estimated_arrival_window_begin": "2014-07-04T10:48:11+00:00",
"estimated_arrival_window_end": "2014-07-04T18:48:11+00:00"
}
}
}
}`)
}
func accessJSON() []byte {
return []byte(`
{
"access_token": "c.FmDPkzyzaQe...",
"expires_in": 315360000
}`)
}