Skip to content

Commit 8ae3914

Browse files
committed
Improve tests
1 parent 2b4dc1d commit 8ae3914

File tree

2 files changed

+28
-20
lines changed

2 files changed

+28
-20
lines changed

internal/template/load_test.go

+27-19
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,17 @@ import (
2727
"github.com/stretchr/testify/mock"
2828
)
2929

30+
const (
31+
// Real IDs will be UUIDs v4 like this: 9231a50b-8680-4489-a465-2b769fc310cb
32+
// Here we use these text strings to improve test errors readability
33+
switchyID = "switchy-id"
34+
relayID = "relay-id"
35+
blinkSpeedID = "blink_speed-id"
36+
37+
thingOverriddenID = "thing-overridden-id"
38+
switchyOverriddenID = "switchy-overridden-id"
39+
)
40+
3041
var (
3142
dashboardTemplateTest = map[string]interface{}{
3243
"id": "home-security-alarm-dashboard",
@@ -69,8 +80,7 @@ var (
6980
Widgets: []iotclient.Widget{
7081
{Name: "Switch-name", Height: 1, HeightMobile: 2, Width: 3, WidthMobile: 4,
7182
X: 5, XMobile: 6, Y: 7, YMobile: 8, Options: map[string]interface{}{"showLabels": true}, Type: "Switch",
72-
// variable id is set equal to the thing id by mockThingShow, in order to verify the thing override
73-
Variables: []string{"thing"},
83+
Variables: []string{switchyID},
7484
},
7585
},
7686
}
@@ -80,26 +90,21 @@ var (
8090
Widgets: []iotclient.Widget{
8191
{Name: "Switch-name", Height: 1, HeightMobile: 2, Width: 3, WidthMobile: 4,
8292
X: 5, XMobile: 6, Y: 7, YMobile: 8, Options: map[string]interface{}{"showLabels": true}, Type: "Switch",
83-
// variable id is set equal to the thing id by mockThingShow, in order to verify the thing override
84-
Variables: []string{"overridden"},
93+
Variables: []string{switchyOverriddenID},
8594
},
8695
},
8796
}
8897

8998
dashboardTwoWidgets = &iotclient.Dashboardv2{
9099
Name: "dashboard-two-widgets",
91-
// in this test, the variable id is a concatenation of thing_id and variable_id
92-
// this depends on the mocked function getVariableID
93100
Widgets: []iotclient.Widget{
94101
{Name: "blink_speed", Height: 7, Width: 8,
95102
X: 7, Y: 5, Options: map[string]interface{}{"min": float64(0), "max": float64(5000)}, Type: "Slider",
96-
// variable id is set equal to the thing id by mockThingShow, in order to verify the thing override
97-
Variables: []string{"remote-controlled-lights"},
103+
Variables: []string{blinkSpeedID},
98104
},
99105
{Name: "relay_2", Height: 5, Width: 5,
100106
X: 5, Y: 0, Options: map[string]interface{}{"showLabels": true}, Type: "Switch",
101-
// variable id is set equal to the thing id by mockThingShow, in order to verify the thing override
102-
Variables: []string{"remote-controlled-lights"},
107+
Variables: []string{relayID},
103108
},
104109
},
105110
}
@@ -143,17 +148,20 @@ func TestLoadTemplate(t *testing.T) {
143148
func TestLoadDashboard(t *testing.T) {
144149
mockClient := &mocks.Client{}
145150
mockThingShow := func(thingID string) *iotclient.ArduinoThing {
146-
thing := &iotclient.ArduinoThing{
151+
if thingID == thingOverriddenID {
152+
return &iotclient.ArduinoThing{
153+
Properties: []iotclient.ArduinoProperty{
154+
{Id: switchyOverriddenID, Name: "switchy"},
155+
},
156+
}
157+
}
158+
return &iotclient.ArduinoThing{
147159
Properties: []iotclient.ArduinoProperty{
148-
// variable id is set equal to the thing id in order to verify the thing override
149-
// dashboard-with-variable variable
150-
{Id: thingID, Name: "variable"},
151-
// dashboard-two-widgets variables
152-
{Id: thingID, Name: "relay_2"},
153-
{Id: thingID, Name: "blink_speed"},
160+
{Id: switchyID, Name: "switchy"},
161+
{Id: relayID, Name: "relay_2"},
162+
{Id: blinkSpeedID, Name: "blink_speed"},
154163
},
155164
}
156-
return thing
157165
}
158166
mockClient.On("ThingShow", mock.AnythingOfType("string")).Return(mockThingShow, nil)
159167

@@ -194,7 +202,7 @@ func TestLoadDashboard(t *testing.T) {
194202
{
195203
name: "dashboard with variable, thing is overridden",
196204
file: "testdata/dashboard-with-variable.yaml",
197-
override: map[string]string{"thing": "overridden"},
205+
override: map[string]string{"thing": thingOverriddenID},
198206
want: dashboardVariableOverride,
199207
},
200208

internal/template/testdata/dashboard-with-variable.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ widgets:
77
showLabels: true
88
variables:
99
- thing_id: thing
10-
variable_id: variable
10+
variable_id: switchy
1111
type: Switch
1212
width: 3
1313
width_mobile: 4

0 commit comments

Comments
 (0)