-
-
Notifications
You must be signed in to change notification settings - Fork 5
Add dashboard create command #50
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
c04011f
Add dashboard create command
polldo f88dfa2
Add tests
polldo 6ed7285
Inject iotClient as dependency
polldo 0becfc8
Update widget options filter
polldo b06323c
Update go.mod
polldo b72e27b
Fix wrong widget options in tests
polldo 1d996a4
Remove share details
polldo 46092d3
Remove duplicated filter
polldo 0d26146
Move getVariableID in dashboard file
polldo e58bfc9
Rename dashboard template structs
polldo cdf4888
Update readme
polldo ded4984
Improve comments
polldo 60f38b3
Improve errors
polldo 328ceb5
Fix typo
polldo d2661fb
Comment dashboard template marshal method
polldo 45b3b42
remove unused testdata file
polldo 39b49e4
Improve uuid test
polldo 2b4dc1d
Improve test messages
polldo 8ae3914
Improve tests
polldo b4473a7
Move iot Client mock
polldo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Add tests
- Loading branch information
commit f88dfa22b9f96c354485ea3e1e2275703b77b228
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
polldo marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
got.Widgets[i].Id = "" | ||
polldo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
if !cmp.Equal(got, tt.want) { | ||
t.Errorf("Wrong template received, got=\n%s", cmp.Diff(tt.want, got)) | ||
polldo marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
} | ||
}) | ||
} | ||
} | ||
|
||
func mockGetVariableID(thingID string, variableName string) (string, error) { | ||
return thingID + "-" + variableName, nil | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} | ||
] | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.