Skip to content
Open
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
208 changes: 208 additions & 0 deletions QueryKit.IntegrationTests/Tests/PropertyResolverTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,214 @@ public async Task property_is_not_prevented_by_another_property_whose_query_name
people[0].Id.Should().Be(fakePerson.Id);
}

[Fact]
public async Task prevented_property_in_arithmetic_is_not_filtered()
{
// Arrange
var testingServiceScope = new TestingServiceScope();
var title = new Faker().Lorem.Sentence();
var fakePerson = new FakeTestingPersonBuilder()
.WithTitle(title)
.WithAge(5)
.Build();
await testingServiceScope.InsertAsync(fakePerson);

var input = $"""Title == "{title}" && (Age + 0) > 10""";
var config = new QueryKitConfiguration(config =>
{
config.Property<TestingPerson>(x => x.Age!).PreventFilter();
});

// Act
var people = await testingServiceScope.DbContext().People
.ApplyQueryKitFilter(input, config)
.ToListAsync();

// Assert
people.Should().ContainSingle(x => x.Id == fakePerson.Id);
}

[Fact]
public async Task prevented_property_on_the_right_side_is_not_compared()
{
// Arrange
var testingServiceScope = new TestingServiceScope();
var title = new Faker().Lorem.Sentence();
var fakePerson = new FakeTestingPersonBuilder()
.WithTitle(title)
.WithFirstName("Same")
.WithLastName("Same")
.WithAge(30)
.Build();
await testingServiceScope.InsertAsync(fakePerson);

var input = $"""Title == "{title}" && (FirstName == LastName || Age > 100)""";
var config = new QueryKitConfiguration(config =>
{
config.IgnoredClauseBehavior = IgnoredClauseBehavior.Remove;
config.Property<TestingPerson>(x => x.LastName!).PreventFilter();
});

// Act
var people = await testingServiceScope.DbContext().People
.ApplyQueryKitFilter(input, config)
.ToListAsync();

// Assert
people.Should().BeEmpty();
}

[Fact]
public async Task prevented_property_in_a_list_is_not_filtered_in_any_case()
{
// Arrange
var testingServiceScope = new TestingServiceScope();
var title = new Faker().Lorem.Sentence();
var fakePerson = new FakeTestingPersonBuilder()
.WithTitle(title)
.WithFirstName("Paul")
.WithLastName("Other")
.Build();
await testingServiceScope.InsertAsync(fakePerson);

var input = $"""Title == "{title}" && (firstname, LastName) == "Paul" """;
var config = new QueryKitConfiguration(config =>
{
config.Property<TestingPerson>(x => x.FirstName!).PreventFilter();
});

// Act
var people = await testingServiceScope.DbContext().People
.ApplyQueryKitFilter(input, config)
.ToListAsync();

// Assert
people.Should().BeEmpty();
}

[Fact]
public async Task prevented_sort_property_with_a_query_name_is_not_sorted_when_written_by_its_member_name()
{
// Arrange
var testingServiceScope = new TestingServiceScope();
var title = new Faker().Lorem.Sentence();
var firstPerson = new FakeTestingPersonBuilder()
.WithTitle(title)
.WithFirstName("A")
.WithAge(1)
.Build();
var secondPerson = new FakeTestingPersonBuilder()
.WithTitle(title)
.WithFirstName("B")
.WithAge(2)
.Build();
await testingServiceScope.InsertAsync(firstPerson, secondPerson);

var input = "firstname desc, Age";
var config = new QueryKitConfiguration(config =>
{
config.Property<TestingPerson>(x => x.FirstName!).HasQueryName("first").PreventSort();
});

// Act
var people = await testingServiceScope.DbContext().People
.Where(x => x.Title == title)
.ApplyQueryKitSort(input, config)
.ToListAsync();

// Assert
people.Select(x => x.Id).Should().Equal(firstPerson.Id, secondPerson.Id);
}

[Fact]
public async Task prevented_custom_operation_is_not_filtered()
{
// Arrange
var testingServiceScope = new TestingServiceScope();
var title = new Faker().Lorem.Sentence();
var fakePerson = new FakeTestingPersonBuilder()
.WithTitle(title)
.WithAge(5)
.Build();
await testingServiceScope.InsertAsync(fakePerson);

var input = $"""Title == "{title}" && adult == true""";
var config = new QueryKitConfiguration(config =>
{
config.CustomOperation<TestingPerson>((x, op, value) => x.Age > 17).HasQueryName("adult").PreventFilter();
});

// Act
var people = await testingServiceScope.DbContext().People
.ApplyQueryKitFilter(input, config)
.ToListAsync();

// Assert
people.Should().ContainSingle(x => x.Id == fakePerson.Id);
}

[Fact]
public async Task prevented_derived_property_is_not_filtered()
{
// Arrange
var testingServiceScope = new TestingServiceScope();
var title = new Faker().Lorem.Sentence();
var fakePerson = new FakeTestingPersonBuilder()
.WithTitle(title)
.WithFirstName("Paul")
.WithLastName("Other")
.Build();
await testingServiceScope.InsertAsync(fakePerson);

var input = $"""Title == "{title}" && full == "no match" """;
var config = new QueryKitConfiguration(config =>
{
config.DerivedProperty<TestingPerson>(x => x.FirstName + " " + x.LastName).HasQueryName("full").PreventFilter();
});

// Act
var people = await testingServiceScope.DbContext().People
.ApplyQueryKitFilter(input, config)
.ToListAsync();

// Assert
people.Should().ContainSingle(x => x.Id == fakePerson.Id);
}

[Fact]
public async Task prevented_derived_sort_property_is_not_sorted()
{
// Arrange
var testingServiceScope = new TestingServiceScope();
var title = new Faker().Lorem.Sentence();
var firstPerson = new FakeTestingPersonBuilder()
.WithTitle(title)
.WithFirstName("A")
.WithAge(1)
.Build();
var secondPerson = new FakeTestingPersonBuilder()
.WithTitle(title)
.WithFirstName("B")
.WithAge(2)
.Build();
await testingServiceScope.InsertAsync(firstPerson, secondPerson);

var input = "full desc, Age";
var config = new QueryKitConfiguration(config =>
{
config.DerivedProperty<TestingPerson>(x => x.FirstName + " " + x.LastName).HasQueryName("full").PreventSort();
});

// Act
var people = await testingServiceScope.DbContext().People
.Where(x => x.Title == title)
.ApplyQueryKitSort(input, config)
.ToListAsync();

// Assert
people.Select(x => x.Id).Should().Equal(firstPerson.Id, secondPerson.Id);
}

[Theory]
[InlineData("first-name")]
[InlineData("_first")]
Expand Down
Loading
Loading