Skip to content

Commit 91a2170

Browse files
committed
add unit tests
1 parent 7e10306 commit 91a2170

File tree

2 files changed

+152
-6
lines changed

2 files changed

+152
-6
lines changed

internal/orchestrator/bricks/bricks_test.go

Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -489,3 +489,155 @@ bricks:
489489
require.NoError(t, os.MkdirAll(pythonDir, 0755))
490490
require.NoError(t, os.WriteFile(filepath.Join(pythonDir, "main.py"), []byte("print('hello')"), 0600))
491491
}
492+
493+
func TestAppBrickInstanceModelsDetails(t *testing.T) {
494+
495+
bIndex := &bricksindex.BricksIndex{
496+
Bricks: []bricksindex.Brick{
497+
{
498+
ID: "arduino:object_detection",
499+
Name: "Object Detection",
500+
Category: "video",
501+
ModelName: "yolox-object-detection", // Default model
502+
Variables: []bricksindex.BrickVariable{
503+
{Name: "EI_OBJ_DETECTION_MODEL", DefaultValue: "default_path", Description: "path to the model file"},
504+
{Name: "CUSTOM_MODEL_PATH", DefaultValue: "/home/arduino/.arduino-bricks/ei-models", Description: "path to the custom model directory"},
505+
},
506+
},
507+
{
508+
ID: "arduino:weather_forecast",
509+
Name: "Weather Forecast",
510+
Category: "miscellaneous",
511+
ModelName: "",
512+
},
513+
},
514+
}
515+
516+
mIndex := &modelsindex.ModelsIndex{
517+
Models: []modelsindex.AIModel{
518+
519+
{
520+
ID: "yolox-object-detection",
521+
Name: "General purpose object detection - YoloX",
522+
ModuleDescription: "General purpose object detection...",
523+
Bricks: []string{"arduino:object_detection", "arduino:video_object_detection"},
524+
},
525+
{
526+
ID: "face-detection",
527+
Name: "Lightweight-Face-Detection",
528+
Bricks: []string{"arduino:object_detection", "arduino:video_object_detection"},
529+
},
530+
}}
531+
532+
svc := &Service{
533+
bricksIndex: bIndex,
534+
modelsIndex: mIndex,
535+
}
536+
537+
tests := []struct {
538+
name string
539+
app *app.ArduinoApp
540+
brickID string
541+
expectedError string
542+
validate func(*testing.T, BrickInstance)
543+
}{
544+
{
545+
name: "Brick not found in global Index",
546+
brickID: "arduino:non_existent_brick",
547+
app: &app.ArduinoApp{
548+
Descriptor: app.AppDescriptor{Bricks: []app.Brick{}},
549+
},
550+
expectedError: "brick not found",
551+
},
552+
{
553+
name: "Brick found in Index but not added to App",
554+
brickID: "arduino:object_detection",
555+
app: &app.ArduinoApp{
556+
Descriptor: app.AppDescriptor{
557+
Bricks: []app.Brick{
558+
{ID: "arduino:weather_forecast"},
559+
},
560+
},
561+
},
562+
expectedError: "brick arduino:object_detection not added in the app",
563+
},
564+
{
565+
name: "Success - Standard Brick without Model",
566+
brickID: "arduino:weather_forecast",
567+
app: &app.ArduinoApp{
568+
Descriptor: app.AppDescriptor{
569+
Bricks: []app.Brick{
570+
{ID: "arduino:weather_forecast"},
571+
},
572+
},
573+
},
574+
validate: func(t *testing.T, res BrickInstance) {
575+
require.Equal(t, "arduino:weather_forecast", res.ID)
576+
require.Equal(t, "Weather Forecast", res.Name)
577+
require.Equal(t, "installed", res.Status)
578+
require.Empty(t, res.ModelID)
579+
require.Empty(t, res.CompatibleModels)
580+
},
581+
},
582+
{
583+
name: "Success - Brick with Default Model",
584+
brickID: "arduino:object_detection",
585+
app: &app.ArduinoApp{
586+
Descriptor: app.AppDescriptor{
587+
Bricks: []app.Brick{
588+
{
589+
ID: "arduino:object_detection",
590+
},
591+
},
592+
},
593+
},
594+
validate: func(t *testing.T, res BrickInstance) {
595+
require.Equal(t, "arduino:object_detection", res.ID)
596+
require.Equal(t, "yolox-object-detection", res.ModelID)
597+
require.Len(t, res.CompatibleModels, 2)
598+
require.Equal(t, "yolox-object-detection", res.CompatibleModels[0].ID)
599+
require.Equal(t, "face-detection", res.CompatibleModels[1].ID)
600+
},
601+
},
602+
{
603+
name: "Success - Brick with Overridden Model in App",
604+
brickID: "arduino:object_detection",
605+
app: &app.ArduinoApp{
606+
Descriptor: app.AppDescriptor{
607+
Bricks: []app.Brick{
608+
{
609+
ID: "arduino:object_detection",
610+
Model: "face-detection",
611+
},
612+
},
613+
},
614+
},
615+
validate: func(t *testing.T, res BrickInstance) {
616+
require.Equal(t, "arduino:object_detection", res.ID)
617+
require.Equal(t, "face-detection", res.ModelID)
618+
require.Len(t, res.CompatibleModels, 2)
619+
require.Equal(t, "yolox-object-detection", res.CompatibleModels[0].ID)
620+
require.Equal(t, "face-detection", res.CompatibleModels[1].ID)
621+
},
622+
},
623+
}
624+
625+
for _, tt := range tests {
626+
t.Run(tt.name, func(t *testing.T) {
627+
result, err := svc.AppBrickInstanceDetails(tt.app, tt.brickID)
628+
629+
if tt.expectedError != "" {
630+
require.Error(t, err)
631+
if err != nil {
632+
require.Contains(t, err.Error(), tt.expectedError)
633+
}
634+
return
635+
}
636+
637+
require.NoError(t, err)
638+
if tt.validate != nil {
639+
tt.validate(t, result)
640+
}
641+
})
642+
}
643+
}

internal/orchestrator/bricks/types.go

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -85,9 +85,3 @@ type BrickDetailsResult struct {
8585
UsedByApps []AppReference `json:"used_by_apps"`
8686
Models []AIModel `json:"models"`
8787
}
88-
89-
type AIModel struct {
90-
ID string `json:"id"`
91-
Name string `json:"name"`
92-
Description string `json:"description"`
93-
}

0 commit comments

Comments
 (0)