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
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<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]
public void moveFile_should_overwrite_existing_file()
{

View File

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

View File

@ -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
{

View File

@ -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<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]
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<IRepository>()
// .Setup(f => f.All<RootDir>())
// .Returns(sonicRepo.All<RootDir>);
//Act
var result = Mocker.Resolve<RootDirProvider>().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>();
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<RootDirProvider>();
Assert.Throws<DirectoryNotFoundException>(() => 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>();
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>();
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<DiskProvider>(MockBehavior.Strict)
.Setup(m => m.FolderExists(path)).Returns(false);
const string path = "d:\\bad folder";
var result = Mocker.Resolve<RootDirProvider>().GetUnmappedFolders(path);
result.Should().NotBeNull();
result.Should().BeEmpty();
Mocker.VerifyAllMocks();
Mocker.GetMock<DiskProvider>().Verify(c => c.GetDirectories(It.IsAny<String>()), Times.Never());
}
[Test]
[ExpectedException(typeof(ArgumentException))]
public void empty_folder_path_throws()
public void GetUnmappedFolders_throw_on_empty_folders()
{
Mocker.Resolve<RootDirProvider>().GetUnmappedFolders("");
Assert.Throws<ArgumentException>(() => Mocker.Resolve<RootDirProvider>().GetUnmappedFolders(""));
}
[TestCase("")]
[TestCase(null)]
[TestCase("BAD PATH")]
[ExpectedException(typeof(ArgumentException))]
public void invalid_folder_path_throws_on_add(string path)
{
Mocker.Resolve<RootDirProvider>().Add(new RootDir { Id = 0, Path = path });
Assert.Throws<ArgumentException>(() =>
Mocker.Resolve<RootDirProvider>().Add(new RootDir { Id = 0, Path = path })
);
}
}

View File

@ -32,10 +32,10 @@ namespace NzbDrone.Core.Providers
return _database.Fetch<RootDir>();
}
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<RootDir>(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<RootDir>(rootDirId);
if (!_diskProvider.FolderExists(rootDir.Path))
{
throw new DirectoryNotFoundException("Can't add root directory that doesn't exist.");
}
}
public List<String> GetUnmappedFolders(string path)