Skip to content

Commit

Permalink
Merge pull request #191 from Azure/refactor_TestTenantIDFromContext
Browse files Browse the repository at this point in the history
refactor TestTenantIDFromContext to use subtests and remove else
  • Loading branch information
mjlshen authored Jun 10, 2024
2 parents 5bb0b64 + 0dff6f8 commit ace4e8b
Showing 1 changed file with 21 additions and 14 deletions.
35 changes: 21 additions & 14 deletions frontend/pkg/frontend/context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

0 comments on commit ace4e8b

Please sign in to comment.