Refactor router test.

* Split test-cases into own runners
This commit is contained in:
2023-02-07 05:35:32 +00:00
parent 3507805c29
commit 2cfe36590f

View File

@@ -22,7 +22,7 @@ func TestWebserviceGetSuperHeros(t *testing.T) {
router := api.NewRouter(heros, 5, nil)
// Test cases
for _, tt := range []struct {
for _, tc := range []struct {
name string // Test name
url string // URL to test
encrypted bool // Encrypted identities
@@ -33,8 +33,11 @@ func TestWebserviceGetSuperHeros(t *testing.T) {
{"Test retrieve superheroes that match given superpower(s)", "/superheroes?superpowers=healing", false, &[]string{"healing"}},
{"Test retrieve superheroes that match given superpower(s) with encrypted identities", "/superheroes?superpowers=healing&encrypted=true", true, &[]string{"healing"}},
} {
// Test with valid domain model
req, err := http.NewRequest(http.MethodGet, tt.url, strings.NewReader(""))
// Run test case
t.Run(tc.name, func(t *testing.T) {
// Create request
req, err := http.NewRequest(http.MethodGet, tc.url, strings.NewReader(""))
if err != nil {
t.Fatal("Error creating request: ", err)
}
@@ -79,7 +82,7 @@ func TestWebserviceGetSuperHeros(t *testing.T) {
}
// Test encrypted identities
if tt.encrypted {
if tc.encrypted {
if hero.Name == "superman" && (hero.Identity.FirstName != "hqfwp" || hero.Identity.LastName != "pjsy") {
t.Errorf("Expected encoded superman identity")
}
@@ -95,19 +98,20 @@ func TestWebserviceGetSuperHeros(t *testing.T) {
}
// Test superpowers filter
if tt.filterBy != nil {
for _, wantPower := range *tt.filterBy {
if tc.filterBy != nil {
for _, wantPower := range *tc.filterBy {
if !hero.Has(wantPower) {
t.Errorf("Expected superpower %s to be in %s", wantPower, hero.SuperPowers)
}
}
}
}
})
}
}
// TestWebserviceStoreSuperhero tests the HandleWebserviceRequest function
// TODO: Test invalid domain models
// TODO: Test invalid storing data (e.g. invalid JSON)
func TestWebserviceStoreSuperhero(t *testing.T) {
// Load heros
heros, err := deesee.Load(dataPath)