2013-03-07 04:34:56 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using FizzWare.NBuilder;
|
|
|
|
|
using Moq;
|
|
|
|
|
using NUnit.Framework;
|
|
|
|
|
using NzbDrone.Common;
|
|
|
|
|
using NzbDrone.Core.MediaFiles;
|
2013-05-13 00:36:23 +00:00
|
|
|
|
using NzbDrone.Core.MediaFiles.Commands;
|
2013-03-07 04:34:56 +00:00
|
|
|
|
using NzbDrone.Core.Test.Framework;
|
|
|
|
|
using NzbDrone.Core.Tv;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
|
|
|
|
|
namespace NzbDrone.Core.Test.MediaFileTests
|
|
|
|
|
{
|
2013-05-13 00:36:23 +00:00
|
|
|
|
public class MediaFileTableCleanupServiceFixture : CoreTest<MediaFileTableCleanupService>
|
2013-03-07 04:34:56 +00:00
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
private void GiveEpisodeFiles(IEnumerable<EpisodeFile> episodeFiles)
|
|
|
|
|
{
|
|
|
|
|
Mocker.GetMock<IMediaFileService>()
|
|
|
|
|
.Setup(c => c.GetFilesBySeries(It.IsAny<int>()))
|
|
|
|
|
.Returns(episodeFiles.ToList());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private const string DeletedPath = "ANY FILE WITH THIS PATH IS CONSIDERED DELETED!";
|
|
|
|
|
|
|
|
|
|
[SetUp]
|
|
|
|
|
public void SetUp()
|
|
|
|
|
{
|
2013-05-10 23:53:50 +00:00
|
|
|
|
Mocker.GetMock<IDiskProvider>()
|
2013-03-07 04:34:56 +00:00
|
|
|
|
.Setup(e => e.FileExists(It.Is<String>(c => c != DeletedPath)))
|
|
|
|
|
.Returns(true);
|
2013-05-13 00:36:23 +00:00
|
|
|
|
|
|
|
|
|
Mocker.GetMock<IEpisodeService>()
|
|
|
|
|
.Setup(c => c.GetEpisodesByFileId(It.IsAny<int>()))
|
|
|
|
|
.Returns(new List<Episode> { new Episode() });
|
2013-03-07 04:34:56 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
|
public void should_skip_files_that_exist_in_disk()
|
|
|
|
|
{
|
|
|
|
|
var episodeFiles = Builder<EpisodeFile>.CreateListOfSize(10)
|
|
|
|
|
.Build();
|
|
|
|
|
|
|
|
|
|
GiveEpisodeFiles(episodeFiles);
|
|
|
|
|
|
2013-05-13 00:36:23 +00:00
|
|
|
|
Subject.Execute(new CleanMediaFileDb(0));
|
2013-03-07 04:34:56 +00:00
|
|
|
|
|
|
|
|
|
Mocker.GetMock<IEpisodeService>().Verify(c => c.UpdateEpisode(It.IsAny<Episode>()), Times.Never());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
|
public void should_delete_none_existing_files()
|
|
|
|
|
{
|
|
|
|
|
var episodeFiles = Builder<EpisodeFile>.CreateListOfSize(10)
|
|
|
|
|
.Random(2)
|
|
|
|
|
.With(c => c.Path = DeletedPath)
|
|
|
|
|
.Build();
|
|
|
|
|
|
|
|
|
|
GiveEpisodeFiles(episodeFiles);
|
|
|
|
|
|
2013-05-13 00:36:23 +00:00
|
|
|
|
Subject.Execute(new CleanMediaFileDb(0));
|
2013-03-07 04:34:56 +00:00
|
|
|
|
|
|
|
|
|
Mocker.GetMock<IMediaFileService>().Verify(c => c.Delete(It.Is<EpisodeFile>(e => e.Path == DeletedPath)), Times.Exactly(2));
|
|
|
|
|
|
|
|
|
|
}
|
2013-05-13 00:36:23 +00:00
|
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
|
public void should_delete_files_that_dont_belong_to_any_episodes()
|
|
|
|
|
{
|
|
|
|
|
var episodeFiles = Builder<EpisodeFile>.CreateListOfSize(10)
|
|
|
|
|
.Random(10)
|
|
|
|
|
.With(c => c.Path = "ExistingPath")
|
|
|
|
|
.Build();
|
|
|
|
|
|
|
|
|
|
GiveEpisodeFiles(episodeFiles);
|
|
|
|
|
GivenFilesAreNotAttachedToEpisode();
|
|
|
|
|
|
|
|
|
|
Subject.Execute(new CleanMediaFileDb(0));
|
|
|
|
|
|
|
|
|
|
Mocker.GetMock<IMediaFileService>().Verify(c => c.Delete(It.IsAny<EpisodeFile>()), Times.Exactly(10));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void GivenFilesAreNotAttachedToEpisode()
|
|
|
|
|
{
|
|
|
|
|
Mocker.GetMock<IEpisodeService>()
|
|
|
|
|
.Setup(c => c.GetEpisodesByFileId(It.IsAny<int>()))
|
|
|
|
|
.Returns(new List<Episode>());
|
|
|
|
|
}
|
2013-03-07 04:34:56 +00:00
|
|
|
|
}
|
|
|
|
|
}
|