Skip to content

Commit e713470

Browse files
committed
Test AppBrickInstanceDetails.
1 parent 3bea62b commit e713470

File tree

4 files changed

+91
-26
lines changed

4 files changed

+91
-26
lines changed

internal/orchestrator/bricks/bricks_test.go

Lines changed: 69 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -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

282308
func 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+
}

internal/orchestrator/bricks/testdata/bricks-list.yaml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,13 @@ bricks:
3131
require_devices: false
3232
category: audio
3333
model_name: glass-breaking
34+
- id: arduino:video_object_detection
35+
name: Video Object Detection
36+
description: This object analyze video streams from a camera.
37+
require_container: true
38+
require_model: true
39+
require_devices: true
40+
mount_devices_into_container: true
41+
ports: []
42+
category: null
43+
model_name: yolox-object-detection

internal/orchestrator/bricks/testdata/dummy-app/app.yaml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@ description: Control the LED from the Arduino IoT Cloud using RPC calls
33
icon: ☁️
44
ports: []
55
bricks:
6-
- arduino:arduino_cloud:
6+
- arduino:arduino_cloud:
7+
- arduino:video_object_detection:

internal/orchestrator/bricks/testdata/models-list.yaml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,13 @@ models:
77
"EI_AUDIO_CLASSIFICATION_MODEL": "/models/ootb/ei/glass-breaking.eim"
88
bricks:
99
- arduino:audio_classification
10+
- face-detection:
11+
runner: brick
12+
name : "Lightweight-Face-Detection"
13+
description: "Face bounding box detection. This model is trained on the WIDER FACE dataset and can detect faces in images."
14+
model_configuration:
15+
"EI_OBJ_DETECTION_MODEL": "/models/ootb/ei/lw-face-det.eim"
16+
model_labels:
17+
- face
18+
bricks:
19+
- arduino:object_detection

0 commit comments

Comments
 (0)