diff --git a/test/System.Linq.Dynamic.Core.Tests/Helpers/Models/User.cs b/test/System.Linq.Dynamic.Core.Tests/Helpers/Models/User.cs index 801f8099..b758d408 100644 --- a/test/System.Linq.Dynamic.Core.Tests/Helpers/Models/User.cs +++ b/test/System.Linq.Dynamic.Core.Tests/Helpers/Models/User.cs @@ -10,6 +10,10 @@ public class User public string UserName { get; set; } + public DateTime BirthDate { get; set; } + + public DateTime? EndDate { get; set; } + public int? NullableInt { get; set; } public int Income { get; set; } @@ -69,10 +73,12 @@ public static IList GenerateSampleModels(int total, bool allowNullableProf Id = Guid.NewGuid(), SnowflakeId = new SnowflakeId(((ulong)long.MaxValue + (ulong)i + 2UL)), UserName = "User" + i, - Income = 1 + (i % 15) * 100 + Income = 1 + (i % 15) * 100, + BirthDate = DateTime.UtcNow.AddYears(-50), + EndDate = i % 2 == 0 ? DateTime.UtcNow.AddYears(99) : null }; - if (!allowNullableProfiles || (i % 8) != 5) + if (!allowNullableProfiles || i % 8 != 5) { user.Profile = new UserProfile { diff --git a/test/System.Linq.Dynamic.Core.Tests/QueryableTests.Max.cs b/test/System.Linq.Dynamic.Core.Tests/QueryableTests.Max.cs index f539a0d1..517d5958 100644 --- a/test/System.Linq.Dynamic.Core.Tests/QueryableTests.Max.cs +++ b/test/System.Linq.Dynamic.Core.Tests/QueryableTests.Max.cs @@ -1,36 +1,90 @@ using System.Linq.Dynamic.Core.Tests.Helpers.Models; +using FluentAssertions; using Xunit; -namespace System.Linq.Dynamic.Core.Tests +namespace System.Linq.Dynamic.Core.Tests; + +public partial class QueryableTests { - public partial class QueryableTests + [Fact] + public void Max() + { + // Arrange + var incomes = User.GenerateSampleModels(10).Select(u => u.Income).ToArray(); + + // Act + var expected = incomes.Max(); + var actual = incomes.AsQueryable().Max(); + + // Assert + Assert.Equal(expected, actual); + } + + [Fact] + public void Max_Selector() { - [Fact] - public void Max() - { - // Arrange - var incomes = User.GenerateSampleModels(100).Select(u => u.Income); - - // Act - var expected = incomes.Max(); - var actual = incomes.AsQueryable().Max(); - - // Assert - Assert.Equal(expected, actual); - } - - [Fact] - public void Max_Selector() - { - // Arrange - var users = User.GenerateSampleModels(100); - - // Act - var expected = users.Max(u => u.Income); - var result = users.AsQueryable().Max("Income"); - - // Assert - Assert.Equal(expected, result); - } + // Arrange + var users = User.GenerateSampleModels(10); + + // Act + var expected = users.Max(u => u.Income); + var result = users.AsQueryable().Max("Income"); + + // Assert + Assert.Equal(expected, result); + } + + [Fact] + public void Max_Where_On_Int() + { + // Arrange + var users = User.GenerateSampleModels(10); + + // Act + var typed = users + .Where(u => users.Max(m => m.Income) == u.Income) + .ToList(); + var dynamic = users.AsQueryable() + .Where("@0.Max(Income) == Income", users) + .ToList(); + + // Assert + dynamic.Should().BeEquivalentTo(typed); + } + + [Fact] + public void Max_Where_On_DateTime() + { + // Arrange + var users = User.GenerateSampleModels(10); + + // Act + var typed = users + .Where(u => users.Max(m => m.BirthDate) == u.BirthDate) + .ToList(); + var dynamic = users.AsQueryable() + .Where("@0.Max(BirthDate) == BirthDate", users) + .ToList(); + + // Assert + dynamic.Should().BeEquivalentTo(typed); + } + + [Fact] + public void Max_Where_On_NullableDateTime() + { + // Arrange + var users = User.GenerateSampleModels(10); + + // Act + var typed = users + .Where(u => users.Max(m => m.EndDate) == u.EndDate) + .ToList(); + var dynamic = users.AsQueryable() + .Where("@0.Max(EndDate) == EndDate", users) + .ToList(); + + // Assert + dynamic.Should().BeEquivalentTo(typed); } -} +} \ No newline at end of file