func TestGetSeries_ReturnsCorrectSeries(t *testing.T) { s := createTestServer() defer s.Close() defer os.Remove(TEST_DATABASE) var count int = 4 populateSampleSeries(t, count) s1, err := database.GetSeries(3) if err != nil { t.Errorf("unexpected error: %v", err) } r, err := http.Get(fmt.Sprintf("%s/api/series/%d", s.URL, s1.Id)) if err != nil { t.Errorf("unexpected error: %v", err) } var response Series err = json.NewDecoder(r.Body).Decode(&response) if err != nil { t.Errorf("unexpected error: %v", err) } if s1.Id != response.Id { t.Errorf("expected id %d, got %d", s1.Id, response.Id) } if s1.Title != response.Title { t.Errorf("expected title %q, got %q", s1.Title, response.Title) } }
func TestDeleteSeries(t *testing.T) { s := createTestServer() defer s.Close() defer os.Remove(TEST_DATABASE) var count int = 4 populateSampleSeries(t, count) r, err := Delete(fmt.Sprintf("%s/api/series/%d", s.URL, 1)) if err != nil { t.Errorf("unexpected error: %v", err) } if r.StatusCode != http.StatusOK { t.Errorf("expected %d status code, got %d", http.StatusOK, r.StatusCode) } var rm Series err = json.NewDecoder(r.Body).Decode(&rm) if err != nil { t.Errorf("unexpected error: %v", err) } if 1 != rm.Id { t.Errorf("expected returned item id %d, got id %d", 1, rm.Id) } // Double check this doesn't exist in the database if series, err := database.GetSeries(1); err == nil { t.Errorf("expected error fetching deleted series, got %v", series) } }
func FetchMetadataForEpisode(e *Episode) error { log.Printf("Fetching metadata for episode: %q", e.Title) if e.TvdbSeriesId == 0 { s, err := database.GetSeries(e.SeriesId) if err != nil { return err } e.TvdbSeriesId = s.SeriesId log.Printf("Adding series id to episode: %d", e.TvdbSeriesId) } b, err := tvdbconn.GetEpisodeBySeasonEp(int(e.TvdbSeriesId), int(e.SeasonNumber), int(e.EpisodeNumber), "en") if err != nil { log.Printf("Error querying TheTVDB for %s: %v", e.Title, err) return err } singleEpisode, err := tvdb.ParseSingleEpisode(b) if err != nil { log.Printf("Error parsing single episode: %v", err) return err } episode := singleEpisode.Episode e.Title = episode.EpisodeName e.Overview = episode.Overview e.ImdbId = episode.ImdbId e.Director = episode.Director e.Writer = episode.Writer e.SeasonId = int64(episode.SeasonId) e.GuestStars = episode.GuestStars e.AbsoluteNumber = episode.AbsoluteNumber // IMDB info title, err := imdb.NewTitle(http.DefaultClient, e.ImdbId) if err != nil { log.Printf("Failed to get extended info from imdb: %v", err) } else { e.Poster = title.Poster.ContentURL } err = database.SaveEpisode(e) if err != nil { return err } return nil }
// GET /api/series/:id // Returns a single Series identified by :id func SeriesShow(w http.ResponseWriter, r *http.Request) (interface{}, error) { idstr := getPathParam(r, "id") id, err := strconv.ParseInt(idstr, 10, 64) if err != nil { return nil, err } s, err := database.GetSeries(id) if err != nil { if err == sql.ErrNoRows { return nil, notFound{} } else { return nil, err } } return s, nil }
func TestCreateSeries(t *testing.T) { s := createTestServer() defer s.Close() defer os.Remove(TEST_DATABASE) series := createTestSeries() b, err := json.Marshal(series) if err != nil { t.Errorf("Unexpected error: %v", err) } r, err := Put(s.URL+"/api/series", "application/json", strings.NewReader(fmt.Sprintf("%s", b))) if err != nil { t.Errorf("Unexpected error: %v", err) } var response Series err = json.NewDecoder(r.Body).Decode(&response) if err != nil { t.Errorf("Unexpected error: %v", err) } if response.Id == 0 { t.Errorf("expected non-zero series Id, got %v", response.Id) } if response.Title != series.Title { t.Errorf("expected matching titles, got %v", response) } // Check that it is actually in the database _, err = database.GetSeries(response.Id) if err != nil { t.Errorf("Failed to find created series in database: %v", err) } }