Permalink
Browse files

Testing java.sql.Timestamp objects returned by with-query-results

java.sql.Timestamp will use the local time zone if the time zone is not passed into the java.sql.ResultSet getter functions. This causes errors if the date stored in the database is in a different time zone.
  • Loading branch information...
1 parent 629893e commit 195397b1b2a0245d2439ab9963fe2138450a27f3 @jestinepaul committed Jul 21, 2012
Showing with 12 additions and 0 deletions.
  1. +12 −0 src/test/clojure/clojure/java/test_jdbc.clj
@@ -134,6 +134,8 @@
[:appearance "VARCHAR(32)"]
[:cost :int]
[:grade :real]
+ (if (= "postgresql" p)
+ [:expiry "TIMESTAMP WITHOUT TIME ZONE"])
:table-spec (if (or (= "mysql" p) (and (string? db) (re-find #"mysql:" db)))
"ENGINE=InnoDB" ""))))
@@ -185,6 +187,16 @@
(sql/do-prepared "INSERT INTO fruit2 ( name, appearance, cost, grade ) VALUES ( ?, ?, ?, ? )" ["test" "test" 1 1.0] ["two" "two" 2 2.0])
(is (= 2 (sql/with-query-results res ["SELECT * FROM fruit2"] (count res)))))))
+(deftest test-timestamp
+ (doseq [db (test-specs)]
+ (if (= "postgresql" (:subprotocol db))
+ (sql/with-connection db
+ (create-test-table :fruit2 db)
+ (sql/do-prepared "INSERT INTO fruit2 ( name, appearance, cost, grade, expiry ) VALUES ( 'test', 'test', 1, 1.0, '2012/07/23')")
+ (is (= (.getTime (.getTime (doto (java.util.GregorianCalendar. (java.util.TimeZone/getTimeZone "UTC"))
+ (.set 2012 6 23 0 0 0)))) ; Month value is 0-based. e.g., 0 for January.
+ (sql/with-query-results res ["SELECT * FROM fruit2"] (.getTime (:expiry (first res))))))))))
+
(deftest test-insert-rows
(doseq [db (test-specs)]
(sql/with-connection db

0 comments on commit 195397b

Please sign in to comment.