gomusic/routes_test.go
2025-02-14 07:47:48 +10:00

129 lines
3.8 KiB
Go

package main
import (
"encoding/json"
"fmt"
"github.com/stretchr/testify/assert"
"net/http"
"net/http/httptest"
"testing"
)
func TestAlive(t *testing.T) {
db := setupTestDatabase("testalive")
env := &Env{db: db}
router := setupRouter(env, spotifyClientID, spotifyClientSecret)
w := httptest.NewRecorder()
req, _ := http.NewRequest("GET", "/alive", nil)
router.ServeHTTP(w, req)
assert.Equal(t, 200, w.Code)
assert.Equal(t, "yes!", w.Body.String())
}
func TestGetArtistByID(t *testing.T) {
db := setupTestDatabase("testgetartistbyid")
env := &Env{db: db}
router := setupRouter(env, spotifyClientID, spotifyClientSecret)
w := httptest.NewRecorder()
req, _ := http.NewRequest("GET", "/artists/0TnOYISbd1XYRBk9myaseg", nil)
router.ServeHTTP(w, req)
assert.Equal(t, 200, w.Code)
// Dynamic data is hard to test so here we validate JSON response
// And we check known fields which are not likely to change
// We then check that the DB was updated correctly just to be sure
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 ArtistProfile struct: %s", err.Error()))
}
assert.Equal(t, "0TnOYISbd1XYRBk9myaseg", resp.SpotifyID)
assert.Equal(t, "Pitbull", resp.Name)
var artist ArtistProfile
dbResult := env.db.Take(&artist)
if dbResult.Error != nil {
assert.Fail(t, "Failed to retrieve new info from test database")
}
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())
}