Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
127 changes: 126 additions & 1 deletion QueryKit.IntegrationTests/Tests/DatabaseFilteringTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ namespace QueryKit.IntegrationTests.Tests;
using Microsoft.EntityFrameworkCore;
using SharedTestingHelper.Fakes;
using SharedTestingHelper.Fakes.Author;
using SharedTestingHelper.Fakes.Ingredients;
using SharedTestingHelper.Fakes.Recipes;
using WebApiTestProject.Database;
using WebApiTestProject.Entities;
Expand All @@ -28,7 +29,7 @@ public async Task can_filter_by_string()
await testingServiceScope.InsertAsync(fakePersonOne, fakePersonTwo);

var input = $"""{nameof(TestingPerson.Title)} == "{fakePersonOne.Title}" """;

// Act
var queryablePeople = testingServiceScope.DbContext().People;
var appliedQueryable = queryablePeople.ApplyQueryKitFilter(input);
Expand All @@ -39,6 +40,130 @@ public async Task can_filter_by_string()
people[0].Id.Should().Be(fakePersonOne.Id);
}

[Fact]
public async Task can_filter_by_string_for_collection()
{
// Arrange
var testingServiceScope = new TestingServiceScope();
var faker = new Faker();
var fakeIngredientOne = new FakeIngredientBuilder()
.WithName(faker.Lorem.Sentence())
.Build();
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.Name == "{fakeIngredientOne.Name}" """;

// Act
var queryableRecipes = testingServiceScope.DbContext().Recipes;
var appliedQueryable = queryableRecipes.ApplyQueryKitFilter(input);
var recipes = await appliedQueryable.ToListAsync();

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

[Fact]
public async Task can_filter_by_string_for_collection_with_count()
{
// Arrange
var testingServiceScope = new TestingServiceScope();
var faker = new Faker();
var fakeIngredientOne = new FakeIngredientBuilder()
.WithName(faker.Lorem.Sentence())
.Build();
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 = $"""Title == "{fakeRecipeOne.Title}" && Ingredients #>= 1""";

// Act
var queryableRecipes = testingServiceScope.DbContext().Recipes;
var appliedQueryable = queryableRecipes.ApplyQueryKitFilter(input);
var recipes = await appliedQueryable.ToListAsync();

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

[Fact]
public async Task can_filter_by_string_for_collection_contains()
{
// Arrange
var testingServiceScope = new TestingServiceScope();
var faker = new Faker();
var fakeIngredientOne = new FakeIngredientBuilder()
.WithName($"{faker.Lorem.Sentence()}partial")
.Build();
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.Name @= "partial" """;

// Act
var queryableRecipes = testingServiceScope.DbContext().Recipes;
var appliedQueryable = queryableRecipes.ApplyQueryKitFilter(input);
var recipes = await appliedQueryable.ToListAsync();

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

[Fact]
public async Task can_filter_by_string_for_collection_does_not_contain()
{
// Arrange
var testingServiceScope = new TestingServiceScope();
var faker = new Faker();
var fakeIngredientOne = new FakeIngredientBuilder()
.WithName($"{faker.Lorem.Sentence()}partial")
.Build();
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.Name !@= "partial" """;

// Act
var queryableRecipes = testingServiceScope.DbContext().Recipes;
var appliedQueryable = queryableRecipes.ApplyQueryKitFilter(input);
var recipes = await appliedQueryable.ToListAsync();

// Assert
recipes.FirstOrDefault(x => x.Id == fakeRecipeOne.Id).Should().BeNull();
recipes.FirstOrDefault(x => x.Id == fakeRecipeTwo.Id).Should().NotBeNull();
}

[Fact]
public async Task can_use_soundex_equals()
{
Expand Down
Loading