我在使用 clojure 和 postgresql 时遇到问题。
例如,当我将“2017-06-27”插入 DB(日期格式列)时,它会保存“2017-06-26”,当我选择此结果时,返回的对象是#inst“2017-06-28T03: 00:00.000-00:00”
最奇怪的是,当我在本地机器上运行测试时会发生这种情况,因为我创建了一个 docker 设置,并且相同的代码可以完美运行。
以下是代码:
(ns balances.db
(:require [clj-time.coerce :as c]
[clj-time.format :as f]))
; Function to convert data objects from queries to string
(defn db-to-str [date]
(f/unparse (f/formatters :date) (c/from-sql-date date))
)
; Function to convert string to data objetcs that can be inserted in db
(defn str-to-db [date]
(c/to-sql-date date)
)
(ns balances.operation
(:require [clojure.java.jdbc :as jdbc]
[balances.db :refer [db-spec]]))
(defn create-operation [accountid amount description operationdate]
(jdbc/insert! db-spec :operations {
:accountid accountid
:amount amount
:description description
:operationdate operationdate
})
)
(ns balances.operation-test
(:use clojure.test
ring.mock.request
balances.core-test)
(:require [balances.operation :as operation]
[balances.db :as db]))
(use-fixtures :each db/clear-db-fixture)
(deftest operation
(testing "create operation"
(let [op (first (operation/create-operation 1 100 "Test operation" (db/str-to-db "2017-06-27")))]
(is (= "2017-06-27" (db/db-to-str (op :operationdate))))
)
)
)
在本地环境中,创建操作测试失败,返回:
expected: (= "2017-06-27" (db/db-to-str (op :operationdate)))
actual: (not (= "2017-06-27" "2017-06-26"))
在 docker 环境中,测试通过。