diff --git a/routes_test.go b/routes_test.go index 08a1c09..5a4b226 100644 --- a/routes_test.go +++ b/routes_test.go @@ -9,7 +9,7 @@ import ( "testing" ) -func TestAliveRoute(t *testing.T) { +func TestAlive(t *testing.T) { db := setupTestDatabase("testalive") env := &Env{db: db} router := setupRouter(env, spotifyClientID, spotifyClientSecret) @@ -22,7 +22,7 @@ func TestAliveRoute(t *testing.T) { assert.Equal(t, "yes!", w.Body.String()) } -func TestGetArtistByIDRoute(t *testing.T) { +func TestGetArtistByID(t *testing.T) { db := setupTestDatabase("testgetartistbyid") env := &Env{db: db} router := setupRouter(env, spotifyClientID, spotifyClientSecret) @@ -39,7 +39,7 @@ func TestGetArtistByIDRoute(t *testing.T) { var resp ArtistProfile err := json.NewDecoder(w.Body).Decode(&resp) if err != nil { - assert.Fail(t, fmt.Sprintf("Could not validate and parse JSON response into SpotifyResponse struct: %s", err.Error())) + assert.Fail(t, fmt.Sprintf("Could not validate and parse JSON response into ArtistProfile struct: %s", err.Error())) } assert.Equal(t, "0TnOYISbd1XYRBk9myaseg", resp.SpotifyID) assert.Equal(t, "Pitbull", resp.Name) @@ -53,3 +53,77 @@ func TestGetArtistByIDRoute(t *testing.T) { assert.Equal(t, "Pitbull", artist.Name) assert.Equal(t, "0TnOYISbd1XYRBk9myaseg", artist.SpotifyID) } + +func TestGetArtistByIDBadParams(t *testing.T) { + db := setupTestDatabase("testgetartistbyidbadparams") + env := &Env{db: db} + router := setupRouter(env, spotifyClientID, spotifyClientSecret) + + w := httptest.NewRecorder() + req, _ := http.NewRequest("GET", "/artists/ExampleIDthatiscertainlyinvalid", nil) + router.ServeHTTP(w, req) + + assert.Equal(t, 400, w.Code) + assert.Equal(t, `{"Error":"Bad request"}`, w.Body.String()) +} + +func TestGetArtistByName(t *testing.T) { + db := setupTestDatabase("testgetartistbyname") + env := &Env{db: db} + router := setupRouter(env, spotifyClientID, spotifyClientSecret) + + // We should request Kanye's data so it is populated in the DB for this test + prew := httptest.NewRecorder() + preReq, _ := http.NewRequest("GET", "/artists/5K4W6rqBFWDnAN6FQUkS6x", nil) + router.ServeHTTP(prew, preReq) + + w := httptest.NewRecorder() + req, _ := http.NewRequest("GET", "/artists?name=kanye", nil) + router.ServeHTTP(w, req) + + assert.Equal(t, 200, w.Code) + fmt.Println(w.Body.String()) + + // Define some custom types to make response parsing easier + type SearchResponse struct { + SpotifyID string + Name string + } + type ResponseList []SearchResponse + + var resp ResponseList + err := json.NewDecoder(w.Body).Decode(&resp) + if err != nil { + assert.Fail(t, fmt.Sprintf("Could not validate and parse JSON response: %s", err.Error())) + } + + // only response should be kanye + assert.Equal(t, "Kanye West", resp[0].Name) + assert.Equal(t, "5K4W6rqBFWDnAN6FQUkS6x", resp[0].SpotifyID) +} + +func TestGetArtistByNameEmptyParams(t *testing.T) { + db := setupTestDatabase("testgetartistbynameemptyparams") + env := &Env{db: db} + router := setupRouter(env, spotifyClientID, spotifyClientSecret) + + w := httptest.NewRecorder() + req, _ := http.NewRequest("GET", "/artists?name=", nil) + router.ServeHTTP(w, req) + + assert.Equal(t, 200, w.Code) + assert.Equal(t, `[]`, w.Body.String()) +} + +func TestGetArtistByNameMissingParams(t *testing.T) { + db := setupTestDatabase("testgetartistbynamemissingparams") + env := &Env{db: db} + router := setupRouter(env, spotifyClientID, spotifyClientSecret) + + w := httptest.NewRecorder() + req, _ := http.NewRequest("GET", "/artists", nil) + router.ServeHTTP(w, req) + + assert.Equal(t, 400, w.Code) + assert.Equal(t, `{"Error":"name parameter was not supplied"}`, w.Body.String()) +}