2010-10-21 03:49:23 +02:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
2010-10-24 09:46:58 +02:00
|
|
|
|
using System.Linq.Expressions;
|
2010-10-21 03:49:23 +02:00
|
|
|
|
using System.Text;
|
2010-10-24 09:46:58 +02:00
|
|
|
|
using FizzWare.NBuilder;
|
2010-10-21 03:49:23 +02:00
|
|
|
|
using Gallio.Framework;
|
|
|
|
|
using MbUnit.Framework;
|
|
|
|
|
using MbUnit.Framework.ContractVerifiers;
|
|
|
|
|
using Moq;
|
|
|
|
|
using Ninject;
|
|
|
|
|
using Ninject.Moq;
|
2010-10-30 04:46:32 +02:00
|
|
|
|
using NzbDrone.Core.Model;
|
2010-10-21 03:49:23 +02:00
|
|
|
|
using NzbDrone.Core.Providers;
|
|
|
|
|
using NzbDrone.Core.Repository;
|
|
|
|
|
using NzbDrone.Core.Repository.Quality;
|
|
|
|
|
using SubSonic.Repository;
|
|
|
|
|
|
|
|
|
|
namespace NzbDrone.Core.Test
|
|
|
|
|
{
|
|
|
|
|
[TestFixture]
|
|
|
|
|
// ReSharper disable InconsistentNaming
|
|
|
|
|
public class MediaFileProviderTests
|
|
|
|
|
{
|
2010-10-24 09:46:58 +02:00
|
|
|
|
|
2010-10-21 03:49:23 +02:00
|
|
|
|
[Test]
|
2010-10-24 09:46:58 +02:00
|
|
|
|
[Description("Verifies that a new file imported properly")]
|
|
|
|
|
public void import_new_file()
|
2010-10-21 03:49:23 +02:00
|
|
|
|
{
|
|
|
|
|
//Arrange
|
2010-10-24 09:46:58 +02:00
|
|
|
|
/////////////////////////////////////////
|
|
|
|
|
|
|
|
|
|
//Constants
|
2010-10-30 04:46:32 +02:00
|
|
|
|
const string fileName = @"WEEDS.S03E01-06.DUAL.BDRip.XviD.AC3.-HELLYWOOD.avi";
|
2010-10-24 09:46:58 +02:00
|
|
|
|
const int seasonNumber = 3;
|
|
|
|
|
const int episodeNumner = 01;
|
|
|
|
|
const int size = 12345;
|
|
|
|
|
|
|
|
|
|
//Fakes
|
|
|
|
|
var fakeSeries = Builder<Series>.CreateNew().Build();
|
|
|
|
|
var fakeEpisode = Builder<Episode>.CreateNew().With(c => c.SeriesId = fakeSeries.SeriesId).Build();
|
|
|
|
|
|
|
|
|
|
//Mocks
|
2010-10-21 03:49:23 +02:00
|
|
|
|
var repository = new Mock<IRepository>();
|
2010-10-24 09:46:58 +02:00
|
|
|
|
repository.Setup(r => r.Exists<EpisodeFile>(It.IsAny<Expression<Func<EpisodeFile, Boolean>>>())).Returns(false).Verifiable();
|
2011-03-30 08:18:35 +02:00
|
|
|
|
repository.Setup(r => r.Add(It.IsAny<EpisodeFile>())).Returns(0).Verifiable();
|
2010-10-24 09:46:58 +02:00
|
|
|
|
|
|
|
|
|
var episodeProvider = new Mock<IEpisodeProvider>();
|
|
|
|
|
episodeProvider.Setup(e => e.GetEpisode(fakeSeries.SeriesId, seasonNumber, episodeNumner)).Returns(fakeEpisode).Verifiable();
|
2010-10-21 03:49:23 +02:00
|
|
|
|
|
2010-10-24 09:46:58 +02:00
|
|
|
|
var diskProvider = new Mock<IDiskProvider>();
|
|
|
|
|
diskProvider.Setup(e => e.GetSize(fileName)).Returns(12345).Verifiable();
|
2010-10-21 03:49:23 +02:00
|
|
|
|
|
|
|
|
|
var kernel = new MockingKernel();
|
|
|
|
|
kernel.Bind<IRepository>().ToConstant(repository.Object);
|
|
|
|
|
|
2010-10-24 09:46:58 +02:00
|
|
|
|
kernel.Bind<IEpisodeProvider>().ToConstant(episodeProvider.Object);
|
|
|
|
|
kernel.Bind<IDiskProvider>().ToConstant(diskProvider.Object);
|
|
|
|
|
kernel.Bind<IMediaFileProvider>().To<MediaFileProvider>();
|
2010-10-21 03:49:23 +02:00
|
|
|
|
|
|
|
|
|
//Act
|
2010-10-24 09:46:58 +02:00
|
|
|
|
var result = kernel.Get<IMediaFileProvider>().ImportFile(fakeSeries, fileName);
|
2010-10-21 03:49:23 +02:00
|
|
|
|
|
|
|
|
|
//Assert
|
2010-10-24 09:46:58 +02:00
|
|
|
|
repository.VerifyAll();
|
|
|
|
|
episodeProvider.VerifyAll();
|
|
|
|
|
diskProvider.VerifyAll();
|
|
|
|
|
Assert.IsNotNull(result);
|
|
|
|
|
repository.Verify(r => r.Add<EpisodeFile>(result), Times.Once());
|
2010-10-21 03:49:23 +02:00
|
|
|
|
|
2011-03-30 08:18:35 +02:00
|
|
|
|
//Currently can't verify this since the list of episodes are loaded
|
|
|
|
|
//Dynamically by SubSonic
|
|
|
|
|
//Assert.AreEqual(fakeEpisode, result.Episodes[0]);
|
|
|
|
|
|
2010-10-24 09:46:58 +02:00
|
|
|
|
Assert.AreEqual(fakeEpisode.SeriesId, result.SeriesId);
|
2011-03-30 08:18:35 +02:00
|
|
|
|
Assert.AreEqual(QualityTypes.BDRip, result.Quality);
|
2010-10-24 09:46:58 +02:00
|
|
|
|
Assert.AreEqual(Parser.NormalizePath(fileName), result.Path);
|
|
|
|
|
Assert.AreEqual(size, result.Size);
|
|
|
|
|
Assert.AreEqual(false, result.Proper);
|
|
|
|
|
Assert.AreNotEqual(new DateTime(), result.DateAdded);
|
2010-10-21 03:49:23 +02:00
|
|
|
|
}
|
|
|
|
|
|
2010-10-24 09:46:58 +02:00
|
|
|
|
|
2010-10-21 03:49:23 +02:00
|
|
|
|
[Test]
|
2010-10-24 09:46:58 +02:00
|
|
|
|
[Description("Verifies that an existing file will skip import")]
|
|
|
|
|
public void import_existing_file()
|
2010-10-21 03:49:23 +02:00
|
|
|
|
{
|
2010-10-24 09:46:58 +02:00
|
|
|
|
//Arrange
|
|
|
|
|
/////////////////////////////////////////
|
|
|
|
|
|
|
|
|
|
//Constants
|
|
|
|
|
const string fileName = "WEEDS.S03E01-06.DUAL.BDRip.XviD.AC3.-HELLYWOOD.avi";
|
|
|
|
|
|
|
|
|
|
//Fakes
|
|
|
|
|
var fakeSeries = Builder<Series>.CreateNew().Build();
|
|
|
|
|
var fakeEpisode = Builder<Episode>.CreateNew().With(c => c.SeriesId = fakeSeries.SeriesId).Build();
|
|
|
|
|
|
|
|
|
|
//Mocks
|
|
|
|
|
var repository = new Mock<IRepository>(MockBehavior.Strict);
|
|
|
|
|
repository.Setup(r => r.Exists<EpisodeFile>(It.IsAny<Expression<Func<EpisodeFile, Boolean>>>())).Returns(true).Verifiable();
|
|
|
|
|
|
|
|
|
|
var episodeProvider = new Mock<IEpisodeProvider>(MockBehavior.Strict);
|
|
|
|
|
var diskProvider = new Mock<IDiskProvider>(MockBehavior.Strict);
|
|
|
|
|
|
|
|
|
|
var kernel = new MockingKernel();
|
|
|
|
|
kernel.Bind<IRepository>().ToConstant(repository.Object);
|
|
|
|
|
|
|
|
|
|
kernel.Bind<IEpisodeProvider>().ToConstant(episodeProvider.Object);
|
|
|
|
|
kernel.Bind<IDiskProvider>().ToConstant(diskProvider.Object);
|
|
|
|
|
kernel.Bind<IMediaFileProvider>().To<MediaFileProvider>();
|
|
|
|
|
|
|
|
|
|
//Act
|
|
|
|
|
var result = kernel.Get<IMediaFileProvider>().ImportFile(fakeSeries, fileName);
|
|
|
|
|
|
|
|
|
|
//Assert
|
|
|
|
|
repository.VerifyAll();
|
|
|
|
|
episodeProvider.VerifyAll();
|
|
|
|
|
diskProvider.VerifyAll();
|
|
|
|
|
Assert.IsNull(result);
|
|
|
|
|
repository.Verify(r => r.Add<EpisodeFile>(result), Times.Never());
|
2010-10-21 03:49:23 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Test]
|
2010-10-24 09:46:58 +02:00
|
|
|
|
[Description("Verifies that a file with no episode is skipped")]
|
|
|
|
|
public void import_file_with_no_episode()
|
2010-10-21 03:49:23 +02:00
|
|
|
|
{
|
2010-10-24 09:46:58 +02:00
|
|
|
|
//Arrange
|
|
|
|
|
/////////////////////////////////////////
|
|
|
|
|
|
|
|
|
|
//Constants
|
|
|
|
|
const string fileName = "WEEDS.S03E01-06.DUAL.BDRip.XviD.AC3.-HELLYWOOD.avi";
|
|
|
|
|
const int seasonNumber = 3;
|
|
|
|
|
const int episodeNumner = 01;
|
|
|
|
|
|
|
|
|
|
//Fakes
|
|
|
|
|
var fakeSeries = Builder<Series>.CreateNew().Build();
|
|
|
|
|
|
|
|
|
|
//Mocks
|
|
|
|
|
var repository = new Mock<IRepository>(MockBehavior.Strict);
|
|
|
|
|
repository.Setup(r => r.Exists<EpisodeFile>(It.IsAny<Expression<Func<EpisodeFile, Boolean>>>())).Returns(false).Verifiable();
|
|
|
|
|
|
|
|
|
|
var episodeProvider = new Mock<IEpisodeProvider>(MockBehavior.Strict);
|
|
|
|
|
episodeProvider.Setup(e => e.GetEpisode(fakeSeries.SeriesId, seasonNumber, episodeNumner)).Returns<Episode>(null).Verifiable();
|
|
|
|
|
|
|
|
|
|
var diskProvider = new Mock<IDiskProvider>(MockBehavior.Strict);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var kernel = new MockingKernel();
|
|
|
|
|
kernel.Bind<IRepository>().ToConstant(repository.Object);
|
|
|
|
|
kernel.Bind<IEpisodeProvider>().ToConstant(episodeProvider.Object);
|
|
|
|
|
kernel.Bind<IDiskProvider>().ToConstant(diskProvider.Object);
|
|
|
|
|
kernel.Bind<IMediaFileProvider>().To<MediaFileProvider>();
|
|
|
|
|
|
|
|
|
|
//Act
|
|
|
|
|
var result = kernel.Get<IMediaFileProvider>().ImportFile(fakeSeries, fileName);
|
|
|
|
|
|
|
|
|
|
//Assert
|
|
|
|
|
repository.VerifyAll();
|
|
|
|
|
episodeProvider.VerifyAll();
|
|
|
|
|
diskProvider.VerifyAll();
|
|
|
|
|
Assert.IsNull(result);
|
|
|
|
|
repository.Verify(r => r.Add<EpisodeFile>(result), Times.Never());
|
2010-10-21 03:49:23 +02:00
|
|
|
|
}
|
|
|
|
|
|
2010-10-24 09:46:58 +02:00
|
|
|
|
|
2010-10-30 04:46:32 +02:00
|
|
|
|
[Test]
|
|
|
|
|
[Row("Season {season}\\S{season:00}E{episode:00} - {title} - {quality}", "Season 6\\S06E08 - Lethal Inspection - hdtv")]
|
|
|
|
|
[Row("Season {season}\\{series} - {season:##}{episode:00} - {title} - {quality}", "Season 6\\Futurama - 608 - Lethal Inspection - hdtv")]
|
|
|
|
|
[Row("Season {season}\\{series} - {season:##}{episode:00} - {title}", "Season 6\\Futurama - 608 - Lethal Inspection")]
|
|
|
|
|
public void test_file_path_generation(string patern, string path)
|
|
|
|
|
{
|
|
|
|
|
var fakeConfig = new Mock<IConfigProvider>();
|
|
|
|
|
fakeConfig.Setup(c => c.EpisodeNameFormat).Returns(patern);
|
|
|
|
|
|
|
|
|
|
var kernel = new MockingKernel();
|
|
|
|
|
kernel.Bind<IConfigProvider>().ToConstant(fakeConfig.Object);
|
|
|
|
|
kernel.Bind<IMediaFileProvider>().To<MediaFileProvider>();
|
2010-10-24 09:46:58 +02:00
|
|
|
|
|
2010-10-30 04:46:32 +02:00
|
|
|
|
var fakeEpisode = new EpisodeModel
|
|
|
|
|
{
|
|
|
|
|
SeasonNumber = 6,
|
|
|
|
|
EpisodeNumber = 8,
|
|
|
|
|
EpisodeTitle = "Lethal Inspection",
|
|
|
|
|
Quality = QualityTypes.HDTV,
|
|
|
|
|
SeriesTitle = "Futurama"
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
//Act
|
|
|
|
|
var result = kernel.Get<IMediaFileProvider>().GenerateEpisodePath(fakeEpisode);
|
|
|
|
|
|
|
|
|
|
//Assert
|
|
|
|
|
Assert.AreEqual(path.ToLowerInvariant(), result.ToLowerInvariant());
|
|
|
|
|
}
|
2010-10-24 09:46:58 +02:00
|
|
|
|
|
2010-10-21 03:49:23 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|