From 1ba74e0eeafe80a14ae8d5ab5e81d2811b04fadc Mon Sep 17 00:00:00 2001 From: "kay.one" Date: Mon, 20 Jun 2011 23:34:45 -0700 Subject: [PATCH] Drop folder is now wired. needs more tests. --- NzbDrone.Core.Test/DiskScanProviderTest.cs | 5 ++++ NzbDrone.Core.Test/EpisodeProviderTest.cs | 18 ++++++++++---- NzbDrone.Core.Test/MediaFileProviderTests.cs | 16 ++++++++++--- NzbDrone.Core/Providers/DiskScanProvider.cs | 11 +++++---- NzbDrone.Core/Providers/MediaFileProvider.cs | 25 ++++++++++++++++---- 5 files changed, 60 insertions(+), 15 deletions(-) diff --git a/NzbDrone.Core.Test/DiskScanProviderTest.cs b/NzbDrone.Core.Test/DiskScanProviderTest.cs index 52ae7ce18..ec98b8695 100644 --- a/NzbDrone.Core.Test/DiskScanProviderTest.cs +++ b/NzbDrone.Core.Test/DiskScanProviderTest.cs @@ -373,6 +373,11 @@ public void scan_series_should_update_last_scan_date() .Setup(c => c.GetEpisodeBySeries(It.IsAny())) .Returns(new List { new Episode() }); + + mocker.GetMock() + .Setup(c => c.GetSeriesFiles(It.IsAny())) + .Returns(new List()); + mocker.Resolve().Scan(new Series()); mocker.VerifyAllMocks(); diff --git a/NzbDrone.Core.Test/EpisodeProviderTest.cs b/NzbDrone.Core.Test/EpisodeProviderTest.cs index 9277c8ac7..02c40742c 100644 --- a/NzbDrone.Core.Test/EpisodeProviderTest.cs +++ b/NzbDrone.Core.Test/EpisodeProviderTest.cs @@ -8,7 +8,9 @@ using Moq; using NUnit.Framework; using NzbDrone.Core.Providers; +using NzbDrone.Core.Providers.Core; using NzbDrone.Core.Repository; +using NzbDrone.Core.Repository.Quality; using NzbDrone.Core.Test.Framework; using PetaPoco; using TvdbLib.Data; @@ -81,7 +83,7 @@ public void GetEpisodes_by_season_episode_doesnt_exists() var db = MockLib.GetEmptyDatabase(); mocker.SetConstant(db); - + //Act var episode = mocker.Resolve().GetEpisode(1, 1, 1); @@ -101,7 +103,7 @@ public void GetEpisode_with_EpisodeFile() var fakeFile = Builder.CreateNew().With(f => f.EpisodeFileId).Build(); var fakeEpisodes = Builder.CreateListOfSize(5) .WhereAll().Have(e => e.SeriesId = 1).WhereTheFirst(1).Have(e => e.EpisodeFileId = 1).Have(e => e.EpisodeFile = fakeFile).Build(); - + db.InsertMany(fakeEpisodes); db.Insert(fakeFile); @@ -562,13 +564,21 @@ public void IsSeasonIgnored_should_return_true_if_invalid_series() public void Add_daily_show_episodes() { var mocker = new AutoMoqer(); - mocker.SetConstant(MockLib.GetEmptyDatabase()); + var db = MockLib.GetEmptyDatabase(); + mocker.SetConstant(db); mocker.Resolve(); + + mocker.GetMock() + .Setup(e => e.DefaultQualityProfile).Returns(1); + + db.Insert(Builder.CreateNew().Build()); + + const int tvDbSeriesId = 71256; //act var seriesProvider = mocker.Resolve(); - seriesProvider.AddSeries("c:\\test\\", tvDbSeriesId, 0); + seriesProvider.AddSeries("c:\\test\\", tvDbSeriesId, 1); var episodeProvider = mocker.Resolve(); episodeProvider.RefreshEpisodeInfo(seriesProvider.GetSeries(tvDbSeriesId)); diff --git a/NzbDrone.Core.Test/MediaFileProviderTests.cs b/NzbDrone.Core.Test/MediaFileProviderTests.cs index a02b6c1bc..999359d97 100644 --- a/NzbDrone.Core.Test/MediaFileProviderTests.cs +++ b/NzbDrone.Core.Test/MediaFileProviderTests.cs @@ -25,7 +25,7 @@ namespace NzbDrone.Core.Test // ReSharper disable InconsistentNaming public class MediaFileProviderTests : TestBase { - + [Test] @@ -59,10 +59,17 @@ public void get_series_files() public void Scan_series_should_skip_series_with_no_episodes() { var mocker = new AutoMoqer(MockBehavior.Strict); + mocker.GetMock() .Setup(c => c.GetEpisodeBySeries(12)) .Returns(new List()); + mocker.GetMock() + .Setup(e => e.RepairLinks()).Returns(0); + mocker.GetMock() + .Setup(e => e.DeleteOrphaned()).Returns(0); + + var series = Builder.CreateNew() .With(s => s.SeriesId = 12).Build(); @@ -97,12 +104,14 @@ public void CleanEpisodesWithNonExistantFiles() database.InsertMany(episodes); //Act - mocker.Resolve().CleanEpisodesWithNonExistantFiles(); + var removedLinks = mocker.Resolve().RepairLinks(); var result = database.Fetch(); //Assert result.Should().HaveSameCount(episodes); result.Should().OnlyContain(e => e.EpisodeFileId == 0); + removedLinks.Should().Be(10); + ExceptionVerification.ExcpectedWarns(1); } [Test] @@ -119,12 +128,13 @@ public void DeleteOrphanedEpisodeFiles() database.InsertMany(episodeFiles); //Act - mocker.Resolve().DeleteOrphanedEpisodeFiles(); + mocker.Resolve().DeleteOrphaned(); var result = database.Fetch(); //Assert result.Should().HaveCount(5); result.Should().OnlyContain(e => e.EpisodeFileId > 0); + ExceptionVerification.ExcpectedWarns(1); } } } \ No newline at end of file diff --git a/NzbDrone.Core/Providers/DiskScanProvider.cs b/NzbDrone.Core/Providers/DiskScanProvider.cs index 441348f90..7783c376b 100644 --- a/NzbDrone.Core/Providers/DiskScanProvider.cs +++ b/NzbDrone.Core/Providers/DiskScanProvider.cs @@ -53,12 +53,18 @@ public virtual List Scan(Series series) /// Path to scan public virtual List Scan(Series series, string path) { + _mediaFileProvider.DeleteOrphaned(); + _mediaFileProvider.RepairLinks(); + if (_episodeProvider.GetEpisodeBySeries(series.SeriesId).Count == 0) { Logger.Debug("Series {0} has no episodes. skipping", series.Title); return new List(); } + var seriesFile = _mediaFileProvider.GetSeriesFiles(series.SeriesId); + CleanUp(seriesFile); + var mediaFileList = GetVideoFiles(path); var importedFiles = new List(); @@ -199,11 +205,8 @@ public virtual bool RenameEpisodeFile(EpisodeFile episodeFile) /// Removes files that no longer exist on disk from the database /// /// list of files to verify - public virtual void CleanUp(List files) + public virtual void CleanUp(IList files) { - _mediaFileProvider.CleanEpisodesWithNonExistantFiles(); - _mediaFileProvider.DeleteOrphanedEpisodeFiles(); - foreach (var episodeFile in files) { if (!_diskProvider.FileExists(episodeFile.Path)) diff --git a/NzbDrone.Core/Providers/MediaFileProvider.cs b/NzbDrone.Core/Providers/MediaFileProvider.cs index 9d56da9fe..d9e4271ec 100644 --- a/NzbDrone.Core/Providers/MediaFileProvider.cs +++ b/NzbDrone.Core/Providers/MediaFileProvider.cs @@ -74,24 +74,41 @@ public virtual FileInfo CalculateFilePath(Series series, int seasonNumber, strin return new FileInfo(path); } - public virtual void CleanEpisodesWithNonExistantFiles() + public virtual int RepairLinks() { - _database.Execute(@"UPDATE Episodes SET EpisodeFileId = 0 + Logger.Debug("Verifying Episode>Episode file relationships."); + var updated = _database.Execute(@"UPDATE Episodes SET EpisodeFileId = 0 WHERE EpisodeFileId IN (SELECT Episodes.EpisodeFileId FROM Episodes LEFT OUTER JOIN EpisodeFiles ON Episodes.EpisodeFileId = EpisodeFiles.EpisodeFileId WHERE Episodes.EpisodeFileId > 0 AND EpisodeFiles.EpisodeFileId IS null)"); + + if (updated > 0) + { + Logger.Warn("Removed {0} invalid links to episode files.", updated); + } + + return updated; } - public virtual void DeleteOrphanedEpisodeFiles() + public virtual int DeleteOrphaned() { - _database.Execute(@"DELETE FROM EpisodeFiles + Logger.Debug("Deleting orphaned files."); + + var updated = _database.Execute(@"DELETE FROM EpisodeFiles WHERE EpisodeFileId IN (SELECT EpisodeFiles.EpisodeFileId FROM EpisodeFiles LEFT OUTER JOIN Episodes ON EpisodeFiles.EpisodeFileId = Episodes.EpisodeFileId WHERE Episodes.EpisodeFileId IS null)"); + + if (updated > 0) + { + Logger.Warn("Removed {0} orphaned files.", updated); + } + + return updated; } public virtual string GetNewFilename(IList episodes, string seriesTitle, QualityTypes quality)