2011-10-23 22:35:16 +02:00
|
|
|
|
using System;
|
|
|
|
|
using System.Linq;
|
2011-06-17 04:27:10 +02:00
|
|
|
|
using FluentAssertions;
|
2011-06-02 06:50:10 +02:00
|
|
|
|
using NUnit.Framework;
|
2011-04-04 05:50:12 +02:00
|
|
|
|
using NzbDrone.Core.Providers.Core;
|
2010-10-21 03:49:23 +02:00
|
|
|
|
using NzbDrone.Core.Repository;
|
2011-05-19 05:55:35 +02:00
|
|
|
|
using NzbDrone.Core.Test.Framework;
|
2010-09-24 08:16:43 +02:00
|
|
|
|
|
2011-10-21 01:42:17 +02:00
|
|
|
|
namespace NzbDrone.Core.Test.ProviderTests
|
2010-09-24 08:16:43 +02:00
|
|
|
|
{
|
|
|
|
|
[TestFixture]
|
2010-09-24 20:14:15 +02:00
|
|
|
|
// ReSharper disable InconsistentNaming
|
2011-11-13 08:27:16 +01:00
|
|
|
|
public class ConfigProviderTest : CoreTest
|
2010-09-24 08:16:43 +02:00
|
|
|
|
{
|
2012-01-25 04:09:49 +01:00
|
|
|
|
[SetUp]
|
|
|
|
|
public void SetUp()
|
|
|
|
|
{
|
|
|
|
|
WithRealDb();
|
|
|
|
|
}
|
|
|
|
|
|
2010-09-24 08:16:43 +02:00
|
|
|
|
[Test]
|
2011-06-17 04:27:10 +02:00
|
|
|
|
public void Add_new_value_to_database()
|
2010-09-24 08:16:43 +02:00
|
|
|
|
{
|
2010-09-24 20:14:15 +02:00
|
|
|
|
const string key = "MY_KEY";
|
|
|
|
|
const string value = "MY_VALUE";
|
2010-09-24 08:16:43 +02:00
|
|
|
|
|
2011-12-15 05:15:53 +01:00
|
|
|
|
Mocker.Resolve<ConfigProvider>().SetValue(key, value);
|
|
|
|
|
Mocker.Resolve<ConfigProvider>().GetValue(key, "").Should().Be(value);
|
2011-06-17 04:27:10 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
|
public void Get_value_from_database()
|
|
|
|
|
{
|
|
|
|
|
const string key = "MY_KEY";
|
|
|
|
|
const string value = "MY_VALUE";
|
2011-04-07 05:34:48 +02:00
|
|
|
|
|
|
|
|
|
|
2012-01-25 04:09:49 +01:00
|
|
|
|
Db.Insert(new Config { Key = key, Value = value });
|
|
|
|
|
Db.Insert(new Config { Key = "Other Key", Value = "OtherValue" });
|
2010-09-24 08:16:43 +02:00
|
|
|
|
|
|
|
|
|
//Act
|
2011-12-15 05:15:53 +01:00
|
|
|
|
var result = Mocker.Resolve<ConfigProvider>().GetValue(key, "");
|
2010-09-24 08:16:43 +02:00
|
|
|
|
|
|
|
|
|
//Assert
|
2011-06-17 04:27:10 +02:00
|
|
|
|
result.Should().Be(value);
|
2010-09-24 08:16:43 +02:00
|
|
|
|
}
|
|
|
|
|
|
2011-06-17 04:27:10 +02:00
|
|
|
|
|
2010-09-24 08:16:43 +02:00
|
|
|
|
[Test]
|
2011-06-17 04:27:10 +02:00
|
|
|
|
public void Get_value_should_return_default_when_no_value()
|
2010-09-24 08:16:43 +02:00
|
|
|
|
{
|
2010-09-24 20:14:15 +02:00
|
|
|
|
const string key = "MY_KEY";
|
|
|
|
|
const string value = "MY_VALUE";
|
2010-09-24 08:16:43 +02:00
|
|
|
|
|
|
|
|
|
//Act
|
2011-12-15 05:15:53 +01:00
|
|
|
|
var result = Mocker.Resolve<ConfigProvider>().GetValue(key, value);
|
2010-09-24 08:16:43 +02:00
|
|
|
|
|
|
|
|
|
//Assert
|
2011-06-17 04:27:10 +02:00
|
|
|
|
result.Should().Be(value);
|
|
|
|
|
}
|
2011-06-02 06:50:10 +02:00
|
|
|
|
|
2011-06-17 04:27:10 +02:00
|
|
|
|
[Test]
|
2011-06-18 04:51:53 +02:00
|
|
|
|
public void New_value_should_update_old_value_new_value()
|
2011-06-17 04:27:10 +02:00
|
|
|
|
{
|
|
|
|
|
const string key = "MY_KEY";
|
|
|
|
|
const string originalValue = "OLD_VALUE";
|
|
|
|
|
const string newValue = "NEW_VALUE";
|
|
|
|
|
|
2012-01-25 04:09:49 +01:00
|
|
|
|
Db.Insert(new Config { Key = key, Value = originalValue });
|
2011-06-17 04:27:10 +02:00
|
|
|
|
|
|
|
|
|
//Act
|
2011-12-15 05:15:53 +01:00
|
|
|
|
Mocker.Resolve<ConfigProvider>().SetValue(key, newValue);
|
|
|
|
|
var result = Mocker.Resolve<ConfigProvider>().GetValue(key, "");
|
2011-06-17 04:27:10 +02:00
|
|
|
|
|
|
|
|
|
//Assert
|
|
|
|
|
result.Should().Be(newValue);
|
2012-01-25 04:09:49 +01:00
|
|
|
|
Db.Fetch<Config>().Should().HaveCount(1);
|
2010-09-24 08:16:43 +02:00
|
|
|
|
}
|
2011-06-17 04:27:10 +02:00
|
|
|
|
|
2011-06-18 04:51:53 +02:00
|
|
|
|
[Test]
|
|
|
|
|
public void New_value_should_update_old_value_same_value()
|
|
|
|
|
{
|
|
|
|
|
const string key = "MY_KEY";
|
|
|
|
|
const string value = "OLD_VALUE";
|
|
|
|
|
|
|
|
|
|
//Act
|
2011-12-15 05:15:53 +01:00
|
|
|
|
Mocker.Resolve<ConfigProvider>().SetValue(key, value);
|
|
|
|
|
Mocker.Resolve<ConfigProvider>().SetValue(key, value);
|
|
|
|
|
var result = Mocker.Resolve<ConfigProvider>().GetValue(key, "");
|
2011-06-18 04:51:53 +02:00
|
|
|
|
|
|
|
|
|
//Assert
|
|
|
|
|
result.Should().Be(value);
|
2012-01-25 04:09:49 +01:00
|
|
|
|
Db.Fetch<Config>().Should().HaveCount(1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
|
public void get_value_with_persist_should_store_default_value()
|
|
|
|
|
{
|
|
|
|
|
const string key = "MY_KEY";
|
|
|
|
|
string value = Guid.NewGuid().ToString();
|
|
|
|
|
|
|
|
|
|
//Act
|
|
|
|
|
Mocker.Resolve<ConfigProvider>().GetValue(key, value, persist: true).Should().Be(value);
|
|
|
|
|
Mocker.Resolve<ConfigProvider>().GetValue(key, string.Empty).Should().Be(value);
|
2011-06-18 04:51:53 +02:00
|
|
|
|
}
|
|
|
|
|
|
2012-01-25 04:09:49 +01:00
|
|
|
|
[Test]
|
|
|
|
|
public void get_value_with_out_persist_should_not_store_default_value()
|
|
|
|
|
{
|
|
|
|
|
const string key = "MY_KEY";
|
|
|
|
|
string value1 = Guid.NewGuid().ToString();
|
|
|
|
|
string value2 = Guid.NewGuid().ToString();
|
|
|
|
|
|
|
|
|
|
//Act
|
|
|
|
|
Mocker.Resolve<ConfigProvider>().GetValue(key, value1).Should().Be(value1);
|
|
|
|
|
Mocker.Resolve<ConfigProvider>().GetValue(key, value2).Should().Be(value2);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
|
public void uguid_should_only_be_set_once()
|
|
|
|
|
{
|
|
|
|
|
var guid1 = Mocker.Resolve<ConfigProvider>().UGuid;
|
|
|
|
|
var guid2 = Mocker.Resolve<ConfigProvider>().UGuid;
|
|
|
|
|
|
|
|
|
|
guid1.Should().Be(guid2);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
|
public void uguid_should_return_valid_result_on_first_call()
|
|
|
|
|
{
|
|
|
|
|
var guid = Mocker.Resolve<ConfigProvider>().UGuid;
|
2012-02-05 07:34:36 +01:00
|
|
|
|
guid.Should().NotBeEmpty();
|
2012-01-25 04:09:49 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2011-10-23 22:35:16 +02: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()
|
|
|
|
|
{
|
2011-12-15 05:15:53 +01:00
|
|
|
|
var configProvider = Mocker.Resolve<ConfigProvider>();
|
2012-01-26 02:52:47 +01:00
|
|
|
|
var allProperties = typeof(ConfigProvider).GetProperties().Where(p => p.GetSetMethod() != null);
|
2011-10-23 22:35:16 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//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);
|
|
|
|
|
}
|
|
|
|
|
|
2012-01-25 04:09:49 +01:00
|
|
|
|
Db.Fetch<Config>().Should()
|
2011-10-23 22:35:16 +02:00
|
|
|
|
.HaveSameCount(allProperties, "two different properties are writing to the same key in db. Copy/Past fail.");
|
|
|
|
|
}
|
2010-09-24 08:16:43 +02:00
|
|
|
|
}
|
2011-04-10 04:44:01 +02:00
|
|
|
|
}
|