I'm working on an ASP .NET Core 7 WebAPI, which handles Datetimes. I use Entity Framework Core 7 and Npgsql with Postgres.
As I want to save Datetimes without timezones to the database, I use the switch on the AppContext
to enable it, according to Npgsql docs.
The issue is, in my integration test, everything is fine (date in response body is correct) until the date on the created record in the database, the Datetime
object I get is shifted to 1 hour in the futur, event though, when I check in the database with a SQL Client, the date is correct. Here is the code of my integration test :
[Fact]
public async Task CreatePoll_CreatesPollInDb()
{
// Given
var createPollRequest = new CreatePollRequest
("Test Poll", DateTime.Now.AddDays(90), new List<DateTime> { DateTime.Now, DateTime.Now.AddDays(1) } );
// When
HttpResponseMessage response = await Client.PostAsJsonAsync("/api/polls", createPollRequest);
// Then
response.StatusCode.Should().Be(HttpStatusCode.Created);
var pollFromResponse = await response.Content.ReadFromJsonAsync<Poll>();
pollFromResponse.Id.Should().NotBeEmpty();
pollFromResponse.Name.Should().Be(createPollRequest.Name);
pollFromResponse.Dates.Should().BeEquivalentTo(createPollRequest.Dates);
pollFromResponse.ExpirationDate.Should().Be(createPollRequest.ExpirationDate);
Poll pollInDb = await DbContext.Polls.FirstAsync(record => record.Id == pollFromResponse.Id);
pollInDb.Name.Should().Be(createPollRequest.Name);
pollInDb.ExpirationDate.Should().Be(createPollRequest.ExpirationDate);
pollInDb.Dates.Should().BeEquivalentTo(createPollRequest.Dates);
}
The DbContext
used in the test is not the one of the application, it is created specifically for the test as a separate fixture. The test runs against a real postgres database running in Docker.