Skip to content

Commit 7c4e9b8

Browse files
committed
Add mixed consumption patterns test
Test that the same value group can be consumed as both map and slice in different parts of the same application. This validates an important real-world use case where hybrid consumption patterns are needed.
1 parent c90ed23 commit 7c4e9b8

File tree

1 file changed

+55
-0
lines changed

1 file changed

+55
-0
lines changed

map_groups_test.go

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -468,5 +468,60 @@ func TestMapValueGroupsEdgeCases(t *testing.T) {
468468
require.Error(t, err)
469469
assert.Contains(t, err.Error(), "value groups may be consumed as slices or string-keyed maps only")
470470
})
471+
472+
t.Run("mixed consumption patterns", func(t *testing.T) {
473+
t.Parallel()
474+
475+
var mapServices map[string]testSimpleService
476+
var sliceServices []testSimpleService
477+
478+
app := fxtest.New(t,
479+
fx.Provide(
480+
// Named services for map consumption
481+
fx.Annotate(
482+
func() testSimpleService { return &testBasicService{name: "auth-service"} },
483+
fx.ResultTags(`name:"auth" group:"services"`),
484+
),
485+
fx.Annotate(
486+
func() testSimpleService { return &testBasicService{name: "billing-service"} },
487+
fx.ResultTags(`name:"billing" group:"services"`),
488+
),
489+
fx.Annotate(
490+
func() testSimpleService { return &testBasicService{name: "metrics-service"} },
491+
fx.ResultTags(`name:"metrics" group:"services"`),
492+
),
493+
),
494+
fx.Invoke(fx.Annotate(
495+
func(services map[string]testSimpleService) {
496+
mapServices = services
497+
},
498+
fx.ParamTags(`group:"services"`),
499+
)),
500+
fx.Invoke(fx.Annotate(
501+
func(services []testSimpleService) {
502+
sliceServices = services
503+
},
504+
fx.ParamTags(`group:"services"`),
505+
)),
506+
)
507+
defer app.RequireStart().RequireStop()
508+
509+
// Map consumption should work with named services
510+
require.Len(t, mapServices, 3)
511+
require.Contains(t, mapServices, "auth")
512+
require.Contains(t, mapServices, "billing")
513+
require.Contains(t, mapServices, "metrics")
514+
assert.Equal(t, "auth-service", mapServices["auth"].GetName())
515+
assert.Equal(t, "billing-service", mapServices["billing"].GetName())
516+
assert.Equal(t, "metrics-service", mapServices["metrics"].GetName())
517+
518+
// Slice consumption should also work with the same services
519+
require.Len(t, sliceServices, 3)
520+
serviceNames := make([]string, len(sliceServices))
521+
for i, service := range sliceServices {
522+
serviceNames[i] = service.GetName()
523+
}
524+
assert.ElementsMatch(t, []string{"auth-service", "billing-service", "metrics-service"}, serviceNames)
525+
})
471526
}
472527

0 commit comments

Comments
 (0)