Skip to content

Commit 29789a1

Browse files
committed
Updated tests to utilize constants helpers for repeated strings
1 parent 405b9d2 commit 29789a1

7 files changed

Lines changed: 212 additions & 115 deletions

File tree

src/EventLogExpert.UI.Tests/Models/FilterComparisonTests.cs

Lines changed: 29 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using EventLogExpert.Eventing.Models;
55
using EventLogExpert.UI.Models;
66
using EventLogExpert.UI.Tests.TestUtils;
7+
using EventLogExpert.UI.Tests.TestUtils.Constants;
78
using System.Linq.Dynamic.Core.Exceptions;
89

910
namespace EventLogExpert.UI.Tests.Models;
@@ -14,10 +15,10 @@ public sealed class FilterComparisonTests
1415
public void Expression_WhenCompoundCondition_ShouldMatchCorrectEvent()
1516
{
1617
// Arrange
17-
FilterComparison model = new() { Value = "Id == 100 && Level == \"Error\"" };
18-
DisplayEventModel matchingEvent = EventUtils.CreateTestEvent(100, level: "Error");
19-
DisplayEventModel nonMatchingIdEvent = EventUtils.CreateTestEvent(200, level: "Error");
20-
DisplayEventModel nonMatchingLevelEvent = EventUtils.CreateTestEvent(100, level: "Information");
18+
FilterComparison model = new() { Value = Constants.FilterIdEquals100AndLevelError };
19+
DisplayEventModel matchingEvent = EventUtils.CreateTestEvent(100, level: Constants.EventLevelError);
20+
DisplayEventModel nonMatchingIdEvent = EventUtils.CreateTestEvent(200, level: Constants.EventLevelError);
21+
DisplayEventModel nonMatchingLevelEvent = EventUtils.CreateTestEvent(100, level: Constants.EventLevelInformation);
2122

2223
// Act
2324
bool matchResult = model.Expression(matchingEvent);
@@ -34,9 +35,9 @@ public void Expression_WhenCompoundCondition_ShouldMatchCorrectEvent()
3435
public void Expression_WhenComputerNameEquals_ShouldMatchCorrectEvent()
3536
{
3637
// Arrange
37-
FilterComparison model = new() { Value = "ComputerName == \"SERVER01\"" };
38-
DisplayEventModel matchingEvent = EventUtils.CreateTestEvent(computerName: "SERVER01");
39-
DisplayEventModel nonMatchingEvent = EventUtils.CreateTestEvent(computerName: "SERVER02");
38+
FilterComparison model = new() { Value = Constants.FilterComputerNameEqualsServer01 };
39+
DisplayEventModel matchingEvent = EventUtils.CreateTestEvent(computerName: Constants.EventComputerServer01);
40+
DisplayEventModel nonMatchingEvent = EventUtils.CreateTestEvent(computerName: Constants.EventComputerServer02);
4041

4142
// Act
4243
bool matchResult = model.Expression(matchingEvent);
@@ -51,13 +52,13 @@ public void Expression_WhenComputerNameEquals_ShouldMatchCorrectEvent()
5152
public void Expression_WhenDescriptionContains_ShouldMatchCorrectEvent()
5253
{
5354
// Arrange
54-
FilterComparison model = new() { Value = "Description.Contains(\"error occurred\")" };
55+
FilterComparison model = new() { Value = Constants.FilterDescriptionContainsErrorOccurred };
5556

5657
DisplayEventModel matchingEvent =
57-
EventUtils.CreateTestEvent(description: "An error occurred in the application");
58+
EventUtils.CreateTestEvent(description: Constants.EventDescriptionErrorOccurred);
5859

5960
DisplayEventModel nonMatchingEvent =
60-
EventUtils.CreateTestEvent(description: "Operation completed successfully");
61+
EventUtils.CreateTestEvent(description: Constants.EventDescriptionSuccess);
6162

6263
// Act
6364
bool matchResult = model.Expression(matchingEvent);
@@ -72,7 +73,7 @@ public void Expression_WhenDescriptionContains_ShouldMatchCorrectEvent()
7273
public void Expression_WhenIdEquals_ShouldMatchCorrectEvent()
7374
{
7475
// Arrange
75-
FilterComparison model = new() { Value = "Id == 100" };
76+
FilterComparison model = new() { Value = Constants.FilterIdEquals100 };
7677
DisplayEventModel matchingEvent = EventUtils.CreateTestEvent(100);
7778
DisplayEventModel nonMatchingEvent = EventUtils.CreateTestEvent(200);
7879

@@ -89,7 +90,7 @@ public void Expression_WhenIdEquals_ShouldMatchCorrectEvent()
8990
public void Expression_WhenIdGreaterThan_ShouldMatchCorrectEvent()
9091
{
9192
// Arrange
92-
FilterComparison model = new() { Value = "Id > 100" };
93+
FilterComparison model = new() { Value = Constants.FilterIdGreaterThan100 };
9394
DisplayEventModel matchingEvent = EventUtils.CreateTestEvent(150);
9495
DisplayEventModel nonMatchingEvent = EventUtils.CreateTestEvent(50);
9596

@@ -106,7 +107,7 @@ public void Expression_WhenIdGreaterThan_ShouldMatchCorrectEvent()
106107
public void Expression_WhenIdNotEquals_ShouldMatchCorrectEvent()
107108
{
108109
// Arrange
109-
FilterComparison model = new() { Value = "Id != 100" };
110+
FilterComparison model = new() { Value = Constants.FilterIdNotEquals100 };
110111
DisplayEventModel matchingEvent = EventUtils.CreateTestEvent(200);
111112
DisplayEventModel nonMatchingEvent = EventUtils.CreateTestEvent(100);
112113

@@ -123,9 +124,9 @@ public void Expression_WhenIdNotEquals_ShouldMatchCorrectEvent()
123124
public void Expression_WhenLevelEquals_ShouldMatchCorrectEvent()
124125
{
125126
// Arrange
126-
FilterComparison model = new() { Value = "Level == \"Error\"" };
127-
DisplayEventModel matchingEvent = EventUtils.CreateTestEvent(level: "Error");
128-
DisplayEventModel nonMatchingEvent = EventUtils.CreateTestEvent(level: "Information");
127+
FilterComparison model = new() { Value = Constants.FilterLevelEqualsError };
128+
DisplayEventModel matchingEvent = EventUtils.CreateTestEvent(level: Constants.EventLevelError);
129+
DisplayEventModel nonMatchingEvent = EventUtils.CreateTestEvent(level: Constants.EventLevelInformation);
129130

130131
// Act
131132
bool matchResult = model.Expression(matchingEvent);
@@ -140,7 +141,7 @@ public void Expression_WhenLevelEquals_ShouldMatchCorrectEvent()
140141
public void Expression_WhenOrCondition_ShouldMatchCorrectEvent()
141142
{
142143
// Arrange
143-
FilterComparison model = new() { Value = "Id == 100 || Id == 200" };
144+
FilterComparison model = new() { Value = Constants.FilterIdEquals100Or200 };
144145
DisplayEventModel matchingEvent1 = EventUtils.CreateTestEvent(100);
145146
DisplayEventModel matchingEvent2 = EventUtils.CreateTestEvent(200);
146147
DisplayEventModel nonMatchingEvent = EventUtils.CreateTestEvent(300);
@@ -160,9 +161,9 @@ public void Expression_WhenOrCondition_ShouldMatchCorrectEvent()
160161
public void Expression_WhenSourceContains_ShouldMatchCorrectEvent()
161162
{
162163
// Arrange
163-
FilterComparison model = new() { Value = "Source.Contains(\"Test\")" };
164-
DisplayEventModel matchingEvent = EventUtils.CreateTestEvent(source: "TestSource");
165-
DisplayEventModel nonMatchingEvent = EventUtils.CreateTestEvent(source: "OtherSource");
164+
FilterComparison model = new() { Value = Constants.FilterSourceContainsTest };
165+
DisplayEventModel matchingEvent = EventUtils.CreateTestEvent(source: Constants.EventSourceTestSource);
166+
DisplayEventModel nonMatchingEvent = EventUtils.CreateTestEvent(source: Constants.EventSourceOtherSource);
166167

167168
// Act
168169
bool matchResult = model.Expression(matchingEvent);
@@ -177,9 +178,9 @@ public void Expression_WhenSourceContains_ShouldMatchCorrectEvent()
177178
public void Expression_WhenTaskCategoryContains_ShouldMatchCorrectEvent()
178179
{
179180
// Arrange
180-
FilterComparison model = new() { Value = "TaskCategory.Contains(\"Security\")" };
181-
DisplayEventModel matchingEvent = EventUtils.CreateTestEvent(taskCategory: "Security Audit");
182-
DisplayEventModel nonMatchingEvent = EventUtils.CreateTestEvent(taskCategory: "System");
181+
FilterComparison model = new() { Value = Constants.FilterTaskCategoryContainsSecurity };
182+
DisplayEventModel matchingEvent = EventUtils.CreateTestEvent(taskCategory: Constants.EventTaskCategorySecurity);
183+
DisplayEventModel nonMatchingEvent = EventUtils.CreateTestEvent(taskCategory: Constants.EventTaskCategorySystem);
183184

184185
// Act
185186
bool matchResult = model.Expression(matchingEvent);
@@ -197,7 +198,7 @@ public void Expression_WhenValidValue_ShouldContainFunc()
197198
FilterComparison model = new();
198199

199200
// Act
200-
model.Value = "Id == 100";
201+
model.Value = Constants.FilterIdEquals100;
201202

202203
// Assert
203204
Assert.NotNull(model.Expression);
@@ -207,14 +208,14 @@ public void Expression_WhenValidValue_ShouldContainFunc()
207208
public void Expression_WhenValueUpdated_ShouldUpdateExpression()
208209
{
209210
// Arrange
210-
FilterComparison model = new() { Value = "Id == 100" };
211+
FilterComparison model = new() { Value = Constants.FilterIdEquals100 };
211212
DisplayEventModel event100 = EventUtils.CreateTestEvent(100);
212213
DisplayEventModel event200 = EventUtils.CreateTestEvent(200);
213214

214215
// Act
215216
bool initialMatch100 = model.Expression(event100);
216217
bool initialMatch200 = model.Expression(event200);
217-
model.Value = "Id == 200";
218+
model.Value = Constants.FilterIdEquals200;
218219
bool updatedMatch100 = model.Expression(event100);
219220
bool updatedMatch200 = model.Expression(event200);
220221

@@ -242,7 +243,7 @@ public void Value_WhenInvalidPropertyName_ShouldThrow()
242243
FilterComparison model = new();
243244

244245
// Act & Assert
245-
Assert.Throws<ParseException>(() => model.Value = "InvalidProperty == 100");
246+
Assert.Throws<ParseException>(() => model.Value = Constants.FilterInvalidProperty);
246247
}
247248

248249
[Fact]
@@ -252,6 +253,6 @@ public void Value_WhenNotValid_ShouldThrow()
252253
FilterComparison model = new();
253254

254255
// Act & Assert
255-
Assert.Throws<ParseException>(() => model.Value = "Id == invalid");
256+
Assert.Throws<ParseException>(() => model.Value = Constants.FilterInvalidValue);
256257
}
257258
}

src/EventLogExpert.UI.Tests/Models/FilterDataTests.cs

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using EventLogExpert.UI.Models;
2+
using EventLogExpert.UI.Tests.TestUtils.Constants;
23

34
namespace EventLogExpert.UI.Tests.Models;
45

@@ -8,7 +9,7 @@ public sealed class FilterDataTests
89
public void Category_WhenChanged_ShouldClearValue()
910
{
1011
// Arrange
11-
FilterData model = new() { Category = FilterCategory.Id, Value = "100" };
12+
FilterData model = new() { Category = FilterCategory.Id, Value = Constants.FilterValue100 };
1213

1314
// Act
1415
model.Category = FilterCategory.Level;
@@ -21,7 +22,7 @@ public void Category_WhenChanged_ShouldClearValue()
2122
public void Category_WhenChanged_ShouldClearValues()
2223
{
2324
// Arrange
24-
FilterData model = new() { Category = FilterCategory.Id, Values = ["100", "1000"] };
25+
FilterData model = new() { Category = FilterCategory.Id, Values = [Constants.FilterValue100, Constants.FilterValue1000] };
2526

2627
// Act
2728
model.Category = FilterCategory.Level;
@@ -47,14 +48,14 @@ public void Category_WhenChanged_ShouldUpdateCategory()
4748
public void Category_WhenChangedMultipleTimes_ShouldClearValueEachTime()
4849
{
4950
// Arrange
50-
FilterData model = new() { Category = FilterCategory.Id, Value = "100" };
51+
FilterData model = new() { Category = FilterCategory.Id, Value = Constants.FilterValue100 };
5152

5253
// Act & Assert - First change
5354
model.Category = FilterCategory.Level;
5455
Assert.Null(model.Value);
5556

5657
// Re-set value
57-
model.Value = "Error";
58+
model.Value = Constants.EventLevelError;
5859

5960
// Act & Assert - Second change
6061
model.Category = FilterCategory.Source;
@@ -65,7 +66,7 @@ public void Category_WhenChangedMultipleTimes_ShouldClearValueEachTime()
6566
public void Category_WhenChangedToSameCategory_ShouldStillClearValue()
6667
{
6768
// Arrange
68-
FilterData model = new() { Category = FilterCategory.Id, Value = "100" };
69+
FilterData model = new() { Category = FilterCategory.Id, Value = Constants.FilterValue100 };
6970

7071
// Act
7172
model.Category = FilterCategory.Id;
@@ -78,7 +79,7 @@ public void Category_WhenChangedToSameCategory_ShouldStillClearValue()
7879
public void Category_WhenChangedToSameCategory_ShouldStillClearValues()
7980
{
8081
// Arrange
81-
FilterData model = new() { Category = FilterCategory.Id, Values = ["100", "200"] };
82+
FilterData model = new() { Category = FilterCategory.Id, Values = [Constants.FilterValue100, Constants.FilterValue200] };
8283

8384
// Act
8485
model.Category = FilterCategory.Id;
@@ -94,8 +95,8 @@ public void Category_WhenChangedWithBothValueAndValues_ShouldClearBoth()
9495
FilterData model = new()
9596
{
9697
Category = FilterCategory.Id,
97-
Value = "100",
98-
Values = ["200", "300"]
98+
Value = Constants.FilterValue100,
99+
Values = [Constants.FilterValue200, Constants.FilterValue300]
99100
};
100101

101102
// Act
@@ -138,7 +139,7 @@ public void Evaluator_WhenCategoryChanged_ShouldNotBeCleared()
138139
{
139140
Category = FilterCategory.Id,
140141
Evaluator = FilterEvaluator.NotEqual,
141-
Value = "100"
142+
Value = Constants.FilterValue100
142143
};
143144

144145
// Act
@@ -200,10 +201,10 @@ public void Value_WhenSet_ShouldRetainValue()
200201
FilterData model = new() { Category = FilterCategory.Id };
201202

202203
// Act
203-
model.Value = "500";
204+
model.Value = Constants.FilterValue500;
204205

205206
// Assert
206-
Assert.Equal("500", model.Value);
207+
Assert.Equal(Constants.FilterValue500, model.Value);
207208
}
208209

209210
[Fact]
@@ -213,13 +214,13 @@ public void Values_WhenModifiedDirectly_ShouldReflectChanges()
213214
FilterData model = new() { Category = FilterCategory.Id };
214215

215216
// Act
216-
model.Values.Add("100");
217-
model.Values.Add("200");
217+
model.Values.Add(Constants.FilterValue100);
218+
model.Values.Add(Constants.FilterValue200);
218219

219220
// Assert
220221
Assert.Equal(2, model.Values.Count);
221-
Assert.Contains("100", model.Values);
222-
Assert.Contains("200", model.Values);
222+
Assert.Contains(Constants.FilterValue100, model.Values);
223+
Assert.Contains(Constants.FilterValue200, model.Values);
223224
}
224225

225226
[Fact]
@@ -229,12 +230,12 @@ public void Values_WhenSet_ShouldRetainValues()
229230
FilterData model = new() { Category = FilterCategory.Id };
230231

231232
// Act
232-
model.Values = ["100", "200", "300"];
233+
model.Values = [Constants.FilterValue100, Constants.FilterValue200, Constants.FilterValue300];
233234

234235
// Assert
235236
Assert.Equal(3, model.Values.Count);
236-
Assert.Contains("100", model.Values);
237-
Assert.Contains("200", model.Values);
238-
Assert.Contains("300", model.Values);
237+
Assert.Contains(Constants.FilterValue100, model.Values);
238+
Assert.Contains(Constants.FilterValue200, model.Values);
239+
Assert.Contains(Constants.FilterValue300, model.Values);
239240
}
240241
}

0 commit comments

Comments
 (0)