gomusic/routes_test.go

55 lines
1.6 KiB
Go

package main
import (
"net/http"
"encoding/json"
"net/http/httptest"
"github.com/stretchr/testify/assert"
"testing"
"fmt"
)
func TestAliveRoute(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 TestGetArtistByIDRoute(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 spotifyResp SpotifyResponse
err := json.NewDecoder(w.Body).Decode(&spotifyResp)
if err != nil {
assert.Fail(t, fmt.Sprintf("Could not validate and parse JSON response into SpotifyResponse struct: %s", err.Error()))
}
assert.Equal(t, "0TnOYISbd1XYRBk9myaseg", spotifyResp.ID)
assert.Equal(t, "Pitbull", spotifyResp.Name)
var artist ArtistProfile
dbResult := env.db.First(&artist)
if dbResult.Error != nil {
assert.Fail(t, fmt.Sprintf("Failed to retrieve new info from test database: %s", err.Error()))
}
assert.Equal(t, "Pitbull", artist.Name)
assert.Equal(t, "0TnOYISbd1XYRBk9myaseg", artist.SpotifyID)
}