views.sql/test/views/sql/vexec_tests.clj

65 lines
3.4 KiB
Clojure
Raw Permalink Normal View History

2016-05-31 18:20:09 -04:00
(ns views.sql.vexec-tests
(:use
clojure.test
views.sql.test-fixtures
views.sql.core)
(:require
[clojure.java.jdbc :as jdbc]
[views.core :as views]))
(defn vexec-redefs-fixture [f]
(reset! redefs-called {})
(with-redefs
2016-05-31 18:30:36 -04:00
[jdbc/query (->redef-fn :jdbc/query :jdbc/query-return-value)
jdbc/execute! (->redef-fn :jdbc/execute! :jdbc/execute!-return-value)
views/put-hints! (->redef-fn :views/put-hints!)]
2016-05-31 18:20:09 -04:00
(f)))
(use-fixtures :each clear-query-cache-fixture reset-test-view-system-fixture vexec-redefs-fixture)
(deftest vexec-runs-query-and-puts-hints
(let [sqlvec ["INSERT INTO example (field1, field2, field3) VALUES ('test', 'N', NULL)"]
result (vexec! test-view-system test-db sqlvec)]
2016-05-31 18:32:59 -04:00
(is (called-with-args? :jdbc/execute! test-db sqlvec))
2016-05-31 18:20:09 -04:00
(is (= :jdbc/execute!-return-value result))
2016-05-31 18:30:36 -04:00
(is (called-with-args? :views/put-hints! test-view-system [(views/hint nil #{:example} hint-type)]))))
2016-05-31 18:20:09 -04:00
(deftest vexec-runs-query-with-returning-clause
(let [sqlvec ["INSERT INTO example (field1, field2, field3) VALUES ('test', 'N', NULL) RETURNING *"]
result (vexec! test-view-system test-db sqlvec)]
2016-05-31 18:32:59 -04:00
(is (called-with-args? :jdbc/query test-db sqlvec))
2016-05-31 18:20:09 -04:00
(is (= :jdbc/query-return-value result))
2016-05-31 18:30:36 -04:00
(is (called-with-args? :views/put-hints! test-view-system [(views/hint nil #{:example} hint-type)]))))
2016-05-31 18:20:09 -04:00
(deftest namespace-is-passed-along-to-hints-via-vexec
(let [sqlvec ["INSERT INTO example (field1, field2, field3) VALUES ('test', 'N', NULL)"]
result (vexec! test-view-system test-db sqlvec {:namespace :foobar})]
2016-05-31 18:32:59 -04:00
(is (called-with-args? :jdbc/execute! test-db sqlvec))
2016-05-31 18:20:09 -04:00
(is (= :jdbc/execute!-return-value result))
2016-05-31 18:30:36 -04:00
(is (called-with-args? :views/put-hints! test-view-system [(views/hint :foobar #{:example} hint-type)]))))
2016-05-31 18:20:09 -04:00
(deftest manually-provided-hints-to-vexec-are-passed-to-views-system
(let [sqlvec ["INSERT INTO example (field1, field2, field3) VALUES ('test', 'N', NULL)"]
result (vexec! test-view-system test-db sqlvec [:foo :bar])]
2016-05-31 18:32:59 -04:00
(is (called-with-args? :jdbc/execute! test-db sqlvec))
2016-05-31 18:20:09 -04:00
(is (= :jdbc/execute!-return-value result))
2016-05-31 18:30:36 -04:00
(is (called-with-args? :views/put-hints! test-view-system [(views/hint nil #{:foo :bar} hint-type)]))))
2016-05-31 18:20:09 -04:00
(deftest manually-provided-hints-to-vexec-also-requires-specifying-returning-option
(let [sqlvec ["INSERT INTO example (field1, field2, field3) VALUES ('test', 'N', NULL) RETURNING *"]
result (vexec! test-view-system test-db sqlvec [:foo :bar])]
; means jdbc/execute! was called which is correct behaviour since the returning option was not specified.
; if jdbc/execute! was really called it would throw an exception since the INSERT query used has a RETURNING clause
2016-05-31 18:32:59 -04:00
(is (called-with-args? :jdbc/execute! test-db sqlvec))
2016-05-31 18:20:09 -04:00
(is (= :jdbc/execute!-return-value result))
2016-05-31 18:30:36 -04:00
(is (called-with-args? :views/put-hints! test-view-system [(views/hint nil #{:foo :bar} hint-type)])))
2016-05-31 18:20:09 -04:00
; manually reset some things
(reset! redefs-called {})
(reset! query-info-cache {})
(let [sqlvec ["INSERT INTO example (field1, field2, field3) VALUES ('test', 'N', NULL) RETURNING *"]
result (vexec! test-view-system test-db sqlvec [:foo :bar] {:returning? true})]
2016-05-31 18:32:59 -04:00
(is (called-with-args? :jdbc/query test-db sqlvec))
2016-05-31 18:20:09 -04:00
(is (= :jdbc/query-return-value result))
2016-05-31 18:30:36 -04:00
(is (called-with-args? :views/put-hints! test-view-system [(views/hint nil #{:foo :bar} hint-type)]))))