-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathAndSpecificationTests.cs
More file actions
182 lines (138 loc) · 6.35 KB
/
AndSpecificationTests.cs
File metadata and controls
182 lines (138 loc) · 6.35 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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
using DKNet.EfCore.Specifications;
using EfCore.Repos.Tests.TestEntities;
using Shouldly;
namespace EfCore.Repos.Tests;
/// <summary>
/// Tests for the AndSpecification class functionality
/// </summary>
public class AndSpecificationTests
{
[Fact]
public void Constructor_WithBothSpecificationsHavingFilters_ShouldCombineWithAnd()
{
// Arrange
var leftSpec = new TestSpecification();
leftSpec.AddTestFilter(u => u.FirstName == "John");
var rightSpec = new TestSpecification();
rightSpec.AddTestFilter(u => u.LastName == "Doe");
// Act
var andSpec = new AndSpecification<User>(leftSpec, rightSpec);
// Assert
andSpec.FilterQuery.ShouldNotBeNull();
// Test combined filter with matching entity
var matchingUser = new User("test") { FirstName = "John", LastName = "Doe" };
andSpec.Match(matchingUser).ShouldBeTrue();
// Test combined filter with non-matching entity (wrong first name)
var nonMatchingUser1 = new User("test") { FirstName = "Jane", LastName = "Doe" };
andSpec.Match(nonMatchingUser1).ShouldBeFalse();
// Test combined filter with non-matching entity (wrong last name)
var nonMatchingUser2 = new User("test") { FirstName = "John", LastName = "Smith" };
andSpec.Match(nonMatchingUser2).ShouldBeFalse();
}
[Fact]
public void Constructor_WithLeftSpecificationNull_ShouldUseRightSpecification()
{
// Arrange
var leftSpec = new TestSpecification(); // No filter
var rightSpec = new TestSpecification();
rightSpec.AddTestFilter(u => u.LastName == "Doe");
// Act
var andSpec = new AndSpecification<User>(leftSpec, rightSpec);
// Assert
andSpec.FilterQuery.ShouldNotBeNull();
var user = new User("test") { FirstName = "John", LastName = "Doe" };
andSpec.Match(user).ShouldBeTrue();
var user2 = new User("test") { FirstName = "John", LastName = "Smith" };
andSpec.Match(user2).ShouldBeFalse();
}
[Fact]
public void Constructor_WithRightSpecificationNull_ShouldUseLeftSpecification()
{
// Arrange
var leftSpec = new TestSpecification();
leftSpec.AddTestFilter(u => u.FirstName == "John");
var rightSpec = new TestSpecification(); // No filter
// Act
var andSpec = new AndSpecification<User>(leftSpec, rightSpec);
// Assert
andSpec.FilterQuery.ShouldNotBeNull();
var user = new User("test") { FirstName = "John", LastName = "Doe" };
andSpec.Match(user).ShouldBeTrue();
var user2 = new User("test") { FirstName = "Jane", LastName = "Doe" };
andSpec.Match(user2).ShouldBeFalse();
}
[Fact]
public void Constructor_WithBothSpecificationsNull_ShouldHaveNullFilter()
{
// Arrange
var leftSpec = new TestSpecification(); // No filter
var rightSpec = new TestSpecification(); // No filter
// Act
var andSpec = new AndSpecification<User>(leftSpec, rightSpec);
// Assert
andSpec.FilterQuery.ShouldBeNull();
var user = new User("test") { FirstName = "John", LastName = "Doe" };
andSpec.Match(user).ShouldBeFalse(); // Returns false when FilterQuery is null
}
[Fact]
public void Constructor_WithComplexExpressions_ShouldCombineCorrectly()
{
// Arrange
var leftSpec = new TestSpecification();
leftSpec.AddTestFilter(u => u.FirstName.StartsWith("J"));
var rightSpec = new TestSpecification();
rightSpec.AddTestFilter(u => u.LastName.Length > 3);
// Act
var andSpec = new AndSpecification<User>(leftSpec, rightSpec);
// Assert
andSpec.FilterQuery.ShouldNotBeNull();
// Test with user that matches both conditions
var matchingUser = new User("test") { FirstName = "John", LastName = "Smith" }; // J* and length > 3
andSpec.Match(matchingUser).ShouldBeTrue();
// Test with user that matches first but not second
var nonMatchingUser1 = new User("test") { FirstName = "John", LastName = "Do" }; // J* but length <= 3
andSpec.Match(nonMatchingUser1).ShouldBeFalse();
// Test with user that matches second but not first
var nonMatchingUser2 = new User("test") { FirstName = "Mike", LastName = "Johnson" }; // Not J* but length > 3
andSpec.Match(nonMatchingUser2).ShouldBeFalse();
}
[Fact]
public void Constructor_WithNestedAndSpecifications_ShouldWork()
{
// Arrange
var spec1 = new TestSpecification();
spec1.AddTestFilter(u => u.FirstName == "John");
var spec2 = new TestSpecification();
spec2.AddTestFilter(u => u.LastName == "Doe");
var spec3 = new TestSpecification();
spec3.AddTestFilter(u => u.FirstName.Length > 2);
var andSpec1 = new AndSpecification<User>(spec1, spec2);
// Act
var nestedAndSpec = new AndSpecification<User>(andSpec1, spec3);
// Assert
nestedAndSpec.FilterQuery.ShouldNotBeNull();
// Test with user that matches all three conditions
var matchingUser = new User("test") { FirstName = "John", LastName = "Doe" };
nestedAndSpec.Match(matchingUser).ShouldBeTrue();
// Test with user that doesn't match first condition
var nonMatchingUser = new User("test") { FirstName = "Jane", LastName = "Doe" };
nestedAndSpec.Match(nonMatchingUser).ShouldBeFalse();
}
[Fact]
public void Constructor_WithIdenticalSpecifications_ShouldWork()
{
// Arrange
var spec1 = new TestSpecification();
spec1.AddTestFilter(u => u.FirstName == "John");
var spec2 = new TestSpecification();
spec2.AddTestFilter(u => u.FirstName == "John");
// Act
var andSpec = new AndSpecification<User>(spec1, spec2);
// Assert
andSpec.FilterQuery.ShouldNotBeNull();
var matchingUser = new User("test") { FirstName = "John", LastName = "Doe" };
andSpec.Match(matchingUser).ShouldBeTrue();
var nonMatchingUser = new User("test") { FirstName = "Jane", LastName = "Doe" };
andSpec.Match(nonMatchingUser).ShouldBeFalse();
}
}