added tests for basic config set/get to db

pull/2/head
kay.one 14 years ago
parent 1620721efe
commit 772452aa8b

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

@ -32,8 +32,20 @@
</PropertyGroup>
<ItemGroup>
<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="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.Core" />
<Reference Include="System.Xml.Linq" />
@ -46,6 +58,7 @@
</Reference>
</ItemGroup>
<ItemGroup>
<Compile Include="DbConfigControllerTest.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="TvDbControllerTest.cs" />
</ItemGroup>
@ -55,6 +68,10 @@
<Name>NzbDrone.Core</Name>
</ProjectReference>
</ItemGroup>
<ItemGroup>
<Content Include="Moq\Moq.dll" />
<Content Include="Moq\Moq.xml" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- 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.

@ -10,15 +10,14 @@ namespace NzbDrone.Core.Controllers
public class DbConfigController : IConfigController
{
private const string _seriesroots = "SeriesRoots";
private readonly IDiskController _diskController;
private readonly ILog _logger;
private readonly IRepository _sonicRepo;
public DbConfigController(ILog logger, IDiskController diskController, IRepository dataRepository)
public DbConfigController(ILog logger, IRepository dataRepository)
{
_logger = logger;
_diskController = diskController;
_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;
@ -66,14 +65,24 @@ namespace NzbDrone.Core.Controllers
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 (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);
_sonicRepo.Add(new Config { Key = key, Value = value });
var dbValue = _sonicRepo.Single<Config>(key);
if (dbValue == null)
{
_sonicRepo.Add(new Config { Key = key, Value = value });
}
else
{
dbValue.Value = value;
_sonicRepo.Update(dbValue);
}
}
}
}

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

Binary file not shown.

Binary file not shown.

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

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

@ -9,15 +9,16 @@
Settings</h2>
<% 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>
<fieldset>
<legend>General</legend>
<div class="editor-label">
<%: Html.LabelFor(m => m.RootPath) %>
<%: Html.LabelFor(m => m.TvFolder) %>
</div>
<div class="editor-field">
<%: Html.TextBoxFor(m => m.RootPath) %>
<%: Html.TextBoxFor(m => m.TvFolder) %>
<%: Html.ValidationMessageFor(m => m.TvFolder) %>
</div>
<p>
<input type="submit" value="Save" />

Loading…
Cancel
Save