持久保存到数据库

时间:2016-10-11 08:28:44

标签: clojure h2 korma clj-time

我的数据库有一个像这样的列dob:

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

对于TDD,我使用korma和lobos对内存中的H2数据库进行测试:

(: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)))

我有一个辅助函数来转换日期字符串和从日期字符串转换:

 (: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))

然而,当我测试商店并以某种方式检索时,我总是在日期之间得到一天的差异:

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

失败:实际:(不是(= {:dob“1935-06-05”} {:dob“1935-06-04”}))

0 个答案:

没有答案