func TestDeleteEpisode(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 // Add the episode to the library if err := lib.Add(episode, mockLogEntry); err != nil { t.Fatalf("failed to add the episode: %q", err) } // Add the episode to the library if err := lib.Delete(episode, mockLogEntry); err != nil { t.Fatalf("failed to remove the episode: %q", err) } // Ensure the index if valid gotIDs := lib.ShowIDs() expectedIDs := map[string]index.IndexedShow{} if !reflect.DeepEqual(expectedIDs, gotIDs) { t.Errorf("invalid show ids, expected %+v got %+v", expectedIDs, gotIDs) } // Rebuild 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 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) } }