diff --git a/Tests/FluentPostgresDriverTests/FluentPostgresDriverTests.swift b/Tests/FluentPostgresDriverTests/FluentPostgresDriverTests.swift index 2501e4a..2923b1c 100644 --- a/Tests/FluentPostgresDriverTests/FluentPostgresDriverTests.swift +++ b/Tests/FluentPostgresDriverTests/FluentPostgresDriverTests.swift @@ -245,6 +245,47 @@ final class FluentPostgresDriverTests: XCTestCase { try await db.schema(Seq.schema).delete() } + func testSwift6DatePrecisionBug() async throws { + final class Event: Model, @unchecked Sendable { + static let schema = "orgs" + + @ID(custom: "id", generatedBy: .database) var id: Int? + @Field(key: "date") var date: Date + + init() {} + } + + struct CreateEvent: AsyncMigration { + func prepare(on database: any Database) async throws { + try await database.schema("orgs") + .field("id", .int, .identifier(auto: true)) + .field("date", .datetime, .required) + .create() + } + + func revert(on database: any Database) async throws { + try await database.schema("orgs").delete() + } + } + + try await CreateEvent().prepare(on: self.db) + do { + let date = Date() + let new = Event() + XCTAssertEqual(date.timeIntervalSince1970, date.timeIntervalSince1970) + new.date = date + XCTAssertEqual(new.date.timeIntervalSince1970, date.timeIntervalSince1970) + try await new.save(on: self.db) + XCTAssertEqual(new.date.timeIntervalSince1970, date.timeIntervalSince1970) + let fetched = try await Event.query(on: self.db).first()! + XCTAssertEqual(fetched.date.timeIntervalSince1970, date.timeIntervalSince1970) + } catch { + try? await CreateEvent().revert(on: self.db) + throw error + } + try await CreateEvent().revert(on: self.db) + } + var benchmarker: FluentBenchmarker { .init(databases: self.dbs) } var eventLoopGroup: any EventLoopGroup { MultiThreadedEventLoopGroup.singleton }