Cleaned up ConfigProvider. Added reflection based test for all properties in ConfigProvider.

This commit is contained in:
kay.one 2011-10-23 13:35:16 -07:00
parent aa418a444b
commit c23e736cc6
3 changed files with 62 additions and 72 deletions

View File

@ -1,9 +1,14 @@
using AutoMoq;
using System;
using System.Linq;
using System.Reflection;
using AutoMoq;
using FluentAssertions;
using Moq;
using NUnit.Framework;
using NzbDrone.Core.Providers.Core;
using NzbDrone.Core.Repository;
using NzbDrone.Core.Test.Framework;
using PetaPoco;
namespace NzbDrone.Core.Test.ProviderTests
{
@ -110,5 +115,54 @@ namespace NzbDrone.Core.Test.ProviderTests
db.Fetch<Config>().Should().HaveCount(1);
}
[Test]
[Description("This test will use reflection to ensure each config property read/writes to a unique key")]
public void config_properties_should_write_and_read_using_same_key()
{
var mocker = new AutoMoqer(MockBehavior.Strict);
var db = MockLib.GetEmptyDatabase();
mocker.SetConstant(db);
var configProvider = mocker.Resolve<ConfigProvider>();
var allProperties = typeof(ConfigProvider).GetProperties();
//Act
foreach (var propertyInfo in allProperties)
{
object value = null;
if (propertyInfo.PropertyType == typeof(string))
{
value = new Guid().ToString();
}
else if (propertyInfo.PropertyType == typeof(int))
{
value = DateTime.Now.Millisecond;
}
else if (propertyInfo.PropertyType == typeof(bool))
{
value = true;
}
else if (propertyInfo.PropertyType.BaseType == typeof(Enum))
{
value = 0;
}
propertyInfo.GetSetMethod().Invoke(configProvider, new[] { value });
var returnValue = propertyInfo.GetGetMethod().Invoke(configProvider, null);
if (propertyInfo.PropertyType.BaseType == typeof(Enum))
{
returnValue = (int)returnValue;
}
returnValue.Should().Be(value, propertyInfo.Name);
}
db.Fetch<Config>().Should()
.HaveSameCount(allProperties, "two different properties are writing to the same key in db. Copy/Past fail.");
}
}
}

View File

@ -20,34 +20,13 @@ namespace NzbDrone.Core.Providers.Core
_database = database;
}
public IList<Config> All()
{
return _database.Fetch<Config>();
}
public ConfigProvider()
{
}
public virtual String ApiKey
public IList<Config> All()
{
get { return GetValue("ApiKey"); }
set { SetValue("ApiKey", value); }
}
public virtual String EpisodeNameFormat
{
get { return GetValue("EpisodeNameFormat"); }
set { SetValue("EpisodeNameFormat", value); }
}
public virtual String SeriesRoot
{
get { return GetValue("SeriesRoots"); }
set { SetValue("SeriesRoots", value); }
return _database.Fetch<Config>();
}
public virtual String NzbMatrixUsername
@ -106,20 +85,6 @@ namespace NzbDrone.Core.Providers.Core
set { SetValue("NewzbinPassword", value); }
}
public virtual int SyncFrequency
{
get { return GetValueInt("SyncFrequency"); }
set { SetValue("SyncFrequency", value); }
}
public virtual Boolean DownloadPropers
{
get { return GetValueBoolean("DownloadPropers"); }
set { SetValue("DownloadPropers", value); }
}
public virtual String SabHost
{
get { return GetValue("SabHost", "localhost"); }
@ -176,20 +141,6 @@ namespace NzbDrone.Core.Providers.Core
set { SetValue("SabTvDropDirectory", value); }
}
public virtual Boolean UseBlackhole
{
get { return GetValueBoolean("UseBlackhole"); }
set { SetValue("UseBlackhole", value); }
}
public virtual String BlackholeDirectory
{
get { return GetValue("BlackholeDirectory"); }
set { SetValue("BlackholeDirectory", value); }
}
public virtual bool SortingIncludeSeriesName
{
get { return GetValueBoolean("Sorting_SeriesName", true); }
@ -252,13 +203,6 @@ namespace NzbDrone.Core.Providers.Core
set { SetValue("DefaultQualityProfile", value); }
}
public virtual Boolean XbmcEnabled
{
get { return GetValueBoolean("XbmcEnabled"); }
set { SetValue("XbmcEnabled", value); }
}
public virtual Boolean XbmcNotifyOnGrab
{
get { return GetValueBoolean("XbmcNotifyOnGrab"); }
@ -328,30 +272,26 @@ namespace NzbDrone.Core.Providers.Core
public virtual string GetValue(string key, object defaultValue)
{
string value;
var dbValue = _database.SingleOrDefault<Config>("WHERE [Key] =@0", key);
if (dbValue != null && !String.IsNullOrEmpty(dbValue.Value))
return dbValue.Value;
Logger.Trace("Unable to find config key '{0}' defaultValue:'{1}'", key, defaultValue);
value = defaultValue.ToString();
return value;
return defaultValue.ToString();
}
public virtual void SetValue(string key, Boolean value)
private void SetValue(string key, Boolean value)
{
SetValue(key, value.ToString());
}
public virtual void SetValue(string key, int value)
private void SetValue(string key, int value)
{
SetValue(key, value.ToString());
}
public virtual void SetValue(string key, string value)
public void SetValue(string key, string value)
{
if (String.IsNullOrEmpty(key))
throw new ArgumentOutOfRangeException("key");

View File

@ -1,8 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using AutoMoq;
using AutoMoq;
using FizzWare.NBuilder;
using Moq;
using NUnit.Framework;