package main import ( "encoding/json" "fmt" "github.com/stretchr/testify/assert" "net/http" "net/http/httptest" "testing" ) 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.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) }