Skip to content

Commit 1417e7b

Browse files
committed
feat: test new .net 8 type
1 parent ec2a810 commit 1417e7b

File tree

11 files changed

+133
-7
lines changed

11 files changed

+133
-7
lines changed

QueryKit.IntegrationTests/Tests/DatabaseFilteringTests.cs

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -467,6 +467,35 @@ public async Task can_filter_nested_property_using_ownsone()
467467
people[0].Id.Should().Be(fakePersonOne.Id);
468468
}
469469

470+
[Fact]
471+
public async Task can_filter_nested_property_using_ownsone_with_alias()
472+
{
473+
// Arrange
474+
var testingServiceScope = new TestingServiceScope();
475+
var faker = new Faker();
476+
var fakePersonOne = new FakeTestingPersonBuilder()
477+
.WithPhysicalAddress(new Address(faker.Address.StreetAddress()
478+
, faker.Address.SecondaryAddress()
479+
, faker.Address.City()
480+
, faker.Address.State()
481+
, faker.Address.ZipCode()
482+
, faker.Address.Country()))
483+
.Build();
484+
await testingServiceScope.InsertAsync(fakePersonOne);
485+
var input = $"""state == "{fakePersonOne.PhysicalAddress.State}" """;
486+
487+
// Act
488+
var config = new QueryKitConfiguration(config =>
489+
{
490+
config.Property<TestingPerson>(x => x.PhysicalAddress.State).HasQueryName("state");
491+
});
492+
var people = testingServiceScope.DbContext().People.ApplyQueryKitFilter(input, config).ToList();
493+
494+
// Assert
495+
people.Count.Should().Be(1);
496+
people[0].Id.Should().Be(fakePersonOne.Id);
497+
}
498+
470499
[Fact]
471500
public async Task can_filter_by_decimal()
472501
{
@@ -727,4 +756,62 @@ public async Task can_filter_on_child_entity_with_config()
727756
people.FirstOrDefault(x => x.Id == fakeRecipeOne.Id).Should().NotBeNull();
728757
people.FirstOrDefault(x => x.Id == fakeRecipeTwo.Id).Should().BeNull();
729758
}
759+
760+
[Fact]
761+
public async Task can_filter_with_child_props_for_complex_property()
762+
{
763+
// Arrange
764+
var testingServiceScope = new TestingServiceScope();
765+
var faker = new Faker();
766+
var fakePersonOne = new FakeRecipeBuilder()
767+
.WithCollectionEmail(faker.Internet.Email())
768+
.Build();
769+
var fakePersonTwo = new FakeRecipeBuilder()
770+
.Build();
771+
await testingServiceScope.InsertAsync(fakePersonOne, fakePersonTwo);
772+
773+
var input = $"""CollectionEmail.Value == "{fakePersonOne.CollectionEmail.Value}" """;
774+
775+
// Act
776+
var queryablePeople = testingServiceScope.DbContext().Recipes;
777+
var config = new QueryKitConfiguration(config =>
778+
{
779+
config.Property<Recipe>(x => x.CollectionEmail.Value);//.HasQueryName("email");
780+
});
781+
var appliedQueryable = queryablePeople.ApplyQueryKitFilter(input, config);
782+
var people = await appliedQueryable.ToListAsync();
783+
784+
// Assert
785+
people.Count.Should().Be(1);
786+
people[0].Id.Should().Be(fakePersonOne.Id);
787+
}
788+
789+
[Fact]
790+
public async Task can_filter_with_child_props_for_complex_property_with_alias()
791+
{
792+
// Arrange
793+
var testingServiceScope = new TestingServiceScope();
794+
var faker = new Faker();
795+
var fakePersonOne = new FakeRecipeBuilder()
796+
.WithCollectionEmail(faker.Internet.Email())
797+
.Build();
798+
var fakePersonTwo = new FakeRecipeBuilder()
799+
.Build();
800+
await testingServiceScope.InsertAsync(fakePersonOne, fakePersonTwo);
801+
802+
var input = $"""email == "{fakePersonOne.CollectionEmail.Value}" """;
803+
804+
// Act
805+
var queryablePeople = testingServiceScope.DbContext().Recipes;
806+
var config = new QueryKitConfiguration(config =>
807+
{
808+
config.Property<Recipe>(x => x.CollectionEmail.Value).HasQueryName("email");
809+
});
810+
var appliedQueryable = queryablePeople.ApplyQueryKitFilter(input, config);
811+
var people = await appliedQueryable.ToListAsync();
812+
813+
// Assert
814+
people.Count.Should().Be(1);
815+
people[0].Id.Should().Be(fakePersonOne.Id);
816+
}
730817
}

QueryKit.WebApiTestProject/Database/RecipeConfiguration.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ public void Configure(EntityTypeBuilder<Recipe> builder)
1313
{
1414
builder.Property(x => x.Tags).HasColumnType("text[]");
1515

16+
builder.ComplexProperty(x => x.CollectionEmail,
17+
x => x.Property(y => y.Value)
18+
.HasColumnName("collection_email"));
19+
1620
// example for a simple 1:1 value object
1721
// builder.Property(x => x.Percent)
1822
// .HasConversion(x => x.Value, x => new Percent(x))

QueryKit.WebApiTestProject/Entities/Recipes/Models/RecipeForCreation.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,5 @@ public sealed class RecipeForCreation
88
public int? Rating { get; set; }
99
public DateOnly? DateOfOrigin { get; set; }
1010
public bool HaveMadeItMyself { get; set; }
11-
11+
public string CollectionEmail { get; set; }
1212
}

QueryKit.WebApiTestProject/Entities/Recipes/Models/RecipeForUpdate.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,5 @@ public sealed class RecipeForUpdate
88
public int? Rating { get; set; }
99
public DateOnly? DateOfOrigin { get; set; }
1010
public bool HaveMadeItMyself { get; set; }
11-
11+
public string CollectionEmail { get; set; }
1212
}

QueryKit.WebApiTestProject/Entities/Recipes/Recipe.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ private set
2323
}
2424
}
2525

26+
public EmailAddress CollectionEmail { get; private set; }
27+
2628
public string Directions { get; private set; }
2729

2830
public int? Rating { get; private set; }
@@ -55,6 +57,7 @@ public static Recipe Create(RecipeForCreation recipeForCreation)
5557
newRecipe.Rating = recipeForCreation.Rating;
5658
newRecipe.DateOfOrigin = recipeForCreation.DateOfOrigin;
5759
newRecipe.HaveMadeItMyself = recipeForCreation.HaveMadeItMyself;
60+
newRecipe.CollectionEmail = new EmailAddress(recipeForCreation.CollectionEmail);
5861
return newRecipe;
5962
}
6063

@@ -66,6 +69,7 @@ public Recipe Update(RecipeForUpdate recipeForUpdate)
6669
Rating = recipeForUpdate.Rating;
6770
DateOfOrigin = recipeForUpdate.DateOfOrigin;
6871
HaveMadeItMyself = recipeForUpdate.HaveMadeItMyself;
72+
CollectionEmail = new EmailAddress(recipeForUpdate.CollectionEmail);
6973
return this;
7074
}
7175

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

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

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,8 @@ protected override void Up(MigrationBuilder migrationBuilder)
5353
rating = table.Column<int>(type: "integer", nullable: true),
5454
date_of_origin = table.Column<DateOnly>(type: "date", nullable: true),
5555
have_made_it_myself = table.Column<bool>(type: "boolean", nullable: false),
56-
tags = table.Column<List<string>>(type: "text[]", nullable: false)
56+
tags = table.Column<List<string>>(type: "text[]", nullable: false),
57+
collection_email = table.Column<string>(type: "text", nullable: true)
5758
},
5859
constraints: table =>
5960
{

QueryKit.WebApiTestProject/Migrations/TestingDbContextModelSnapshot.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,15 @@ protected override void BuildModel(ModelBuilder modelBuilder)
157157
.HasColumnType("text")
158158
.HasColumnName("visibility");
159159

160+
b.ComplexProperty<Dictionary<string, object>>("CollectionEmail", "QueryKit.WebApiTestProject.Entities.Recipes.Recipe.CollectionEmail#EmailAddress", b1 =>
161+
{
162+
b1.IsRequired();
163+
164+
b1.Property<string>("Value")
165+
.HasColumnType("text")
166+
.HasColumnName("collection_email");
167+
});
168+
160169
b.HasKey("Id")
161170
.HasName("pk_recipes");
162171

README.md

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -331,13 +331,18 @@ public sealed class PersonConfiguration : IEntityTypeConfiguration<SpecialPerson
331331
public void Configure(EntityTypeBuilder<SpecialPerson> builder)
332332
{
333333
builder.HasKey(x => x.Id);
334-
335-
// Option 1
334+
335+
// Option 1 (as of .NET 8)
336+
builder.ComplexProperty(x => x.Email,
337+
x => x.Property(y => y.Value)
338+
.HasColumnName("email"));
339+
340+
// Option 2
336341
builder.Property(x => x.Email)
337342
.HasConversion(x => x.Value, x => new EmailAddress(x))
338343
.HasColumnName("email");
339344

340-
// Option 2
345+
// Option 3
341346
builder.OwnsOne(x => x.Email, opts =>
342347
{
343348
opts.Property(x => x.Value).HasColumnName("email");

SharedTestingHelper/Fakes/Recipes/FakeRecipeBuilder.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,12 @@ public FakeRecipeBuilder WithHaveMadeItMyself(bool haveMadeItMyself)
4949
return this;
5050
}
5151

52+
public FakeRecipeBuilder WithCollectionEmail(string collectionEmail)
53+
{
54+
_creationData.CollectionEmail = collectionEmail;
55+
return this;
56+
}
57+
5258
public Recipe Build()
5359
{
5460
var result = Recipe.Create(_creationData);

0 commit comments

Comments
 (0)