-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathEngineTest.cs
More file actions
100 lines (87 loc) · 3.28 KB
/
EngineTest.cs
File metadata and controls
100 lines (87 loc) · 3.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.IO;
using Xunit;
using FlagsmithEngine.Interfaces;
using FlagsmithEngine;
using System.Linq;
namespace EngineTest
{
public class EngineTest
{
private IEngine _iengine;
private static string TestCasesPath = Directory.GetParent(Environment.CurrentDirectory).Parent.Parent.FullName + "/EngineTestData/test_cases/";
private struct TestCase
{
[JsonProperty("context")]
public EvaluationContext<object, object> Context { get; set; }
[JsonProperty("result")]
public EvaluationResult<object, object> Result { get; set; }
}
private static TestCase GetTestCase(string filename)
{
var path = TestCasesPath + filename;
using (StreamReader r = new StreamReader(path))
{
return JsonConvert.DeserializeObject<TestCase>(r.ReadToEnd());
}
}
public EngineTest()
{
_iengine = new Engine();
}
[Theory]
[MemberData(nameof(ExtractTestCaseFilenames))]
public void Test_Engine(String testCaseFilename)
{
// Given
var testCase = GetTestCase(testCaseFilename);
// When
var result = _iengine.GetEvaluationResult(testCase.Context);
// Then
Assert.Equivalent(testCase.Result, result, strict: true);
}
public static IEnumerable<object[]> ExtractTestCaseFilenames()
{
var testCases = new List<object[]>();
var testCasePaths = Directory.GetFiles(TestCasesPath, "*.json").Concat(Directory.GetFiles(TestCasesPath, "*.jsonc"));
foreach (var testCasePath in testCasePaths)
{
testCases.Add(new object[] { testCasePath.Replace(TestCasesPath, "") });
}
return testCases;
}
[Fact]
public void TestGetEvaluationResult_ShouldNotMutateOriginalContextIdentity()
{
// Arrange
var engine = new Engine();
var context = new EvaluationContext<object, object>
{
Environment = new EnvironmentContext
{
Key = "test-env",
Name = "Test Environment"
},
Identity = new IdentityContext
{
Identifier = "user-123",
Key = null // Empty Key triggers the clone+mutate logic in GetEnrichedEvaluationContext
},
Features = new Dictionary<string, FeatureContext<object>>(),
Segments = new Dictionary<string, SegmentContext<object, object>>()
};
// Act
var result = engine.GetEvaluationResult(context);
// Assert: The original context's Identity.Key should still be null
Assert.Null(context.Identity.Key);
// ...and the rest of the context should remain unchanged
Assert.Equal("test-env", context.Environment.Key);
Assert.Equal("user-123", context.Identity.Identifier);
Assert.Empty(context.Features);
Assert.Empty(context.Segments);
}
}
}