sharee.bike-App/SharedBusinessLogic.Tests/Model/Settings/TestJsonSettingsDictionary.cs
2024-04-09 12:53:23 +02:00

200 lines
6.8 KiB
C#

using System;
using System.Collections.Generic;
using Newtonsoft.Json;
using NUnit.Framework;
using Serilog.Events;
using ShareeBike.Model.Settings;
using ShareeBike.Settings;
namespace SharedBusinessLogic.Tests.Model.Settings
{
[TestFixture]
public class TestJsonSettingsDictionary
{
/// <summary> Verifies that empty log fiel leads to expected default value and doesn not throw exceptions.</summary>
[Test]
public void TestGetCopriHostUri_NoFile()
{
Assert.That(JsonSettingsDictionary.GetCopriHostUri(new Dictionary<string, string>()), Is.Null);
}
/// <summary> Verifies that empty log file leads to expected default value and doesn not throw exceptions.</summary>
[Test]
public void TestGetPolling()
{
// Serialize parameters.
var dict = new Dictionary<string, string>()
.SetPollingParameters(new PollingParameters(new TimeSpan(0, 0, 0, 15, 0), false));
// Deserialize parameters.
Assert.That(
dict.GetPollingParameters(), Is.EqualTo(new PollingParameters(new TimeSpan(0, 0, 0, 15, 0), false)));
}
/// <summary> Verifies that empty log fiel leads to expected default value and doesn not throw exceptions.</summary>
[Test]
public void TestGetPolling_NoFile()
{
Assert.That(JsonSettingsDictionary.GetPollingParameters(new Dictionary<string, string>()), Is.Null);
}
[Test]
public void TestGetGetCopriHostUri()
{
var dict = new Dictionary<string, string>()
.SetCopriHostUri("http://1.2.3.4");
Assert.That(
JsonSettingsDictionary.GetCopriHostUri(dict), Is.EqualTo(new Uri("http://1.2.3.4")));
}
/// <summary> Verifies that empty log fiel leads to expected default value and doesn not throw exceptions.</summary>
[Test]
public void TestGetLoggingLevel()
{
var dict = new Dictionary<string, string>()
.SetMinimumLoggingLevel(0); // Verbose = 0
Assert.That(
JsonSettingsDictionary.GetMinimumLoggingLevel(dict), Is.EqualTo(LogEventLevel.Verbose)); // LogEventLevel.Error = 4
}
/// <summary> Verifies that empty log fiel leads to expected default value and doesn not throw exceptions.</summary>
[Test]
public void TestGetLoggingLevel_NoFile()
{
Assert.That(JsonSettingsDictionary.GetMinimumLoggingLevel(new Dictionary<string, string>()), Is.Null);
}
[Test]
public void TestGetAppVersion_FirstInstall()
{
var dict = new Dictionary<string, string>();
Assert.That(JsonSettingsDictionary.GetAppVersion(dict), Is.Null);
}
[Test]
public void TestGetAppVersion_LegacyTo115()
{
var dict = new Dictionary<string, string> { { "AppVersion", "7.2.3.9" } };
Assert.That(JsonSettingsDictionary.GetAppVersion(dict), Is.EqualTo(new Version(7, 2, 3, 9)));
dict = new Dictionary<string, string> { { "AppVersion", "7.2.3" } };
Assert.That(JsonSettingsDictionary.GetAppVersion(dict), Is.EqualTo(new Version(7, 2, 3)));
}
[Test]
public void TestGetAppVersion_Json()
{
var dict = new Dictionary<string, string> { { "AppVersion", "\"3.1.2.117\"" } };
Assert.That(JsonSettingsDictionary.GetAppVersion(dict), Is.EqualTo(new Version(3, 1, 2, 117)));
}
[Test]
public void TestSetAppVersion_Json()
{
var dict = new Dictionary<string, string>()
.SetAppVersion(new Version(47, 12, 3));
Assert.That(JsonSettingsDictionary.GetAppVersion(dict), Is.EqualTo(new Version(47, 12, 3)));
}
[Test]
public void TestGetShowWhatsNew_FirstInstall()
{
var dict = new Dictionary<string, string>();
Assert.That(JsonSettingsDictionary.GetWhatsNew(dict), Is.Null);
}
[Test]
public void TestGetShowWhatsNew_Json()
{
var dict = new Dictionary<string, string> { { "ShowWhatsNew", "\"3.1.2.117\"" } };
Assert.That(JsonSettingsDictionary.GetWhatsNew(dict), Is.EqualTo(new Version(3, 1, 2, 117)));
dict = new Dictionary<string, string> { { "ShowWhatsNew", "\"3.1.2\"" } };
Assert.That(JsonSettingsDictionary.GetWhatsNew(dict), Is.EqualTo(new Version(3, 1, 2)));
}
[Test]
public void TestSetShowWhats_Json()
{
var dict = new Dictionary<string, string>()
.SetWhatsNew(new Version(47, 12, 3));
Assert.That(JsonSettingsDictionary.GetWhatsNew(dict), Is.EqualTo(new Version(47, 12, 3)));
}
[Test]
public void TestGetEntryTNoLegacySettingsKeyNull() => Assert.That(
JsonSettingsDictionary.GetEntry<string>(null, new Dictionary<string, string>()),
Is.Null);
[Test]
public void TestGetEntryTNoLegacySettingsNull() => Assert.That(
JsonSettingsDictionary.GetEntry<string>("Hi", null),
Is.Null);
[Test]
public void TestGetEntryTNoLegacyEntryNotFound() => Assert.That(
JsonSettingsDictionary.GetEntry<string>("Hi", new Dictionary<string, string> { { "Ho", @"""Hu""" } }),
Is.Null);
[Test]
public void TestGetEntryTNoLegacyEntryFound() => Assert.That(
JsonSettingsDictionary.GetEntry<string>("Hi", new Dictionary<string, string> { { "Hi", @"""Ho""" } }),
Is.EqualTo("Ho"));
[Test]
public void TestGetEntryT() => Assert.That(
JsonSettingsDictionary.GetEntry<string>("Hi", new Dictionary<string, string> { { "Hi", @"""Ho""" } }, (value) => @"""Hey"""),
Is.EqualTo("Hey"));
[Test]
public void TestGetActiveTheme() => Assert.That(
JsonSettingsDictionary.GetActiveTheme(new Dictionary<string, string> { { "Theme", @"""LastenradBayern""" } }),
Is.EqualTo(typeof(ShareeBike.Themes.LastenradBayern).Name));
[Test]
public void TestGetActiveThemeLegacy() => Assert.That(
JsonSettingsDictionary.GetActiveTheme(new Dictionary<string, string> { { "Theme", @"""ShareeBike.Themes.LastenradBayern""" } }),
Is.EqualTo(typeof(ShareeBike.Themes.LastenradBayern).Name));
[Test]
public void TestSetActiveTheme()
{
var settings = JsonSettingsDictionary.SetActiveTheme(new Dictionary<string, string>(), @"LastenradBayern");
Assert.That(
settings,
Contains.Key("Theme"));
settings = JsonSettingsDictionary.SetActiveTheme(new Dictionary<string, string>(), @"LastenradBayern");
Assert.That(
settings["Theme"],
Is.EqualTo(JsonConvert.SerializeObject(typeof(ShareeBike.Themes.LastenradBayern).Name)));
}
[Test]
public void TestGetStartupSettingsStartupPage() => Assert.That(
JsonSettingsDictionary.GetStartupSettings(new Dictionary<string, string> {
{
"StartupSettings",
JsonConvert.SerializeObject(new StartupSettings { StartupPage = ShareeBike.ViewTypes.ContactPage})
}
}).StartupPage,
Is.EqualTo(ShareeBike.ViewTypes.ContactPage));
[Test]
public void TestSetStartupSettingsStartupPage()
{
var settings = JsonSettingsDictionary.SetStartupSettings(
new Dictionary<string, string>(),
new StartupSettings { StartupPage = ShareeBike.ViewTypes.ContactPage });
Assert.That(
settings,
Contains.Key("StartupSettings"));
Assert.That(
settings["StartupSettings"],
Is.EqualTo(JsonConvert.SerializeObject(new StartupSettings { StartupPage = ShareeBike.ViewTypes.ContactPage })));
}
}
}