diff --git a/NzbDrone.Common.Test/DiskProviderTests.cs b/NzbDrone.Common.Test/DiskProviderFixture.cs similarity index 87% rename from NzbDrone.Common.Test/DiskProviderTests.cs rename to NzbDrone.Common.Test/DiskProviderFixture.cs index a39d7a5e6..58b4fd6b5 100644 --- a/NzbDrone.Common.Test/DiskProviderTests.cs +++ b/NzbDrone.Common.Test/DiskProviderFixture.cs @@ -1,5 +1,5 @@ // ReSharper disable InconsistentNaming -using System; + using System.IO; using System.Linq; using FluentAssertions; @@ -9,7 +9,7 @@ using NzbDrone.Test.Common; namespace NzbDrone.Common.Test { [TestFixture] - public class DiskProviderTests : TestBase + public class DiskProviderFixture : TestBase { DirectoryInfo BinFolder; DirectoryInfo BinFolderCopy; @@ -34,6 +34,24 @@ namespace NzbDrone.Common.Test } } + [Test] + public void directory_exist_should_be_able_to_find_existing_folder() + { + Mocker.Resolve().FolderExists(TempFolder).Should().BeTrue(); + } + + [Test] + public void directory_exist_should_be_able_to_find_existing_unc_share() + { + Mocker.Resolve().FolderExists(@"\\localhost\c$").Should().BeTrue(); + } + + [Test] + public void directory_exist_should_not_be_able_to_find_none_existing_folder() + { + Mocker.Resolve().FolderExists(@"C:\ThisBetterNotExist\").Should().BeFalse(); + } + [Test] public void moveFile_should_overwrite_existing_file() { diff --git a/NzbDrone.Common.Test/NzbDrone.Common.Test.csproj b/NzbDrone.Common.Test/NzbDrone.Common.Test.csproj index bf433394f..79fc3efe3 100644 --- a/NzbDrone.Common.Test/NzbDrone.Common.Test.csproj +++ b/NzbDrone.Common.Test/NzbDrone.Common.Test.csproj @@ -65,7 +65,7 @@ - + diff --git a/NzbDrone.Core.Test/ProviderTests/ConfigProviderTest.cs b/NzbDrone.Core.Test/ProviderTests/ConfigProviderTest.cs index e6d26a982..0037240eb 100644 --- a/NzbDrone.Core.Test/ProviderTests/ConfigProviderTest.cs +++ b/NzbDrone.Core.Test/ProviderTests/ConfigProviderTest.cs @@ -1,15 +1,10 @@ using System; using System.Linq; -using System.Reflection; - using FluentAssertions; -using Moq; using NUnit.Framework; using NzbDrone.Core.Providers.Core; using NzbDrone.Core.Repository; using NzbDrone.Core.Test.Framework; -using NzbDrone.Test.Common.AutoMoq; -using PetaPoco; namespace NzbDrone.Core.Test.ProviderTests { diff --git a/NzbDrone.Core.Test/ProviderTests/RootDirProviderTest.cs b/NzbDrone.Core.Test/ProviderTests/RootDirProviderTest.cs index ffd1f387a..a8ae8a867 100644 --- a/NzbDrone.Core.Test/ProviderTests/RootDirProviderTest.cs +++ b/NzbDrone.Core.Test/ProviderTests/RootDirProviderTest.cs @@ -1,6 +1,7 @@ // ReSharper disable RedundantUsingDirective using System; +using System.IO; using System.Linq; using FluentAssertions; @@ -19,120 +20,107 @@ namespace NzbDrone.Core.Test.ProviderTests // ReSharper disable InconsistentNaming public class RootDirProviderTest : CoreTest { + [SetUp] + public void Setup() + { + Mocker.GetMock() + .Setup(m => m.FolderExists(It.IsAny())) + .Returns(true); + } + + private void WithNoneExistingFolder() + { + Mocker.GetMock() + .Setup(m => m.FolderExists(It.IsAny())) + .Returns(false); + } + [Test] - public void GetRootDirs() + public void GetRootDir_should_return_all_existing_roots() { - //Setup - + WithRealDb(); - var emptyDatabase = TestDbHelper.GetEmptyDatabase(); - Mocker.SetConstant(emptyDatabase); - emptyDatabase.Insert(new RootDir { Path = @"C:\TV" }); - emptyDatabase.Insert(new RootDir { Path = @"C:\TV2" }); + Db.Insert(new RootDir { Path = @"C:\TV" }); + Db.Insert(new RootDir { Path = @"C:\TV2" }); - //Mocker.GetMock() - // .Setup(f => f.All()) - // .Returns(sonicRepo.All); - - //Act var result = Mocker.Resolve().GetAll(); - - //Assert - Assert.AreEqual(result.Count, 2); + result.Should().HaveCount(2); } + [TestCase("D:\\TV Shows\\")] [TestCase("//server//folder")] - public void AddRootDir(string path) + public void should_be_able_to_add_root_dir(string path) { - //Setup - - Mocker.SetConstant(TestDbHelper.GetEmptyDatabase()); + WithRealDb(); //Act var rootDirProvider = Mocker.Resolve(); rootDirProvider.Add(new RootDir { Path = path }); - //Assert var rootDirs = rootDirProvider.GetAll(); - rootDirs.Should().NotBeEmpty(); rootDirs.Should().HaveCount(1); - path.Should().Be(rootDirs.First().Path); + rootDirs.First().Path.Should().Be(path); + } + + [Test] + public void should_throw_if_folder_being_added_doesnt_exist() + { + WithNoneExistingFolder(); + + var rootDirProvider = Mocker.Resolve(); + Assert.Throws(() => rootDirProvider.Add(new RootDir { Path = "C:\\TEST" })); } [Test] - public void RemoveRootDir() + public void should_be_able_to_remove_root_dir() { - //Setup - - Mocker.SetConstant(TestDbHelper.GetEmptyDatabase()); + WithRealDb(); //Act var rootDirProvider = Mocker.Resolve(); rootDirProvider.Add(new RootDir { Path = @"C:\TV" }); + rootDirProvider.Add(new RootDir { Path = @"C:\TV2" }); rootDirProvider.Remove(1); //Assert var rootDirs = rootDirProvider.GetAll(); - rootDirs.Should().BeEmpty(); + rootDirs.Should().HaveCount(1); } - [Test] - public void GetRootDir() - { - //Setup - - Mocker.SetConstant(TestDbHelper.GetEmptyDatabase()); - const int id = 1; - const string path = @"C:\TV"; - - //Act - var rootDirProvider = Mocker.Resolve(); - rootDirProvider.Add(new RootDir { Id = id, Path = path }); - - //Assert - var rootDir = rootDirProvider.GetRootDir(id); - rootDir.Id.Should().Be(1); - rootDir.Path.Should().Be(path); - } [Test] public void None_existing_folder_returns_empty_list() { - const string path = "d:\\bad folder"; + WithNoneExistingFolder(); - - Mocker.GetMock(MockBehavior.Strict) - .Setup(m => m.FolderExists(path)).Returns(false); + const string path = "d:\\bad folder"; var result = Mocker.Resolve().GetUnmappedFolders(path); result.Should().NotBeNull(); result.Should().BeEmpty(); - - Mocker.VerifyAllMocks(); + Mocker.GetMock().Verify(c => c.GetDirectories(It.IsAny()), Times.Never()); } [Test] - [ExpectedException(typeof(ArgumentException))] - public void empty_folder_path_throws() + public void GetUnmappedFolders_throw_on_empty_folders() { - - Mocker.Resolve().GetUnmappedFolders(""); + Assert.Throws(() => Mocker.Resolve().GetUnmappedFolders("")); } [TestCase("")] [TestCase(null)] [TestCase("BAD PATH")] - [ExpectedException(typeof(ArgumentException))] public void invalid_folder_path_throws_on_add(string path) { - - Mocker.Resolve().Add(new RootDir { Id = 0, Path = path }); + Assert.Throws(() => + Mocker.Resolve().Add(new RootDir { Id = 0, Path = path }) + ); } } diff --git a/NzbDrone.Core/Providers/RootDirProvider.cs b/NzbDrone.Core/Providers/RootDirProvider.cs index ed3a9be47..2b07f459b 100644 --- a/NzbDrone.Core/Providers/RootDirProvider.cs +++ b/NzbDrone.Core/Providers/RootDirProvider.cs @@ -32,10 +32,10 @@ namespace NzbDrone.Core.Providers return _database.Fetch(); } - public virtual int Add(RootDir rootDir) + public virtual void Add(RootDir rootDir) { ValidatePath(rootDir); - return Convert.ToInt32(_database.Insert(rootDir)); + _database.Insert(rootDir); } public virtual void Remove(int rootDirId) @@ -43,17 +43,17 @@ namespace NzbDrone.Core.Providers _database.Delete(rootDirId); } - private static void ValidatePath(RootDir rootDir) + private void ValidatePath(RootDir rootDir) { if (String.IsNullOrWhiteSpace(rootDir.Path) || !Path.IsPathRooted(rootDir.Path)) { throw new ArgumentException("Invalid path"); } - } - public virtual RootDir GetRootDir(int rootDirId) - { - return _database.SingleOrDefault(rootDirId); + if (!_diskProvider.FolderExists(rootDir.Path)) + { + throw new DirectoryNotFoundException("Can't add root directory that doesn't exist."); + } } public List GetUnmappedFolders(string path)