DateTimeOffset precision issue
See original GitHub issueBefore submitting
This repo is for Npgsql ADO.NET issues only. Entity Framework issues belong in Npgsql.EntityFrameworkCore.PostgreSQL for EF Core, or EntityFramework6.Npgsql for EF 6.x.
understood, this seems to be an npgsql behavior as far as i can tell
Steps to reproduce
Ideally include a complete code listing that we can run to reproduce the issue. Alternatively, you can provide a project/solution that we can run.
So I have an integration test that creates a user in by db and then confirms that it was accurately created. See the test and full working repo here.
The test looks like this:
[Test]
public async Task AddPatientCommand_Adds_New_Patient_To_Db()
{
// Arrange
var fakePatientOne = new FakePatientForCreationDto { }.Generate();
// Act
var command = new AddPatientCommand(fakePatientOne);
var patientReturned = await SendAsync(command);
var patientCreated = await ExecuteDbContextAsync(db => db.Patients.SingleOrDefaultAsync());
// Assert
patientReturned.Should().BeEquivalentTo(fakePatientOne, options =>
options.ExcludingMissingMembers());
patientCreated.Should().BeEquivalentTo(fakePatientOne, options =>
options.ExcludingMissingMembers());
}
It works and passes with sql server and is seemingly working here as well, but the test is failing due to what looks like a precision issue on a DateTimeOffset?
property.
The issue
Describe what is not working as expected. If you are seeing an exception, include the full exceptions details (message and stack trace).
I would expect the test to pass, but it would seem that the precision between a datetimeoffset in EF and npgsql doesn’t match. As you can see in the error message, the Dob
property is matching, save for the extra 2
in the EF object. I would expect the npgsql retrieved value to match.
Message:
Expected member Dob to be <2021-03-22 22:45:15.0749082 -4h>, but found <2021-03-22 22:45:15.074908 -4h>.
With configuration:
- Use declared types and members
- Compare enums by value
- Try to match member by name
- Without automatic conversion.
- Be strict about the order of items in byte arrays
Stack Trace:
LateBoundTestFramework.Throw(String message)
TestFrameworkProvider.Throw(String message)
CollectingAssertionStrategy.ThrowIfAny(IDictionary`2 context)
EquivalencyValidator.AssertEquality(EquivalencyValidationContext context)
ObjectAssertions.BeEquivalentTo[TExpectation](TExpectation expectation, Func`2 config, String because, Object[] becauseArgs)
AddPatientCommandTests.AddPatientCommand_Adds_New_Patient_To_Db() line 28
GenericAdapter`1.BlockUntilCompleted()
NoMessagePumpStrategy.WaitForCompletion(AwaitAdapter awaitable)
AsyncToSyncAdapter.Await(Func`1 invoke)
TestMethodCommand.RunTestMethod(TestExecutionContext context)
TestMethodCommand.Execute(TestExecutionContext context)
<>c__DisplayClass1_0.<Execute>b__0()
BeforeAndAfterTestCommand.RunTestMethodInThreadAbortSafeZone(TestExecutionContext context, Action action)
Further technical details
Npgsql version: 5.0.3 PostgreSQL version: latest Operating system: linux in docker
Other details about my project setup: the repo (and the integration tests in it) linked above should run locally if you have docker installed
if you need any more info I’d be happy to provide it. thanks so much!
Issue Analytics
- State:
- Created 2 years ago
- Comments:8 (4 by maintainers)
Hello!
I’m afraid there is nothing we can do, as this is a Postgres limitation (SqlServer’s
DateTimeOffset
has a precision of 10^-7, while Postgres supports up to 10^-6).yeah, I see what you mean now 🙁
since the thing i care about is my tests, it looks like I have a single ‘set it and forget it’ capability with fluent assertions to always evaluate datetimes to whatever precision i want and will probably be the route i go. thanks for the input on this!