jordimontana82 / fake-xrm-easy

The testing framework for Dynamics CRM and Dynamics 365 which runs on an In-Memory context and deals with mocks or fakes for you
https://dynamicsvalue.com/get-started/overview?source=git
Other
263 stars 182 forks source link

ThisMonth in Query Expression fails on last day after midday #587

Open Skubakoob opened 2 years ago

Skubakoob commented 2 years ago

Describe the bug When running a test with a query for DateTime = ThisMonth on the last day of the month after midday, the test can fail

To Reproduce The code from repo as below sets the to Date to something like "1/31/2022 12:00:00 AM" This means when querying on something like created on, it will not include items created after midday

i.e. query.Criteria.AddCondition("createdon", ConditionOperator.ThisMonth);

Repo code:

case ConditionOperator.ThisMonth: // From first day of this month to last day of this month                    
fromDate = new DateTime(thisYear, thisMonth, 1);
// Last day of this month: Add one month to the first of this month, and then remove one day
toDate = new DateTime(thisYear, thisMonth, 1).AddMonths(1).AddDays(-1);

Expected behavior Should include items created after midday, i.e. this should resolve it:

var toDate = new DateTime(thisYear, thisMonth, 1, 23,59,59).AddMonths(1).AddDays(-1);

When I get a minute today I can create a PR, I guess it will probably affect some of the other date queries i.e. LastMonth.

And I guess if we are being really pedantic, it should take into account millisends too so maybe this new DateTime(thisYear, thisMonth, 1).Date.AddDays(1).AddTicks(-1);