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