1

我的数据库有一个这样的列 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)))

我有一个助手 f-tion 来转换日期字符串:

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

4

0 回答 0