luminus-framework/luminus

test error on Windows - timestamp fractional seconds

jacobmorzinski opened this issue · 3 comments

While working through the Guestbook tutorial, I had an error during the "Adding some tests" section. It appears that java.time.LocalDateTime/now on Windows has 0.1-microsecond precision, but the H2 database was created with microsecond precision.

For example LocalDateTime/now returns a time ending in ".5992517" but it is saved in the DB as ".599252". So lein test often fails because the timestamp saved in the DB does not match the original timestamp of the test.

Possible fixes

I can think of two fixes. One option is to change the test to work with the DB schema. Another option is to change the DB to work with the test timestamp. Do you have a preference between these ideas, or prefer something else altogether?

  1. Change the test to match the DB: In test\db\core.clj, change the test to truncate the timestamp to microsecond precision before saving to the DB. For example:
    (let [timestamp (-> (java.time.LocalDateTime/now)
                        (.truncatedTo (java.time.temporal.ChronoUnit/MICROS)))]
  1. Change the DB to match the test: In resources\migrations\<date>-add-users-table.up.sql, change the DB schema to use more than 6 digits of fractional precision. For example:
 timestamp TIMESTAMP(7));

For information about H2 timestamps see http://www.h2database.com/html/datatypes.html#timestamp_type

Error

Here is the main part of the error I get:

FAIL in (test-message) (core.clj:29)
expected: {:name "Bob",
           :message "Hello, World",
           :timestamp
           #object[java.time.LocalDateTime 0x74786396 "2019-07-21T19:56:44.599251700"]}
  actual: {:name "Bob",
           :message "Hello, World",
           :timestamp
           #object[java.time.LocalDateTime 0x8d4d58b "2019-07-21T19:56:44.599252"]}
    diff: - {:timestamp
             #object[java.time.LocalDateTime 0x74786396 "2019-07-21T19:56:44.599251700"]}
          + {:timestamp
             #object[java.time.LocalDateTime 0x8d4d58b "2019-07-21T19:56:44.599252"]}

Hi,

I think my preference would be fore the second option using TIMESTAMP(7) in the schema. If you'd be up for doing a PR that would be great as I don't have a Windows machine handy.

Here you go. Let me know if anything looks strange.

Looks great thanks for the report and the fix. ;)