Seems like working solely with date without time would be the true test. The following is done with Entity Framework Core.
Table structure
CREATE TABLE [dbo].[Sales](
[Id] [int] IDENTITY(1,1) NOT NULL,
[SaleDate] [datetime2](7) NULL,
[ShipCountry] [int] NULL,
CONSTRAINT [PK_Sales] PRIMARY KEY CLUSTERED
(
[Id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF,
IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
ALTER TABLE [dbo].[Sales] ADD CONSTRAINT [DF_Sales_SaleDate] DEFAULT (getdate()) FOR [SaleDate]
Test queries
SELECT Id,
SaleDate,
ShipCountry
FROM dbo.Sales;
DECLARE @LastWeek NVARCHAR(10)= '2021-06-18';
SELECT Id,
SaleDate,
ShipCountry
FROM dbo.Sales
WHERE CONVERT(DATE, SaleDate) > CAST(@LastWeek AS DATE)
AND dbo.Sales.ShipCountry = 1;
Using the second query above coupled with a unit test you can validate the results. In this case two records are expected to be returned.
using System;
using System.Linq;
using EntityFrameworkLibrary.Data;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using SalesUnitTestProject.Base;
namespace SalesUnitTestProject
{
[TestClass]
public partial class MainTest : TestBase
{
[TestMethod]
public void TestMethod1()
{
var saleDate = new DateTime(2021, 6, 25);
var shipCountry = 1;
var expectedCount = 2;
saleDate = saleDate.AddDays(-7);
using DatabaseContext context = new DatabaseContext();
var results = context
.Sales
.Where(sales => sales.SaleDate.Value.Date > saleDate.Date &&
sales.ShipCountry == shipCountry)
.ToList();
Assert.AreEqual(results.Count, expectedCount);
}
}
}
All the above is very basic, we could get into mocking and using a class such as the following for the current date.
public class DateTimeProvider : IDisposable
{
private static readonly AsyncLocal<DateTime?> _injectedDateTime = new();
/// <summary>
/// Gets DateTime now.
/// </summary>
/// <value>
/// The DateTime now.
/// </value>
public static DateTime Now => _injectedDateTime.Value ?? DateTime.Now;
/// <summary>
/// Injects the actual date time.
/// </summary>
/// <param name="actualDateTime">The actual date time.</param>
public static IDisposable InjectActualDateTime(DateTime actualDateTime)
{
_injectedDateTime.Value = actualDateTime;
return new DateTimeProvider();
}
public void Dispose()
{
_injectedDateTime.Value = null;
}
}