Cleaned and fixed broken tests

This commit is contained in:
Mark McDowall 2012-10-19 18:42:42 -07:00
parent db585b3bd7
commit f9370a8aec
4 changed files with 41 additions and 93 deletions

View File

@ -77,9 +77,11 @@ namespace NzbDrone.Common
public virtual long GetSize(string path) public virtual long GetSize(string path)
{ {
if (!FileExists(path))
throw new FileNotFoundException("File doesn't exist: " + path);
var fi = new FileInfo(path); var fi = new FileInfo(path);
return fi.Length; return fi.Length;
//return new FileInfo(path).Length;
} }
public virtual String CreateDirectory(string path) public virtual String CreateDirectory(string path)
@ -218,14 +220,6 @@ namespace NzbDrone.Common
return String.Equals(firstPath.NormalizePath(), secondPath.NormalizePath(), StringComparison.InvariantCultureIgnoreCase); return String.Equals(firstPath.NormalizePath(), secondPath.NormalizePath(), StringComparison.InvariantCultureIgnoreCase);
} }
public virtual long GetFileSize(string path)
{
if (!FileExists(path))
throw new FileNotFoundException("File doesn't exist: " + path);
return new FileInfo(path).Length;
}
public virtual void FileSetLastWriteTimeUtc(string path, DateTime dateTime) public virtual void FileSetLastWriteTimeUtc(string path, DateTime dateTime)
{ {
File.SetLastWriteTimeUtc(path, dateTime); File.SetLastWriteTimeUtc(path, dateTime);

View File

@ -22,11 +22,20 @@ namespace NzbDrone.Core.Test.ProviderTests.DiskScanProviderTests
{ {
public static object[] ImportTestCases = public static object[] ImportTestCases =
{ {
new object[] { QualityTypes.SDTV }, new object[] { QualityTypes.SDTV, false },
new object[] { QualityTypes.DVD }, new object[] { QualityTypes.DVD, true },
new object[] { QualityTypes.HDTV } new object[] { QualityTypes.HDTV, false }
}; };
private readonly long SIZE = 50.Megabytes();
public void With50MBFile()
{
Mocker.GetMock<DiskProvider>()
.Setup(d => d.GetSize(It.IsAny<String>()))
.Returns(50.Megabytes());
}
[Test] [Test]
public void import_new_file_should_succeed() public void import_new_file_should_succeed()
{ {
@ -36,10 +45,7 @@ namespace NzbDrone.Core.Test.ProviderTests.DiskScanProviderTests
var fakeEpisode = Builder<Episode>.CreateNew().Build(); var fakeEpisode = Builder<Episode>.CreateNew().Build();
//Mocks //Mocks
With50MBFile();
Mocker.GetMock<DiskProvider>()
.Setup(e => e.GetSize(newFile)).Returns(12345).Verifiable();
Mocker.GetMock<MediaFileProvider>() Mocker.GetMock<MediaFileProvider>()
.Setup(p => p.Exists(It.IsAny<String>())) .Setup(p => p.Exists(It.IsAny<String>()))
@ -52,7 +58,7 @@ namespace NzbDrone.Core.Test.ProviderTests.DiskScanProviderTests
var result = Mocker.Resolve<DiskScanProvider>().ImportFile(fakeSeries, newFile); var result = Mocker.Resolve<DiskScanProvider>().ImportFile(fakeSeries, newFile);
//Assert //Assert
VerifyFileImport(result, Mocker, fakeEpisode, 12345); VerifyFileImport(result, Mocker, fakeEpisode, SIZE);
} }
@ -60,7 +66,6 @@ namespace NzbDrone.Core.Test.ProviderTests.DiskScanProviderTests
public void import_new_file_with_better_same_quality_should_succeed(QualityTypes currentFileQuality, bool currentFileProper) public void import_new_file_with_better_same_quality_should_succeed(QualityTypes currentFileQuality, bool currentFileProper)
{ {
const string newFile = @"WEEDS.S03E01.DUAL.1080p.HELLYWOOD.mkv"; const string newFile = @"WEEDS.S03E01.DUAL.1080p.HELLYWOOD.mkv";
const int size = 12345;
//Fakes //Fakes
var fakeSeries = Builder<Series>.CreateNew().Build(); var fakeSeries = Builder<Series>.CreateNew().Build();
@ -71,11 +76,7 @@ namespace NzbDrone.Core.Test.ProviderTests.DiskScanProviderTests
).Build(); ).Build();
With50MBFile();
Mocker.GetMock<DiskProvider>()
.Setup(e => e.GetSize(newFile)).Returns(12345).Verifiable();
Mocker.GetMock<EpisodeProvider>() Mocker.GetMock<EpisodeProvider>()
.Setup(e => e.GetEpisodesByParseResult(It.IsAny<EpisodeParseResult>())).Returns(new List<Episode> { fakeEpisode }); .Setup(e => e.GetEpisodesByParseResult(It.IsAny<EpisodeParseResult>())).Returns(new List<Episode> { fakeEpisode });
@ -84,7 +85,7 @@ namespace NzbDrone.Core.Test.ProviderTests.DiskScanProviderTests
var result = Mocker.Resolve<DiskScanProvider>().ImportFile(fakeSeries, newFile); var result = Mocker.Resolve<DiskScanProvider>().ImportFile(fakeSeries, newFile);
//Assert //Assert
VerifyFileImport(result, Mocker, fakeEpisode, size); VerifyFileImport(result, Mocker, fakeEpisode, SIZE);
} }
[TestCase("WEEDS.S03E01.DUAL.DVD.XviD.AC3.-HELLYWOOD.avi")] [TestCase("WEEDS.S03E01.DUAL.DVD.XviD.AC3.-HELLYWOOD.avi")]
@ -101,10 +102,7 @@ namespace NzbDrone.Core.Test.ProviderTests.DiskScanProviderTests
.Build(); .Build();
//Mocks //Mocks
With50MBFile();
Mocker.GetMock<DiskProvider>()
.Setup(e => e.GetSize(fileName)).Returns(12345).Verifiable();
Mocker.GetMock<MediaFileProvider>() Mocker.GetMock<MediaFileProvider>()
.Setup(p => p.Exists(It.IsAny<String>())) .Setup(p => p.Exists(It.IsAny<String>()))
@ -123,19 +121,14 @@ namespace NzbDrone.Core.Test.ProviderTests.DiskScanProviderTests
[Test] [Test]
public void import_unparsable_file_should_skip() public void import_unparsable_file_should_skip()
{ {
const string fileName = @"WEEDS.avi"; const string fileName = @"C:\Test\WEEDS.avi";
const int size = 12345;
var fakeSeries = Builder<Series>.CreateNew().Build(); var fakeSeries = Builder<Series>.CreateNew().Build();
Mocker.GetMock<MediaFileProvider>() Mocker.GetMock<MediaFileProvider>()
.Setup(p => p.Exists(It.IsAny<String>())).Returns(false); .Setup(p => p.Exists(It.IsAny<String>())).Returns(false);
Mocker.GetMock<DiskProvider>() With50MBFile();
.Setup(e => e.GetSize(fileName)).Returns(size).Verifiable();
//Act //Act
var result = Mocker.Resolve<DiskScanProvider>().ImportFile(fakeSeries, fileName); var result = Mocker.Resolve<DiskScanProvider>().ImportFile(fakeSeries, fileName);
@ -145,30 +138,6 @@ namespace NzbDrone.Core.Test.ProviderTests.DiskScanProviderTests
ExceptionVerification.ExpectedWarns(1); ExceptionVerification.ExpectedWarns(1);
} }
[Test]
public void import_sample_file_should_skip()
{
const string fileName = @"2011.01.10 - Denis Leary - sample - HD TV.mkv";
const int size = 12345;
//Fakes
var fakeSeries = Builder<Series>.CreateNew().Build();
//Mocks
Mocker.GetMock<MediaFileProvider>()
.Setup(p => p.Exists(It.IsAny<String>())).Returns(false);
Mocker.GetMock<DiskProvider>()
.Setup(e => e.GetSize(fileName)).Returns(size).Verifiable();
//Act
var result = Mocker.Resolve<DiskScanProvider>().ImportFile(fakeSeries, fileName);
//Assert
VerifySkipImport(result, Mocker);
}
[Test] [Test]
public void import_existing_file_should_skip() public void import_existing_file_should_skip()
{ {
@ -181,6 +150,8 @@ namespace NzbDrone.Core.Test.ProviderTests.DiskScanProviderTests
.Setup(p => p.Exists(It.IsAny<String>())) .Setup(p => p.Exists(It.IsAny<String>()))
.Returns(true); .Returns(true);
With50MBFile();
//Act //Act
var result = Mocker.Resolve<DiskScanProvider>().ImportFile(fakeSeries, fileName); var result = Mocker.Resolve<DiskScanProvider>().ImportFile(fakeSeries, fileName);
@ -198,14 +169,12 @@ namespace NzbDrone.Core.Test.ProviderTests.DiskScanProviderTests
var fakeSeries = Builder<Series>.CreateNew().Build(); var fakeSeries = Builder<Series>.CreateNew().Build();
//Mocks //Mocks
With50MBFile();
Mocker.GetMock<MediaFileProvider>() Mocker.GetMock<MediaFileProvider>()
.Setup(p => p.Exists(It.IsAny<String>())) .Setup(p => p.Exists(It.IsAny<String>()))
.Returns(false); .Returns(false);
Mocker.GetMock<DiskProvider>(MockBehavior.Strict)
.Setup(e => e.GetSize(fileName)).Returns(90000000000);
Mocker.GetMock<EpisodeProvider>() Mocker.GetMock<EpisodeProvider>()
.Setup(c => c.GetEpisodesByParseResult(It.IsAny<EpisodeParseResult>())) .Setup(c => c.GetEpisodesByParseResult(It.IsAny<EpisodeParseResult>()))
.Returns(new List<Episode>()); .Returns(new List<Episode>());
@ -222,7 +191,6 @@ namespace NzbDrone.Core.Test.ProviderTests.DiskScanProviderTests
[TestCase("WEEDS.S03E01.DUAL.bluray.x264.AC3.-HELLYWOOD.mkv")] [TestCase("WEEDS.S03E01.DUAL.bluray.x264.AC3.-HELLYWOOD.mkv")]
public void import_new_file_episode_has_better_quality_than_existing(string fileName) public void import_new_file_episode_has_better_quality_than_existing(string fileName)
{ {
//Fakes //Fakes
var fakeSeries = Builder<Series>.CreateNew().Build(); var fakeSeries = Builder<Series>.CreateNew().Build();
var fakeEpisode = Builder<Episode>.CreateNew() var fakeEpisode = Builder<Episode>.CreateNew()
@ -232,10 +200,7 @@ namespace NzbDrone.Core.Test.ProviderTests.DiskScanProviderTests
.Build(); .Build();
//Mocks //Mocks
With50MBFile();
Mocker.GetMock<DiskProvider>()
.Setup(e => e.GetSize(fileName)).Returns(12345).Verifiable();
Mocker.GetMock<MediaFileProvider>() Mocker.GetMock<MediaFileProvider>()
.Setup(p => p.Exists(It.IsAny<String>())) .Setup(p => p.Exists(It.IsAny<String>()))
@ -248,7 +213,7 @@ namespace NzbDrone.Core.Test.ProviderTests.DiskScanProviderTests
var result = Mocker.Resolve<DiskScanProvider>().ImportFile(fakeSeries, fileName); var result = Mocker.Resolve<DiskScanProvider>().ImportFile(fakeSeries, fileName);
//Assert //Assert
VerifyFileImport(result, Mocker, fakeEpisode, 12345); VerifyFileImport(result, Mocker, fakeEpisode, SIZE);
Mocker.GetMock<RecycleBinProvider>().Verify(p => p.DeleteFile(It.IsAny<string>()), Times.Once()); Mocker.GetMock<RecycleBinProvider>().Verify(p => p.DeleteFile(It.IsAny<string>()), Times.Once());
} }
@ -267,8 +232,7 @@ namespace NzbDrone.Core.Test.ProviderTests.DiskScanProviderTests
.Build()) .Build())
.Build(); .Build();
Mocker.GetMock<DiskProvider>() With50MBFile();
.Setup(e => e.GetSize(fileName)).Returns(12345).Verifiable();
Mocker.GetMock<MediaFileProvider>() Mocker.GetMock<MediaFileProvider>()
.Setup(p => p.Exists(It.IsAny<String>())) .Setup(p => p.Exists(It.IsAny<String>()))
@ -281,7 +245,7 @@ namespace NzbDrone.Core.Test.ProviderTests.DiskScanProviderTests
var result = Mocker.Resolve<DiskScanProvider>().ImportFile(fakeSeries, fileName); var result = Mocker.Resolve<DiskScanProvider>().ImportFile(fakeSeries, fileName);
//Assert //Assert
VerifyFileImport(result, Mocker, fakeEpisodes[0], 12345); VerifyFileImport(result, Mocker, fakeEpisodes[0], SIZE);
Mocker.GetMock<RecycleBinProvider>().Verify(p => p.DeleteFile(It.IsAny<string>()), Times.Once()); Mocker.GetMock<RecycleBinProvider>().Verify(p => p.DeleteFile(It.IsAny<string>()), Times.Once());
} }
@ -301,9 +265,7 @@ namespace NzbDrone.Core.Test.ProviderTests.DiskScanProviderTests
//Mocks //Mocks
With50MBFile();
Mocker.GetMock<DiskProvider>()
.Setup(e => e.GetSize(fileName)).Returns(12345).Verifiable();
Mocker.GetMock<MediaFileProvider>() Mocker.GetMock<MediaFileProvider>()
.Setup(p => p.Exists(It.IsAny<String>())) .Setup(p => p.Exists(It.IsAny<String>()))
@ -341,10 +303,7 @@ namespace NzbDrone.Core.Test.ProviderTests.DiskScanProviderTests
.Build(); .Build();
//Mocks //Mocks
With50MBFile();
Mocker.GetMock<DiskProvider>()
.Setup(e => e.GetSize(fileName)).Returns(12345).Verifiable();
Mocker.GetMock<MediaFileProvider>() Mocker.GetMock<MediaFileProvider>()
.Setup(p => p.Exists(It.IsAny<String>())) .Setup(p => p.Exists(It.IsAny<String>()))
@ -357,7 +316,7 @@ namespace NzbDrone.Core.Test.ProviderTests.DiskScanProviderTests
var result = Mocker.Resolve<DiskScanProvider>().ImportFile(fakeSeries, fileName); var result = Mocker.Resolve<DiskScanProvider>().ImportFile(fakeSeries, fileName);
//Assert //Assert
VerifyFileImport(result, Mocker, fakeEpisode1, 12345); VerifyFileImport(result, Mocker, fakeEpisode1, SIZE);
Mocker.GetMock<RecycleBinProvider>().Verify(p => p.DeleteFile(It.IsAny<string>()), Times.Exactly(2)); Mocker.GetMock<RecycleBinProvider>().Verify(p => p.DeleteFile(It.IsAny<string>()), Times.Exactly(2));
} }
@ -375,10 +334,7 @@ namespace NzbDrone.Core.Test.ProviderTests.DiskScanProviderTests
.Build(); .Build();
//Mocks //Mocks
With50MBFile();
Mocker.GetMock<DiskProvider>()
.Setup(e => e.GetSize(fileName)).Returns(12345).Verifiable();
Mocker.GetMock<MediaFileProvider>() Mocker.GetMock<MediaFileProvider>()
.Setup(p => p.Exists(It.IsAny<String>())) .Setup(p => p.Exists(It.IsAny<String>()))
@ -391,7 +347,7 @@ namespace NzbDrone.Core.Test.ProviderTests.DiskScanProviderTests
var result = Mocker.Resolve<DiskScanProvider>().ImportFile(fakeSeries, fileName); var result = Mocker.Resolve<DiskScanProvider>().ImportFile(fakeSeries, fileName);
//Assert //Assert
VerifyFileImport(result, Mocker, fakeEpisode, 12345); VerifyFileImport(result, Mocker, fakeEpisode, SIZE);
Mocker.GetMock<DiskProvider>().Verify(p => p.DeleteFile(It.IsAny<string>()), Times.Never()); Mocker.GetMock<DiskProvider>().Verify(p => p.DeleteFile(It.IsAny<string>()), Times.Never());
} }
@ -409,15 +365,14 @@ namespace NzbDrone.Core.Test.ProviderTests.DiskScanProviderTests
.Returns(false); .Returns(false);
Mocker.GetMock<DiskProvider>() Mocker.GetMock<DiskProvider>()
.Setup(d => d.GetFileSize(path)) .Setup(d => d.GetSize(path))
.Returns(20.Megabytes()); .Returns(20.Megabytes());
Mocker.Resolve<DiskScanProvider>().ImportFile(series, path).Should().BeNull(); Mocker.Resolve<DiskScanProvider>().ImportFile(series, path).Should().BeNull();
} }
private static void VerifyFileImport(EpisodeFile result, AutoMoqer Mocker, Episode fakeEpisode, int size) private static void VerifyFileImport(EpisodeFile result, AutoMoqer Mocker, Episode fakeEpisode, long size)
{ {
Mocker.VerifyAllMocks();
result.Should().NotBeNull(); result.Should().NotBeNull();
result.SeriesId.Should().Be(fakeEpisode.SeriesId); result.SeriesId.Should().Be(fakeEpisode.SeriesId);
result.Size.Should().Be(size); result.Size.Should().Be(size);
@ -432,7 +387,6 @@ namespace NzbDrone.Core.Test.ProviderTests.DiskScanProviderTests
private static void VerifySkipImport(EpisodeFile result, AutoMoqer Mocker) private static void VerifySkipImport(EpisodeFile result, AutoMoqer Mocker)
{ {
Mocker.VerifyAllMocks();
result.Should().BeNull(); result.Should().BeNull();
Mocker.GetMock<MediaFileProvider>().Verify(p => p.Add(It.IsAny<EpisodeFile>()), Times.Never()); Mocker.GetMock<MediaFileProvider>().Verify(p => p.Add(It.IsAny<EpisodeFile>()), Times.Never());
Mocker.GetMock<EpisodeProvider>().Verify(p => p.UpdateEpisode(It.IsAny<Episode>()), Times.Never()); Mocker.GetMock<EpisodeProvider>().Verify(p => p.UpdateEpisode(It.IsAny<Episode>()), Times.Never());

View File

@ -111,7 +111,7 @@ namespace NzbDrone.Core.Test.ProviderTests.PostDownloadProviderTests
Mocker.Resolve<PostDownloadProvider>().ProcessVideoFile(file); Mocker.Resolve<PostDownloadProvider>().ProcessVideoFile(file);
//Assert //Assert
Mocker.GetMock<DiskProvider>().Verify(s => s.GetFileSize(It.IsAny<String>()), Times.Never()); Mocker.GetMock<DiskProvider>().Verify(s => s.GetSize(It.IsAny<String>()), Times.Never());
ExceptionVerification.IgnoreWarns(); ExceptionVerification.IgnoreWarns();
} }
@ -149,7 +149,7 @@ namespace NzbDrone.Core.Test.ProviderTests.PostDownloadProviderTests
.Returns(series); .Returns(series);
Mocker.GetMock<DiskProvider>() Mocker.GetMock<DiskProvider>()
.Setup(s => s.GetFileSize(downloadName)) .Setup(s => s.GetSize(downloadName))
.Returns(10); .Returns(10);
Mocker.GetMock<DiskProvider>() Mocker.GetMock<DiskProvider>()
@ -178,7 +178,7 @@ namespace NzbDrone.Core.Test.ProviderTests.PostDownloadProviderTests
.Returns(fakeSeries); .Returns(fakeSeries);
Mocker.GetMock<DiskProvider>() Mocker.GetMock<DiskProvider>()
.Setup(s => s.GetFileSize(downloadName)) .Setup(s => s.GetSize(downloadName))
.Returns(8); .Returns(8);
//Act //Act

View File

@ -138,7 +138,7 @@ namespace NzbDrone.Core.Providers
return; return;
} }
var size = _diskProvider.GetFileSize(videoFile); var size = _diskProvider.GetSize(videoFile);
var freeSpace = _diskProvider.FreeDiskSpace(new DirectoryInfo(series.Path)); var freeSpace = _diskProvider.FreeDiskSpace(new DirectoryInfo(series.Path));
if (Convert.ToUInt64(size) > freeSpace) if (Convert.ToUInt64(size) > freeSpace)