477 lines
16 KiB
C#
477 lines
16 KiB
C#
|
using greyn.Deployment;
|
||
|
using System;
|
||
|
using System.Dynamic;
|
||
|
using System.Collections.Generic;
|
||
|
using System.Reflection;
|
||
|
using Newtonsoft.Json;
|
||
|
using System.Globalization;
|
||
|
|
||
|
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
|
||
|
|
||
|
private ExpandoObject parse(string confstr)
|
||
|
{
|
||
|
return JsonConvert.DeserializeObject<ExpandoObject>(confstr) ?? new ExpandoObject();
|
||
|
}
|
||
|
|
||
|
[Test]
|
||
|
public void parseworks()
|
||
|
{
|
||
|
Assert.IsNotNull(parse(PerfectConfiguration));
|
||
|
}
|
||
|
|
||
|
[Test]
|
||
|
public void parseworksasexpected()
|
||
|
{
|
||
|
var expandoed = parse(PerfectConfiguration);
|
||
|
Assert.IsNotNull(expandoed);
|
||
|
var casted = (IDictionary<string, object?>)expandoed;
|
||
|
Assert.IsNotNull(casted);
|
||
|
//Console.WriteLine(JsonConvert.SerializeObject(casted, Formatting.Indented));
|
||
|
}
|
||
|
|
||
|
#region jokes
|
||
|
[Test]
|
||
|
public void fuckit_werealreadytestingthetest_letstestthattest()
|
||
|
{
|
||
|
parseworks();
|
||
|
Assert.Pass();
|
||
|
}
|
||
|
[Test]
|
||
|
public void more_tests_more_good()
|
||
|
{
|
||
|
fuckit_werealreadytestingthetest_letstestthattest();
|
||
|
Assert.Pass();
|
||
|
}
|
||
|
[Test]
|
||
|
public void what_do_you_mean_a_bug_escaped_tests()
|
||
|
{
|
||
|
more_tests_more_good();
|
||
|
Assert.Pass();
|
||
|
}
|
||
|
|
||
|
[Test]
|
||
|
public void vibecheck()
|
||
|
{
|
||
|
Assert.NotZero(new Random().Next(0, 2));
|
||
|
}
|
||
|
|
||
|
[Test]
|
||
|
public void populateExpando_doesnt_explode()
|
||
|
{
|
||
|
var toReturn = new AConfigurationType();
|
||
|
var actualConfig = parse(PerfectConfiguration);
|
||
|
greyn.Deployment.ConfigurationBootstrapper.populateExpando(toReturn, ref actualConfig);
|
||
|
Assert.Pass();
|
||
|
}
|
||
|
#endregion
|
||
|
|
||
|
[Test]
|
||
|
public void populateExpando_getsmissing_property()
|
||
|
{
|
||
|
var actualConfig = parse(@"{
|
||
|
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}
|
||
|
]
|
||
|
}
|
||
|
}");
|
||
|
greyn.Deployment.ConfigurationBootstrapper.populateExpando(new AConfigurationType(), ref actualConfig);
|
||
|
var casted = (IDictionary<string, object?>)actualConfig;
|
||
|
Console.WriteLine(casted["aValueTypeButNotAField"]);
|
||
|
Assert.Pass();
|
||
|
}
|
||
|
|
||
|
[Test]
|
||
|
public void populateExpando_getsmissing_field()
|
||
|
{
|
||
|
var actualConfig = parse(@"{
|
||
|
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}
|
||
|
]
|
||
|
}
|
||
|
}");
|
||
|
greyn.Deployment.ConfigurationBootstrapper.populateExpando(new AConfigurationType(), ref actualConfig);
|
||
|
var casted = (IDictionary<string, object?>)actualConfig;
|
||
|
Console.WriteLine(casted["aField"]);
|
||
|
Assert.Pass();
|
||
|
}
|
||
|
[Test]
|
||
|
public void populateExpando_getsmissing_subobject()
|
||
|
{
|
||
|
var actualConfig = parse(@"{
|
||
|
aValueTypeButNotAField: 796.651,
|
||
|
aField: ""I've decided"",
|
||
|
subtyped2:
|
||
|
{
|
||
|
aValueType: 95.298,
|
||
|
aValueTypeButNotAField: 9242,
|
||
|
anEnumerableType:
|
||
|
[
|
||
|
{""test5"": 420.73},
|
||
|
{""test6"": 420.74}
|
||
|
]
|
||
|
}
|
||
|
}");
|
||
|
greyn.Deployment.ConfigurationBootstrapper.populateExpando(new AConfigurationType(), ref actualConfig);
|
||
|
var casted = (IDictionary<string, object?>)actualConfig;
|
||
|
Console.WriteLine(casted["subtyped"]);
|
||
|
Assert.Pass();
|
||
|
}
|
||
|
[Test]
|
||
|
public void populateExpando_getsmissing_subobject_field()
|
||
|
{
|
||
|
var actualConfig = parse(@"{
|
||
|
aValueTypeButNotAField: 796.651,
|
||
|
aField: ""I've decided"",
|
||
|
subtyped:
|
||
|
{
|
||
|
aValueTypeButNotAField: 9241,
|
||
|
anEnumerableType:
|
||
|
[
|
||
|
{""test3"": 420.71},
|
||
|
{""test4"": 420.72}
|
||
|
]
|
||
|
},
|
||
|
subtyped2:
|
||
|
{
|
||
|
aValueType: 95.298,
|
||
|
aValueTypeButNotAField: 9242,
|
||
|
anEnumerableType:
|
||
|
[
|
||
|
{""test5"": 420.73},
|
||
|
{""test6"": 420.74}
|
||
|
]
|
||
|
}
|
||
|
}");
|
||
|
greyn.Deployment.ConfigurationBootstrapper.populateExpando(new AConfigurationType(), ref actualConfig);
|
||
|
var casted = (IDictionary<string, object?>)actualConfig;
|
||
|
var subtypeCasted = (IDictionary<string, object?>)casted["subtyped"];
|
||
|
Console.WriteLine(subtypeCasted["aValueType"]);
|
||
|
Assert.Pass();
|
||
|
}
|
||
|
[Test]
|
||
|
public void populateExpando_getsmissing_subobject_property()
|
||
|
{
|
||
|
var actualConfig = parse(@"{
|
||
|
aValueTypeButNotAField: 796.651,
|
||
|
aField: ""I've decided"",
|
||
|
subtyped:
|
||
|
{
|
||
|
aValueType: 94.298,
|
||
|
anEnumerableType:
|
||
|
[
|
||
|
{""test3"": 420.71},
|
||
|
{""test4"": 420.72}
|
||
|
]
|
||
|
},
|
||
|
subtyped2:
|
||
|
{
|
||
|
aValueType: 95.298,
|
||
|
aValueTypeButNotAField: 9242,
|
||
|
anEnumerableType:
|
||
|
[
|
||
|
{""test5"": 420.73},
|
||
|
{""test6"": 420.74}
|
||
|
]
|
||
|
}
|
||
|
}");
|
||
|
greyn.Deployment.ConfigurationBootstrapper.populateExpando(new AConfigurationType(), ref actualConfig);
|
||
|
var casted = (IDictionary<string, object?>)actualConfig;
|
||
|
var subtypeCasted = (IDictionary<string, object?>)casted["subtyped"];
|
||
|
Console.WriteLine(subtypeCasted["aValueTypeButNotAField"]);
|
||
|
Assert.Pass();
|
||
|
}
|
||
|
[Test]
|
||
|
public void populateExpando_getsmissing_subobject_enumerable()
|
||
|
{
|
||
|
var actualConfig = parse(@"{
|
||
|
aValueTypeButNotAField: 796.651,
|
||
|
aField: ""I've decided"",
|
||
|
subtyped:
|
||
|
{
|
||
|
aValueType: 94.298,
|
||
|
aValueTypeButNotAField: 9241,
|
||
|
},
|
||
|
subtyped2:
|
||
|
{
|
||
|
aValueType: 95.298,
|
||
|
aValueTypeButNotAField: 9242,
|
||
|
anEnumerableType:
|
||
|
[
|
||
|
{""test5"": 420.73},
|
||
|
{""test6"": 420.74}
|
||
|
]
|
||
|
}
|
||
|
}");
|
||
|
greyn.Deployment.ConfigurationBootstrapper.populateExpando(new AConfigurationType(), ref actualConfig);
|
||
|
var casted = (IDictionary<string, object?>)actualConfig;
|
||
|
var subtypeCasted = (IDictionary<string, object?>)casted["subtyped"];
|
||
|
Console.WriteLine(subtypeCasted["anEnumerableType"]);
|
||
|
Assert.Pass();
|
||
|
}
|
||
|
[Test]
|
||
|
public void populateExpando_doesntoverridenullednullable()
|
||
|
{
|
||
|
var actualConfig = parse(@"{
|
||
|
aValueTypeButNotAField: 796.651,
|
||
|
aField: ""I've decided"",
|
||
|
subtyped: null,
|
||
|
subtyped2:
|
||
|
{
|
||
|
aValueType: 95.298,
|
||
|
aValueTypeButNotAField: 9242,
|
||
|
anEnumerableType:
|
||
|
[
|
||
|
{""test5"": 420.73},
|
||
|
{""test6"": 420.74}
|
||
|
]
|
||
|
}
|
||
|
}");
|
||
|
greyn.Deployment.ConfigurationBootstrapper.populateExpando(new AConfigurationType(), ref actualConfig);
|
||
|
var casted = (IDictionary<string, object?>)actualConfig;
|
||
|
Assert.IsNull(casted["subtyped"]);
|
||
|
}
|
||
|
[Test]
|
||
|
public void populateExpando_doesntremoveextras()
|
||
|
{
|
||
|
var actualConfig = parse(@"{
|
||
|
hiImHereToo: ""sup"",
|
||
|
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}
|
||
|
]
|
||
|
}
|
||
|
}");
|
||
|
greyn.Deployment.ConfigurationBootstrapper.populateExpando(new AConfigurationType(), ref actualConfig);
|
||
|
var casted = (IDictionary<string, object?>)actualConfig;
|
||
|
Assert.IsNotNull(casted["hiImHereToo"]);
|
||
|
}
|
||
|
[Test]
|
||
|
public void populateExpando_populatesblank()
|
||
|
{
|
||
|
var actualConfig = parse("{}");
|
||
|
greyn.Deployment.ConfigurationBootstrapper.populateExpando(new AConfigurationType(), ref actualConfig);
|
||
|
var casted = (IDictionary<string, object?>)actualConfig;
|
||
|
var subtypeCasted = (IDictionary<string, object?>)casted["subtyped"];
|
||
|
Console.WriteLine(subtypeCasted["aValueTypeButNotAField"]);
|
||
|
Assert.Pass();
|
||
|
}
|
||
|
[Test]
|
||
|
public void readfromExpando_doesnt_explode()
|
||
|
{
|
||
|
var toReturn = new AConfigurationType();
|
||
|
var actualConfig = parse(PerfectConfiguration);
|
||
|
greyn.Deployment.ConfigurationBootstrapper.readFromExpando(ref toReturn, actualConfig);
|
||
|
Assert.Pass();
|
||
|
}
|
||
|
[Test]
|
||
|
public void readfromExpando_does()
|
||
|
{
|
||
|
var readConfig = parse(PerfectConfiguration);
|
||
|
var conf = new AConfigurationType();
|
||
|
greyn.Deployment.ConfigurationBootstrapper.readFromExpando(ref conf, readConfig);
|
||
|
Assert.AreEqual(conf.aValueTypeButNotAField, 796.651);
|
||
|
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 readfromExpando_acceptsnull()
|
||
|
{
|
||
|
var readConfig = parse(PerfectConfiguration);
|
||
|
var conf = new AConfigurationType();
|
||
|
greyn.Deployment.ConfigurationBootstrapper.readFromExpando(ref conf, readConfig);
|
||
|
|
||
|
Assert.AreEqual(conf.aValueTypeButNotAField, 796.651);
|
||
|
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 readfromExpando_usesdefaultfornullablemissing()
|
||
|
{
|
||
|
var readConfig = parse(@"{
|
||
|
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 = new AConfigurationType();
|
||
|
greyn.Deployment.ConfigurationBootstrapper.readFromExpando(ref conf, readConfig);
|
||
|
Assert.AreEqual(conf.aValueTypeButNotAField, 796.651);
|
||
|
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 readfromExpando_usesdefaultfornonnullablemissing()
|
||
|
{
|
||
|
var readConfig = parse(@"{
|
||
|
|
||
|
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 = new AConfigurationType();
|
||
|
greyn.Deployment.ConfigurationBootstrapper.readFromExpando(ref conf, readConfig);
|
||
|
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);
|
||
|
}
|
||
|
}
|