question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

DateTimeOffset precision issue

See original GitHub issue

Before 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:closed
  • Created 2 years ago
  • Comments:8 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
vonzshikcommented, Mar 30, 2021

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).

0reactions
pdevito3commented, Apr 1, 2021

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!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Losing precision when saving a DateTimeOffset setting in ...
Losing precision when saving a DateTimeOffset setting in project settings. The first variable is the original value, before serialization. The ...
Read more >
datetimeoffset (Transact-SQL) - SQL Server
Defines a date that is combined with a time of a day based on a 24-hour clock like datetime2, and adds time zone...
Read more >
How to modify the precision of a datetimeoffset column?
In a sql-server database with data I have several columns with type datetimeoffset(7) . Now I would like to change the precision part...
Read more >
Clarification about Precision on Edm.DateTimeOffset
In Odata documentation it states that if no Precision value is specified it will have an arbitrary precision for decimals while it will...
Read more >
Understanding 'datetimeoffset' Storage Size in SQL Server
This makes the maximum size of a datetime2 value 9 bytes – 1 byte stores precision plus 8 bytes for data storage at...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found