Drop folder is now wired. needs more tests.

pull/4/head
kay.one 14 years ago
parent 5c45515b00
commit 1ba74e0eea

@ -373,6 +373,11 @@ namespace NzbDrone.Core.Test
.Setup(c => c.GetEpisodeBySeries(It.IsAny<long>())) .Setup(c => c.GetEpisodeBySeries(It.IsAny<long>()))
.Returns(new List<Episode> { new Episode() }); .Returns(new List<Episode> { new Episode() });
mocker.GetMock<MediaFileProvider>()
.Setup(c => c.GetSeriesFiles(It.IsAny<int>()))
.Returns(new List<EpisodeFile>());
mocker.Resolve<DiskScanProvider>().Scan(new Series()); mocker.Resolve<DiskScanProvider>().Scan(new Series());
mocker.VerifyAllMocks(); mocker.VerifyAllMocks();

@ -8,7 +8,9 @@ using FluentAssertions;
using Moq; using Moq;
using NUnit.Framework; using NUnit.Framework;
using NzbDrone.Core.Providers; using NzbDrone.Core.Providers;
using NzbDrone.Core.Providers.Core;
using NzbDrone.Core.Repository; using NzbDrone.Core.Repository;
using NzbDrone.Core.Repository.Quality;
using NzbDrone.Core.Test.Framework; using NzbDrone.Core.Test.Framework;
using PetaPoco; using PetaPoco;
using TvdbLib.Data; using TvdbLib.Data;
@ -81,7 +83,7 @@ namespace NzbDrone.Core.Test
var db = MockLib.GetEmptyDatabase(); var db = MockLib.GetEmptyDatabase();
mocker.SetConstant(db); mocker.SetConstant(db);
//Act //Act
var episode = mocker.Resolve<EpisodeProvider>().GetEpisode(1, 1, 1); var episode = mocker.Resolve<EpisodeProvider>().GetEpisode(1, 1, 1);
@ -101,7 +103,7 @@ namespace NzbDrone.Core.Test
var fakeFile = Builder<EpisodeFile>.CreateNew().With(f => f.EpisodeFileId).Build(); var fakeFile = Builder<EpisodeFile>.CreateNew().With(f => f.EpisodeFileId).Build();
var fakeEpisodes = Builder<Episode>.CreateListOfSize(5) var fakeEpisodes = Builder<Episode>.CreateListOfSize(5)
.WhereAll().Have(e => e.SeriesId = 1).WhereTheFirst(1).Have(e => e.EpisodeFileId = 1).Have(e => e.EpisodeFile = fakeFile).Build(); .WhereAll().Have(e => e.SeriesId = 1).WhereTheFirst(1).Have(e => e.EpisodeFileId = 1).Have(e => e.EpisodeFile = fakeFile).Build();
db.InsertMany(fakeEpisodes); db.InsertMany(fakeEpisodes);
db.Insert(fakeFile); db.Insert(fakeFile);
@ -562,13 +564,21 @@ namespace NzbDrone.Core.Test
public void Add_daily_show_episodes() public void Add_daily_show_episodes()
{ {
var mocker = new AutoMoqer(); var mocker = new AutoMoqer();
mocker.SetConstant(MockLib.GetEmptyDatabase()); var db = MockLib.GetEmptyDatabase();
mocker.SetConstant(db);
mocker.Resolve<TvDbProvider>(); mocker.Resolve<TvDbProvider>();
mocker.GetMock<ConfigProvider>()
.Setup(e => e.DefaultQualityProfile).Returns(1);
db.Insert(Builder<QualityProfile>.CreateNew().Build());
const int tvDbSeriesId = 71256; const int tvDbSeriesId = 71256;
//act //act
var seriesProvider = mocker.Resolve<SeriesProvider>(); var seriesProvider = mocker.Resolve<SeriesProvider>();
seriesProvider.AddSeries("c:\\test\\", tvDbSeriesId, 0); seriesProvider.AddSeries("c:\\test\\", tvDbSeriesId, 1);
var episodeProvider = mocker.Resolve<EpisodeProvider>(); var episodeProvider = mocker.Resolve<EpisodeProvider>();
episodeProvider.RefreshEpisodeInfo(seriesProvider.GetSeries(tvDbSeriesId)); episodeProvider.RefreshEpisodeInfo(seriesProvider.GetSeries(tvDbSeriesId));

@ -25,7 +25,7 @@ namespace NzbDrone.Core.Test
// ReSharper disable InconsistentNaming // ReSharper disable InconsistentNaming
public class MediaFileProviderTests : TestBase public class MediaFileProviderTests : TestBase
{ {
[Test] [Test]
@ -59,10 +59,17 @@ namespace NzbDrone.Core.Test
public void Scan_series_should_skip_series_with_no_episodes() public void Scan_series_should_skip_series_with_no_episodes()
{ {
var mocker = new AutoMoqer(MockBehavior.Strict); var mocker = new AutoMoqer(MockBehavior.Strict);
mocker.GetMock<EpisodeProvider>() mocker.GetMock<EpisodeProvider>()
.Setup(c => c.GetEpisodeBySeries(12)) .Setup(c => c.GetEpisodeBySeries(12))
.Returns(new List<Episode>()); .Returns(new List<Episode>());
mocker.GetMock<MediaFileProvider>()
.Setup(e => e.RepairLinks()).Returns(0);
mocker.GetMock<MediaFileProvider>()
.Setup(e => e.DeleteOrphaned()).Returns(0);
var series = Builder<Series>.CreateNew() var series = Builder<Series>.CreateNew()
.With(s => s.SeriesId = 12).Build(); .With(s => s.SeriesId = 12).Build();
@ -97,12 +104,14 @@ namespace NzbDrone.Core.Test
database.InsertMany(episodes); database.InsertMany(episodes);
//Act //Act
mocker.Resolve<MediaFileProvider>().CleanEpisodesWithNonExistantFiles(); var removedLinks = mocker.Resolve<MediaFileProvider>().RepairLinks();
var result = database.Fetch<Episode>(); var result = database.Fetch<Episode>();
//Assert //Assert
result.Should().HaveSameCount(episodes); result.Should().HaveSameCount(episodes);
result.Should().OnlyContain(e => e.EpisodeFileId == 0); result.Should().OnlyContain(e => e.EpisodeFileId == 0);
removedLinks.Should().Be(10);
ExceptionVerification.ExcpectedWarns(1);
} }
[Test] [Test]
@ -119,12 +128,13 @@ namespace NzbDrone.Core.Test
database.InsertMany(episodeFiles); database.InsertMany(episodeFiles);
//Act //Act
mocker.Resolve<MediaFileProvider>().DeleteOrphanedEpisodeFiles(); mocker.Resolve<MediaFileProvider>().DeleteOrphaned();
var result = database.Fetch<EpisodeFile>(); var result = database.Fetch<EpisodeFile>();
//Assert //Assert
result.Should().HaveCount(5); result.Should().HaveCount(5);
result.Should().OnlyContain(e => e.EpisodeFileId > 0); result.Should().OnlyContain(e => e.EpisodeFileId > 0);
ExceptionVerification.ExcpectedWarns(1);
} }
} }
} }

@ -53,12 +53,18 @@ namespace NzbDrone.Core.Providers
/// <param name="path">Path to scan</param> /// <param name="path">Path to scan</param>
public virtual List<EpisodeFile> Scan(Series series, string path) public virtual List<EpisodeFile> Scan(Series series, string path)
{ {
_mediaFileProvider.DeleteOrphaned();
_mediaFileProvider.RepairLinks();
if (_episodeProvider.GetEpisodeBySeries(series.SeriesId).Count == 0) if (_episodeProvider.GetEpisodeBySeries(series.SeriesId).Count == 0)
{ {
Logger.Debug("Series {0} has no episodes. skipping", series.Title); Logger.Debug("Series {0} has no episodes. skipping", series.Title);
return new List<EpisodeFile>(); return new List<EpisodeFile>();
} }
var seriesFile = _mediaFileProvider.GetSeriesFiles(series.SeriesId);
CleanUp(seriesFile);
var mediaFileList = GetVideoFiles(path); var mediaFileList = GetVideoFiles(path);
var importedFiles = new List<EpisodeFile>(); var importedFiles = new List<EpisodeFile>();
@ -199,11 +205,8 @@ namespace NzbDrone.Core.Providers
/// Removes files that no longer exist on disk from the database /// Removes files that no longer exist on disk from the database
/// </summary> /// </summary>
/// <param name = "files">list of files to verify</param> /// <param name = "files">list of files to verify</param>
public virtual void CleanUp(List<EpisodeFile> files) public virtual void CleanUp(IList<EpisodeFile> files)
{ {
_mediaFileProvider.CleanEpisodesWithNonExistantFiles();
_mediaFileProvider.DeleteOrphanedEpisodeFiles();
foreach (var episodeFile in files) foreach (var episodeFile in files)
{ {
if (!_diskProvider.FileExists(episodeFile.Path)) if (!_diskProvider.FileExists(episodeFile.Path))

@ -74,24 +74,41 @@ namespace NzbDrone.Core.Providers
return new FileInfo(path); 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 WHERE EpisodeFileId IN
(SELECT Episodes.EpisodeFileId FROM Episodes (SELECT Episodes.EpisodeFileId FROM Episodes
LEFT OUTER JOIN EpisodeFiles LEFT OUTER JOIN EpisodeFiles
ON Episodes.EpisodeFileId = EpisodeFiles.EpisodeFileId ON Episodes.EpisodeFileId = EpisodeFiles.EpisodeFileId
WHERE Episodes.EpisodeFileId > 0 AND EpisodeFiles.EpisodeFileId IS null)"); 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 WHERE EpisodeFileId IN
(SELECT EpisodeFiles.EpisodeFileId FROM EpisodeFiles (SELECT EpisodeFiles.EpisodeFileId FROM EpisodeFiles
LEFT OUTER JOIN Episodes LEFT OUTER JOIN Episodes
ON EpisodeFiles.EpisodeFileId = Episodes.EpisodeFileId ON EpisodeFiles.EpisodeFileId = Episodes.EpisodeFileId
WHERE Episodes.EpisodeFileId IS null)"); WHERE Episodes.EpisodeFileId IS null)");
if (updated > 0)
{
Logger.Warn("Removed {0} orphaned files.", updated);
}
return updated;
} }
public virtual string GetNewFilename(IList<Episode> episodes, string seriesTitle, QualityTypes quality) public virtual string GetNewFilename(IList<Episode> episodes, string seriesTitle, QualityTypes quality)

Loading…
Cancel
Save