2013-02-24 19:18:48 +00:00
|
|
|
|
using System;
|
2015-11-24 05:57:01 +00:00
|
|
|
|
using System.Collections.Generic;
|
2013-02-24 19:18:48 +00:00
|
|
|
|
using System.Linq;
|
|
|
|
|
using FluentAssertions;
|
2015-11-24 05:57:01 +00:00
|
|
|
|
using Moq;
|
2013-02-24 19:18:48 +00:00
|
|
|
|
using NUnit.Framework;
|
|
|
|
|
using NzbDrone.Core.Configuration;
|
2015-11-24 05:57:01 +00:00
|
|
|
|
using NzbDrone.Test.Common;
|
2013-02-24 19:18:48 +00:00
|
|
|
|
|
|
|
|
|
namespace NzbDrone.Core.Test.Configuration
|
|
|
|
|
{
|
|
|
|
|
[TestFixture]
|
2015-11-24 05:57:01 +00:00
|
|
|
|
public class ConfigServiceFixture : TestBase<ConfigService>
|
2013-02-24 19:18:48 +00:00
|
|
|
|
{
|
|
|
|
|
[SetUp]
|
|
|
|
|
public void SetUp()
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
|
public void Add_new_value_to_database()
|
|
|
|
|
{
|
2015-11-24 05:57:01 +00:00
|
|
|
|
const string key = "RssSyncInterval";
|
|
|
|
|
const int value = 12;
|
2013-02-24 19:18:48 +00:00
|
|
|
|
|
2015-11-24 05:57:01 +00:00
|
|
|
|
Subject.RssSyncInterval = value;
|
2013-02-24 19:18:48 +00:00
|
|
|
|
|
2015-11-24 05:57:01 +00:00
|
|
|
|
AssertUpsert(key, value);
|
2013-02-24 19:18:48 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
|
public void Get_value_should_return_default_when_no_value()
|
|
|
|
|
{
|
2017-01-24 20:26:51 +00:00
|
|
|
|
Subject.RssSyncInterval.Should().Be(60);
|
2013-02-24 19:18:48 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
|
public void get_value_with_persist_should_store_default_value()
|
|
|
|
|
{
|
2015-11-24 05:57:01 +00:00
|
|
|
|
var salt = Subject.HmacSalt;
|
|
|
|
|
salt.Should().NotBeNullOrWhiteSpace();
|
|
|
|
|
AssertUpsert("HmacSalt", salt);
|
2013-02-24 19:18:48 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
|
public void get_value_with_out_persist_should_not_store_default_value()
|
|
|
|
|
{
|
2015-11-24 05:57:01 +00:00
|
|
|
|
var interval = Subject.RssSyncInterval;
|
2017-01-24 20:26:51 +00:00
|
|
|
|
interval.Should().Be(60);
|
2015-11-24 05:57:01 +00:00
|
|
|
|
Mocker.GetMock<IConfigRepository>().Verify(c => c.Insert(It.IsAny<Config>()), Times.Never());
|
|
|
|
|
}
|
2013-02-24 19:18:48 +00:00
|
|
|
|
|
2015-11-24 05:57:01 +00:00
|
|
|
|
private void AssertUpsert(string key, object value)
|
|
|
|
|
{
|
2015-11-26 20:05:37 +00:00
|
|
|
|
Mocker.GetMock<IConfigRepository>().Verify(c => c.Upsert(key.ToLowerInvariant(), value.ToString()));
|
2013-02-24 19:18:48 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[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 configProvider = Subject;
|
|
|
|
|
var allProperties = typeof(ConfigService).GetProperties().Where(p => p.GetSetMethod() != null).ToList();
|
|
|
|
|
|
2015-11-24 05:57:01 +00:00
|
|
|
|
var keys = new List<string>();
|
|
|
|
|
var values = new List<Config>();
|
|
|
|
|
|
2015-11-26 20:05:37 +00:00
|
|
|
|
Mocker.GetMock<IConfigRepository>().Setup(c => c.Upsert(It.IsAny<string>(), It.IsAny<string>())).Callback<string, string>((key, value) =>
|
2015-11-24 05:57:01 +00:00
|
|
|
|
{
|
2015-11-26 20:05:37 +00:00
|
|
|
|
keys.Add(key);
|
|
|
|
|
values.Add(new Config { Key = key, Value = value });
|
2015-11-24 05:57:01 +00:00
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
Mocker.GetMock<IConfigRepository>().Setup(c => c.All()).Returns(values);
|
2013-02-24 19:18:48 +00:00
|
|
|
|
|
|
|
|
|
foreach (var propertyInfo in allProperties)
|
|
|
|
|
{
|
|
|
|
|
object value = null;
|
|
|
|
|
|
|
|
|
|
if (propertyInfo.PropertyType == typeof(string))
|
|
|
|
|
{
|
2013-03-24 10:09:32 +00:00
|
|
|
|
value = Guid.NewGuid().ToString();
|
2013-02-24 19:18:48 +00:00
|
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
|
2015-11-24 05:57:01 +00:00
|
|
|
|
keys.Should().OnlyHaveUniqueItems();
|
2013-02-24 19:18:48 +00:00
|
|
|
|
}
|
2016-11-01 23:02:14 +00:00
|
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
|
public void should_ignore_null_properties()
|
|
|
|
|
{
|
|
|
|
|
Mocker.GetMock<IConfigRepository>()
|
|
|
|
|
.Setup(v => v.Get("downloadedepisodesfolder"))
|
|
|
|
|
.Returns(new Config { Id = 1, Key = "DownloadedEpisodesFolder", Value = @"C:\test".AsOsAgnostic() });
|
|
|
|
|
|
|
|
|
|
var dict = new Dictionary<string, object>();
|
|
|
|
|
dict.Add("DownloadedEpisodesFolder", null);
|
|
|
|
|
Subject.SaveConfigDictionary(dict);
|
|
|
|
|
|
|
|
|
|
Mocker.GetMock<IConfigRepository>().Verify(c => c.Upsert("downloadedepisodesfolder", It.IsAny<string>()), Times.Never());
|
|
|
|
|
}
|
2013-02-24 19:18:48 +00:00
|
|
|
|
}
|
2016-11-01 23:02:14 +00:00
|
|
|
|
}
|