Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add tests
  • Loading branch information
polldo committed Oct 12, 2021
commit f88dfa22b9f96c354485ea3e1e2275703b77b228
219 changes: 219 additions & 0 deletions internal/template/load_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,219 @@
// This file is part of arduino-cloud-cli.
//
// Copyright (C) 2021 ARDUINO SA (http://www.arduino.cc/)
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.

package template

import (
"testing"

iotclient "github.com/arduino/iot-client-go"
"github.com/google/go-cmp/cmp"
)

const (
uuidv4Length = 36
)

var (
dashboardTemplate = map[string]interface{}{
"id": "home-security-alarm-dashboard",
"name": "Home Security Alarm",
"widgets": []interface{}{
map[string]interface{}{
"type": "Messenger", "name": "message_update",
"variables": []interface{}{map[string]interface{}{"thing_id": "home-security-alarm", "variable_id": "message_update"}},
},
map[string]interface{}{
"type": "Switch", "name": "light_alarm",
"variables": []interface{}{map[string]interface{}{"thing_id": "home-security-alarm", "variable_id": "light_alarm"}},
"options": map[string]interface{}{"showLabels": true},
},
},
}

dashboardDetailed = &iotclient.Dashboardv2{
Name: "dashboard",
Widgets: []iotclient.Widget{
{Name: "Switch-name", Height: 1, HeightMobile: 2, Width: 3, WidthMobile: 4,
X: 5, XMobile: 6, Y: 7, YMobile: 8, Options: map[string]interface{}{"showLabels": true},
Type: "Switch",
},
},
}

dashboardNoOptions = &iotclient.Dashboardv2{
Name: "dashboard-no-options",
Widgets: []iotclient.Widget{
{Name: "Switch-name", Height: 1, HeightMobile: 2, Width: 3, WidthMobile: 4,
X: 5, XMobile: 6, Y: 7, YMobile: 8, Options: map[string]interface{}{},
Type: "Switch",
},
},
}

dashboardWithVariable = &iotclient.Dashboardv2{
Name: "dashboard-with-variable",
Widgets: []iotclient.Widget{
{Name: "Switch-name", Height: 1, HeightMobile: 2, Width: 3, WidthMobile: 4,
X: 5, XMobile: 6, Y: 7, YMobile: 8, Options: map[string]interface{}{"showLabels": true},
// in this test, the variable id is a concatenation of thing_id and variable_id
// this depends on the mocked function getVariableID
Type: "Switch", Variables: []string{"thing-variable"},
},
},
}

dashboardVariableOverride = &iotclient.Dashboardv2{
Name: "dashboard-with-variable",
Widgets: []iotclient.Widget{
{Name: "Switch-name", Height: 1, HeightMobile: 2, Width: 3, WidthMobile: 4,
X: 5, XMobile: 6, Y: 7, YMobile: 8, Options: map[string]interface{}{"showLabels": true},
// in this test, the variable id is a concatenation of thing_id and variable_id
// this depends on the mocked function getVariableID
Type: "Switch", Variables: []string{"overridden-variable"},
},
},
}

dashboardTwoWidgets = &iotclient.Dashboardv2{
Name: "dashboard-two-widgets",
Widgets: []iotclient.Widget{
{Name: "blink_speed", Height: 7, Width: 8,
X: 7, Y: 5, Options: map[string]interface{}{"min": float64(0), "max": float64(5000)},
Type: "Slider", Variables: []string{"remote-controlled-lights-blink_speed"},
},
{Name: "relay_2", Height: 5, Width: 5,
X: 5, Y: 0, Options: map[string]interface{}{"showLabels": true},
Type: "Switch", Variables: []string{"remote-controlled-lights-relay_2"},
},
},
}
)

func TestLoadTemplate(t *testing.T) {
tests := []struct {
name string
file string
override map[string]string
want map[string]interface{}
}{

{
name: "yaml dashboard template",
file: "testdata/home-security-dashboard.yaml",
want: dashboardTemplate,
},

{
name: "json dashboard template",
file: "testdata/home-security-dashboard.json",
want: dashboardTemplate,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
var got map[string]interface{}
err := loadTemplate(tt.file, &got)
if err != nil {
t.Errorf("%v", err)
}
if !cmp.Equal(got, tt.want) {
t.Errorf("Wrong template received, got=\n%s", cmp.Diff(tt.want, got))
}
})
}
}

func TestLoadDashboard(t *testing.T) {
tests := []struct {
name string
file string
override map[string]string
want *iotclient.Dashboardv2
}{
{
name: "dashboard detailed",
file: "testdata/dashboard-detailed.yaml",
override: nil,
want: dashboardDetailed,
},

{
name: "dashboard with wrong options to be filtered out",
file: "testdata/dashboard-wrong-options.yaml",
override: nil,
want: dashboardDetailed,
},

{
name: "dashboard without options, should have a not nil map",
file: "testdata/dashboard-no-options.yaml",
override: nil,
want: dashboardNoOptions,
},

{
name: "dashboard with variable, mocked variable id is concatenation of thing_id and variable_id",
file: "testdata/dashboard-with-variable.yaml",
override: nil,
want: dashboardWithVariable,
},

{
name: "dashboard with variable, thing is overridden",
file: "testdata/dashboard-with-variable.yaml",
override: map[string]string{"thing": "overridden"},
want: dashboardVariableOverride,
},

{
name: "dashboard with two widgets",
file: "testdata/dashboard-two-widgets.yaml",
override: nil,
want: dashboardTwoWidgets,
},
}

vargetter.getVariableID = mockGetVariableID

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := LoadDashboard(tt.file, tt.override)
if err != nil {
t.Errorf("%v", err)
}

for i := range got.Widgets {
// check widget id generation
id := got.Widgets[i].Id
if len(id) != uuidv4Length {
t.Errorf("Widget ID is wrong: = %s", id)
}
got.Widgets[i].Id = ""
}

if !cmp.Equal(got, tt.want) {
t.Errorf("Wrong template received, got=\n%s", cmp.Diff(tt.want, got))
}
})
}
}

func mockGetVariableID(thingID string, variableName string) (string, error) {
return thingID + "-" + variableName, nil
}
14 changes: 14 additions & 0 deletions internal/template/testdata/dashboard-detailed.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
name: dashboard
widgets:
- height: 1
height_mobile: 2
name: Switch-name
options:
showLabels: true
type: Switch
width: 3
width_mobile: 4
x: 5
x_mobile: 6
"y": 7
y_mobile: 8
12 changes: 12 additions & 0 deletions internal/template/testdata/dashboard-no-options.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
name: dashboard-no-options
widgets:
- height: 1
height_mobile: 2
name: Switch-name
type: Switch
width: 3
width_mobile: 4
x: 5
x_mobile: 6
"y": 7
y_mobile: 8
26 changes: 26 additions & 0 deletions internal/template/testdata/dashboard-two-widgets.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
id: dashboard-two-widgets
name: dashboard-two-widgets
widgets:
- type: Slider
name: blink_speed
width: 8
height: 7
x: 7
y: 5
variables:
- thing_id: remote-controlled-lights
variable_id: blink_speed
options:
min: 0
max: 5000
- type: Switch
name: relay_2
width: 5
height: 5
x: 5
y: 0
variables:
- thing_id: remote-controlled-lights
variable_id: relay_2
options:
showLabels: true
17 changes: 17 additions & 0 deletions internal/template/testdata/dashboard-with-variable.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
name: dashboard-with-variable
widgets:
- height: 1
height_mobile: 2
name: Switch-name
options:
showLabels: true
variables:
- thing_id: thing
variable_id: variable
type: Switch
width: 3
width_mobile: 4
x: 5
x_mobile: 6
"y": 7
y_mobile: 8
18 changes: 18 additions & 0 deletions internal/template/testdata/dashboard-wrong-options.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
name: dashboard
widgets:
- height: 1
height_mobile: 2
name: Switch-name
options:
showLabels: true
mode: false
percentage: 100
step: true
interpolation: true
type: Switch
width: 3
width_mobile: 4
x: 5
x_mobile: 6
"y": 7
y_mobile: 8
29 changes: 29 additions & 0 deletions internal/template/testdata/home-security-dashboard.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"id": "home-security-alarm-dashboard",
"name": "Home Security Alarm",
"widgets": [
{
"type": "Messenger",
"name": "message_update",
"variables": [
{
"thing_id": "home-security-alarm",
"variable_id": "message_update"
}
]
},
{
"type": "Switch",
"name": "light_alarm",
"variables": [
{
"thing_id": "home-security-alarm",
"variable_id": "light_alarm"
}
],
"options": {
"showLabels": true
}
}
]
}
15 changes: 15 additions & 0 deletions internal/template/testdata/home-security-dashboard.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
id: home-security-alarm-dashboard
name: Home Security Alarm
widgets:
- type: Messenger
name: message_update
variables:
- thing_id: home-security-alarm
variable_id: message_update
- type: Switch
name: light_alarm
variables:
- thing_id: home-security-alarm
variable_id: light_alarm
options:
showLabels: true
Loading