1

My db has a column dob like this:

|    dob     |
+------------+
| 1935-06-05 |

For TDD I test against an in-memory H2 database using korma and lobos:

(:use (lobos [migration :only [defmigration]] core schema config)
( :require [korma.core :as k] )

(defmigration create-table-readings
  (up [] (create
           (table :readings
                  (date :dob :not-null)
                  )))
  (down [] (drop
             (table :readings))))

(defn insert 
  [row]
  (k/insert readings (k/values row)))

I have a helper f-tion to convert to and from date strings:

 (:require 
    [clj-time.core :as t]
    [clj-time.format :as f]
    [clj-time.coerce :as c])

(def custom-formatter (f/formatter "yyyy-MM-dd"))

(defn from-sql-to-string
  "formats date from sql date to string"
  [sql-time]
  (f/unparse custom-formatter 
             (c/from-sql-date sql-time)))

(defn from-string-to-sql
  "formats date string and returns sql date"
  [string-time]
  (c/to-sql-date string-time))

yet when I test store and retrieve somehow I always get a one day difference between the dates:

(deftest database-insert-tests
  (testing "Testing simple insert"
           (let [test-row { :dob "1935-06-05" }]
             (database/insert test-row)
             (let [first-row (first (database/select-all))]
               (is (= test-row
                      (assoc first-row
                             :dob 
                             (t/from-sql-to-string (:dob first-row)))
                      ))))))

FAIL: actual: (not (= { :dob "1935-06-05" } { :dob "1935-06-04" }))

fbielejec
  • 3,492
  • 4
  • 27
  • 35
  • What did you try to debug your issue? Did you check what values are actually stored in DB table? What values are passed to and returned from your `from-sql-to-string` and `from-string-to-sql` functions? – Piotrek Bzdyl Oct 11 '16 at 19:03
  • Don't think this is a problem, I suspect some daytime difference issues perhaps start/end of day is treated differently. – fbielejec Oct 11 '16 at 19:10
  • Exactly what I am thinking and your functions using date/time API might be the source of the problem. I would check them and the content of the database table nevertheless. – Piotrek Bzdyl Oct 11 '16 at 19:11

0 Answers0