Auto stash before merge of "main" and "origin/main"
This commit is contained in:
parent
21d7128e74
commit
521190475d
41 changed files with 991 additions and 1150 deletions
|
|
@ -1,139 +1,78 @@
|
|||
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using Moq;
|
||||
using Newtonsoft.Json.Linq;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
namespace Tests.ConfigurationTests
|
||||
using Tests;
|
||||
using Core.Configurations.SmartConfig;
|
||||
using Core.Configurations.Common;
|
||||
|
||||
namespace Tests.ConfigurationTests;
|
||||
|
||||
[TestClass]
|
||||
public class ConfigurationTests : TestFixture
|
||||
{
|
||||
[TestClass]
|
||||
public class KeyValueJsonHandlingTests : TestFixture
|
||||
{
|
||||
[TestInitialize]
|
||||
public void Init()
|
||||
{
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void FunMixedTypesTest()
|
||||
{
|
||||
var pairs = new List<KeyValuePair<string, object>>
|
||||
{
|
||||
new("developer:coffeeLevel", 9001.5),
|
||||
new("developer:bugsFixed", 42),
|
||||
new("developer:awake", false),
|
||||
new("compiler:errors:semicolon:0", "Missing ;"),
|
||||
new("compiler:errors:semicolon:1", "Found ; but why?"),
|
||||
new("computer:ram:status", "Out of memory"),
|
||||
new("computer:cpu:temperature", 99.9),
|
||||
new("friday:beer:count", 6),
|
||||
new("friday:code:working", true),
|
||||
new("friday:bugs:count", int.MaxValue),
|
||||
new("real:object", JObject.Parse(@"{
|
||||
""beer"": {
|
||||
""count"": 6
|
||||
},
|
||||
""code"": {
|
||||
""working"": true
|
||||
},
|
||||
""bugs"": {
|
||||
""count"": 2147483647
|
||||
}
|
||||
}"))
|
||||
};
|
||||
|
||||
|
||||
[TestMethod]
|
||||
public void ConfigurationSettingsTest()
|
||||
{
|
||||
var pairs = new List<KeyValuePair<string, JToken>>
|
||||
{
|
||||
new("Debug", true),
|
||||
// Database konfiguration
|
||||
new("Database:ConnectionString", "Server=db.example.com;Port=5432"),
|
||||
new("Database:Timeout", 30),
|
||||
new("Database:UseSSL", true),
|
||||
|
||||
var result = KeyValueToJson.Convert(pairs);
|
||||
// Logging konfiguration med JObject
|
||||
new("Logging:FileOptions", JObject.Parse(@"{
|
||||
'Path': '/var/logs/app.log',
|
||||
'MaxSizeMB': 100,
|
||||
'RetentionDays': 7
|
||||
}")),
|
||||
|
||||
var expected = JObject.Parse(@"{
|
||||
'developer': {
|
||||
'coffeeLevel': 9001.5,
|
||||
'bugsFixed': 42,
|
||||
'awake': false
|
||||
},
|
||||
'compiler': {
|
||||
'errors': { 'semicolon': ['Missing ;', 'Found ; but why?'] }
|
||||
},
|
||||
'computer': {
|
||||
'ram': { 'status': 'Out of memory' },
|
||||
'cpu': { 'temperature': 99.9 }
|
||||
},
|
||||
'friday': {
|
||||
'beer': { 'count': 6 },
|
||||
'code': { 'working': true },
|
||||
'bugs': { 'count': 2147483647 }
|
||||
// Feature flags med kompleks konfiguration
|
||||
new("Features:Experimental", JObject.Parse(@"{
|
||||
'Enabled': true,
|
||||
'RolloutPercentage': 25,
|
||||
'AllowedUserGroups': ['beta']
|
||||
}")),
|
||||
|
||||
// API endpoints med array
|
||||
new("API:Endpoints", "/api/users"),
|
||||
new("API:Endpoints", "/api/products")
|
||||
};
|
||||
|
||||
var result = KeyValueToJson.Convert(pairs);
|
||||
|
||||
var expected = JObject.Parse(@"{
|
||||
'Debug' : true,
|
||||
'Database': {
|
||||
'ConnectionString': 'Server=db.example.com;Port=5432',
|
||||
'Timeout': 30,
|
||||
'UseSSL': true
|
||||
},
|
||||
'Logging': {
|
||||
'FileOptions': {
|
||||
'Path': '/var/logs/app.log',
|
||||
'MaxSizeMB': 100,
|
||||
'RetentionDays': 7
|
||||
}
|
||||
}");
|
||||
},
|
||||
'Features': {
|
||||
'Experimental': {
|
||||
'Enabled': true,
|
||||
'RolloutPercentage': 25,
|
||||
'AllowedUserGroups': ['beta']
|
||||
}
|
||||
},
|
||||
'API': {
|
||||
'Endpoints': ['/api/users', '/api/products']
|
||||
}
|
||||
}");
|
||||
|
||||
Assert.IsTrue(JToken.DeepEquals(expected, result));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public static class KeyValueToJson
|
||||
{
|
||||
public static JObject Convert(List<KeyValuePair<string, object>> pairs)
|
||||
{
|
||||
var root = new JObject();
|
||||
|
||||
foreach (var pair in pairs)
|
||||
{
|
||||
var keys = pair.Key.Split(':');
|
||||
var current = root;
|
||||
|
||||
// Gennemgå hierarkiet og opret underobjekter, hvis de ikke eksisterer
|
||||
for (int i = 0; i < keys.Length - 1; i++)
|
||||
{
|
||||
var key = keys[i];
|
||||
|
||||
if (current[key] == null)
|
||||
{
|
||||
current[key] = new JObject();
|
||||
}
|
||||
|
||||
current = (JObject)current[key];
|
||||
}
|
||||
|
||||
// Håndter den sidste nøgle og tilføj værdien
|
||||
var lastKey = keys[keys.Length - 1];
|
||||
var value = ConvertValue(pair.Value);
|
||||
|
||||
// Hvis den sidste nøgle allerede eksisterer, tilføj til en liste
|
||||
if (current[lastKey] != null)
|
||||
{
|
||||
// Hvis den allerede er en liste, tilføj til listen
|
||||
if (current[lastKey].Type == JTokenType.Array)
|
||||
{
|
||||
((JArray)current[lastKey]).Add(value);
|
||||
}
|
||||
// Hvis den ikke er en liste, konverter til en liste
|
||||
else
|
||||
{
|
||||
var existingValue = current[lastKey];
|
||||
current[lastKey] = new JArray { existingValue, value };
|
||||
}
|
||||
}
|
||||
// Ellers tilføj som en enkelt værdi
|
||||
else
|
||||
{
|
||||
current[lastKey] = value;
|
||||
}
|
||||
}
|
||||
|
||||
return root;
|
||||
}
|
||||
|
||||
private static JToken ConvertValue(object value)
|
||||
{
|
||||
// Konverter værdien til det korrekte JToken-format
|
||||
return value switch
|
||||
{
|
||||
int i => new JValue(i),
|
||||
double d => new JValue(d),
|
||||
bool b => new JValue(b),
|
||||
string s => new JValue(s),
|
||||
_ => new JValue(value.ToString()) // Fallback for andre typer
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
Assert.IsTrue(JToken.DeepEquals(expected, result));
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue