Skip to content

Commit

Permalink
Add some more tests for Max (#744)
Browse files Browse the repository at this point in the history
  • Loading branch information
StefH authored Sep 10, 2023
1 parent 0b56ddd commit 6c38a13
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 32 deletions.
10 changes: 8 additions & 2 deletions test/System.Linq.Dynamic.Core.Tests/Helpers/Models/User.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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; }
Expand Down Expand Up @@ -69,10 +73,12 @@ public static IList<User> 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
{
Expand Down
114 changes: 84 additions & 30 deletions test/System.Linq.Dynamic.Core.Tests/QueryableTests.Max.cs
Original file line number Diff line number Diff line change
@@ -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);
}
}
}

0 comments on commit 6c38a13

Please sign in to comment.