Biarity / Sieve

⚗️ Clean & extensible Sorting, Filtering, and Pagination for ASP.NET Core
Other
1.19k stars 132 forks source link

Filtering on a `DateOnly` struct doesn't filter list #179

Open pdevito3 opened 2 years ago

pdevito3 commented 2 years ago

Describe the bug Filtering by DateOnly does not work

To Reproduce Filtering by a DateTime works:

[Test]
    public async Task can_filter_author_list_using_date()
    {
        //Arrange
        var DateTimeNow = DateTime.Now;
        var fakeAuthorOne = FakeAuthor.Generate(new FakeAuthorForCreationDto()
            .RuleFor(a => a.Dob, f => f.Date.Past())            
            .Generate());
        var fakeAuthorTwo = FakeAuthor.Generate(new FakeAuthorForCreationDto()
            .RuleFor(a => a.Dob, _ => DateTimeNow)            
            .Generate());
        var author = new List<Author>();
        author.Add(fakeAuthorOne);
        author.Add(fakeAuthorTwo);
        var mockDbData = author.AsQueryable().BuildMock();
        var queryParameters = new AuthorParametersDto() { Filters = $"Dob>={DateTimeNow}" };

        _authorRepository
            .Setup(x => x.Query())
            .Returns(mockDbData);

        //Act
        var query = new GetAuthorList.AuthorListQuery(queryParameters);
        var handler = new GetAuthorList.Handler(_authorRepository.Object, _mapper, _sieveProcessor);
        var authors = await handler.Handle(query, CancellationToken.None);

        // Assert
        authors.Should().HaveCount(1);
        authors
            .FirstOrDefault()
            .Should().BeEquivalentTo(fakeAuthorTwo, options =>
                options.ExcludingMissingMembers()
                    .Excluding(x => x.PhysicalAddress));
    }

but using the same setup for a DateOnly does not filter a list

[Test]
    public async Task can_filter_author_list_using_dateonly()
    {
        //Arrange
        var dateOnlyNow = DateOnly.FromDateTime(DateTime.Now);
        var fakeAuthorOne = FakeAuthor.Generate(new FakeAuthorForCreationDto()
            .RuleFor(a => a.DayWorldEnds, f => f.Date.PastDateOnly())            
            .Generate());
        var fakeAuthorTwo = FakeAuthor.Generate(new FakeAuthorForCreationDto()
            .RuleFor(a => a.DayWorldEnds, _ => dateOnlyNow)            
            .Generate());
        var author = new List<Author>();
        author.Add(fakeAuthorOne);
        author.Add(fakeAuthorTwo);
        var mockDbData = author.AsQueryable().BuildMock();
        var queryParameters = new AuthorParametersDto() { Filters = $"DayWorldEnds>={DateOnlyNow}" };

        _authorRepository
            .Setup(x => x.Query())
            .Returns(mockDbData);

        //Act
        var query = new GetAuthorList.AuthorListQuery(queryParameters);
        var handler = new GetAuthorList.Handler(_authorRepository.Object, _mapper, _sieveProcessor);
        var authors = await handler.Handle(query, CancellationToken.None);

        // Assert
        authors.Should().HaveCount(1);
        authors
            .FirstOrDefault()
            .Should().BeEquivalentTo(fakeAuthorTwo, options =>
                options.ExcludingMissingMembers()
                    .Excluding(x => x.PhysicalAddress));
    }

Expected behavior The list should be filtered by matching records on the DateOnly prop

Screenshots If applicable, add screenshots to help explain your problem.

Desktop (please complete the following information):

Smartphone (please complete the following information):

Additional context Add any other context about the problem here.

genuinefafa commented 1 year ago

I wonder how to solve this issue; If I try to do a <=2002/05/28 if something is dated 2002-05-28Z00:01:00 it is not returned. I was wonder if I can fine tune Processor 🤔