func TestHasVideo(t *testing.T) { var invalidType *mockInvalidType lib := &Library{} expected := ErrInvalidIndexVideoType _, got := lib.HasVideo(invalidType) if got != expected { t.Errorf("expected error %q, got %q", expected, got) } }
func TestAddEpisode(t *testing.T) { lib, err := newMockLibrary() defer lib.cleanup() if err != nil { t.Fatalf("expected no error, got %q", err) } // Get a mock show show, err := lib.mockShow() if err != nil { t.Fatalf("expected no error, got %q", err) } // The mock detailer fakes episodes, let's remove them show.Episodes = nil // Get a mock episode episode, err := lib.mockEpisode("episodeTest.mp4") if err != nil { t.Fatalf("expected no error, got %q", err) } // Set the show of the episode episode.Show = show oldEpisodePath := episode.Path // Add the episode to the library if err := lib.Add(episode, mockLogEntry); err != nil { t.Fatalf("failed to add the episode: %q", err) } // Check the new file location expectedPath := filepath.Join(lib.tmpDir, "shows/Show tt12345/Season 1/episodeTest.mp4") if episode.Path != expectedPath { t.Errorf("file location, expected %q got %q", expectedPath, episode.Path) } // Check that the old path is a symlink that point to the episode's new path gotNewPath, err := filepath.EvalSymlinks(oldEpisodePath) if err != nil { t.Fatalf("expected no error, got %q", err) } if gotNewPath != episode.Path { t.Errorf("invalid symlink, expected %q got %q", episode.Path, gotNewPath) } // Get a new mock episode episode, err = lib.mockEpisode("episodeTest.mp4") if err != nil { t.Fatalf("expected no error, got %q", err) } episode.Show = show // Add the same episode again, this should replace the old one if err := lib.Add(episode, mockLogEntry); err != nil { t.Fatalf("failed to add the episode again: %q", err) } // Test the show content testShow(t, episode, lib) // Test the season testSeason(t, episode, lib) episodeFromLib, err := lib.GetEpisode(episode.ShowImdbID, episode.Season, episode.Episode) if err != nil { t.Fatalf("expected no error, got %q", err) } // The show is not retrieved by get the GetEpisode method, let's add it // manually episodeFromLib.Show = episode.Show if !reflect.DeepEqual(episode, episodeFromLib) { t.Errorf("invalid episode from lib, expected %+v got %+v", episode, episodeFromLib) } // Expected indexed season expectedIndexedSeason := index.IndexedSeason{ Path: filepath.Join(lib.tmpDir, "shows/Show tt12345/Season 1"), Episodes: map[int]string{ 1: filepath.Join(lib.tmpDir, "shows/Show tt12345/Season 1/episodeTest.mp4"), }, } // Expected indexed show expectedIndexedShow := index.IndexedShow{ Path: filepath.Join(lib.tmpDir, "shows/Show tt12345"), Seasons: map[int]index.IndexedSeason{ 1: expectedIndexedSeason, }, } // Expected IDs expectedIDs := map[string]index.IndexedShow{ "tt12345": expectedIndexedShow, } // Ensure the index if valid gotIDs := lib.ShowIDs() if !reflect.DeepEqual(expectedIDs, gotIDs) { t.Errorf("invalid show ids, expected %+v got %+v", expectedIDs, gotIDs) } // Ensure the library has the show episode hasEpisode, err := lib.HasVideo(episode) if err != nil { t.Fatalf("expected no error, got %q", err) } if !hasEpisode { t.Fatal("the episode should be in the index") } // Get the indexed show gotIndexedShow, err := lib.GetIndexedShow(episode.ShowImdbID) if err != nil { t.Fatalf("expected no error, got %q", err) } if !reflect.DeepEqual(expectedIndexedShow, gotIndexedShow) { t.Errorf("invalid show ids, expected %+v got %+v", expectedIndexedShow, gotIndexedShow) } // Get the indexed season gotIndexedSeason, err := lib.GetIndexedSeason(episode.ShowImdbID, episode.Season) if err != nil { t.Fatalf("expected no error, got %q", err) } if !reflect.DeepEqual(expectedIndexedSeason, gotIndexedSeason) { t.Errorf("invalid show ids, expected %+v got %+v", expectedIndexedSeason, gotIndexedSeason) } // Rebuild the index, the episode should be found and added to the index if err := lib.RebuildIndex(mockLogEntry); err != nil { t.Fatalf("expected no error, got %q", err) } // Ensure the index is still valid after a rebuild gotIDs = lib.ShowIDs() if !reflect.DeepEqual(expectedIDs, gotIDs) { t.Errorf("invalid show ids, expected %+v got %+v", expectedIDs, gotIDs) } }
func TestAddMovie(t *testing.T) { lib, err := newMockLibrary() defer lib.cleanup() if err != nil { t.Fatalf("expected no error, got %q", err) } m, err := lib.mockMovie("movieTest.mp4") if err != nil { t.Fatalf("expected no error, got %q", err) } oldMoviePath := m.Path // Add the movie to the library if err := lib.Add(m, mockLogEntry); err != nil { t.Fatalf("failed to add the movie: %q", err) } // Check the new file location expectedPath := filepath.Join(lib.tmpDir, "movies/Movie tt12345 (2000)/movieTest.mp4") if m.Path != expectedPath { t.Errorf("file location, expected %q got %q", expectedPath, m.Path) } // Check that the old path is a symlink that point to the movie's new path gotNewPath, err := filepath.EvalSymlinks(oldMoviePath) if err != nil { t.Fatalf("expected no error, got %q", err) } if gotNewPath != m.Path { t.Errorf("invalid symlink, expected %q got %q", m.Path, gotNewPath) } // Get the same mock movie again m, err = lib.mockMovie("movieTest.mp4") if err != nil { t.Fatalf("expected no error, got %q", err) } // Add the movie again, this should replace the old file if err := lib.Add(m, mockLogEntry); err != nil { t.Fatalf("failed to add the movie again: %q", err) } // Check the content of the downloaded files for _, imgPath := range []string{ m.MovieFanartPath(), m.MovieThumbPath(), } { content, err := ioutil.ReadFile(imgPath) if err != nil { t.Fatalf("failed to add the movie: %q", err) } // The mock content comes from the httptest server if string(content) != "mockContent" { t.Error("invalid image content") } } // Check the movie index expectedIDs := []string{m.ImdbID} gotIDs := lib.MovieIDs() if !reflect.DeepEqual(gotIDs, expectedIDs) { t.Errorf("invalid ids, expected %+v got %+v", expectedIDs, gotIDs) } // Ensure the library has the movie hasMovie, err := lib.HasVideo(m) if err != nil { t.Fatalf("expected no error, got %q", err) } if !hasMovie { t.Fatal("the movie should be in the index") } // Get the movie from the lib movieFromLib, err := lib.GetMovie(m.ImdbID) if err != nil { t.Fatalf("expected no error, got %q", err) } if !reflect.DeepEqual(m, movieFromLib) { t.Errorf("invalid movie from lib, expected %+v got %+v", m, movieFromLib) } // Rebuild the index, the movie should be found and added to the index if err := lib.RebuildIndex(mockLogEntry); err != nil { t.Fatalf("expected no error, got %q", err) } // Check the movie index again gotIDs = lib.MovieIDs() if !reflect.DeepEqual(gotIDs, expectedIDs) { t.Errorf("invalid ids, expected %+v got %+v", expectedIDs, gotIDs) } }