Drop folder is now wired. needs more tests.

pull/3113/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>()))
.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.VerifyAllMocks();

@ -8,7 +8,9 @@ using FluentAssertions;
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 @@ namespace NzbDrone.Core.Test
var db = MockLib.GetEmptyDatabase();
mocker.SetConstant(db);
//Act
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 fakeEpisodes = Builder<Episode>.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 @@ namespace NzbDrone.Core.Test
public void Add_daily_show_episodes()
{
var mocker = new AutoMoqer();
mocker.SetConstant(MockLib.GetEmptyDatabase());
var db = MockLib.GetEmptyDatabase();
mocker.SetConstant(db);
mocker.Resolve<TvDbProvider>();
mocker.GetMock<ConfigProvider>()
.Setup(e => e.DefaultQualityProfile).Returns(1);
db.Insert(Builder<QualityProfile>.CreateNew().Build());
const int tvDbSeriesId = 71256;
//act
var seriesProvider = mocker.Resolve<SeriesProvider>();
seriesProvider.AddSeries("c:\\test\\", tvDbSeriesId, 0);
seriesProvider.AddSeries("c:\\test\\", tvDbSeriesId, 1);
var episodeProvider = mocker.Resolve<EpisodeProvider>();
episodeProvider.RefreshEpisodeInfo(seriesProvider.GetSeries(tvDbSeriesId));

@ -25,7 +25,7 @@ namespace NzbDrone.Core.Test
// ReSharper disable InconsistentNaming
public class MediaFileProviderTests : TestBase
{
[Test]
@ -59,10 +59,17 @@ namespace NzbDrone.Core.Test
public void Scan_series_should_skip_series_with_no_episodes()
{
var mocker = new AutoMoqer(MockBehavior.Strict);
mocker.GetMock<EpisodeProvider>()
.Setup(c => c.GetEpisodeBySeries(12))
.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()
.With(s => s.SeriesId = 12).Build();
@ -97,12 +104,14 @@ namespace NzbDrone.Core.Test
database.InsertMany(episodes);
//Act
mocker.Resolve<MediaFileProvider>().CleanEpisodesWithNonExistantFiles();
var removedLinks = mocker.Resolve<MediaFileProvider>().RepairLinks();
var result = database.Fetch<Episode>();
//Assert
result.Should().HaveSameCount(episodes);
result.Should().OnlyContain(e => e.EpisodeFileId == 0);
removedLinks.Should().Be(10);
ExceptionVerification.ExcpectedWarns(1);
}
[Test]
@ -119,12 +128,13 @@ namespace NzbDrone.Core.Test
database.InsertMany(episodeFiles);
//Act
mocker.Resolve<MediaFileProvider>().DeleteOrphanedEpisodeFiles();
mocker.Resolve<MediaFileProvider>().DeleteOrphaned();
var result = database.Fetch<EpisodeFile>();
//Assert
result.Should().HaveCount(5);
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>
public virtual List<EpisodeFile> 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<EpisodeFile>();
}
var seriesFile = _mediaFileProvider.GetSeriesFiles(series.SeriesId);
CleanUp(seriesFile);
var mediaFileList = GetVideoFiles(path);
var importedFiles = new List<EpisodeFile>();
@ -199,11 +205,8 @@ namespace NzbDrone.Core.Providers
/// Removes files that no longer exist on disk from the database
/// </summary>
/// <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)
{
if (!_diskProvider.FileExists(episodeFile.Path))

@ -74,24 +74,41 @@ namespace NzbDrone.Core.Providers
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<Episode> episodes, string seriesTitle, QualityTypes quality)

Loading…
Cancel
Save