add helper for #567 and helper-based tests

Signed-off-by: Sean Corfield <sean@corfield.org>
This commit is contained in:
Sean Corfield 2025-02-20 17:12:31 -08:00
parent 4d1f5f83b7
commit f0eb68f151
No known key found for this signature in database
2 changed files with 25 additions and 8 deletions

View file

@ -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 (ns honey.sql.helpers
"Helper functions for the built-in clauses in honey.sql. "Helper functions for the built-in clauses in honey.sql.
@ -58,7 +58,7 @@
bulk-collect-info [& args] bulk-collect-info [& args]
(as they are for all helper functions)." (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] (:require [clojure.core :as c]
[honey.sql :as h])) [honey.sql :as h]))
@ -452,6 +452,14 @@
[& clauses] [& clauses]
(generic :except-all (cons {} clauses))) (generic :except-all (cons {} clauses)))
(defn assert
"Accepts an expression (predicate).
Produces: ASSERT expression"
{:arglists '([expr])}
[& args]
(generic-1 :assert args))
(defn select (defn select
"Accepts any number of column names, or column/alias "Accepts any number of column names, or column/alias
pairs, or SQL expressions (optionally aliased): pairs, or SQL expressions (optionally aliased):

View file

@ -131,9 +131,18 @@
(sql/format '{select (((get-in (. (object {_id 1 b "thing"}) b) c (lift 1) d)))})))) (sql/format '{select (((get-in (. (object {_id 1 b "thing"}) b) c (lift 1) d)))}))))
(deftest assert-statement (deftest assert-statement
(is (= ["ASSERT NOT EXISTS (SELECT 1 FROM users WHERE email = 'james @example.com')"] (testing "quoted sql"
(sql/format '{assert (not-exists {select 1 from users where (= email "james @example.com")})} (is (= ["ASSERT NOT EXISTS (SELECT 1 FROM users WHERE email = 'james @example.com')"]
:inline true))) (sql/format '{assert (not-exists {select 1 from users where (= email "james @example.com")})}
(is (= ["ASSERT TRUE"] :inline true)))
(sql/format '{assert true} (is (= ["ASSERT TRUE"]
:inline 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}))))))