Skip to content

Commit

Permalink
get-else throws if nil is used for default value
Browse files Browse the repository at this point in the history
  • Loading branch information
tonsky committed Jan 24, 2016
1 parent a98dc77 commit 4d27be6
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 1 deletion.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# WIP

- `get-else` throws if `nil` is used for default value

# 0.15.0

- Better error messages for upsert conflicts
Expand Down
2 changes: 2 additions & 0 deletions src/datascript/query.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,8 @@

(defn- -get-else
[db e a else-val]
(when (nil? else-val)
(raise "get-else: nil default value is not supported" {:error :query/where}))
(if-let [datom (first (db/-search db [e a]))]
(:v datom)
else-val))
Expand Down
7 changes: 6 additions & 1 deletion test/datascript/test/query_fns.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,12 @@
(is (= (d/q '[:find ?e ?age ?height
:where [?e :age ?age]
[(get-else $ ?e :height 300) ?height]] db)
#{[1 15 300] [2 22 240] [3 37 300]})))
#{[1 15 300] [2 22 240] [3 37 300]}))

(is (thrown-with-msg? ExceptionInfo #"get-else: nil default value is not supported"
(d/q '[:find ?e ?height
:where [?e :age]
[(get-else $ ?e :height nil) ?height]] db))))

(testing "get-some"
(is (= (d/q '[:find ?e ?a ?v
Expand Down

0 comments on commit 4d27be6

Please sign in to comment.