Skip to content

Commit bd736ae

Browse files
committed
feat: enum handling with tests
1 parent f223a03 commit bd736ae

9 files changed

Lines changed: 69 additions & 26 deletions

File tree

QueryKit.IntegrationTests/Tests/DatabaseFilteringTests.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -501,21 +501,21 @@ public async Task can_filter_complex_expression()
501501
var fakePersonOne = new FakeTestingPersonBuilder()
502502
.WithTitle("Waffle & Chicken")
503503
.WithAge(35)
504-
.WithBirthMonth("January")
504+
.WithBirthMonth(BirthMonthEnum.January)
505505
.WithRating(4.0M)
506506
.WithSpecificDate(new DateTime(2022, 07, 01, 00, 00, 03, DateTimeKind.Utc))
507507
.WithDate(DateOnly.FromDateTime(new DateTime(2022, 07, 01)))
508508
.Build();
509509
var fakePersonTwo = new FakeTestingPersonBuilder()
510510
.WithTitle("Lamb")
511511
.WithAge(17)
512-
.WithBirthMonth("February")
512+
.WithBirthMonth(BirthMonthEnum.February)
513513
.WithRating(3.4M)
514514
.WithSpecificDate(new DateTime(2022, 07, 01, 00, 00, 03, DateTimeKind.Utc))
515515
.Build();
516516
await testingServiceScope.InsertAsync(fakePersonOne, fakePersonTwo);
517517

518-
var input = $"""""((Title @=* "waffle & chicken" && Age > 30) || Id == "{fakePersonOne.Id}" || Title == "lamb" || Title == null) && (Age < 18 || (BirthMonth == "January" && Title _= "ally")) || Rating > 3.5 || SpecificDate == 2022-07-01T00:00:03Z && (Date == 2022-07-01 || Time == 00:00:03)""""";
518+
var input = $"""""((Title @=* "waffle & chicken" && Age > 30) || Id == "{fakePersonOne.Id}" || Title == "lamb" || Title == null) && (Age < 18 || (BirthMonth == 1 && Title _= "ally")) || Rating > 3.5 || SpecificDate == 2022-07-01T00:00:03Z && (Date == 2022-07-01 || Time == 00:00:03)""""";
519519

520520
// Act
521521
var queryablePeople = testingServiceScope.DbContext().People;

QueryKit.IntegrationTests/Tests/DatabaseSortingTests.cs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,22 +16,22 @@ public async Task can_sort_items_with_mixed_order_directions()
1616
var fakePersonOne = new FakeTestingPersonBuilder()
1717
.WithTitle("alpha")
1818
.WithAge(10)
19-
.WithBirthMonth("January")
19+
.WithBirthMonth(BirthMonthEnum.January)
2020
.Build();
2121
var fakePersonTwo = new FakeTestingPersonBuilder()
2222
.WithTitle("beta")
2323
.WithAge(100)
24-
.WithBirthMonth("February")
24+
.WithBirthMonth(BirthMonthEnum.February)
2525
.Build();
2626
var fakePersonThree = new FakeTestingPersonBuilder()
2727
.WithTitle("beta")
2828
.WithAge(50)
29-
.WithBirthMonth("March")
29+
.WithBirthMonth(BirthMonthEnum.March)
3030
.Build();
3131
var fakePersonFour = new FakeTestingPersonBuilder()
3232
.WithTitle("beta")
3333
.WithAge(20)
34-
.WithBirthMonth("April")
34+
.WithBirthMonth(BirthMonthEnum.April)
3535
.Build();
3636
await testingServiceScope.InsertAsync(fakePersonOne, fakePersonTwo, fakePersonThree, fakePersonFour);
3737

@@ -177,12 +177,12 @@ public async Task can_sort_multiple_items_with_same_order_direction()
177177
var fakePersonOne = new FakeTestingPersonBuilder()
178178
.WithTitle("alpha")
179179
.WithAge(10)
180-
.WithBirthMonth("January")
180+
.WithBirthMonth(BirthMonthEnum.January)
181181
.Build();
182182
var fakePersonTwo = new FakeTestingPersonBuilder()
183183
.WithTitle("beta")
184184
.WithAge(100)
185-
.WithBirthMonth("February")
185+
.WithBirthMonth(BirthMonthEnum.February)
186186
.Build();
187187
await testingServiceScope.InsertAsync(fakePersonOne, fakePersonTwo);
188188

@@ -213,17 +213,17 @@ public async Task can_sort_items_with_null_value_property()
213213
var fakePersonTwo = new FakeTestingPersonBuilder()
214214
.WithTitle("alpha")
215215
.WithAge(100)
216-
.WithBirthMonth("January")
216+
.WithBirthMonth(BirthMonthEnum.January)
217217
.Build();
218218
var fakePersonThree = new FakeTestingPersonBuilder()
219219
.WithTitle("beta")
220220
.WithAge(50)
221-
.WithBirthMonth("March")
221+
.WithBirthMonth(BirthMonthEnum.March)
222222
.Build();
223223
var fakePersonFour = new FakeTestingPersonBuilder()
224224
.WithTitle("beta")
225225
.WithAge(20)
226-
.WithBirthMonth("February")
226+
.WithBirthMonth(BirthMonthEnum.February)
227227
.Build();
228228
await testingServiceScope.InsertAsync(fakePersonOne, fakePersonTwo, fakePersonThree, fakePersonFour);
229229

QueryKit.WebApiTestProject/Entities/TestingPerson.cs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ public class TestingPerson
44
{
55
public string? Title { get; set; }
66
public int? Age { get; set; }
7-
public string? BirthMonth { get; set; }
7+
public BirthMonthEnum? BirthMonth { get; set; }
88
public decimal? Rating { get; set; }
99
public DateOnly? Date { get; set; }
1010
public bool? Favorite { get; set; }
@@ -14,4 +14,20 @@ public class TestingPerson
1414
public Guid Id { get; set; } = Guid.NewGuid();
1515
public EmailAddress Email { get; set; }
1616
public Address PhysicalAddress { get; set; }
17+
}
18+
19+
public enum BirthMonthEnum
20+
{
21+
January = 1,
22+
February = 2,
23+
March = 3,
24+
April = 4,
25+
May = 5,
26+
June = 6,
27+
July = 7,
28+
August = 8,
29+
September = 9,
30+
October = 10,
31+
November = 11,
32+
December = 12
1733
}

QueryKit.WebApiTestProject/Migrations/20230724020352_BaseTestingMigration.Designer.cs renamed to QueryKit.WebApiTestProject/Migrations/20231222014351_BaseTestingMigration.Designer.cs

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

QueryKit.WebApiTestProject/Migrations/20230724020352_BaseTestingMigration.cs renamed to QueryKit.WebApiTestProject/Migrations/20231222014351_BaseTestingMigration.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ protected override void Up(MigrationBuilder migrationBuilder)
2222
id = table.Column<Guid>(type: "uuid", nullable: false),
2323
title = table.Column<string>(type: "text", nullable: true),
2424
age = table.Column<int>(type: "integer", nullable: true),
25-
birth_month = table.Column<string>(type: "text", nullable: true),
25+
birth_month = table.Column<int>(type: "integer", nullable: true),
2626
rating = table.Column<decimal>(type: "numeric", nullable: true),
2727
date = table.Column<DateOnly>(type: "date", nullable: true),
2828
favorite = table.Column<bool>(type: "boolean", nullable: true),

QueryKit.WebApiTestProject/Migrations/TestingDbContextModelSnapshot.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ protected override void BuildModel(ModelBuilder modelBuilder)
1818
{
1919
#pragma warning disable 612, 618
2020
modelBuilder
21-
.HasAnnotation("ProductVersion", "7.0.5")
21+
.HasAnnotation("ProductVersion", "8.0.0")
2222
.HasAnnotation("Relational:MaxIdentifierLength", 63);
2323

2424
NpgsqlModelBuilderExtensions.HasPostgresExtension(modelBuilder, "fuzzystrmatch");
@@ -174,8 +174,8 @@ protected override void BuildModel(ModelBuilder modelBuilder)
174174
.HasColumnType("integer")
175175
.HasColumnName("age");
176176

177-
b.Property<string>("BirthMonth")
178-
.HasColumnType("text")
177+
b.Property<int?>("BirthMonth")
178+
.HasColumnType("integer")
179179
.HasColumnName("birth_month");
180180

181181
b.Property<DateOnly?>("Date")

QueryKit/FilterParser.cs

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ private static Expression CreateRightExprFromType(Type leftExprType, string righ
180180
}
181181

182182
var rawType = targetType;
183-
183+
184184
targetType = TransformTargetTypeIfNullable(targetType);
185185

186186
if (TypeConversionFunctions.TryGetValue(targetType, out var conversionFunction))
@@ -304,10 +304,22 @@ private static Expression CreateRightExprFromType(Type leftExprType, string righ
304304
return Expression.Constant(convertedValue, leftExprType);
305305
}
306306

307-
if (targetType.IsEnum)
307+
if (rawType.IsEnum || (Nullable.GetUnderlyingType(rawType)?.IsEnum ?? false))
308308
{
309-
var enumValue = Enum.Parse(targetType, right);
310-
return Expression.Constant(enumValue, targetType);
309+
var enumType = Nullable.GetUnderlyingType(rawType) ?? rawType;
310+
311+
if (right == "null" && Nullable.GetUnderlyingType(rawType) != null)
312+
{
313+
return Expression.Constant(null, rawType);
314+
}
315+
316+
var enumValue = Enum.Parse(enumType, right);
317+
var constant = Expression.Constant(enumValue, enumType);
318+
319+
if (rawType == enumType) return constant;
320+
321+
var nullableCtor = rawType.GetConstructor(new[] {enumType});
322+
return Expression.New(nullableCtor, constant);
311323
}
312324

313325
throw new InvalidOperationException($"Unsupported value '{right}' for type '{targetType.Name}'");

README.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,21 @@ If you want to filter on the count of a collection, you can prefix some of the o
187187
var input = """"Ingredients #>= 0"""";
188188
```
189189

190+
#### Filtering Enums
191+
192+
You can filter enums with their respective integer value:
193+
194+
```csharp
195+
var input = "BirthMonth == 1";
196+
197+
public enum BirthMonthEnum
198+
{
199+
January = 1,
200+
February = 2,
201+
//...
202+
}
203+
```
204+
190205
### Settings
191206

192207
#### Property Settings

SharedTestingHelper/Fakes/FakeTestingPersonBuilder.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public FakeTestingPersonBuilder WithEmail(string email)
2727
return this;
2828
}
2929

30-
public FakeTestingPersonBuilder WithBirthMonth(string value)
30+
public FakeTestingPersonBuilder WithBirthMonth(BirthMonthEnum? value)
3131
{
3232
_baseTestingPerson.BirthMonth = value;
3333
return this;

0 commit comments

Comments
 (0)