You can no longer add root folders that don't already exist.

This commit is contained in:
kay.one 2012-01-18 19:57:27 -08:00
parent 9406ca9cf5
commit 7c6d745c86
5 changed files with 74 additions and 73 deletions

View File

@ -1,5 +1,5 @@
// ReSharper disable InconsistentNaming // ReSharper disable InconsistentNaming
using System;
using System.IO; using System.IO;
using System.Linq; using System.Linq;
using FluentAssertions; using FluentAssertions;
@ -9,7 +9,7 @@ using NzbDrone.Test.Common;
namespace NzbDrone.Common.Test namespace NzbDrone.Common.Test
{ {
[TestFixture] [TestFixture]
public class DiskProviderTests : TestBase public class DiskProviderFixture : TestBase
{ {
DirectoryInfo BinFolder; DirectoryInfo BinFolder;
DirectoryInfo BinFolderCopy; DirectoryInfo BinFolderCopy;
@ -34,6 +34,24 @@ namespace NzbDrone.Common.Test
} }
} }
[Test]
public void directory_exist_should_be_able_to_find_existing_folder()
{
Mocker.Resolve<DiskProvider>().FolderExists(TempFolder).Should().BeTrue();
}
[Test]
public void directory_exist_should_be_able_to_find_existing_unc_share()
{
Mocker.Resolve<DiskProvider>().FolderExists(@"\\localhost\c$").Should().BeTrue();
}
[Test]
public void directory_exist_should_not_be_able_to_find_none_existing_folder()
{
Mocker.Resolve<DiskProvider>().FolderExists(@"C:\ThisBetterNotExist\").Should().BeFalse();
}
[Test] [Test]
public void moveFile_should_overwrite_existing_file() public void moveFile_should_overwrite_existing_file()
{ {

View File

@ -65,7 +65,7 @@
<ItemGroup> <ItemGroup>
<Compile Include="ConfigFileProviderTest.cs" /> <Compile Include="ConfigFileProviderTest.cs" />
<Compile Include="PathExtentionFixture.cs" /> <Compile Include="PathExtentionFixture.cs" />
<Compile Include="DiskProviderTests.cs" /> <Compile Include="DiskProviderFixture.cs" />
<Compile Include="EnviromentProviderTest.cs" /> <Compile Include="EnviromentProviderTest.cs" />
<Compile Include="ProcessProviderTests.cs" /> <Compile Include="ProcessProviderTests.cs" />
<Compile Include="ServiceProviderTests.cs" /> <Compile Include="ServiceProviderTests.cs" />

View File

@ -1,15 +1,10 @@
using System; using System;
using System.Linq; using System.Linq;
using System.Reflection;
using FluentAssertions; using FluentAssertions;
using Moq;
using NUnit.Framework; using NUnit.Framework;
using NzbDrone.Core.Providers.Core; using NzbDrone.Core.Providers.Core;
using NzbDrone.Core.Repository; using NzbDrone.Core.Repository;
using NzbDrone.Core.Test.Framework; using NzbDrone.Core.Test.Framework;
using NzbDrone.Test.Common.AutoMoq;
using PetaPoco;
namespace NzbDrone.Core.Test.ProviderTests namespace NzbDrone.Core.Test.ProviderTests
{ {

View File

@ -1,6 +1,7 @@
// ReSharper disable RedundantUsingDirective // ReSharper disable RedundantUsingDirective
using System; using System;
using System.IO;
using System.Linq; using System.Linq;
using FluentAssertions; using FluentAssertions;
@ -19,120 +20,107 @@ namespace NzbDrone.Core.Test.ProviderTests
// ReSharper disable InconsistentNaming // ReSharper disable InconsistentNaming
public class RootDirProviderTest : CoreTest public class RootDirProviderTest : CoreTest
{ {
[SetUp]
public void Setup()
{
Mocker.GetMock<DiskProvider>()
.Setup(m => m.FolderExists(It.IsAny<string>()))
.Returns(true);
}
private void WithNoneExistingFolder()
{
Mocker.GetMock<DiskProvider>()
.Setup(m => m.FolderExists(It.IsAny<string>()))
.Returns(false);
}
[Test] [Test]
public void GetRootDirs() public void GetRootDir_should_return_all_existing_roots()
{ {
//Setup WithRealDb();
var emptyDatabase = TestDbHelper.GetEmptyDatabase(); Db.Insert(new RootDir { Path = @"C:\TV" });
Mocker.SetConstant(emptyDatabase); Db.Insert(new RootDir { Path = @"C:\TV2" });
emptyDatabase.Insert(new RootDir { Path = @"C:\TV" });
emptyDatabase.Insert(new RootDir { Path = @"C:\TV2" });
//Mocker.GetMock<IRepository>()
// .Setup(f => f.All<RootDir>())
// .Returns(sonicRepo.All<RootDir>);
//Act
var result = Mocker.Resolve<RootDirProvider>().GetAll(); var result = Mocker.Resolve<RootDirProvider>().GetAll();
result.Should().HaveCount(2);
//Assert
Assert.AreEqual(result.Count, 2);
} }
[TestCase("D:\\TV Shows\\")] [TestCase("D:\\TV Shows\\")]
[TestCase("//server//folder")] [TestCase("//server//folder")]
public void AddRootDir(string path) public void should_be_able_to_add_root_dir(string path)
{ {
//Setup WithRealDb();
Mocker.SetConstant(TestDbHelper.GetEmptyDatabase());
//Act //Act
var rootDirProvider = Mocker.Resolve<RootDirProvider>(); var rootDirProvider = Mocker.Resolve<RootDirProvider>();
rootDirProvider.Add(new RootDir { Path = path }); rootDirProvider.Add(new RootDir { Path = path });
//Assert //Assert
var rootDirs = rootDirProvider.GetAll(); var rootDirs = rootDirProvider.GetAll();
rootDirs.Should().NotBeEmpty();
rootDirs.Should().HaveCount(1); 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<RootDirProvider>();
Assert.Throws<DirectoryNotFoundException>(() => rootDirProvider.Add(new RootDir { Path = "C:\\TEST" }));
} }
[Test] [Test]
public void RemoveRootDir() public void should_be_able_to_remove_root_dir()
{ {
//Setup WithRealDb();
Mocker.SetConstant(TestDbHelper.GetEmptyDatabase());
//Act //Act
var rootDirProvider = Mocker.Resolve<RootDirProvider>(); var rootDirProvider = Mocker.Resolve<RootDirProvider>();
rootDirProvider.Add(new RootDir { Path = @"C:\TV" }); rootDirProvider.Add(new RootDir { Path = @"C:\TV" });
rootDirProvider.Add(new RootDir { Path = @"C:\TV2" });
rootDirProvider.Remove(1); rootDirProvider.Remove(1);
//Assert //Assert
var rootDirs = rootDirProvider.GetAll(); 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>();
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] [Test]
public void None_existing_folder_returns_empty_list() public void None_existing_folder_returns_empty_list()
{ {
const string path = "d:\\bad folder"; WithNoneExistingFolder();
const string path = "d:\\bad folder";
Mocker.GetMock<DiskProvider>(MockBehavior.Strict)
.Setup(m => m.FolderExists(path)).Returns(false);
var result = Mocker.Resolve<RootDirProvider>().GetUnmappedFolders(path); var result = Mocker.Resolve<RootDirProvider>().GetUnmappedFolders(path);
result.Should().NotBeNull(); result.Should().NotBeNull();
result.Should().BeEmpty(); result.Should().BeEmpty();
Mocker.GetMock<DiskProvider>().Verify(c => c.GetDirectories(It.IsAny<String>()), Times.Never());
Mocker.VerifyAllMocks();
} }
[Test] [Test]
[ExpectedException(typeof(ArgumentException))] public void GetUnmappedFolders_throw_on_empty_folders()
public void empty_folder_path_throws()
{ {
Assert.Throws<ArgumentException>(() => Mocker.Resolve<RootDirProvider>().GetUnmappedFolders(""));
Mocker.Resolve<RootDirProvider>().GetUnmappedFolders("");
} }
[TestCase("")] [TestCase("")]
[TestCase(null)] [TestCase(null)]
[TestCase("BAD PATH")] [TestCase("BAD PATH")]
[ExpectedException(typeof(ArgumentException))]
public void invalid_folder_path_throws_on_add(string path) public void invalid_folder_path_throws_on_add(string path)
{ {
Assert.Throws<ArgumentException>(() =>
Mocker.Resolve<RootDirProvider>().Add(new RootDir { Id = 0, Path = path }); Mocker.Resolve<RootDirProvider>().Add(new RootDir { Id = 0, Path = path })
);
} }
} }

View File

@ -32,10 +32,10 @@ namespace NzbDrone.Core.Providers
return _database.Fetch<RootDir>(); return _database.Fetch<RootDir>();
} }
public virtual int Add(RootDir rootDir) public virtual void Add(RootDir rootDir)
{ {
ValidatePath(rootDir); ValidatePath(rootDir);
return Convert.ToInt32(_database.Insert(rootDir)); _database.Insert(rootDir);
} }
public virtual void Remove(int rootDirId) public virtual void Remove(int rootDirId)
@ -43,17 +43,17 @@ namespace NzbDrone.Core.Providers
_database.Delete<RootDir>(rootDirId); _database.Delete<RootDir>(rootDirId);
} }
private static void ValidatePath(RootDir rootDir) private void ValidatePath(RootDir rootDir)
{ {
if (String.IsNullOrWhiteSpace(rootDir.Path) || !Path.IsPathRooted(rootDir.Path)) if (String.IsNullOrWhiteSpace(rootDir.Path) || !Path.IsPathRooted(rootDir.Path))
{ {
throw new ArgumentException("Invalid path"); throw new ArgumentException("Invalid path");
} }
}
public virtual RootDir GetRootDir(int rootDirId) if (!_diskProvider.FolderExists(rootDir.Path))
{ {
return _database.SingleOrDefault<RootDir>(rootDirId); throw new DirectoryNotFoundException("Can't add root directory that doesn't exist.");
}
} }
public List<String> GetUnmappedFolders(string path) public List<String> GetUnmappedFolders(string path)