Skip to content

Commit d007bdd

Browse files
committed
Improve tests
1 parent 2b4dc1d commit d007bdd

File tree

2 files changed

+27
-20
lines changed

2 files changed

+27
-20
lines changed

internal/template/load_test.go

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

30+
const (
31+
// mocked variable IDs
32+
switchyID = "switchy-id"
33+
relayID = "relay-id"
34+
blinkSpeedID = "blink_speed-id"
35+
36+
thingOverriddenID = "thing-overridden-id"
37+
switchyOverriddenID = "switchy-overridden-id"
38+
)
39+
3040
var (
3141
dashboardTemplateTest = map[string]interface{}{
3242
"id": "home-security-alarm-dashboard",
@@ -69,8 +79,7 @@ var (
6979
Widgets: []iotclient.Widget{
7080
{Name: "Switch-name", Height: 1, HeightMobile: 2, Width: 3, WidthMobile: 4,
7181
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"},
82+
Variables: []string{switchyID},
7483
},
7584
},
7685
}
@@ -80,26 +89,21 @@ var (
8089
Widgets: []iotclient.Widget{
8190
{Name: "Switch-name", Height: 1, HeightMobile: 2, Width: 3, WidthMobile: 4,
8291
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"},
92+
Variables: []string{switchyOverriddenID},
8593
},
8694
},
8795
}
8896

8997
dashboardTwoWidgets = &iotclient.Dashboardv2{
9098
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
9399
Widgets: []iotclient.Widget{
94100
{Name: "blink_speed", Height: 7, Width: 8,
95101
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"},
102+
Variables: []string{blinkSpeedID},
98103
},
99104
{Name: "relay_2", Height: 5, Width: 5,
100105
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"},
106+
Variables: []string{relayID},
103107
},
104108
},
105109
}
@@ -143,17 +147,20 @@ func TestLoadTemplate(t *testing.T) {
143147
func TestLoadDashboard(t *testing.T) {
144148
mockClient := &mocks.Client{}
145149
mockThingShow := func(thingID string) *iotclient.ArduinoThing {
146-
thing := &iotclient.ArduinoThing{
150+
if thingID == thingOverriddenID {
151+
return &iotclient.ArduinoThing{
152+
Properties: []iotclient.ArduinoProperty{
153+
{Id: switchyOverriddenID, Name: "switchy"},
154+
},
155+
}
156+
}
157+
return &iotclient.ArduinoThing{
147158
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"},
159+
{Id: switchyID, Name: "switchy"},
160+
{Id: relayID, Name: "relay_2"},
161+
{Id: blinkSpeedID, Name: "blink_speed"},
154162
},
155163
}
156-
return thing
157164
}
158165
mockClient.On("ThingShow", mock.AnythingOfType("string")).Return(mockThingShow, nil)
159166

@@ -194,7 +201,7 @@ func TestLoadDashboard(t *testing.T) {
194201
{
195202
name: "dashboard with variable, thing is overridden",
196203
file: "testdata/dashboard-with-variable.yaml",
197-
override: map[string]string{"thing": "overridden"},
204+
override: map[string]string{"thing": thingOverriddenID},
198205
want: dashboardVariableOverride,
199206
},
200207

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)