2011-10-23 23:35:16 +03:00
|
|
|
using System;
|
|
|
|
using System.Linq;
|
|
|
|
using System.Reflection;
|
2011-11-14 03:22:18 +03:00
|
|
|
|
2011-06-17 05:27:10 +03:00
|
|
|
using FluentAssertions;
|
2011-10-23 23:35:16 +03:00
|
|
|
using Moq;
|
2011-06-02 07:50:10 +03:00
|
|
|
using NUnit.Framework;
|
2011-04-04 06:50:12 +03:00
|
|
|
using NzbDrone.Core.Providers.Core;
|
2010-10-21 04:49:23 +03:00
|
|
|
using NzbDrone.Core.Repository;
|
2011-05-19 06:55:35 +03:00
|
|
|
using NzbDrone.Core.Test.Framework;
|
2011-11-14 03:22:18 +03:00
|
|
|
using NzbDrone.Test.Common.AutoMoq;
|
2011-10-23 23:35:16 +03:00
|
|
|
using PetaPoco;
|
2010-09-24 09:16:43 +03:00
|
|
|
|
2011-10-21 02:42:17 +03:00
|
|
|
namespace NzbDrone.Core.Test.ProviderTests
|
2010-09-24 09:16:43 +03:00
|
|
|
{
|
|
|
|
[TestFixture]
|
2010-09-24 21:14:15 +03:00
|
|
|
// ReSharper disable InconsistentNaming
|
2011-11-13 10:27:16 +03:00
|
|
|
public class ConfigProviderTest : CoreTest
|
2010-09-24 09:16:43 +03:00
|
|
|
{
|
|
|
|
[Test]
|
2011-06-17 05:27:10 +03:00
|
|
|
public void Add_new_value_to_database()
|
2010-09-24 09:16:43 +03:00
|
|
|
{
|
2010-09-24 21:14:15 +03:00
|
|
|
const string key = "MY_KEY";
|
|
|
|
const string value = "MY_VALUE";
|
2010-09-24 09:16:43 +03:00
|
|
|
|
2011-06-17 05:27:10 +03:00
|
|
|
var mocker = new AutoMoqer();
|
2011-11-23 08:58:26 +03:00
|
|
|
var db = TestDbHelper.GetEmptyDatabase();
|
2011-06-17 05:27:10 +03:00
|
|
|
mocker.SetConstant(db);
|
|
|
|
|
|
|
|
//Act
|
|
|
|
mocker.Resolve<ConfigProvider>().SetValue(key, value);
|
|
|
|
|
|
|
|
//Assert
|
|
|
|
mocker.Resolve<ConfigProvider>().GetValue(key, "").Should().Be(value);
|
|
|
|
}
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
public void Get_value_from_database()
|
|
|
|
{
|
|
|
|
const string key = "MY_KEY";
|
|
|
|
const string value = "MY_VALUE";
|
2011-04-07 06:34:48 +03:00
|
|
|
|
|
|
|
var mocker = new AutoMoqer();
|
2011-11-23 08:58:26 +03:00
|
|
|
var db = TestDbHelper.GetEmptyDatabase();
|
2011-06-17 05:27:10 +03:00
|
|
|
mocker.SetConstant(db);
|
2011-04-07 06:34:48 +03:00
|
|
|
|
2011-06-17 05:27:10 +03:00
|
|
|
db.Insert(new Config { Key = key, Value = value });
|
2011-06-18 05:51:53 +03:00
|
|
|
db.Insert(new Config { Key = "Other Key", Value = "OtherValue" });
|
2010-09-24 09:16:43 +03:00
|
|
|
|
|
|
|
//Act
|
2011-06-17 05:27:10 +03:00
|
|
|
var result = mocker.Resolve<ConfigProvider>().GetValue(key, "");
|
2010-09-24 09:16:43 +03:00
|
|
|
|
|
|
|
//Assert
|
2011-06-17 05:27:10 +03:00
|
|
|
result.Should().Be(value);
|
2010-09-24 09:16:43 +03:00
|
|
|
}
|
|
|
|
|
2011-06-17 05:27:10 +03:00
|
|
|
|
2010-09-24 09:16:43 +03:00
|
|
|
[Test]
|
2011-06-17 05:27:10 +03:00
|
|
|
public void Get_value_should_return_default_when_no_value()
|
2010-09-24 09:16:43 +03:00
|
|
|
{
|
2010-09-24 21:14:15 +03:00
|
|
|
const string key = "MY_KEY";
|
|
|
|
const string value = "MY_VALUE";
|
2010-09-24 09:16:43 +03:00
|
|
|
|
2011-04-07 06:34:48 +03:00
|
|
|
var mocker = new AutoMoqer();
|
2011-11-23 08:58:26 +03:00
|
|
|
var db = TestDbHelper.GetEmptyDatabase();
|
2011-06-17 05:27:10 +03:00
|
|
|
mocker.SetConstant(db);
|
2011-04-07 06:34:48 +03:00
|
|
|
|
2010-09-24 09:16:43 +03:00
|
|
|
|
|
|
|
//Act
|
2011-06-17 05:27:10 +03:00
|
|
|
var result = mocker.Resolve<ConfigProvider>().GetValue(key, value);
|
2010-09-24 09:16:43 +03:00
|
|
|
|
|
|
|
//Assert
|
2011-06-17 05:27:10 +03:00
|
|
|
result.Should().Be(value);
|
|
|
|
}
|
2011-06-02 07:50:10 +03:00
|
|
|
|
2011-06-17 05:27:10 +03:00
|
|
|
[Test]
|
2011-06-18 05:51:53 +03:00
|
|
|
public void New_value_should_update_old_value_new_value()
|
2011-06-17 05:27:10 +03:00
|
|
|
{
|
|
|
|
const string key = "MY_KEY";
|
|
|
|
const string originalValue = "OLD_VALUE";
|
|
|
|
const string newValue = "NEW_VALUE";
|
|
|
|
|
|
|
|
var mocker = new AutoMoqer();
|
2011-11-23 08:58:26 +03:00
|
|
|
var db = TestDbHelper.GetEmptyDatabase();
|
2011-06-17 05:27:10 +03:00
|
|
|
mocker.SetConstant(db);
|
|
|
|
|
|
|
|
db.Insert(new Config { Key = key, Value = originalValue });
|
|
|
|
|
|
|
|
//Act
|
|
|
|
mocker.Resolve<ConfigProvider>().SetValue(key, newValue);
|
|
|
|
var result = mocker.Resolve<ConfigProvider>().GetValue(key, "");
|
|
|
|
|
|
|
|
//Assert
|
|
|
|
result.Should().Be(newValue);
|
|
|
|
db.Fetch<Config>().Should().HaveCount(1);
|
2010-09-24 09:16:43 +03:00
|
|
|
}
|
2011-06-17 05:27:10 +03:00
|
|
|
|
2011-06-18 05:51:53 +03:00
|
|
|
[Test]
|
|
|
|
public void New_value_should_update_old_value_same_value()
|
|
|
|
{
|
|
|
|
const string key = "MY_KEY";
|
|
|
|
const string value = "OLD_VALUE";
|
|
|
|
|
|
|
|
|
|
|
|
var mocker = new AutoMoqer();
|
2011-11-23 08:58:26 +03:00
|
|
|
var db = TestDbHelper.GetEmptyDatabase();
|
2011-06-18 05:51:53 +03:00
|
|
|
mocker.SetConstant(db);
|
|
|
|
|
|
|
|
//Act
|
|
|
|
mocker.Resolve<ConfigProvider>().SetValue(key, value);
|
|
|
|
mocker.Resolve<ConfigProvider>().SetValue(key, value);
|
|
|
|
var result = mocker.Resolve<ConfigProvider>().GetValue(key, "");
|
|
|
|
|
|
|
|
//Assert
|
|
|
|
result.Should().Be(value);
|
|
|
|
db.Fetch<Config>().Should().HaveCount(1);
|
|
|
|
}
|
|
|
|
|
2011-10-23 23:35:16 +03: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 mocker = new AutoMoqer(MockBehavior.Strict);
|
2011-11-23 08:58:26 +03:00
|
|
|
var db = TestDbHelper.GetEmptyDatabase();
|
2011-10-23 23:35:16 +03:00
|
|
|
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.");
|
|
|
|
}
|
2010-09-24 09:16:43 +03:00
|
|
|
}
|
2011-04-10 05:44:01 +03:00
|
|
|
}
|