added tests for basic config set/get to db

This commit is contained in:
kay.one 2010-09-23 23:16:43 -07:00
parent 1620721efe
commit 772452aa8b
13 changed files with 5874 additions and 13 deletions

View File

@ -0,0 +1,59 @@
using System;
using System.Collections.Generic;
using System.Text;
using Gallio.Framework;
using log4net;
using MbUnit.Framework;
using MbUnit.Framework.ContractVerifiers;
using Moq;
using NzbDrone.Core.Controllers;
using NzbDrone.Core.Repository;
using SubSonic.Repository;
namespace NzbDrone.Core.Test
{
[TestFixture]
public class DbConfigControllerTest
{
[Test]
public void Overwrite_existing_value()
{
String key = "MY_KEY";
String value = "MY_VALUE";
//setup
var repo = new Mock<IRepository>();
var config = new Config() { Key = key, Value = value };
repo.Setup(r => r.Single<Config>(key)).Returns(config);
var target = new DbConfigController(new Mock<ILog>().Object, repo.Object);
//Act
target.SetValue(key, value);
//Assert
repo.Verify(c => c.Update(config));
repo.Verify(c => c.Add(It.IsAny<Config>()), Times.Never());
}
[Test]
public void Add_new_value()
{
String key = "MY_KEY";
String value = "MY_VALUE";
//setup
var repo = new Mock<IRepository>();
var config = new Config() { Key = key, Value = value };
repo.Setup(r => r.Single<Config>(It.IsAny<string>())).Returns<Config>(null).Verifiable();
var target = new DbConfigController(new Mock<ILog>().Object, repo.Object);
//Act
target.SetValue(key, value);
//Assert
repo.Verify();
repo.Verify(r => r.Update(It.IsAny<Config>()), Times.Never());
repo.Verify(r => r.Add(It.Is<Config>(c => c.Key == key && c.Value == value)), Times.Once());
}
}
}

Binary file not shown.

File diff suppressed because it is too large Load Diff

View File

@ -32,8 +32,20 @@
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<Reference Include="Gallio, Version=3.2.0.0, Culture=neutral, PublicKeyToken=eb9cfa67ee6ab36e, processorArchitecture=MSIL" /> <Reference Include="Gallio, Version=3.2.0.0, Culture=neutral, PublicKeyToken=eb9cfa67ee6ab36e, processorArchitecture=MSIL" />
<Reference Include="log4net, Version=1.2.10.0, Culture=neutral, PublicKeyToken=1b44e1d426115821, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\NzbDrone.Core\Libraries\log4net.dll</HintPath>
</Reference>
<Reference Include="MbUnit, Version=3.2.0.0, Culture=neutral, PublicKeyToken=eb9cfa67ee6ab36e, processorArchitecture=MSIL" /> <Reference Include="MbUnit, Version=3.2.0.0, Culture=neutral, PublicKeyToken=eb9cfa67ee6ab36e, processorArchitecture=MSIL" />
<Reference Include="MbUnit35, Version=3.2.0.0, Culture=neutral, PublicKeyToken=eb9cfa67ee6ab36e, processorArchitecture=MSIL" /> <Reference Include="MbUnit35, Version=3.2.0.0, Culture=neutral, PublicKeyToken=eb9cfa67ee6ab36e, processorArchitecture=MSIL" />
<Reference Include="Moq, Version=4.0.10827.0, Culture=neutral, PublicKeyToken=69f491c39445e920, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>Moq\Moq.dll</HintPath>
</Reference>
<Reference Include="SubSonic.Core, Version=3.0.0.3, Culture=neutral, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\NzbDrone.Core\Libraries\SubSonic.Core.dll</HintPath>
</Reference>
<Reference Include="System" /> <Reference Include="System" />
<Reference Include="System.Core" /> <Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" /> <Reference Include="System.Xml.Linq" />
@ -46,6 +58,7 @@
</Reference> </Reference>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Compile Include="DbConfigControllerTest.cs" />
<Compile Include="Properties\AssemblyInfo.cs" /> <Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="TvDbControllerTest.cs" /> <Compile Include="TvDbControllerTest.cs" />
</ItemGroup> </ItemGroup>
@ -55,6 +68,10 @@
<Name>NzbDrone.Core</Name> <Name>NzbDrone.Core</Name>
</ProjectReference> </ProjectReference>
</ItemGroup> </ItemGroup>
<ItemGroup>
<Content Include="Moq\Moq.dll" />
<Content Include="Moq\Moq.xml" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" /> <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it. <!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets. Other similar extension points exist, see Microsoft.Common.targets.

View File

@ -10,15 +10,14 @@ namespace NzbDrone.Core.Controllers
public class DbConfigController : IConfigController public class DbConfigController : IConfigController
{ {
private const string _seriesroots = "SeriesRoots"; private const string _seriesroots = "SeriesRoots";
private readonly IDiskController _diskController;
private readonly ILog _logger; private readonly ILog _logger;
private readonly IRepository _sonicRepo; private readonly IRepository _sonicRepo;
public DbConfigController(ILog logger, IDiskController diskController, IRepository dataRepository) public DbConfigController(ILog logger, IRepository dataRepository)
{ {
_logger = logger; _logger = logger;
_diskController = diskController;
_sonicRepo = dataRepository; _sonicRepo = dataRepository;
} }
@ -43,7 +42,7 @@ namespace NzbDrone.Core.Controllers
} }
private string GetValue(string key, object defaultValue, bool makePermanent) public string GetValue(string key, object defaultValue, bool makePermanent)
{ {
string value; string value;
@ -66,14 +65,24 @@ namespace NzbDrone.Core.Controllers
return value; return value;
} }
private void SetValue(string key, string value) public void SetValue(string key, string value)
{ {
if (String.IsNullOrEmpty(key)) throw new ArgumentOutOfRangeException("key"); if (String.IsNullOrEmpty(key)) throw new ArgumentOutOfRangeException("key");
if (value == null) throw new ArgumentNullException("key"); if (value == null) throw new ArgumentNullException("key");
_logger.DebugFormat("Writing Setting to file. Key:'{0}' Value:'{1}'", key, value); _logger.DebugFormat("Writing Setting to file. Key:'{0}' Value:'{1}'", key, value);
var dbValue = _sonicRepo.Single<Config>(key);
if (dbValue == null)
{
_sonicRepo.Add(new Config { Key = key, Value = value }); _sonicRepo.Add(new Config { Key = key, Value = value });
} }
else
{
dbValue.Value = value;
_sonicRepo.Update(dbValue);
}
}
} }
} }

View File

@ -11,5 +11,8 @@ namespace NzbDrone.Core.Controllers
set; set;
} }
string GetValue(string key, object defaultValue, bool makePermanent);
void SetValue(string key, string value);
} }
} }

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -21,7 +21,7 @@ namespace NzbDrone.Web.Controllers
public ActionResult Index() public ActionResult Index()
{ {
return View(new SettingsModel() { RootPath = _configController.SeriesRoot }); return View(new SettingsModel() { TvFolder = _configController.SeriesRoot });
} }
[HttpPost] [HttpPost]
@ -29,7 +29,7 @@ namespace NzbDrone.Web.Controllers
{ {
if (ModelState.IsValid) if (ModelState.IsValid)
{ {
_configController.SeriesRoot = model.RootPath; _configController.SeriesRoot = model.TvFolder;
} }
return RedirectToAction("index"); return RedirectToAction("index");

View File

@ -13,7 +13,11 @@ namespace NzbDrone.Web.Models
public class SettingsModel public class SettingsModel
{ {
public String RootPath
[Required]
[DataType(DataType.Text)]
[DisplayName("TV Folder")]
public String TvFolder
{ {
get; get;
set; set;

View File

@ -9,15 +9,16 @@
Settings</h2> Settings</h2>
<% using (Html.BeginForm()) <% using (Html.BeginForm())
{ %> { %>
<%: Html.ValidationSummary(true, "Account creation was unsuccessful. Please correct the errors and try again.") %> <%: Html.ValidationSummary(true, "Unable to save you settings. Please correct the errors and try again.") %>
<div> <div>
<fieldset> <fieldset>
<legend>General</legend> <legend>General</legend>
<div class="editor-label"> <div class="editor-label">
<%: Html.LabelFor(m => m.RootPath) %> <%: Html.LabelFor(m => m.TvFolder) %>
</div> </div>
<div class="editor-field"> <div class="editor-field">
<%: Html.TextBoxFor(m => m.RootPath) %> <%: Html.TextBoxFor(m => m.TvFolder) %>
<%: Html.ValidationMessageFor(m => m.TvFolder) %>
</div> </div>
<p> <p>
<input type="submit" value="Save" /> <input type="submit" value="Save" />