-
-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathCustomFilterPropertyTests.cs
More file actions
236 lines (207 loc) · 9.21 KB
/
Copy pathCustomFilterPropertyTests.cs
File metadata and controls
236 lines (207 loc) · 9.21 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
namespace QueryKit.UnitTests;
using Bogus;
using Configuration;
using Exceptions;
using FluentAssertions;
using Operators;
using WebApiTestProject.Entities;
using WebApiTestProject.Entities.Recipes;
public class CustomFilterPropertyTests
{
[Fact]
public void can_have_child_prop_name_ownsone()
{
var faker = new Faker();
var value = faker.Lorem.Word();
var input = $"""PhysicalAddress.State == "{value}" """;
var filterExpression = FilterParser.ParseFilter<TestingPerson>(input);
filterExpression.ToString().Should().Be($"""x => (x.PhysicalAddress.State == "{value}")""");
}
[Fact]
public void can_have_custom_child_prop_name_ownsone()
{
var faker = new Faker();
var value = faker.Lorem.Word();
var input = $"""state == "{value}" """;
var config = new QueryKitConfiguration(config =>
{
config.Property<TestingPerson>(x => x.PhysicalAddress.State).HasQueryName("state");
});
var filterExpression = FilterParser.ParseFilter<TestingPerson>(input, config);
filterExpression.ToString().Should().Be($"""x => (x.PhysicalAddress.State == "{value}")""");
}
[Fact(Skip = "Will need something like this if i want to support HasConversion in efcore.")]
public void can_have_child_prop_name_for_efcore_HasConversion()
{
var faker = new Faker();
var value = faker.Lorem.Word();
var input = $"""Email.Value == "{value}" """;
var filterExpression = FilterParser.ParseFilter<TestingPerson>(input);
filterExpression.ToString().Should().Be($"""x => (x.Email == "{value}")""");
}
[Fact(Skip = "Will need something like this if i want to support HasConversion in efcore.")]
public void can_have_custom_child_prop_name_for_efcore_HasConversion()
{
var faker = new Faker();
var value = faker.Lorem.Word();
var input = $"""email == "{value}" """;
var config = new QueryKitConfiguration(config =>
{
config.Property<TestingPerson>(x => x.Email).HasQueryName("email");
});
var filterExpression = FilterParser.ParseFilter<TestingPerson>(input, config);
filterExpression.ToString().Should().Be($"""x => (x.Email == "{value}")""");
}
[Fact]
public void can_have_custom_prop_name_for_string()
{
var faker = new Faker();
var value = faker.Lorem.Word();
var input = $"""special_title == "{value}" """;
var config = new QueryKitConfiguration(config =>
{
config.Property<TestingPerson>(x => x.Title).HasQueryName("special_title");
});
var filterExpression = FilterParser.ParseFilter<TestingPerson>(input, config);
filterExpression.ToString().Should().Be($"""x => (x.Title == "{value}")""");
}
[Fact]
public void can_handle_alias_in_value()
{
var faker = new Faker();
var value = faker.Lorem.Word();
var input = $"""special_title == "{value} with special_value" """;
var config = new QueryKitConfiguration(config =>
{
config.Property<TestingPerson>(x => x.Title).HasQueryName("special_title");
});
var filterExpression = FilterParser.ParseFilter<TestingPerson>(input, config);
filterExpression.ToString().Should().Be($"""x => (x.Title == "{value} with special_value")""");
}
[Fact]
public void can_handle_alias_in_value_with_operator_after_it()
{
var faker = new Faker();
var value = faker.Lorem.Word();
var input = $"""special_title == "{value} with special_value @=* a thing" """;
var config = new QueryKitConfiguration(config =>
{
config.Property<TestingPerson>(x => x.Title).HasQueryName("special_title");
});
var filterExpression = FilterParser.ParseFilter<TestingPerson>(input, config);
filterExpression.ToString().Should().Be($"""x => (x.Title == "{value} with special_value @=* a thing")""");
}
[Fact]
public void can_have_custom_prop_name_for_multiple_props()
{
var faker = new Faker();
var stringValue = faker.Lorem.Word();
var guidValue = Guid.NewGuid();
var input = $"""special_title == "{stringValue}" || identifier == "{guidValue}" """;
var config = new QueryKitConfiguration(config =>
{
config.Property<TestingPerson>(x => x.Title).HasQueryName("special_title");
config.Property<TestingPerson>(x => x.Id).HasQueryName("identifier");
});
var filterExpression = FilterParser.ParseFilter<TestingPerson>(input, config);
filterExpression.ToString().Should().Be($"""x => ((x.Title == "{stringValue}") OrElse (x.Id.ToString() == "{guidValue}"))""");
}
[Fact]
public void can_have_custom_prop_name_for_some_props()
{
var faker = new Faker();
var stringValue = faker.Lorem.Word();
var guidValue = Guid.NewGuid();
var input = $"""special_title == "{stringValue}" || Id == "{guidValue}" """;
var config = new QueryKitConfiguration(config =>
{
config.Property<TestingPerson>(x => x.Title).HasQueryName("special_title");
});
var filterExpression = FilterParser.ParseFilter<TestingPerson>(input, config);
filterExpression.ToString().Should().Be($"""x => ((x.Title == "{stringValue}") OrElse (x.Id.ToString() == "{guidValue}"))""");
}
[Fact]
public void can_handle_case_insensitive_custom_props()
{
var faker = new Faker();
var value = faker.Lorem.Word();
var input = $"""SPECIALtitle == "{value}" """;
var config = new QueryKitConfiguration(config =>
{
config.Property<TestingPerson>(x => x.Title).HasQueryName("specialtitle");
});
var filterExpression = FilterParser.ParseFilter<TestingPerson>(input, config);
filterExpression.ToString().Should().Be($"""x => (x.Title == "{value}")""");
}
[Fact]
public void can_have_custom_prop_excluded_from_filter()
{
var faker = new Faker();
var stringValue = faker.Lorem.Word();
var guidValue = Guid.NewGuid();
var input = $"""special_title == "{stringValue}" || Id == "{guidValue}" """;
var config = new QueryKitConfiguration(config =>
{
config.Property<TestingPerson>(x => x.Title).HasQueryName("special_title");
config.Property<TestingPerson>(x => x.Id).PreventFilter();
});
var filterExpression = FilterParser.ParseFilter<TestingPerson>(input, config);
filterExpression.ToString().Should().Be($"""x => ((x.Title == "{stringValue}") OrElse (True == True))""");
}
[Fact]
public void can_have_custom_prop_excluded_from_filter_with_custom_propname()
{
var faker = new Faker();
var stringValue = faker.Lorem.Word();
var guidValue = Guid.NewGuid();
var input = $"""special_title == "{stringValue}" || identifier == "{guidValue}" """;
var config = new QueryKitConfiguration(config =>
{
config.Property<TestingPerson>(x => x.Title).HasQueryName("special_title");
config.Property<TestingPerson>(x => x.Id).HasQueryName("identifier").PreventFilter();
});
var filterExpression = FilterParser.ParseFilter<TestingPerson>(input, config);
filterExpression.ToString().Should().Be($"""x => ((x.Title == "{stringValue}") OrElse (True == True))""");
}
[Fact]
public void filter_prevented_props_always_have_true_equals_true_regardless_of_comparison()
{
var faker = new Faker();
var filterOperator = faker.PickRandom(ComparisonOperator.List.Where(x => x != ComparisonOperator.EqualsOperator()).ToList());
var guidValue = Guid.NewGuid();
var input = $"""Id {filterOperator} "{guidValue}" """;
var config = new QueryKitConfiguration(config =>
{
config.Property<TestingPerson>(x => x.Id).PreventFilter();
});
var filterExpression = FilterParser.ParseFilter<TestingPerson>(input, config);
filterExpression.ToString().Should().Be($"""x => (True == True)""");
}
[Fact]
public void can_throw_error_when_property_has_space()
{
var faker = new Faker();
var propertyName = faker.Lorem.Sentence();
var firstWord = propertyName.Split(' ').First();
var input = $"""{propertyName} == 25""";
var config = new QueryKitConfiguration(config =>
{
config.Property<TestingPerson>(x => x.Id).PreventFilter();
});
var act = () => FilterParser.ParseFilter<TestingPerson>(input, config);
act.Should().Throw<UnknownFilterPropertyException>()
.WithMessage($"The filter property '{firstWord}' was not recognized.");
}
[Fact]
public void can_handle_nonexistent_property()
{
var faker = new Faker();
var input = $"""{faker.Lorem.Word()} == 25""";
var config = new QueryKitConfiguration(config =>
{
config.AllowUnknownProperties = true;
});
var filterExpression = FilterParser.ParseFilter<TestingPerson>(input, config);
filterExpression.ToString().Should().Be("x => (True == True)");
}
}