pdevito3 / QueryKit

🎛️ QueryKit is a .NET library that makes it easier to query your data by providing a fluent and intuitive syntax for filtering and sorting.
Apache License 2.0
147 stars 13 forks source link

Add nested collection support #11

Open pdevito3 opened 1 year ago

pdevito3 commented 1 year ago

ex:

    [Fact]
    public async Task can_filter_by_string_for_nested_collection()
    {
        // Arrange
        var testingServiceScope = new TestingServiceScope();
        var faker = new Faker();
        var fakePreparationOne = new IngredientPreparation()
        {
            Text = faker.Lorem.Sentence()
        };
        var fakeIngredientOne = new FakeIngredientBuilder()
            .Build();
        fakeIngredientOne.AddPreparation(fakePreparationOne);
        var fakeRecipeOne = new FakeRecipeBuilder().Build();
        fakeRecipeOne.AddIngredient(fakeIngredientOne);

        var fakeIngredientTwo = new FakeIngredientBuilder()
            .WithName(faker.Lorem.Sentence())
            .Build();
        var fakeRecipeTwo = new FakeRecipeBuilder().Build();
        fakeRecipeTwo.AddIngredient(fakeIngredientTwo);
        await testingServiceScope.InsertAsync(fakeRecipeOne, fakeRecipeTwo);

        var input = $"""Ingredients.Preparations.Text == "{fakePreparationOne.Text}" """;

        // Act
        var queryableRecipes = testingServiceScope.DbContext().Recipes;
        var appliedQueryable = queryableRecipes.ApplyQueryKitFilter(input);
        // var appliedQueryable = queryableRecipes.Where(x => x.Ingredients.SelectMany(y => y.Preparations).Select(y => y.Text).Any(z => (z == fakePreparationOne.Text)));
        var recipes = await appliedQueryable.ToListAsync();

        // Assert
        recipes.Count.Should().Be(1);
        recipes[0].Id.Should().Be(fakeRecipeOne.Id);
    }