-
-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathPropertyDepthTests.cs
More file actions
258 lines (215 loc) · 8.5 KB
/
PropertyDepthTests.cs
File metadata and controls
258 lines (215 loc) · 8.5 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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
namespace QueryKit.UnitTests;
using QueryKit.Configuration;
using QueryKit.Exceptions;
using FluentAssertions;
using WebApiTestProject.Entities;
public class PropertyDepthTests
{
// Filter tests for global MaxPropertyDepth
[Fact]
public void filter_with_depth_1_allowed_when_max_depth_is_1()
{
var input = """Email.Value == "test@example.com" """;
var config = new QueryKitConfiguration(settings =>
{
settings.MaxPropertyDepth = 1;
});
var filterExpression = FilterParser.ParseFilter<TestingPerson>(input, config);
filterExpression.Should().NotBeNull();
}
[Fact]
public void filter_with_depth_2_throws_when_max_depth_is_1()
{
var input = """PhysicalAddress.PostalCode.Value == "12345" """;
var config = new QueryKitConfiguration(settings =>
{
settings.MaxPropertyDepth = 1;
});
var act = () => FilterParser.ParseFilter<TestingPerson>(input, config);
act.Should().Throw<QueryKitPropertyDepthExceededException>()
.WithMessage("*PhysicalAddress.PostalCode.Value*depth of 2*maximum allowed depth of 1*");
}
[Fact]
public void filter_with_depth_2_allowed_when_max_depth_is_2()
{
var input = """PhysicalAddress.PostalCode.Value == "12345" """;
var config = new QueryKitConfiguration(settings =>
{
settings.MaxPropertyDepth = 2;
});
var filterExpression = FilterParser.ParseFilter<TestingPerson>(input, config);
filterExpression.Should().NotBeNull();
}
[Fact]
public void filter_with_depth_0_allowed_when_max_depth_is_1()
{
var input = """Title == "Test" """;
var config = new QueryKitConfiguration(settings =>
{
settings.MaxPropertyDepth = 1;
});
var filterExpression = FilterParser.ParseFilter<TestingPerson>(input, config);
filterExpression.Should().NotBeNull();
}
[Fact]
public void filter_with_no_max_depth_allows_any_depth()
{
var input = """PhysicalAddress.PostalCode.Value == "12345" """;
var filterExpression = FilterParser.ParseFilter<TestingPerson>(input);
filterExpression.Should().NotBeNull();
}
// Filter tests for per-property MaxDepth override
[Fact]
public void filter_per_property_max_depth_overrides_global()
{
var input = """PhysicalAddress.PostalCode.Value == "12345" """;
var config = new QueryKitConfiguration(settings =>
{
settings.MaxPropertyDepth = 1; // Global limit of 1
settings.Property<TestingPerson>(x => x.PhysicalAddress).HasMaxDepth(2); // Override for PhysicalAddress
});
var filterExpression = FilterParser.ParseFilter<TestingPerson>(input, config);
filterExpression.Should().NotBeNull();
}
[Fact]
public void filter_per_property_max_depth_can_be_more_restrictive()
{
var input = """PhysicalAddress.PostalCode.Value == "12345" """;
var config = new QueryKitConfiguration(settings =>
{
settings.MaxPropertyDepth = 5; // Global limit of 5
settings.Property<TestingPerson>(x => x.PhysicalAddress).HasMaxDepth(1); // Restrict PhysicalAddress to 1
});
var act = () => FilterParser.ParseFilter<TestingPerson>(input, config);
act.Should().Throw<QueryKitPropertyDepthExceededException>()
.WithMessage("*PhysicalAddress.PostalCode.Value*depth of 2*maximum allowed depth of 1*");
}
[Fact]
public void filter_other_properties_still_use_global_max_depth()
{
var input = """Email.Value == "test@example.com" """;
var config = new QueryKitConfiguration(settings =>
{
settings.MaxPropertyDepth = 1;
settings.Property<TestingPerson>(x => x.PhysicalAddress).HasMaxDepth(2);
});
// Email.Value has depth 1, which is within global limit
var filterExpression = FilterParser.ParseFilter<TestingPerson>(input, config);
filterExpression.Should().NotBeNull();
}
// Sort tests for global MaxPropertyDepth
[Fact]
public void sort_with_depth_1_allowed_when_max_depth_is_1()
{
var input = "Email.Value";
var config = new QueryKitConfiguration(settings =>
{
settings.MaxPropertyDepth = 1;
});
var act = () => SortParser.ParseSort<TestingPerson>(input, config);
act.Should().NotThrow();
}
[Fact]
public void sort_with_depth_2_throws_when_max_depth_is_1()
{
var input = "PhysicalAddress.PostalCode.Value";
var config = new QueryKitConfiguration(settings =>
{
settings.MaxPropertyDepth = 1;
});
var act = () => SortParser.ParseSort<TestingPerson>(input, config);
act.Should().Throw<QueryKitPropertyDepthExceededException>()
.WithMessage("*PhysicalAddress.PostalCode.Value*depth of 2*maximum allowed depth of 1*");
}
[Fact]
public void sort_with_depth_2_allowed_when_max_depth_is_2()
{
var input = "PhysicalAddress.PostalCode.Value";
var config = new QueryKitConfiguration(settings =>
{
settings.MaxPropertyDepth = 2;
});
var act = () => SortParser.ParseSort<TestingPerson>(input, config);
act.Should().NotThrow();
}
[Fact]
public void sort_with_no_max_depth_allows_any_depth()
{
var input = "PhysicalAddress.PostalCode.Value";
var act = () => SortParser.ParseSort<TestingPerson>(input, null);
act.Should().NotThrow();
}
// Sort tests for per-property MaxDepth override
[Fact]
public void sort_per_property_max_depth_overrides_global()
{
var input = "PhysicalAddress.PostalCode.Value";
var config = new QueryKitConfiguration(settings =>
{
settings.MaxPropertyDepth = 1; // Global limit of 1
settings.Property<TestingPerson>(x => x.PhysicalAddress).HasMaxDepth(2); // Override for PhysicalAddress
});
var act = () => SortParser.ParseSort<TestingPerson>(input, config);
act.Should().NotThrow();
}
[Fact]
public void sort_per_property_max_depth_can_be_more_restrictive()
{
var input = "PhysicalAddress.PostalCode.Value";
var config = new QueryKitConfiguration(settings =>
{
settings.MaxPropertyDepth = 5; // Global limit of 5
settings.Property<TestingPerson>(x => x.PhysicalAddress).HasMaxDepth(1); // Restrict PhysicalAddress to 1
});
var act = () => SortParser.ParseSort<TestingPerson>(input, config);
act.Should().Throw<QueryKitPropertyDepthExceededException>()
.WithMessage("*PhysicalAddress.PostalCode.Value*depth of 2*maximum allowed depth of 1*");
}
// Property list grouping tests
[Fact]
public void filter_property_list_respects_max_depth()
{
var input = """(Title, PhysicalAddress.PostalCode.Value) @=* "test" """;
var config = new QueryKitConfiguration(settings =>
{
settings.MaxPropertyDepth = 1;
});
var act = () => FilterParser.ParseFilter<TestingPerson>(input, config);
act.Should().Throw<QueryKitPropertyDepthExceededException>();
}
[Fact]
public void filter_property_list_allows_valid_depth()
{
var input = """(Title, Email.Value) @=* "test" """;
var config = new QueryKitConfiguration(settings =>
{
settings.MaxPropertyDepth = 1;
});
var filterExpression = FilterParser.ParseFilter<TestingPerson>(input, config);
filterExpression.Should().NotBeNull();
}
// Edge cases
[Fact]
public void filter_max_depth_of_0_only_allows_root_properties()
{
var input = """Email.Value == "test@example.com" """;
var config = new QueryKitConfiguration(settings =>
{
settings.MaxPropertyDepth = 0;
});
var act = () => FilterParser.ParseFilter<TestingPerson>(input, config);
act.Should().Throw<QueryKitPropertyDepthExceededException>()
.WithMessage("*Email.Value*depth of 1*maximum allowed depth of 0*");
}
[Fact]
public void filter_root_property_allowed_when_max_depth_is_0()
{
var input = """Title == "Test" """;
var config = new QueryKitConfiguration(settings =>
{
settings.MaxPropertyDepth = 0;
});
var filterExpression = FilterParser.ParseFilter<TestingPerson>(input, config);
filterExpression.Should().NotBeNull();
}
}