From cfadae53d1a36f6ac9bb60758b82b6e4864113b5 Mon Sep 17 00:00:00 2001 From: Justin Kramer Date: Fri, 24 Aug 2012 17:08:22 -0400 Subject: [PATCH] always pipe base map through build, to ensure consistency --- src/honeysql/core.clj | 4 +++- test/honeysql/core_test.clj | 36 +++++++++++++++++++++++++++++------- 2 files changed, 32 insertions(+), 8 deletions(-) diff --git a/src/honeysql/core.clj b/src/honeysql/core.clj index a0869d5..c43e17d 100644 --- a/src/honeysql/core.clj +++ b/src/honeysql/core.clj @@ -17,5 +17,7 @@ (reduce (fn [sql-map [op args]] (build-clause op sql-map args)) - base + (if (empty? base) + base + (apply build (apply concat base))) (partition 2 clauses)))) \ No newline at end of file diff --git a/test/honeysql/core_test.clj b/test/honeysql/core_test.clj index 470a766..8a39472 100644 --- a/test/honeysql/core_test.clj +++ b/test/honeysql/core_test.clj @@ -1,15 +1,15 @@ (ns honeysql.core-test (:refer-clojure :exclude [format]) (:require [clojure.test :refer [deftest testing is]] - [honeysql.core :refer :all] + [honeysql.core :as sql] [honeysql.helpers :refer :all])) ;; TODO: more tests (deftest test-select (let [m1 (-> (select :f.* :b.baz :c.quux [:b.bla "bla-bla"] - (call :now) (raw "@x := 10")) - (un-select :c.quux) + (sql/call :now) (sql/raw "@x := 10")) + ;;(un-select :c.quux) (modifiers :distinct) (from [:foo :f] [:baz :b]) (join [[:clod :c] [:= :f.a :c.d] :left] @@ -19,14 +19,36 @@ [:< 1 2 3] [:in :f.e [1 2 3]] [:between :f.e 10 20]]) - (merge-where [:not= nil :b.bla]) + ;;(merge-where [:not= nil :b.bla]) (group :f.a) (having [:< 0 :f.e]) (order-by [:b.baz :desc] :c.quux) (limit 50) - (offset 10))] + (offset 10)) + m2 {:select [:f.* :b.baz :c.quux [:b.bla "bla-bla"] + (sql/call :now) (sql/raw "@x := 10")] + ;;:un-select :c.quux + :modifiers :distinct + :from [[:foo :f] [:baz :b]] + :join [[[:clod :c] [:= :f.a :c.d] :left] + [:draq [:= :f.b :draq.x]]] + :where [:or + [:and [:= :f.a "bort"] [:not= :b.baz "gabba"]] + [:< 1 2 3] + [:in :f.e [1 2 3]] + [:between :f.e 10 20]] + ;;:merge-where [:not= nil :b.bla] + :group-by :f.a + :having [:< 0 :f.e] + :order-by [[:b.baz :desc] :c.quux] + :limit 50 + :offset 10} + m3 (sql/build m2) + m4 (apply sql/build (apply concat m2))] + (testing "Various construction methods are consistent" + (is (= m1 m3))) (testing "SQL data formats correctly" - (is (= (format m1) - ["SELECT DISTINCT f.*, b.baz, b.bla AS \"bla-bla\", NOW(), @x := 10 FROM foo AS f, baz AS b LEFT JOIN clod AS c ON f.a = c.d JOIN draq ON f.b = draq.x WHERE (((f.a = ? AND b.baz <> ?) OR (1 < 2 AND 2 < 3) OR (f.e IN (1, 2, 3)) OR f.e BETWEEN 10 AND 20) AND b.bla IS NOT NULL) GROUP BY f.a HAVING 0 < f.e ORDER BY b.baz DESC, c.quux LIMIT 50 OFFSET 10" "bort" "gabba"]))) + (is (= (sql/format m1) + ["SELECT DISTINCT f.*, b.baz, c.quux, b.bla AS \"bla-bla\", NOW(), @x := 10 FROM foo AS f, baz AS b LEFT JOIN clod AS c ON f.a = c.d JOIN draq ON f.b = draq.x WHERE ((f.a = ? AND b.baz <> ?) OR (1 < 2 AND 2 < 3) OR (f.e IN (1, 2, 3)) OR f.e BETWEEN 10 AND 20) GROUP BY f.a HAVING 0 < f.e ORDER BY b.baz DESC, c.quux LIMIT 50 OFFSET 10" "bort" "gabba"]))) (testing "SQL data prints and reads correctly" (is (= m1 (read-string (pr-str m1)))))))