@@ -28,6 +28,54 @@ import (
2828 "github.com/arduino/go-paths-helper"
2929)
3030
31+ func TestBrickCreate (t * testing.T ) {
32+ bricksIndex , err := bricksindex .GenerateBricksIndexFromFile (paths .New ("testdata" ))
33+ require .Nil (t , err )
34+ brickService := NewService (nil , bricksIndex , nil )
35+
36+ t .Run ("fails if brick id does not exist" , func (t * testing.T ) {
37+ err = brickService .BrickCreate (BrickCreateUpdateRequest {ID : "not-existing-id" }, f .Must (app .Load ("./testdata/my-app" )))
38+ require .Error (t , err )
39+ require .Equal (t , "brick 'not-existing-id' not found" , err .Error ())
40+ })
41+
42+ t .Run ("fails if the requestes variable is not present in the brick definition" , func (t * testing.T ) {
43+ req := BrickCreateUpdateRequest {ID : "arduino:arduino_cloud" , Variables : map [string ]string {
44+ "NON_EXISTING_VARIABLE" : "some-value" ,
45+ }}
46+ err = brickService .BrickCreate (req , f .Must (app .Load ("./testdata/my-app" )))
47+ require .Error (t , err )
48+ require .Equal (t , "variable 'NON_EXISTING_VARIABLE' does not exist on brick 'arduino:arduino_cloud'" , err .Error ())
49+ })
50+
51+ t .Run ("fails if a required variable is set empty" , func (t * testing.T ) {
52+ req := BrickCreateUpdateRequest {ID : "arduino:arduino_cloud" , Variables : map [string ]string {
53+ "ARDUINO_DEVICE_ID" : "" ,
54+ "ARDUINO_SECRET" : "a-secret-a" ,
55+ }}
56+ err = brickService .BrickCreate (req , f .Must (app .Load ("./testdata/my-app" )))
57+ require .Error (t , err )
58+ require .Equal (t , "variable 'ARDUINO_DEVICE_ID' cannot be empty" , err .Error ())
59+ })
60+
61+ t .Run ("fails if a mandatory variable is not present in the request" , func (t * testing.T ) {
62+ req := BrickCreateUpdateRequest {ID : "arduino:arduino_cloud" , Variables : map [string ]string {
63+ "ARDUINO_SECRET" : "a-secret-a" ,
64+ }}
65+ err = brickService .BrickCreate (req , f .Must (app .Load ("./testdata/my-app" )))
66+ require .Error (t , err )
67+ require .Equal (t , "required variable 'ARDUINO_DEVICE_ID' is mandatory" , err .Error ())
68+ })
69+
70+ t .Run ("the brick is added if it does not exist in the app" , func (t * testing.T ) {
71+ req := BrickCreateUpdateRequest {ID : "arduino:dbstorage_sqlstore" }
72+ // TODO: find a better way to test if the brick has been added to the app.yaml
73+ // Currently we only check that there is no error since the app.yaml is populated with the brick at every test execution.
74+ err = brickService .BrickCreate (req , f .Must (app .Load ("./testdata/my-app" )))
75+ require .Nil (t , err )
76+ })
77+ }
78+
3179func TestOverrideBrickVariablesOfApp (t * testing.T ) {
3280 bricksIndex , err := bricksindex .GenerateBricksIndexFromFile (paths .New ("testdata" ))
3381 require .Nil (t , err )
0 commit comments