Skip to content

Commit

Permalink
add helper for #567 and helper-based tests
Browse files Browse the repository at this point in the history
Signed-off-by: Sean Corfield <[email protected]>
  • Loading branch information
seancorfield committed Feb 21, 2025
1 parent 4d1f5f8 commit f0eb68f
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 8 deletions.
12 changes: 10 additions & 2 deletions src/honey/sql/helpers.cljc
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
;; copyright (c) 2020-2024 sean corfield, all rights reserved
;; copyright (c) 2020-2025 sean corfield, all rights reserved

(ns honey.sql.helpers
"Helper functions for the built-in clauses in honey.sql.
Expand Down Expand Up @@ -58,7 +58,7 @@
bulk-collect-info [& args]
(as they are for all helper functions)."
(:refer-clojure :exclude [distinct filter for group-by into partition-by set update])
(:refer-clojure :exclude [assert distinct filter for group-by into partition-by set update])
(:require [clojure.core :as c]
[honey.sql :as h]))

Expand Down Expand Up @@ -452,6 +452,14 @@
[& clauses]
(generic :except-all (cons {} clauses)))

(defn assert
"Accepts an expression (predicate).
Produces: ASSERT expression"
{:arglists '([expr])}
[& args]
(generic-1 :assert args))

(defn select
"Accepts any number of column names, or column/alias
pairs, or SQL expressions (optionally aliased):
Expand Down
21 changes: 15 additions & 6 deletions test/honey/sql/xtdb_test.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -131,9 +131,18 @@
(sql/format '{select (((get-in (. (object {_id 1 b "thing"}) b) c (lift 1) d)))}))))

(deftest assert-statement
(is (= ["ASSERT NOT EXISTS (SELECT 1 FROM users WHERE email = 'james @example.com')"]
(sql/format '{assert (not-exists {select 1 from users where (= email "james @example.com")})}
:inline true)))
(is (= ["ASSERT TRUE"]
(sql/format '{assert true}
:inline true))))
(testing "quoted sql"
(is (= ["ASSERT NOT EXISTS (SELECT 1 FROM users WHERE email = 'james @example.com')"]
(sql/format '{assert (not-exists {select 1 from users where (= email "james @example.com")})}
:inline true)))
(is (= ["ASSERT TRUE"]
(sql/format '{assert true}
:inline true))))
(testing "helper"
(is (= ["ASSERT NOT EXISTS (SELECT 1 FROM users WHERE email = 'james @example.com')"]
(-> (h/assert [:not-exists {:select 1 :from :users :where [:= :email "james @example.com"]}])
(sql/format {:inline true}))))
(is (= ["ASSERT NOT EXISTS (SELECT 1 FROM users WHERE email = 'james @example.com')"]
(-> {}
(h/assert [:not-exists {:select 1 :from :users :where [:= :email "james @example.com"]}])
(sql/format {:inline true}))))))

0 comments on commit f0eb68f

Please sign in to comment.