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
112 changes: 105 additions & 7 deletions QueryKit.IntegrationTests/Tests/HasConversionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -100,11 +100,19 @@
}

[Fact]
public async Task email_value_with_query_name_and_has_conversion_compares_the_child()
public async Task can_filter_by_email_value_with_query_name_and_has_conversion()
{
// Arrange
var testingServiceScope = new TestingServiceScope();
var input = """Email.Value == "a@x.com" """;
var testEmail = $"{Guid.NewGuid()}@example.com";
var person = new FakeTestingPersonBuilder()
.WithEmail(testEmail)
.Build();
var personTwo = new FakeTestingPersonBuilder().Build();

await testingServiceScope.InsertAsync(person, personTwo);

var input = $"""Email.Value == "{testEmail}" """;
var config = new QueryKitConfiguration(config =>
{
config.Property<TestingPerson>(x => x.Email).HasQueryName("mail").HasConversion<string>();
Expand All @@ -113,11 +121,11 @@
// Act
var queryablePeople = testingServiceScope.DbContext().People;
var appliedQueryable = queryablePeople.ApplyQueryKitFilter(input, config);
var act = () => appliedQueryable.ToListAsync();
var people = await appliedQueryable.ToListAsync();

// Assert
await act.Should().ThrowExactlyAsync<InvalidOperationException>()
.WithMessage("The LINQ expression*could not be translated*");
people.Count.Should().Be(1);
people[0].Id.Should().Be(person.Id);
}

[Fact]
Expand Down Expand Up @@ -154,7 +162,96 @@
}

[Fact]
public async Task null_email_with_has_conversion_matches_no_row()
public async Task can_filter_by_nullable_int_with_query_name_and_has_conversion()
{
// Arrange
var testingServiceScope = new TestingServiceScope();
var title = Guid.NewGuid().ToString();
var person = new FakeTestingPersonBuilder()
.WithTitle(title)
.WithAge(41)
.Build();
var personTwo = new FakeTestingPersonBuilder()
.WithTitle(title)
.WithAge(42)
.Build();
await testingServiceScope.InsertAsync(person, personTwo);

var input = $"""Title == "{title}" && years == 41""";
var config = new QueryKitConfiguration(config =>
{
config.Property<TestingPerson>(x => x.Age).HasQueryName("years").HasConversion<string>();

Check warning on line 183 in QueryKit.IntegrationTests/Tests/HasConversionTests.cs

View workflow job for this annotation

GitHub Actions / test

Possible null reference return.

Check warning on line 183 in QueryKit.IntegrationTests/Tests/HasConversionTests.cs

View workflow job for this annotation

GitHub Actions / test

Possible null reference return.

Check warning on line 183 in QueryKit.IntegrationTests/Tests/HasConversionTests.cs

View workflow job for this annotation

GitHub Actions / test

Possible null reference return.

Check warning on line 183 in QueryKit.IntegrationTests/Tests/HasConversionTests.cs

View workflow job for this annotation

GitHub Actions / test

Possible null reference return.
});

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

// Assert
people.Count.Should().Be(1);
people[0].Id.Should().Be(person.Id);
}

[Fact]
public async Task can_filter_by_enum_with_query_name_and_has_conversion()
{
// Arrange
var testingServiceScope = new TestingServiceScope();
var title = Guid.NewGuid().ToString();
var person = new FakeTestingPersonBuilder()
.WithTitle(title)
.WithBirthMonth(BirthMonthEnum.March)
.Build();
var personTwo = new FakeTestingPersonBuilder()
.WithTitle(title)
.WithBirthMonth(BirthMonthEnum.April)
.Build();
await testingServiceScope.InsertAsync(person, personTwo);

var input = $"""Title == "{title}" && month == March""";
var config = new QueryKitConfiguration(config =>
{
config.Property<TestingPerson>(x => x.BirthMonth).HasQueryName("month").HasConversion<string>();

Check warning on line 215 in QueryKit.IntegrationTests/Tests/HasConversionTests.cs

View workflow job for this annotation

GitHub Actions / test

Possible null reference return.

Check warning on line 215 in QueryKit.IntegrationTests/Tests/HasConversionTests.cs

View workflow job for this annotation

GitHub Actions / test

Possible null reference return.

Check warning on line 215 in QueryKit.IntegrationTests/Tests/HasConversionTests.cs

View workflow job for this annotation

GitHub Actions / test

Possible null reference return.

Check warning on line 215 in QueryKit.IntegrationTests/Tests/HasConversionTests.cs

View workflow job for this annotation

GitHub Actions / test

Possible null reference return.
});

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

// Assert
people.Count.Should().Be(1);
people[0].Id.Should().Be(person.Id);
}

[Fact]
public async Task can_filter_by_guid_with_query_name_and_has_conversion()
{
// Arrange
var testingServiceScope = new TestingServiceScope();
var person = new FakeTestingPersonBuilder().Build();
var personTwo = new FakeTestingPersonBuilder().Build();
await testingServiceScope.InsertAsync(person, personTwo);

var input = $"""identifier == "{person.Id}" """;
var config = new QueryKitConfiguration(config =>
{
config.Property<TestingPerson>(x => x.Id).HasQueryName("identifier").HasConversion<string>();
});

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

// Assert
people.Count.Should().Be(1);
people[0].Id.Should().Be(person.Id);
}

[Fact]
public async Task can_filter_by_null_email_with_has_conversion()
{
// Arrange
var testingServiceScope = new TestingServiceScope();
Expand Down Expand Up @@ -182,7 +279,8 @@
var people = await appliedQueryable.ToListAsync();

// Assert
people.Should().BeEmpty();
people.Count.Should().Be(1);
people[0].Id.Should().Be(person.Id);
}

[Fact]
Expand Down
93 changes: 81 additions & 12 deletions QueryKit.UnitTests/HasConversionTests.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
namespace QueryKit.UnitTests;

using Configuration;
using Exceptions;
using FluentAssertions;
using WebApiTestProject.Entities;

Expand Down Expand Up @@ -167,7 +168,7 @@
}

[Fact]
public void child_property_of_converted_parent_with_query_name_compares_the_child()
public void child_property_of_converted_parent_with_query_name_compares_parent()
{
// Arrange
var input = """Email.Value == "a@x.com" """;
Expand All @@ -185,8 +186,8 @@
var filterWithoutQueryName = FilterParser.ParseFilter<TestingPerson>(input, configWithoutQueryName);

// Assert
filterWithQueryName.ToDisplayString().Should().Be("""x => (x.Email.Value == "a@x.com")""");
filterWithoutQueryName.ToDisplayString().Should().Be("""x => (x.Email == new EmailAddress("a@x.com"))""");
filterWithQueryName.ToDisplayString().Should().Be("""x => (x.Email == new EmailAddress("a@x.com"))""");
filterWithQueryName.ToDisplayString().Should().Be(filterWithoutQueryName.ToDisplayString());
}

[Fact]
Expand Down Expand Up @@ -252,7 +253,7 @@
}

[Fact]
public void null_on_reference_type_with_has_conversion_matches_no_row()
public void can_filter_null_on_reference_type_with_has_conversion()
{
// Arrange
var rows = EmailRows();
Expand All @@ -266,7 +267,8 @@
var result = rows.ApplyQueryKitFilter("""Email == null""", config).ToList();

// Assert
result.Should().BeEmpty();
result.Count.Should().Be(1);
result[0].Email.Should().BeNull();
}

[Fact]
Expand Down Expand Up @@ -362,8 +364,10 @@
result[0].Name.Should().Be("two");
}

[Fact]
public void int_with_query_name_and_has_conversion_compares_the_int()
[Theory]
[InlineData("count")]
[InlineData("Number")]
public void int_with_has_conversion_compares_the_int(string queryName)
{
// Arrange
var rows = new List<NumberRow>
Expand All @@ -373,28 +377,77 @@
};
var config = new QueryKitConfiguration(config =>
{
config.Property<NumberRow>(x => x.Number).HasQueryName("count").HasConversion<string>();
config.Property<NumberRow>(x => x.Number).HasQueryName(queryName).HasConversion<string>();
});

// Act
var result = rows.ApplyQueryKitFilter("""count == 2""", config).ToList();
var result = rows.ApplyQueryKitFilter($"{queryName} == 2", config).ToList();

// Assert
result.Count.Should().Be(1);
result[0].Number.Should().Be(2);
}

[Fact]
public void guid_with_query_name_and_has_conversion_compares_a_guid_constant()
public void nullable_int_with_query_name_and_has_conversion_compares_the_int()
{
// Arrange
var rows = new List<NullableNumberRow>
{
new() { Number = 1 },
new() { Number = 2 },
new() { Number = null }
};
var config = new QueryKitConfiguration(config =>
{
config.Property<GuidRow>(x => x.Id).HasQueryName("identifier").HasConversion<string>();
config.Property<NullableNumberRow>(x => x.Number).HasQueryName("count").HasConversion<string>();

Check warning on line 403 in QueryKit.UnitTests/HasConversionTests.cs

View workflow job for this annotation

GitHub Actions / test

Possible null reference return.

Check warning on line 403 in QueryKit.UnitTests/HasConversionTests.cs

View workflow job for this annotation

GitHub Actions / test

Possible null reference return.

Check warning on line 403 in QueryKit.UnitTests/HasConversionTests.cs

View workflow job for this annotation

GitHub Actions / test

Possible null reference return.

Check warning on line 403 in QueryKit.UnitTests/HasConversionTests.cs

View workflow job for this annotation

GitHub Actions / test

Possible null reference return.
});

// Act
var result = rows.ApplyQueryKitFilter("count == 2", config).ToList();

// Assert
result.Count.Should().Be(1);
result[0].Number.Should().Be(2);
}

[Theory]
[InlineData("level")]
[InlineData("Level")]
public void enum_with_has_conversion_compares_the_enum(string queryName)
{
// Arrange
var rows = new List<LevelRow>
{
new() { Level = LevelKind.Low },
new() { Level = LevelKind.High }
};
var config = new QueryKitConfiguration(config =>
{
config.Property<LevelRow>(x => x.Level).HasQueryName(queryName).HasConversion<string>();
});

// Act
var filter = FilterParser.ParseFilter<GuidRow>($"identifier == \"{KnownGuid}\"", config);
var result = rows.ApplyQueryKitFilter($"{queryName} == High", config).ToList();

// Assert
result.Count.Should().Be(1);
result[0].Level.Should().Be(LevelKind.High);
}

[Theory]
[InlineData("identifier")]
[InlineData("Id")]
public void guid_with_has_conversion_compares_a_guid_constant(string queryName)
{
// Arrange
var config = new QueryKitConfiguration(config =>
{
config.Property<GuidRow>(x => x.Id).HasQueryName(queryName).HasConversion<string>();
});

// Act
var filter = FilterParser.ParseFilter<GuidRow>($"{queryName} == \"{KnownGuid}\"", config);

// Assert
filter.ToDisplayString().Should().Be($"x => (x.Id == {KnownGuid})");
Expand Down Expand Up @@ -462,4 +515,20 @@
{
public int Number { get; set; }
}

private class NullableNumberRow
{
public int? Number { get; set; }
}

private enum LevelKind
{
Low,
High
}

private class LevelRow
{
public LevelKind Level { get; set; }
}
}
Loading
Loading