From 0dff6f842b13d8c409f43e60361a647bbc6bbfd0 Mon Sep 17 00:00:00 2001 From: Aldo Fuster Turpin Date: Sat, 8 Jun 2024 15:52:29 +0000 Subject: [PATCH] refactor TestTenantIDFromContext to use subtests and remove elses --- frontend/pkg/frontend/context_test.go | 35 ++++++++++++++++----------- 1 file changed, 21 insertions(+), 14 deletions(-) diff --git a/frontend/pkg/frontend/context_test.go b/frontend/pkg/frontend/context_test.go index 18fbd8b10..ea01b2538 100644 --- a/frontend/pkg/frontend/context_test.go +++ b/frontend/pkg/frontend/context_test.go @@ -36,20 +36,27 @@ func TestTenantIDFromContext(t *testing.T) { } for _, test := range tests { - ctx := ContextWithSubscription(context.Background(), test.sub) - actual, err := TenantIDFromContext(ctx) - if err != nil { - if !test.expectErr { - t.Errorf("expected err to be nil, got %v", err) - } - } else { - if test.expectErr { - t.Error("expected err to be non-nil") - } - - if actual != test.expected { - t.Errorf("expected %s, got %s", test.expected, actual) - } + t.Run(test.name, func(t *testing.T) { + ctx := ContextWithSubscription(context.Background(), test.sub) + actual, err := TenantIDFromContext(ctx) + assertError(t, actual, test.expected, err, test.expectErr) + }) + } +} + +func assertError(t *testing.T, actual string, expected string, err error, expectErr bool) { + if err != nil { + if !expectErr { + t.Errorf("expected err to be nil, got %v", err) } + return + } + + if expectErr { + t.Error("expected err to be non-nil") + } + + if actual != expected { + t.Errorf("expected %s, got %s", expected, actual) } }