Skip to content

Commit

Permalink
add tests and fix empty set handling
Browse files Browse the repository at this point in the history
  • Loading branch information
Erno Kuusela committed Sep 20, 2024
1 parent 78ba5ad commit 257081f
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 15 deletions.
26 changes: 14 additions & 12 deletions src/specql/op.clj
Original file line number Diff line number Diff line change
Expand Up @@ -83,18 +83,20 @@
(defrecord Overlapsop [values]
Op
(to-sql [_ v {transform ::xf/transform type :type enum? :enum? :as column-info}]
(let [values (if transform
(map #(xf/to-sql transform %) values)
values)
param (if enum?
(str "?::" type)
"?")]
[(str v " && ["
(if (empty? values)
"NULL"
(str/join "," (repeat (count values) param)))
"]")
(vec values)])))
(if (empty? values)
["FALSE" []] ; Ensure no results are returned for empty set.
; PostgreSQL also considers .. && ARRAY[] to be false.
; (In set theory the empty set is considered to
(let [values (if transform
(map #(xf/to-sql transform %) values)
values)
param (if enum?
(str "?::" type)
"?")]
[(str v " && ARRAY["
(str/join "," (repeat (count values) param))
"]")
(vec values)]))))

(defn overlaps [values]
(assert (clojure.core/and
Expand Down
13 changes: 10 additions & 3 deletions test/specql/core_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -814,9 +814,16 @@
{:issue/status (op/in #{:issue.status/resolved})}))))

(testing "Overlaps operator"
(is (= (list {:issue/title "I have some issues"})
(fetch db :issue/issue #{:issue/idhistory :issue/title}
{:issue/idhistory (op/overlaps #{42})}))))
(is (= (list {:issue/idhistory [42, 23] :issue/title "I have some issues"})
(fetch db :issue/issue #{:issue/title :issue/idhistory}
{:issue/idhistory (op/overlaps #{(int 42)})})))

(is (empty?
(fetch db :issue/issue #{:issue/title :issue/idhistory}
{:issue/idhistory (op/overlaps #{(int 43)})})))
(is (empty?
(fetch db :issue/issue #{:issue/title :issue/idhistory}
{:issue/idhistory (op/overlaps #{})}))))

(testing "Upsert transformed"
(let [issue (upsert! db :issue/issue
Expand Down

0 comments on commit 257081f

Please sign in to comment.