deployment/deployment.tests/ConfigTests.cs
adam dd7efe9155 "hey, self. ever heard of YAGNI?"
"no, please explain each and every nuance of it in excruciating detail."
"eh. the gist is good enough."
2024-10-30 20:32:33 -04:00

246 lines
8.4 KiB
C#

using greyn.Deployment;
using System;
using System.Dynamic;
using System.Collections.Generic;
using System.Reflection;
using Newtonsoft.Json;
using System.Globalization;
using Newtonsoft.Json.Converters;
namespace deployment.tests;
public class ConfigTests
{
public class AConfigurationSubType
{
public double aValueType {get;set;} = 892.49;
public int aValueTypeButNotAField = 1429;
public Dictionary<string, double> anEnumerableType {get; set;} = new Dictionary<string, double> {{"test1", 420.69}, {"test2", 420.70}};
}
public class AConfigurationType
{
public float aValueTypeButNotAField = 156.697f;
public string aField {get;set;} = "hi there, hello";
public AConfigurationSubType subtyped = new AConfigurationSubType();
public AConfigurationSubType subtyped2 = new AConfigurationSubType();
}
#region Config
private const string PerfectConfiguration = @"{
aValueTypeButNotAField: 796.651,
aField: ""I've decided"",
subtyped:
{
aValueType: 94.298,
aValueTypeButNotAField: 9241,
anEnumerableType:
{
""test3"": 420.71,
""test4"": 420.72
}
},
subtyped2:
{
aValueType: 95.298,
aValueTypeButNotAField: 9242,
anEnumerableType:
{
""test5"": 420.73,
""test6"": 420.74
}
}
}";
private const string EmptyConfiguration = "{}";
private const string MalformedConfiguration = "{{}";
private const string InvalidValueConfiguration_00001 = @"{
aValueTypeButNotAField = ""796.651"",
}";
#endregion
[Test]
public void vibecheck()
{
Assert.AreEqual(0, new Random().Next(0, 2));
}
[TearDown]
public void TearDown()
{
if(File.Exists("appsettings.json"))
File.Delete("appsettings.json");
}
[Test]
public void load_doesnt_explode()
{
File.WriteAllText("appsettings.json", PerfectConfiguration);
var conf = greyn.Deployment.ConfigurationBootstrapper.Load<AConfigurationType>();
Assert.Pass();
}
[Test]
public void load_loads()
{
File.WriteAllText("appsettings.json", PerfectConfiguration);
var conf = greyn.Deployment.ConfigurationBootstrapper.Load<AConfigurationType>();
Assert.AreEqual(conf.aValueTypeButNotAField, 796.651f);
Assert.AreEqual(conf.aField, "I've decided");
Assert.AreEqual(conf.subtyped.aValueType, 94.298);
Assert.AreEqual(conf.subtyped.aValueTypeButNotAField, 9241);
Assert.IsNotNull(conf.subtyped.anEnumerableType);
Assert.AreEqual(conf.subtyped.anEnumerableType["test3"], 420.71);
Assert.AreEqual(conf.subtyped.anEnumerableType["test4"], 420.72);
Assert.IsNotNull(conf.subtyped2);
}
[Test]
public void load_acceptsnullasvalue()
{
File.WriteAllText("appsettings.json", @"{
aValueTypeButNotAField: 796.651,
aField: ""I've decided"",
subtyped:
{
aValueType: 94.298,
aValueTypeButNotAField: 9241,
anEnumerableType: null
},
subtyped2:
{
aValueType: 95.298,
aValueTypeButNotAField: 9242,
anEnumerableType:
{
""test5"": 420.73,
""test6"": 420.74
}
}
}");
var conf = greyn.Deployment.ConfigurationBootstrapper.Load<AConfigurationType>();
Assert.AreEqual(conf.aValueTypeButNotAField, 796.651f);
Assert.AreEqual(conf.aField, "I've decided");
Assert.AreEqual(conf.subtyped.aValueType, 94.298);
Assert.AreEqual(conf.subtyped.aValueTypeButNotAField, 9241);
Assert.IsNotNull(conf.subtyped);
Assert.IsNull(conf.subtyped.anEnumerableType);
Assert.IsNotNull(conf.subtyped2);
Assert.AreEqual(conf.subtyped2.aValueType, 95.298);
Assert.AreEqual(conf.subtyped2.aValueTypeButNotAField, 9242);
Assert.IsNotNull(conf.subtyped2.anEnumerableType);
Assert.AreEqual(conf.subtyped2.anEnumerableType["test5"], 420.73);
Assert.AreEqual(conf.subtyped2.anEnumerableType["test6"], 420.74);
}
[Test]
public void load_acceptsnull_forparenttype()
{
File.WriteAllText("appsettings.json", @"{
aValueTypeButNotAField: 796.651,
aField: ""I've decided"",
subtyped: null,
subtyped2:
{
aValueType: 95.298,
aValueTypeButNotAField: 9242,
anEnumerableType:
{
""test5"": 420.73,
""test6"": 420.74
}
}
}");
var conf = greyn.Deployment.ConfigurationBootstrapper.Load<AConfigurationType>();
Assert.AreEqual(conf.aValueTypeButNotAField, 796.651f);
Assert.AreEqual(conf.aField, "I've decided");
Assert.IsNull(conf.subtyped);
Assert.IsNotNull(conf.subtyped2);
Assert.AreEqual(conf.subtyped2.aValueType, 95.298);
Assert.AreEqual(conf.subtyped2.aValueTypeButNotAField, 9242);
Assert.IsNotNull(conf.subtyped2.anEnumerableType);
Assert.AreEqual(conf.subtyped2.anEnumerableType["test5"], 420.73);
Assert.AreEqual(conf.subtyped2.anEnumerableType["test6"], 420.74);
}
[Test]
public void load_usesdefaultfornullablemissing()
{
File.WriteAllText("appsettings.json", @"{
aValueTypeButNotAField: 796.651,
subtyped:
{
aValueType: 94.298,
aValueTypeButNotAField: 9241,
anEnumerableType:
{
""test3"": 420.71,
""test4"": 420.72
}
},
subtyped2:
{
aValueType: 95.298,
aValueTypeButNotAField: 9242,
anEnumerableType:
{
""test5"": 420.73,
""test6"": 420.74
}
}
}");
var conf = greyn.Deployment.ConfigurationBootstrapper.Load<AConfigurationType>();
Assert.AreEqual(conf.aValueTypeButNotAField, 796.651f);
Assert.AreEqual(conf.aField, "hi there, hello");
Assert.AreEqual(conf.subtyped.aValueType, 94.298);
Assert.AreEqual(conf.subtyped.aValueTypeButNotAField, 9241);
Assert.IsNotNull(conf.subtyped.anEnumerableType);
Assert.AreEqual(conf.subtyped.anEnumerableType["test3"], 420.71);
Assert.AreEqual(conf.subtyped.anEnumerableType["test4"], 420.72);
Assert.IsNotNull(conf.subtyped2);
}
[Test]
public void load_usesdefaultfornonnullablemissing()
{
File.WriteAllText("appsettings.json", @"{
aField: ""I've decided"",
subtyped:
{
aValueType: 94.298,
aValueTypeButNotAField: 9241,
anEnumerableType:
{
""test3"": 420.71,
""test4"": 420.72
}
},
subtyped2:
{
aValueType: 95.298,
aValueTypeButNotAField: 9242,
anEnumerableType:
{
""test5"": 420.73,
""test6"": 420.74
}
}
}");
var conf = greyn.Deployment.ConfigurationBootstrapper.Load<AConfigurationType>();
Assert.AreEqual(conf.aValueTypeButNotAField, 156.697f);
Assert.AreEqual(conf.aField, "I've decided");
Assert.AreEqual(conf.subtyped.aValueType, 94.298);
Assert.AreEqual(conf.subtyped.aValueTypeButNotAField, 9241);
Assert.IsNotNull(conf.subtyped.anEnumerableType);
Assert.AreEqual(conf.subtyped.anEnumerableType["test3"], 420.71);
Assert.AreEqual(conf.subtyped.anEnumerableType["test4"], 420.72);
Assert.IsNotNull(conf.subtyped2);
}
}