ConfigServiceFixture shouldn't be touching the DB.

pull/6/head
Keivan Beigi 9 years ago
parent 04da2d845a
commit 7e023a7944

@ -1,110 +1,59 @@
using System; using System;
using System.Collections.Generic;
using System.Linq; using System.Linq;
using FluentAssertions; using FluentAssertions;
using Moq;
using NUnit.Framework; using NUnit.Framework;
using NzbDrone.Core.Configuration; using NzbDrone.Core.Configuration;
using NzbDrone.Core.Test.Framework; using NzbDrone.Test.Common;
namespace NzbDrone.Core.Test.Configuration namespace NzbDrone.Core.Test.Configuration
{ {
[TestFixture] [TestFixture]
public class ConfigServiceFixture : DbTest<ConfigService, Config> public class ConfigServiceFixture : TestBase<ConfigService>
{ {
[SetUp] [SetUp]
public void SetUp() public void SetUp()
{ {
Mocker.SetConstant<IConfigRepository>(Mocker.Resolve<ConfigRepository>());
Db.All<Config>().ForEach(Db.Delete);
} }
[Test] [Test]
public void Add_new_value_to_database() public void Add_new_value_to_database()
{ {
const string key = "MY_KEY"; const string key = "RssSyncInterval";
const string value = "MY_VALUE"; const int value = 12;
Subject.SetValue(key, value);
Subject.GetValue(key, "").Should().Be(value);
}
[Test]
public void Get_value_from_database()
{
const string key = "MY_KEY";
const string value = "MY_VALUE";
Db.Insert(new Config { Key = key, Value = value });
Db.Insert(new Config { Key = "Other Key", Value = "OtherValue" });
var result = Subject.GetValue(key, ""); Subject.RssSyncInterval = value;
result.Should().Be(value); AssertUpsert(key, value);
} }
[Test] [Test]
public void Get_value_should_return_default_when_no_value() public void Get_value_should_return_default_when_no_value()
{ {
const string key = "MY_KEY"; Subject.RssSyncInterval.Should().Be(15);
const string value = "MY_VALUE";
var result = Subject.GetValue(key, value);
result.Should().Be(value);
}
[Test]
public void New_value_should_update_old_value_new_value()
{
const string key = "MY_KEY";
const string originalValue = "OLD_VALUE";
const string newValue = "NEW_VALUE";
Db.Insert(new Config { Key = key, Value = originalValue });
Subject.SetValue(key, newValue);
var result = Subject.GetValue(key, "");
result.Should().Be(newValue);
AllStoredModels.Should().HaveCount(1);
}
[Test]
public void New_value_should_update_old_value_same_value()
{
const string key = "MY_KEY";
const string value = "OLD_VALUE";
Subject.SetValue(key, value);
Subject.SetValue(key, value);
var result = Subject.GetValue(key, "");
result.Should().Be(value);
AllStoredModels.Should().HaveCount(1);
} }
[Test] [Test]
public void get_value_with_persist_should_store_default_value() public void get_value_with_persist_should_store_default_value()
{ {
const string key = "MY_KEY"; var salt = Subject.HmacSalt;
string value = Guid.NewGuid().ToString(); salt.Should().NotBeNullOrWhiteSpace();
AssertUpsert("HmacSalt", salt);
Subject.GetValue(key, value, persist: true).Should().Be(value);
Subject.GetValue(key, string.Empty).Should().Be(value);
} }
[Test] [Test]
public void get_value_with_out_persist_should_not_store_default_value() public void get_value_with_out_persist_should_not_store_default_value()
{ {
const string key = "MY_KEY"; var interval = Subject.RssSyncInterval;
string value1 = Guid.NewGuid().ToString(); interval.Should().Be(15);
string value2 = Guid.NewGuid().ToString(); Mocker.GetMock<IConfigRepository>().Verify(c => c.Insert(It.IsAny<Config>()), Times.Never());
}
Subject.GetValue(key, value1).Should().Be(value1); private void AssertUpsert(string key, object value)
Subject.GetValue(key, value2).Should().Be(value2); {
Mocker.GetMock<IConfigRepository>().Verify(c => c.Upsert(It.Is<Config>(v => v.Key == key.ToLowerInvariant() && v.Value == value.ToString())));
} }
[Test] [Test]
@ -114,6 +63,17 @@ namespace NzbDrone.Core.Test.Configuration
var configProvider = Subject; var configProvider = Subject;
var allProperties = typeof(ConfigService).GetProperties().Where(p => p.GetSetMethod() != null).ToList(); var allProperties = typeof(ConfigService).GetProperties().Where(p => p.GetSetMethod() != null).ToList();
var keys = new List<string>();
var values = new List<Config>();
Mocker.GetMock<IConfigRepository>().Setup(c => c.Upsert(It.IsAny<Config>())).Callback<Config>(config =>
{
keys.Add(config.Key);
values.Add(config);
});
Mocker.GetMock<IConfigRepository>().Setup(c => c.All()).Returns(values);
foreach (var propertyInfo in allProperties) foreach (var propertyInfo in allProperties)
@ -148,8 +108,7 @@ namespace NzbDrone.Core.Test.Configuration
returnValue.Should().Be(value, propertyInfo.Name); returnValue.Should().Be(value, propertyInfo.Name);
} }
AllStoredModels.Should() keys.Should().OnlyHaveUniqueItems();
.HaveSameCount(allProperties, "two different properties are writing to the same key in db. Copy/Past fail.");
} }
} }
} }

@ -30,12 +30,7 @@ namespace NzbDrone.Core.Configuration
_cache = new Dictionary<string, string>(); _cache = new Dictionary<string, string>();
} }
public IEnumerable<Config> All() private Dictionary<string, object> AllWithDefaults()
{
return _repository.All();
}
public Dictionary<string, object> AllWithDefaults()
{ {
var dict = new Dictionary<string, object>(StringComparer.InvariantCultureIgnoreCase); var dict = new Dictionary<string, object>(StringComparer.InvariantCultureIgnoreCase);
@ -45,7 +40,6 @@ namespace NzbDrone.Core.Configuration
foreach (var propertyInfo in properties) foreach (var propertyInfo in properties)
{ {
var value = propertyInfo.GetValue(this, null); var value = propertyInfo.GetValue(this, null);
dict.Add(propertyInfo.Name, value); dict.Add(propertyInfo.Name, value);
} }
@ -65,7 +59,9 @@ namespace NzbDrone.Core.Configuration
var equal = configValue.Value.ToString().Equals(currentValue.ToString()); var equal = configValue.Value.ToString().Equals(currentValue.ToString());
if (!equal) if (!equal)
{
SetValue(configValue.Key, configValue.Value.ToString()); SetValue(configValue.Key, configValue.Value.ToString());
}
} }
_eventAggregator.PublishEvent(new ConfigSavedEvent()); _eventAggregator.PublishEvent(new ConfigSavedEvent());
@ -331,7 +327,7 @@ namespace NzbDrone.Core.Configuration
return Convert.ToInt32(GetValue(key, defaultValue)); return Convert.ToInt32(GetValue(key, defaultValue));
} }
public T GetValueEnum<T>(string key, T defaultValue) private T GetValueEnum<T>(string key, T defaultValue)
{ {
return (T)Enum.Parse(typeof(T), GetValue(key, defaultValue), true); return (T)Enum.Parse(typeof(T), GetValue(key, defaultValue), true);
} }
@ -346,7 +342,9 @@ namespace NzbDrone.Core.Configuration
string dbValue; string dbValue;
if (_cache.TryGetValue(key, out dbValue) && dbValue != null && !string.IsNullOrEmpty(dbValue)) if (_cache.TryGetValue(key, out dbValue) && dbValue != null && !string.IsNullOrEmpty(dbValue))
{
return dbValue; return dbValue;
}
_logger.Trace("Using default config value for '{0}' defaultValue:'{1}'", key, defaultValue); _logger.Trace("Using default config value for '{0}' defaultValue:'{1}'", key, defaultValue);
@ -354,6 +352,7 @@ namespace NzbDrone.Core.Configuration
{ {
SetValue(key, defaultValue.ToString()); SetValue(key, defaultValue.ToString());
} }
return defaultValue.ToString(); return defaultValue.ToString();
} }
@ -367,44 +366,34 @@ namespace NzbDrone.Core.Configuration
SetValue(key, value.ToString()); SetValue(key, value.ToString());
} }
public void SetValue(string key, string value) private void SetValue(string key, Enum value)
{
SetValue(key, value.ToString().ToLower());
}
private void SetValue(string key, string value)
{ {
key = key.ToLowerInvariant(); key = key.ToLowerInvariant();
_logger.Trace("Writing Setting to database. Key:'{0}' Value:'{1}'", key, value); _logger.Trace("Writing Setting to database. Key:'{0}' Value:'{1}'", key, value);
_repository.Upsert(new Config {Key = key, Value = value});
var dbValue = _repository.Get(key);
if (dbValue == null)
{
_repository.Insert(new Config { Key = key, Value = value });
}
else
{
dbValue.Value = value;
_repository.Update(dbValue);
}
ClearCache(); ClearCache();
} }
public void SetValue(string key, Enum value)
{
SetValue(key, value.ToString().ToLower());
}
private void EnsureCache() private void EnsureCache()
{ {
lock (_cache) lock (_cache)
{ {
if (!_cache.Any()) if (!_cache.Any())
{ {
_cache = All().ToDictionary(c => c.Key.ToLower(), c => c.Value); var all = _repository.All();
_cache = all.ToDictionary(c => c.Key.ToLower(), c => c.Value);
} }
} }
} }
public static void ClearCache() private static void ClearCache()
{ {
lock (_cache) lock (_cache)
{ {

@ -6,8 +6,6 @@ namespace NzbDrone.Core.Configuration
{ {
public interface IConfigService public interface IConfigService
{ {
IEnumerable<Config> All();
Dictionary<string, object> AllWithDefaults();
void SaveConfigDictionary(Dictionary<string, object> configValues); void SaveConfigDictionary(Dictionary<string, object> configValues);
bool IsDefined(string key); bool IsDefined(string key);

Loading…
Cancel
Save