1
0
mirror of https://github.com/Sonarr/Sonarr.git synced 2024-12-12 11:15:43 +02:00
Sonarr/NzbDrone.Core.Test/MediaFileProviderTests.cs

370 lines
13 KiB
C#
Raw Normal View History

2011-05-22 19:53:21 +03:00
// ReSharper disable RedundantUsingDirective
using System;
2011-05-18 08:32:23 +03:00
using System.Collections.Generic;
using System.Linq.Expressions;
2011-05-18 08:32:23 +03:00
using System.Linq;
using AutoMoq;
using FizzWare.NBuilder;
2011-06-03 00:06:46 +03:00
using FluentAssertions;
using Moq;
2011-06-03 00:06:46 +03:00
using NUnit.Framework;
2011-05-18 08:32:23 +03:00
using NzbDrone.Core.Model.Notification;
using NzbDrone.Core.Providers;
2011-04-04 06:50:12 +03:00
using NzbDrone.Core.Providers.Core;
2011-05-18 08:32:23 +03:00
using NzbDrone.Core.Providers.Jobs;
using NzbDrone.Core.Repository;
using NzbDrone.Core.Repository.Quality;
using NzbDrone.Core.Test.Framework;
using SubSonic.Repository;
namespace NzbDrone.Core.Test
{
[TestFixture]
// ReSharper disable InconsistentNaming
public class MediaFileProviderTests : TestBase
{
[Test]
[Description("Verifies that a new file imported properly")]
public void import_new_file()
{
//Arrange
/////////////////////////////////////////
//Constants
2011-05-21 05:06:56 +03:00
const string fileName = @"WEEDS.S03E01.DUAL.BDRip.XviD.AC3.-HELLYWOOD.avi";
const int seasonNumber = 3;
2011-05-21 05:06:56 +03:00
const int episodeNumner = 1;
const int size = 12345;
//Fakes
var fakeSeries = Builder<Series>.CreateNew().Build();
var fakeEpisode = Builder<Episode>.CreateNew().With(c => c.SeriesId = fakeSeries.SeriesId).Build();
//Mocks
var mocker = new AutoMoqer();
2011-05-21 05:06:56 +03:00
mocker.GetMock<DiskProvider>()
.Setup(e => e.GetSize(fileName)).Returns(12345).Verifiable();
mocker.GetMock<IRepository>()
2011-04-10 05:44:01 +03:00
.Setup(r => r.Exists(It.IsAny<Expression<Func<EpisodeFile, Boolean>>>())).Returns(false).Verifiable();
2011-05-21 05:06:56 +03:00
mocker.GetMock<IRepository>()
2011-04-10 05:44:01 +03:00
.Setup(r => r.Add(It.IsAny<EpisodeFile>())).Returns(0).Verifiable();
mocker.GetMock<EpisodeProvider>()
2011-05-21 05:06:56 +03:00
.Setup(e => e.GetEpisode(fakeSeries.SeriesId, seasonNumber, episodeNumner)).Returns(fakeEpisode);
//Act
var result = mocker.Resolve<MediaFileProvider>().ImportFile(fakeSeries, fileName);
//Assert
Assert.IsNotNull(result);
mocker.GetMock<IRepository>().Verify(r => r.Add(result), Times.Once());
2011-05-21 05:06:56 +03:00
mocker.VerifyAllMocks();
Assert.AreEqual(fakeEpisode.SeriesId, result.SeriesId);
2011-05-23 20:20:43 +03:00
Assert.AreEqual(QualityTypes.DVD, result.Quality);
Assert.AreEqual(Parser.NormalizePath(fileName), result.Path);
Assert.AreEqual(size, result.Size);
Assert.AreEqual(false, result.Proper);
Assert.AreNotEqual(new DateTime(), result.DateAdded);
}
[Test]
[Description("Verifies that a new file imported properly")]
public void import_new_daily_file()
{
//Arrange
/////////////////////////////////////////
//Constants
const string fileName = @"2011.01.10 - Denis Leary - HD TV.mkv";
var airDate = new DateTime(2011, 01, 10);
const int size = 12345;
//Fakes
var fakeSeries = Builder<Series>.CreateNew().Build();
var fakeEpisode = Builder<Episode>.CreateNew().With(c => c.SeriesId = fakeSeries.SeriesId).Build();
//Mocks
var mocker = new AutoMoqer();
mocker.GetMock<IRepository>()
.Setup(r => r.Exists(It.IsAny<Expression<Func<EpisodeFile, Boolean>>>())).Returns(false).Verifiable();
mocker.GetMock<IRepository>()
.Setup(r => r.Add(It.IsAny<EpisodeFile>())).Returns(0).Verifiable();
mocker.GetMock<EpisodeProvider>()
.Setup(e => e.GetEpisode(fakeSeries.SeriesId, airDate)).Returns(fakeEpisode).
Verifiable();
mocker.GetMock<DiskProvider>()
.Setup(e => e.GetSize(fileName)).Returns(size).Verifiable();
//Act
var result = mocker.Resolve<MediaFileProvider>().ImportFile(fakeSeries, fileName);
//Assert
Assert.IsNotNull(result);
mocker.GetMock<IRepository>().VerifyAll();
2011-04-10 05:44:01 +03:00
mocker.GetMock<IRepository>().Verify(r => r.Add(result), Times.Once());
mocker.GetMock<EpisodeProvider>().VerifyAll();
mocker.GetMock<DiskProvider>().VerifyAll();
2011-03-30 09:18:35 +03:00
//Currently can't verify this since the list of episodes are loaded
//Dynamically by SubSonic
//Assert.AreEqual(fakeEpisode, result.EpisodeNumbers[0]);
2011-04-04 07:54:58 +03:00
Assert.AreEqual(fakeEpisode.SeriesId, result.SeriesId);
Assert.AreEqual(QualityTypes.HDTV, result.Quality);
Assert.AreEqual(Parser.NormalizePath(fileName), result.Path);
Assert.AreEqual(size, result.Size);
Assert.AreEqual(false, result.Proper);
Assert.AreNotEqual(new DateTime(), result.DateAdded);
}
[Test]
[Description("Verifies that a new file imported properly")]
public void import_existing_season_file()
{
//Arrange
/////////////////////////////////////////
//Constants
const string fileName = @"WEEDS.S03E01.DUAL.BDRip.XviD.AC3.-HELLYWOOD.avi";
const int size = 12345;
//Fakes
var fakeSeries = Builder<Series>.CreateNew().Build();
//Mocks
var mocker = new AutoMoqer();
mocker.GetMock<IRepository>(MockBehavior.Strict)
.Setup(r => r.Exists(It.IsAny<Expression<Func<EpisodeFile, Boolean>>>())).Returns(true).Verifiable();
//mocker.GetMock<EpisodeProvider>()
2011-04-26 03:43:34 +03:00
// .Setup(e => e.GetEpisode(fakeSeries.SeriesId, seasonNumber, episodeNumner)).Returns(fakeEpisode)
// .Verifiable();
mocker.GetMock<DiskProvider>()
.Setup(e => e.GetSize(fileName)).Returns(size).Verifiable();
//Act
var result = mocker.Resolve<MediaFileProvider>().ImportFile(fakeSeries, fileName);
//Assert
mocker.VerifyAllMocks();
Assert.IsNull(result);
}
[Test]
[Description("Verifies that a un-parsable file isn't imported")]
public void import_unparsable_file()
{
//Arrange
/////////////////////////////////////////
//Constants
const string fileName = @"WEEDS.avi";
const int size = 12345;
//Fakes
var fakeSeries = Builder<Series>.CreateNew().Build();
//Mocks
var mocker = new AutoMoqer();
mocker.GetMock<IRepository>(MockBehavior.Strict)
.Setup(r => r.Exists(It.IsAny<Expression<Func<EpisodeFile, Boolean>>>())).Returns(false).Verifiable();
mocker.GetMock<DiskProvider>()
.Setup(e => e.GetSize(fileName)).Returns(size).Verifiable();
//Act
var result = mocker.Resolve<MediaFileProvider>().ImportFile(fakeSeries, fileName);
//Assert
mocker.VerifyAllMocks();
Assert.IsNull(result);
ExceptionVerification.ExcpectedWarns(1);
}
[Test]
[Description("Verifies that a new file imported properly")]
public void import_sample_file()
{
//Arrange
/////////////////////////////////////////
//Constants
const string fileName = @"2011.01.10 - Denis Leary - sample - HD TV.mkv";
var airDate = new DateTime(2011, 01, 10);
const int size = 12345;
//Fakes
var fakeSeries = Builder<Series>.CreateNew().Build();
var fakeEpisode = Builder<Episode>.CreateNew().With(c => c.SeriesId = fakeSeries.SeriesId).Build();
//Mocks
var mocker = new AutoMoqer();
mocker.GetMock<IRepository>()
.Setup(r => r.Exists(It.IsAny<Expression<Func<EpisodeFile, Boolean>>>())).Returns(false).Verifiable();
mocker.GetMock<IRepository>()
.Setup(r => r.Add(It.IsAny<EpisodeFile>())).Returns(0).Verifiable();
mocker.GetMock<EpisodeProvider>()
.Setup(e => e.GetEpisode(fakeSeries.SeriesId, airDate)).Returns(fakeEpisode).
Verifiable();
mocker.GetMock<DiskProvider>()
.Setup(e => e.GetSize(fileName)).Returns(size).Verifiable();
//Act
var result = mocker.Resolve<MediaFileProvider>().ImportFile(fakeSeries, fileName);
//Assert
Assert.IsNull(result);
}
[Test]
[Description("Verifies that an existing file will skip import")]
public void import_existing_file()
{
//Arrange
/////////////////////////////////////////
//Constants
const string fileName = "WEEDS.S03E01-06.DUAL.BDRip.XviD.AC3.-HELLYWOOD.avi";
//Fakes
var fakeSeries = Builder<Series>.CreateNew().Build();
//Mocks
var mocker = new AutoMoqer();
mocker.GetMock<IRepository>(MockBehavior.Strict)
2011-04-10 05:44:01 +03:00
.Setup(r => r.Exists(It.IsAny<Expression<Func<EpisodeFile, Boolean>>>())).Returns(true).Verifiable();
mocker.GetMock<EpisodeProvider>(MockBehavior.Strict);
2011-04-26 03:43:34 +03:00
mocker.GetMock<DiskProvider>()
.Setup(e => e.GetSize(fileName)).Returns(500000).Verifiable();
//Act
var result = mocker.Resolve<MediaFileProvider>().ImportFile(fakeSeries, fileName);
//Assert
Assert.IsNull(result);
2011-04-10 05:44:01 +03:00
mocker.GetMock<IRepository>().Verify(r => r.Add(result), Times.Never());
2011-04-26 03:43:34 +03:00
mocker.VerifyAllMocks();
}
[Test]
[Description("Verifies that a file with no episode is skipped")]
public void import_file_with_no_episode()
{
//Arrange
/////////////////////////////////////////
//Constants
2011-04-19 09:47:17 +03:00
const string fileName = "WEEDS.S03E01.DUAL.BDRip.XviD.AC3.-HELLYWOOD.avi";
const int seasonNumber = 3;
const int episodeNumner = 01;
//Fakes
var fakeSeries = Builder<Series>.CreateNew().Build();
//Mocks
var mocker = new AutoMoqer();
mocker.GetMock<IRepository>(MockBehavior.Strict)
2011-04-10 05:44:01 +03:00
.Setup(r => r.Exists(It.IsAny<Expression<Func<EpisodeFile, Boolean>>>())).Returns(false).Verifiable();
mocker.GetMock<EpisodeProvider>(MockBehavior.Strict)
2011-04-10 05:44:01 +03:00
.Setup(e => e.GetEpisode(fakeSeries.SeriesId, seasonNumber, episodeNumner)).Returns<Episode>(null).
Verifiable();
2011-04-26 03:43:34 +03:00
mocker.GetMock<DiskProvider>(MockBehavior.Strict)
.Setup(e => e.GetSize(fileName)).Returns(90000000000);
//Act
var result = mocker.Resolve<MediaFileProvider>().ImportFile(fakeSeries, fileName);
//Assert
mocker.VerifyAllMocks();
Assert.IsNull(result);
2011-04-10 05:44:01 +03:00
mocker.GetMock<IRepository>().Verify(r => r.Add(result), Times.Never());
ExceptionVerification.ExcpectedWarns(1);
}
2011-05-18 08:32:23 +03:00
[Test]
public void scan_series_should_update_last_scan_date()
{
var mocker = new AutoMoqer();
mocker.GetMock<SeriesProvider>()
.Setup(c => c.UpdateSeries(It.Is<Series>(s => s.LastDiskSync != null))).Verifiable();
mocker.Resolve<MediaFileProvider>().Scan(new Series());
mocker.VerifyAllMocks();
}
[Test]
public void scan_media_job_should_not_scan_new_series()
{
var mocker = new AutoMoqer();
2011-05-27 09:03:57 +03:00
var fakeSeries = Builder<Series>.CreateListOfSize(2)
2011-05-23 08:51:46 +03:00
.WhereTheFirst(1).Has(c => c.Episodes = new List<Episode>())
.AndTheNext(1).Has(c => c.Episodes = Builder<Episode>.CreateListOfSize(10).Build())
2011-05-27 09:03:57 +03:00
.Build();
2011-05-18 08:32:23 +03:00
mocker.GetMock<SeriesProvider>()
2011-05-23 08:51:46 +03:00
.Setup(c => c.GetAllSeries()).Returns(fakeSeries);
2011-05-18 08:32:23 +03:00
mocker.GetMock<MediaFileProvider>(MockBehavior.Strict)
2011-05-23 08:51:46 +03:00
.Setup(c => c.Scan(fakeSeries.ToList()[1])).Returns(new List<EpisodeFile>()).Verifiable();
2011-05-18 08:32:23 +03:00
mocker.Resolve<DiskScanJob>().Start(new ProgressNotification("test"), 0);
2011-05-18 08:32:23 +03:00
mocker.VerifyAllMocks();
}
[Test]
public void get_season_files()
{
var episodes = Builder<Episode>.CreateListOfSize(20)
.WhereTheFirst(8)
.Has(c => c.EpisodeFile = new EpisodeFile())
.AndTheRemaining()
.Has(c => c.EpisodeFile = null)
.Build().ToList();
var mocker = new AutoMoqer();
mocker.GetMock<SeasonProvider>()
.Setup(c => c.GetSeason(12))
.Returns(Builder<Season>.CreateNew().With(c => c.Episodes = episodes).Build())
.Verifiable();
var result = mocker.Resolve<MediaFileProvider>().GetSeasonFiles(12);
2011-06-03 00:06:46 +03:00
result.Should().HaveCount(8);
result.Should().NotContainNulls();
2011-05-18 08:32:23 +03:00
mocker.VerifyAllMocks();
}
}
2011-04-10 05:44:01 +03:00
}