@@ -75,13 +75,15 @@ func TestBrickCreate(t *testing.T) {
7575 require .Nil (t , paths .New ("testdata/dummy-app" ).CopyDirTo (tempDummyApp ))
7676
7777 req := BrickCreateUpdateRequest {ID : "arduino:dbstorage_sqlstore" }
78- err = brickService .BrickCreate (req , f .Must (app .Load (tempDummyApp .String ())))
78+ before := f .Must (app .Load (tempDummyApp .String ()))
79+ err = brickService .BrickCreate (req , before )
7980 require .Nil (t , err )
8081 after , err := app .Load (tempDummyApp .String ())
8182 require .Nil (t , err )
82- require . Len (t , after .Descriptor . Bricks , 2 )
83- require . Equal (t , "arduino:dbstorage_sqlstore" , after . Descriptor . Bricks [ 1 ]. ID )
83+ requireBricksSizeUpdatedBy (t , before .Descriptor , after . Descriptor , 1 )
84+ requireBricksContain (t , after . Descriptor , "arduino:dbstorage_sqlstore" )
8485 })
86+
8587 t .Run ("the variables of a brick are updated" , func (t * testing.T ) {
8688 tempDummyApp := paths .New ("testdata/dummy-app.brick-override.temp" )
8789 err := tempDummyApp .RemoveAll ()
@@ -102,13 +104,14 @@ func TestBrickCreate(t *testing.T) {
102104 },
103105 }
104106
105- err = brickService .BrickCreate (req , f .Must (app .Load (tempDummyApp .String ())))
107+ before := f .Must (app .Load (tempDummyApp .String ()))
108+ err = brickService .BrickCreate (req , before )
106109 require .Nil (t , err )
107110
108111 after , err := app .Load (tempDummyApp .String ())
109112 require .Nil (t , err )
110- require . Len (t , after .Descriptor . Bricks , 1 )
111- require . Equal (t , "arduino:arduino_cloud" , after . Descriptor . Bricks [ 0 ]. ID )
113+ requireBricksSizeUpdatedBy (t , before .Descriptor , after . Descriptor , 0 )
114+ requireBricksContain (t , after . Descriptor , "arduino:arduino_cloud" )
112115 require .Equal (t , deviceID , after .Descriptor .Bricks [0 ].Variables ["ARDUINO_DEVICE_ID" ])
113116 require .Equal (t , secret , after .Descriptor .Bricks [0 ].Variables ["ARDUINO_SECRET" ])
114117 })
@@ -256,28 +259,51 @@ func TestBrickCreateWithModulesUpdate(t *testing.T) {
256259 }
257260}
258261
259- /*
260- t.Run("add a brick to an App", func(t *testing.T) {
261- dummyApp := appDummySetup(t)
262+ func TestAppBrickInstanceDetails (t * testing.T ) {
263+ bricksIndex , err := bricksindex .GenerateBricksIndexFromFile (paths .New ("testdata" ))
264+ require .Nil (t , err )
265+ modelsIndex , err := modelsindex .GenerateModelsIndexFromFile (paths .New ("testdata" ))
266+ require .Nil (t , err )
267+ brickService := NewService (modelsIndex , bricksIndex , nil )
262268
263- model := "glass-breaking"
264- req := BrickCreateUpdateRequest{
265- ID: "arduino:audio_classification",
266- Variables: nil,
267- Model: &model,
268- }
269+ tests := []struct {
270+ name string
271+ brickId string
272+ expectedErrorMessage string
273+ expectedModelId string
274+ }{
275+ {
276+ name : "details for a brick defined in the app" ,
277+ brickId : "arduino:video_object_detection" ,
278+ expectedErrorMessage : "" ,
279+ expectedModelId : "yolox-object-detection" ,
280+ },
269281
270- // act
271- err = brickService.BrickCreate(req, f.Must(app.Load(dummyApp.String())))
272- require.Nil(t, err)
282+ {
283+ name : "details should be not available for a brick not defined in the app" ,
284+ brickId : "arduino:audio_classification" ,
285+ expectedErrorMessage : "brick arduino:audio_classification not added in the app" ,
286+ },
273287
274- // assert
275- after, err := app.Load(dummyApp.String())
276- require.Nil(t, err)
277- require.Len(t, after.Descriptor.Bricks, 2)
278- require.Equal(t, "arduino:audio_classification", after.Descriptor.Bricks[1].ID)
279- })
280- */
288+ {
289+ name : "details for a not exitent brick" ,
290+ brickId : "arduino:notExistentBrick" ,
291+ expectedErrorMessage : "brick not found" ,
292+ },
293+ }
294+
295+ for _ , tt := range tests {
296+ dummyApp := appDummySetup (t )
297+ ymlApp := f .Must (app .Load (dummyApp .String ()))
298+ brickInstance , err := brickService .AppBrickInstanceDetails (& ymlApp , tt .brickId )
299+ if err == nil {
300+ require .Equal (t , tt .brickId , brickInstance .ID )
301+ require .Equal (t , tt .expectedModelId , brickInstance .ModelID )
302+ } else {
303+ require .Equal (t , tt .expectedErrorMessage , err .Error ())
304+ }
305+ }
306+ }
281307
282308func appDummySetup (t * testing.T ) * paths.Path {
283309 tempDummyApp := paths .New ("testdata/dummy-app.temp" )
@@ -286,3 +312,21 @@ func appDummySetup(t *testing.T) *paths.Path {
286312 require .Nil (t , paths .New ("testdata/dummy-app" ).CopyDirTo (tempDummyApp ))
287313 return tempDummyApp
288314}
315+
316+ func requireBricksSizeUpdatedBy (t * testing.T , before app.AppDescriptor , after app.AppDescriptor , value int ) {
317+ require .Len (t , after .Bricks , len (before .Bricks )+ value )
318+ }
319+
320+ // getBrickIndexByBrickId searches the Bricks slice within the AppDescriptor
321+ // for a Brick whose ID matches the provided brickId.
322+ func getBrickIndexByBrickId (application app.AppDescriptor , brickId string ) int {
323+ idx := slices .IndexFunc (application .Bricks , func (b app.Brick ) bool {
324+ return brickId == b .ID
325+ })
326+ return idx
327+ }
328+
329+ func requireBricksContain (t * testing.T , application app.AppDescriptor , brickID string ) {
330+ idx := getBrickIndexByBrickId (application , brickID )
331+ require .NotEqual (t , idx , - 1 )
332+ }
0 commit comments