From c23e736cc6b9b7418fb18511a1aba19461913cd4 Mon Sep 17 00:00:00 2001 From: "kay.one" Date: Sun, 23 Oct 2011 13:35:16 -0700 Subject: [PATCH] Cleaned up ConfigProvider. Added reflection based test for all properties in ConfigProvider. --- .../ProviderTests/ConfigProviderTest.cs | 56 ++++++++++++++- .../Providers/Core/ConfigProvider.cs | 72 ++----------------- .../UpdateProviderStartTest.cs | 6 +- 3 files changed, 62 insertions(+), 72 deletions(-) diff --git a/NzbDrone.Core.Test/ProviderTests/ConfigProviderTest.cs b/NzbDrone.Core.Test/ProviderTests/ConfigProviderTest.cs index 3200202c4..6c4d488b9 100644 --- a/NzbDrone.Core.Test/ProviderTests/ConfigProviderTest.cs +++ b/NzbDrone.Core.Test/ProviderTests/ConfigProviderTest.cs @@ -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().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(); + 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().Should() + .HaveSameCount(allProperties, "two different properties are writing to the same key in db. Copy/Past fail."); + } } } \ No newline at end of file diff --git a/NzbDrone.Core/Providers/Core/ConfigProvider.cs b/NzbDrone.Core/Providers/Core/ConfigProvider.cs index b86bdb34b..00b4f8bf9 100644 --- a/NzbDrone.Core/Providers/Core/ConfigProvider.cs +++ b/NzbDrone.Core/Providers/Core/ConfigProvider.cs @@ -20,34 +20,13 @@ namespace NzbDrone.Core.Providers.Core _database = database; } - public IList All() - { - return _database.Fetch(); - } - public ConfigProvider() { } - public virtual String ApiKey + public IList 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(); } 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("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"); diff --git a/NzbDrone.Update.Test/UpdateProviderStartTest.cs b/NzbDrone.Update.Test/UpdateProviderStartTest.cs index 68cb95fe5..46c37a111 100644 --- a/NzbDrone.Update.Test/UpdateProviderStartTest.cs +++ b/NzbDrone.Update.Test/UpdateProviderStartTest.cs @@ -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;