1
0
Fork 0
mirror of https://github.com/Radarr/Radarr synced 2024-12-29 11:25:26 +00:00
Radarr/NzbDrone.Core.Test/ProviderTests/MediaFileProviderTest.cs

209 lines
6.8 KiB
C#
Raw Normal View History

// ReSharper disable RedundantUsingDirective
using System;
2011-05-18 05:32:23 +00:00
using System.Collections.Generic;
using FizzWare.NBuilder;
2011-06-02 21:06:46 +00:00
using FluentAssertions;
using Moq;
2011-06-02 21:06:46 +00:00
using NUnit.Framework;
2011-11-13 04:07:06 +00:00
using NzbDrone.Common;
using NzbDrone.Core.Model;
using NzbDrone.Core.Providers;
2011-04-04 03:50:12 +00:00
using NzbDrone.Core.Providers.Core;
using NzbDrone.Core.Repository;
2012-10-14 00:36:16 +00:00
using NzbDrone.Core.Repository.Quality;
using NzbDrone.Core.Test.Framework;
using NzbDrone.Test.Common.AutoMoq;
namespace NzbDrone.Core.Test.ProviderTests
{
[TestFixture]
// ReSharper disable InconsistentNaming
public class MediaFileProviderTest : CoreTest
{
2011-05-18 05:32:23 +00:00
[Test]
public void get_series_files()
2011-05-18 05:32:23 +00:00
{
var firstSeriesFiles = Builder<EpisodeFile>.CreateListOfSize(10)
2012-10-14 00:36:16 +00:00
.All()
.With(c => c.Quality = QualityTypes.SDTV)
.With(s => s.SeriesId = 12).Build();
2011-05-18 05:32:23 +00:00
var secondSeriesFiles = Builder<EpisodeFile>.CreateListOfSize(10)
2012-10-14 00:36:16 +00:00
.All()
.With(c => c.Quality = QualityTypes.SDTV)
.With(s => s.SeriesId = 20).Build();
2011-05-18 05:32:23 +00:00
2012-01-25 03:09:49 +00:00
var database = TestDbHelper.GetEmptyDatabase();
database.InsertMany(firstSeriesFiles);
database.InsertMany(secondSeriesFiles);
2011-12-15 04:15:53 +00:00
Mocker.SetConstant(database);
2011-06-02 21:06:46 +00:00
2011-12-15 04:15:53 +00:00
var result = Mocker.Resolve<MediaFileProvider>().GetSeriesFiles(12);
2011-05-18 05:32:23 +00:00
result.Should().HaveSameCount(firstSeriesFiles);
2011-05-18 05:32:23 +00:00
}
[Test]
public void get_season_files()
{
var firstSeriesFiles = Builder<EpisodeFile>.CreateListOfSize(10)
2011-10-18 21:46:06 +00:00
.All()
2012-10-14 00:36:16 +00:00
.With(c => c.Quality = QualityTypes.SDTV)
2011-10-18 21:46:06 +00:00
.With(s => s.SeriesId = 12)
.With(s => s.SeasonNumber = 1)
.Build();
var secondSeriesFiles = Builder<EpisodeFile>.CreateListOfSize(10)
2011-10-18 21:46:06 +00:00
.All()
2012-10-14 00:36:16 +00:00
.With(c => c.Quality = QualityTypes.SDTV)
2011-10-18 21:46:06 +00:00
.With(s => s.SeriesId = 12)
.With(s => s.SeasonNumber = 2)
.Build();
2012-01-25 03:09:49 +00:00
var database = TestDbHelper.GetEmptyDatabase();
database.InsertMany(firstSeriesFiles);
database.InsertMany(secondSeriesFiles);
2011-12-15 04:15:53 +00:00
Mocker.SetConstant(database);
2011-12-15 04:15:53 +00:00
var result = Mocker.Resolve<MediaFileProvider>().GetSeasonFiles(12, 1);
result.Should().HaveSameCount(firstSeriesFiles);
}
[Test]
public void Scan_series_should_skip_series_with_no_episodes()
{
2011-12-15 04:15:53 +00:00
WithStrictMocker();
2011-12-15 04:15:53 +00:00
Mocker.GetMock<EpisodeProvider>()
.Setup(c => c.GetEpisodeBySeries(12))
.Returns(new List<Episode>());
2011-12-15 04:15:53 +00:00
Mocker.GetMock<MediaFileProvider>()
.Setup(e => e.CleanUpDatabase());
2011-12-15 04:15:53 +00:00
Mocker.GetMock<DiskProvider>()
.Setup(c => c.FolderExists(It.IsAny<string>()))
.Returns(true);
var series = Builder<Series>.CreateNew()
.With(s => s.SeriesId = 12).Build();
//Act
2011-12-15 04:15:53 +00:00
Mocker.Resolve<DiskScanProvider>().Scan(series);
//Assert
2011-12-15 04:15:53 +00:00
Mocker.VerifyAllMocks();
}
[Test]
[TestCase("Law & Order: Criminal Intent - S10E07 - Icarus [HDTV]", "Law & Order- Criminal Intent - S10E07 - Icarus [HDTV]")]
public void CleanFileName(string name, string expectedName)
{
//Act
var result = MediaFileProvider.CleanFilename(name);
//Assert
Assert.AreEqual(expectedName, result);
}
[Test]
[TestCase("30 Rock - S01E05 - Episode Title", 1, true, "Season %0s", @"C:\Test\30 Rock\Season 01\30 Rock - S01E05 - Episode Title.mkv")]
[TestCase("30 Rock - S01E05 - Episode Title", 1, true, "Season %s", @"C:\Test\30 Rock\Season 1\30 Rock - S01E05 - Episode Title.mkv")]
[TestCase("30 Rock - S01E05 - Episode Title", 1, false, "Season %0s", @"C:\Test\30 Rock\30 Rock - S01E05 - Episode Title.mkv")]
[TestCase("30 Rock - S01E05 - Episode Title", 1, false, "Season %s", @"C:\Test\30 Rock\30 Rock - S01E05 - Episode Title.mkv")]
[TestCase("30 Rock - S01E05 - Episode Title", 1, true, "ReallyUglySeasonFolder %s", @"C:\Test\30 Rock\ReallyUglySeasonFolder 1\30 Rock - S01E05 - Episode Title.mkv")]
public void CalculateFilePath_SeasonFolder_SingleNumber(string filename, int seasonNumber, bool useSeasonFolder, string seasonFolderFormat, string expectedPath)
{
//Setup
var fakeSeries = Builder<Series>.CreateNew()
.With(s => s.Title = "30 Rock")
.With(s => s.Path = @"C:\Test\30 Rock")
.With(s => s.SeasonFolder = useSeasonFolder)
.Build();
2011-12-15 04:15:53 +00:00
Mocker.GetMock<ConfigProvider>().Setup(e => e.SortingSeasonFolderFormat).Returns(seasonFolderFormat);
//Act
2011-12-15 04:15:53 +00:00
var result = Mocker.Resolve<MediaFileProvider>().CalculateFilePath(fakeSeries, 1, filename, ".mkv");
//Assert
Assert.AreEqual(expectedPath, result.FullName);
}
[Test]
public void DeleteEpisodeFile()
{
//Setup
2012-10-14 00:36:16 +00:00
var episodeFiles = Builder<EpisodeFile>
.CreateListOfSize(10)
.All()
.With(c => c.Quality = QualityTypes.SDTV)
.Build();
2012-01-25 03:09:49 +00:00
var database = TestDbHelper.GetEmptyDatabase();
2011-12-15 04:15:53 +00:00
Mocker.SetConstant(database);
database.InsertMany(episodeFiles);
//Act
2011-12-15 04:15:53 +00:00
Mocker.Resolve<MediaFileProvider>().Delete(1);
var result = database.Fetch<EpisodeFile>();
//Assert
result.Should().HaveCount(9);
result.Should().NotContain(e => e.EpisodeFileId == 1);
}
[Test]
public void GetFileByPath_should_return_null_if_file_does_not_exist_in_database()
{
//Setup
WithRealDb();
//Act
var result = Mocker.Resolve<MediaFileProvider>().GetFileByPath(@"C:\Test\EpisodeFile.avi");
//Resolve
result.Should().BeNull();
}
[Test]
public void GetFileByPath_should_return_EpisodeFile_if_file_exists_in_database()
{
var path = @"C:\Test\EpisodeFile.avi";
//Setup
WithRealDb();
var episodeFile = Builder<EpisodeFile>.CreateNew()
2012-10-14 00:36:16 +00:00
.With(c => c.Quality = QualityTypes.SDTV)
.With(f => f.Path = path.NormalizePath())
.Build();
var episodeFileId = Convert.ToInt32(Db.Insert(episodeFile));
//Act
var result = Mocker.Resolve<MediaFileProvider>().GetFileByPath(path);
//Resolve
result.Should().NotBeNull();
result.Path.Should().Be(path.NormalizePath());
result.EpisodeFileId.Should().Be(episodeFileId);
}
}
2011-04-10 02:44:01 +00:00
}