deployment/deployment.tests/ConfigTests.cs

246 lines
8.4 KiB
C#
Raw Normal View History

2024-10-24 16:05:50 -04:00
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;
2024-10-24 16:05:50 -04:00
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
}
2024-10-24 16:05:50 -04:00
},
subtyped2:
{
aValueType: 95.298,
aValueTypeButNotAField: 9242,
anEnumerableType:
{
""test5"": 420.73,
""test6"": 420.74
}
2024-10-24 16:05:50 -04:00
}
}";
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));
2024-10-24 16:05:50 -04:00
}
[TearDown]
public void TearDown()
2024-10-24 16:05:50 -04:00
{
if(File.Exists("appsettings.json"))
File.Delete("appsettings.json");
2024-10-24 16:05:50 -04:00
}
2024-10-24 16:05:50 -04:00
[Test]
public void load_doesnt_explode()
2024-10-24 16:05:50 -04:00
{
File.WriteAllText("appsettings.json", PerfectConfiguration);
var conf = greyn.Deployment.ConfigurationBootstrapper.Load<AConfigurationType>();
2024-10-24 16:05:50 -04:00
Assert.Pass();
}
[Test]
public void load_loads()
2024-10-24 16:05:50 -04:00
{
File.WriteAllText("appsettings.json", PerfectConfiguration);
var conf = greyn.Deployment.ConfigurationBootstrapper.Load<AConfigurationType>();
2024-10-30 17:35:00 -04:00
Assert.AreEqual(conf.aValueTypeButNotAField, 796.651f);
2024-10-24 16:05:50 -04:00
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
}
}
}");
2024-10-24 16:05:50 -04:00
var conf = greyn.Deployment.ConfigurationBootstrapper.Load<AConfigurationType>();
Assert.AreEqual(conf.aValueTypeButNotAField, 796.651f);
2024-10-24 16:05:50 -04:00
Assert.AreEqual(conf.aField, "I've decided");
Assert.AreEqual(conf.subtyped.aValueType, 94.298);
Assert.AreEqual(conf.subtyped.aValueTypeButNotAField, 9241);
Assert.IsNotNull(conf.subtyped);
2024-10-24 16:05:50 -04:00
Assert.IsNull(conf.subtyped.anEnumerableType);
2024-10-24 16:05:50 -04:00
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>();
2024-10-24 16:05:50 -04:00
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);
2024-10-24 16:05:50 -04:00
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>();
2024-10-24 16:05:50 -04:00
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);
}
}