Skip to content

Commit ac49f40

Browse files
committed
update: tests for child entity
1 parent 93dc654 commit ac49f40

7 files changed

Lines changed: 127 additions & 2 deletions

File tree

QueryKit.IntegrationTests/Tests/DatabaseFilteringTests.cs

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,10 @@ namespace QueryKit.IntegrationTests.Tests;
55
using FluentAssertions;
66
using Microsoft.EntityFrameworkCore;
77
using SharedTestingHelper.Fakes;
8+
using SharedTestingHelper.Fakes.Author;
9+
using SharedTestingHelper.Fakes.Recipes;
810
using WebApiTestProject.Entities;
11+
using WebApiTestProject.Entities.Recipes;
912
using WebApiTestProject.Features;
1013

1114
public class DatabaseFilteringTests : TestBase
@@ -341,4 +344,65 @@ public async Task can_handle_case_sensitive_in_for_string()
341344
// Assert
342345
people.FirstOrDefault(x => x.Id == fakePersonOne.Id).Should().BeNull();
343346
}
347+
348+
[Fact]
349+
public async Task can_filter_on_child_entity()
350+
{
351+
// Arrange
352+
var testingServiceScope = new TestingServiceScope();
353+
var fakeAuthorOne = new FakeAuthorBuilder().Build();
354+
var fakeRecipeOne = new FakeRecipeBuilder().Build();
355+
fakeRecipeOne.SetAuthor(fakeAuthorOne);
356+
357+
var fakeAuthorTwo = new FakeAuthorBuilder().Build();
358+
var fakeRecipeTwo = new FakeRecipeBuilder().Build();
359+
fakeRecipeTwo.SetAuthor(fakeAuthorTwo);
360+
await testingServiceScope.InsertAsync(fakeRecipeOne, fakeRecipeTwo);
361+
362+
var input = $"""Author.Name == "{fakeAuthorOne.Name}" """;
363+
364+
// Act
365+
var queryableRecipe = testingServiceScope.DbContext().Recipes
366+
.Include(x => x.Author);
367+
var appliedQueryable = queryableRecipe.ApplyQueryKitFilter(input);
368+
var people = await appliedQueryable.ToListAsync();
369+
370+
// Assert
371+
people.Count.Should().Be(1);
372+
people.FirstOrDefault(x => x.Id == fakeRecipeOne.Id).Should().NotBeNull();
373+
people.FirstOrDefault(x => x.Id == fakeRecipeTwo.Id).Should().BeNull();
374+
}
375+
376+
[Fact]
377+
public async Task can_filter_on_child_entity_with_config()
378+
{
379+
// Arrange
380+
var testingServiceScope = new TestingServiceScope();
381+
var fakeAuthorOne = new FakeAuthorBuilder().Build();
382+
var fakeRecipeOne = new FakeRecipeBuilder().Build();
383+
fakeRecipeOne.SetAuthor(fakeAuthorOne);
384+
385+
var fakeAuthorTwo = new FakeAuthorBuilder().Build();
386+
var fakeRecipeTwo = new FakeRecipeBuilder().Build();
387+
fakeRecipeTwo.SetAuthor(fakeAuthorTwo);
388+
await testingServiceScope.InsertAsync(fakeRecipeOne, fakeRecipeTwo);
389+
390+
var input = $"""author == "{fakeAuthorOne.Name}" """;
391+
392+
var config = new QueryKitConfiguration(config =>
393+
{
394+
config.Property<Recipe>(x => x.Author.Name).HasQueryName("author");
395+
});
396+
397+
// Act
398+
var queryableRecipe = testingServiceScope.DbContext().Recipes
399+
.Include(x => x.Author);
400+
var appliedQueryable = queryableRecipe.ApplyQueryKitFilter(input, config);
401+
var people = await appliedQueryable.ToListAsync();
402+
403+
// Assert
404+
people.Count.Should().Be(1);
405+
people.FirstOrDefault(x => x.Id == fakeRecipeOne.Id).Should().NotBeNull();
406+
people.FirstOrDefault(x => x.Id == fakeRecipeTwo.Id).Should().BeNull();
407+
}
344408
}

QueryKit.WebApiTestProject/Entities/Recipes/Recipe.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,12 @@ public Recipe Update(RecipeForUpdate recipeForUpdate)
6161
HaveMadeItMyself = recipeForUpdate.HaveMadeItMyself;
6262
return this;
6363
}
64+
65+
public Recipe SetAuthor(Author author)
66+
{
67+
Author = author;
68+
return this;
69+
}
6470

6571
protected Recipe() { } // For EF + Mocking
6672
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
namespace SharedTestingHelper.Fakes.Author;
2+
3+
using QueryKit.WebApiTestProject.Entities.Authors;
4+
using QueryKit.WebApiTestProject.Entities.Authors.Models;
5+
6+
public class FakeAuthorBuilder
7+
{
8+
private AuthorForCreation _creationData = new FakeAuthorForCreation().Generate();
9+
10+
public FakeAuthorBuilder WithModel(AuthorForCreation model)
11+
{
12+
_creationData = model;
13+
return this;
14+
}
15+
16+
public FakeAuthorBuilder WithName(string name)
17+
{
18+
_creationData.Name = name;
19+
return this;
20+
}
21+
22+
public FakeAuthorBuilder WithRecipeId(Guid recipeId)
23+
{
24+
_creationData.RecipeId = recipeId;
25+
return this;
26+
}
27+
28+
public Author Build()
29+
{
30+
var result = Author.Create(_creationData);
31+
return result;
32+
}
33+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
namespace SharedTestingHelper.Fakes.Author;
2+
3+
using AutoBogus;
4+
using QueryKit.WebApiTestProject.Entities.Authors.Models;
5+
6+
public sealed class FakeAuthorForCreation : AutoFaker<AuthorForCreation>
7+
{
8+
public FakeAuthorForCreation()
9+
{
10+
}
11+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
namespace SharedTestingHelper.Fakes.Author;
2+
3+
using AutoBogus;
4+
using QueryKit.WebApiTestProject.Entities.Authors.Models;
5+
6+
public sealed class FakeAuthorForUpdate : AutoFaker<AuthorForUpdate>
7+
{
8+
public FakeAuthorForUpdate()
9+
{
10+
}
11+
}

SharedTestingHelper/Fakes/FakeRecipeBuilder.cs renamed to SharedTestingHelper/Fakes/Recipes/FakeRecipeBuilder.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
namespace SharedTestingHelper.Fakes;
1+
namespace SharedTestingHelper.Fakes.Recipes;
22

33
using QueryKit.WebApiTestProject.Entities.Recipes;
44
using QueryKit.WebApiTestProject.Entities.Recipes.Models;

SharedTestingHelper/Fakes/FakeRecipeForCreation.cs renamed to SharedTestingHelper/Fakes/Recipes/FakeRecipeForCreation.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
namespace SharedTestingHelper.Fakes;
1+
namespace SharedTestingHelper.Fakes.Recipes;
22

33
using AutoBogus;
44
using QueryKit.WebApiTestProject.Entities.Recipes;

0 commit comments

Comments
 (0)