mirror of https://github.com/logseq/logseq
Add regression tests for querying properties with links
These tests were fixed by f1728c48cb
Also cleaned up more of query and tests i.e. split up large fns
pull/4159/head
parent
5a126027eb
commit
1df386ecca
|
@ -141,6 +141,7 @@
|
||||||
l)
|
l)
|
||||||
@vars))
|
@vars))
|
||||||
|
|
||||||
|
;; TODO: Convert -> fns to rules
|
||||||
(defn ->property-query
|
(defn ->property-query
|
||||||
([k v]
|
([k v]
|
||||||
(->property-query k v '?v))
|
(->property-query k v '?v))
|
||||||
|
@ -155,6 +156,16 @@
|
||||||
;; For integer pages that aren't strings
|
;; For integer pages that aren't strings
|
||||||
[(list 'contains? sym (str v))])]))
|
[(list 'contains? sym (str v))])]))
|
||||||
|
|
||||||
|
(defn ->page-property-query
|
||||||
|
[k v]
|
||||||
|
[['?p :block/name]
|
||||||
|
['?p :block/properties '?prop]
|
||||||
|
[(list 'get '?prop (keyword k)) '?v]
|
||||||
|
(list
|
||||||
|
'or
|
||||||
|
[(list '= '?v v)]
|
||||||
|
[(list 'contains? '?v v)])])
|
||||||
|
|
||||||
(defn build-query
|
(defn build-query
|
||||||
([repo e env]
|
([repo e env]
|
||||||
(build-query repo e (assoc env :vars (atom {})) 0))
|
(build-query repo e (assoc env :vars (atom {})) 0))
|
||||||
|
@ -354,15 +365,8 @@
|
||||||
k (string/replace (name k) "_" "-")]
|
k (string/replace (name k) "_" "-")]
|
||||||
(if-not (nil? v)
|
(if-not (nil? v)
|
||||||
(let [v (text/parse-property k v)
|
(let [v (text/parse-property k v)
|
||||||
v (if (coll? v) (first v) v)
|
v (if (coll? v) (first v) v)]
|
||||||
sym '?v]
|
(->page-property-query k v))
|
||||||
[['?p :block/name]
|
|
||||||
['?p :block/properties '?prop]
|
|
||||||
[(list 'get '?prop (keyword k)) sym]
|
|
||||||
(list
|
|
||||||
'or
|
|
||||||
[(list '= sym v)]
|
|
||||||
[(list 'contains? sym v)])])
|
|
||||||
[['?p :block/name]
|
[['?p :block/name]
|
||||||
['?p :block/properties '?prop]
|
['?p :block/properties '?prop]
|
||||||
[(list 'get '?prop (keyword k)) '?prop-v]
|
[(list 'get '?prop (keyword k)) '?prop-v]
|
||||||
|
|
|
@ -17,7 +17,7 @@
|
||||||
:file/content "---
|
:file/content "---
|
||||||
title: Dec 26th, 2020
|
title: Dec 26th, 2020
|
||||||
tags: [[page-tag-1]], page-tag-2
|
tags: [[page-tag-1]], page-tag-2
|
||||||
parent: [[child page 1]]
|
parent: [[child page 1]], [[child-no-space]]
|
||||||
---
|
---
|
||||||
- DONE 26-b1 [[page 1]]
|
- DONE 26-b1 [[page 1]]
|
||||||
created-at:: 1608968448113
|
created-at:: 1608968448113
|
||||||
|
@ -26,6 +26,7 @@ prop-a:: val-a
|
||||||
prop-c:: [[page a]], [[page b]], [[page c]]
|
prop-c:: [[page a]], [[page b]], [[page c]]
|
||||||
prop-num:: 2000
|
prop-num:: 2000
|
||||||
prop-linked-num:: [[3000]]
|
prop-linked-num:: [[3000]]
|
||||||
|
prop-d:: [[no-space-link]]
|
||||||
- LATER 26-b2-modified-later [[page 2]] #tag1
|
- LATER 26-b2-modified-later [[page 2]] #tag1
|
||||||
created-at:: 1608968448114
|
created-at:: 1608968448114
|
||||||
last-modified-at:: 1608968448120
|
last-modified-at:: 1608968448120
|
||||||
|
@ -84,9 +85,6 @@ last-modified-at:: 1609084800002"}]]
|
||||||
|
|
||||||
(defn q-count
|
(defn q-count
|
||||||
[s]
|
[s]
|
||||||
#_(let [db (frontend.db.conn/get-conn test-db)]
|
|
||||||
(prn :DB (d/q '[:find (pull ?b [*]) :where [?b :block/properties ?prop] [(get ?prop :prop-num)]]
|
|
||||||
db)))
|
|
||||||
(let [{:keys [query result]} (q s)]
|
(let [{:keys [query result]} (q s)]
|
||||||
{:query query
|
{:query query
|
||||||
:count (if result
|
:count (if result
|
||||||
|
@ -143,8 +141,53 @@ last-modified-at:: 1609084800002"}]]
|
||||||
|
|
||||||
"(property prop-linked-num 3000)"
|
"(property prop-linked-num 3000)"
|
||||||
{:query (dsl/->property-query "prop-linked-num" 3000)
|
{:query (dsl/->property-query "prop-linked-num" 3000)
|
||||||
|
:count 1}
|
||||||
|
|
||||||
|
"(property prop-d no-space-link)"
|
||||||
|
{:query (dsl/->property-query "prop-d" "no-space-link")
|
||||||
:count 1}))
|
:count 1}))
|
||||||
|
|
||||||
|
(deftest page-property-queries
|
||||||
|
(are [x y] (= (q-count x) y)
|
||||||
|
"(page-property parent)"
|
||||||
|
{:query '[[?p :block/name]
|
||||||
|
[?p :block/properties ?prop]
|
||||||
|
[(get ?prop :parent) ?prop-v]
|
||||||
|
[true]], :count 3}
|
||||||
|
|
||||||
|
"(page-property parent [[child page 1]])"
|
||||||
|
{:query (dsl/->page-property-query "parent" "child page 1")
|
||||||
|
:count 2}
|
||||||
|
|
||||||
|
"(page-property parent [[child-no-space]])"
|
||||||
|
{:query (dsl/->page-property-query "parent" "child-no-space")
|
||||||
|
:count 1}
|
||||||
|
|
||||||
|
"(page-property parent \"child page 1\")"
|
||||||
|
{:query (dsl/->page-property-query "parent" "child page 1")
|
||||||
|
:count 2}
|
||||||
|
|
||||||
|
"(and (page-property parent [[child page 1]]) (page-property parent [[child page 2]]))"
|
||||||
|
{:query '([?p :block/name]
|
||||||
|
[?p :block/properties ?prop]
|
||||||
|
[(get ?prop :parent) ?v]
|
||||||
|
(or [(= ?v "child page 1")] [(contains? ?v "child page 1")])
|
||||||
|
(or [(= ?v "child page 2")] [(contains? ?v "child page 2")]))
|
||||||
|
:count 1}
|
||||||
|
|
||||||
|
"(or (page-property parent [[child page 1]]) (page-property parent [[child page 2]]))"
|
||||||
|
{:query '(or (and
|
||||||
|
[?p :block/name]
|
||||||
|
[?p :block/properties ?prop]
|
||||||
|
[(get ?prop :parent) ?v]
|
||||||
|
(or [(= ?v "child page 1")] [(contains? ?v "child page 1")]))
|
||||||
|
(and
|
||||||
|
[?p :block/name]
|
||||||
|
[?p :block/properties ?prop]
|
||||||
|
[(get ?prop :parent) ?v]
|
||||||
|
(or [(= ?v "child page 2")] [(contains? ?v "child page 2")])))
|
||||||
|
:count 3}))
|
||||||
|
|
||||||
(deftest test-parse
|
(deftest test-parse
|
||||||
[]
|
[]
|
||||||
(testing "nil or blank strings should be ignored"
|
(testing "nil or blank strings should be ignored"
|
||||||
|
@ -262,50 +305,7 @@ last-modified-at:: 1609084800002"}]]
|
||||||
[(contains? #{"page-tag-1" "page-tag-2"} ?tag1)]]
|
[(contains? #{"page-tag-1" "page-tag-2"} ?tag1)]]
|
||||||
:count 2}))
|
:count 2}))
|
||||||
|
|
||||||
(testing "page-property queries"
|
|
||||||
(are [x y] (= (q-count x) y)
|
|
||||||
"(page-property parent)"
|
|
||||||
{:query '[[?p :block/name]
|
|
||||||
[?p :block/properties ?prop]
|
|
||||||
[(get ?prop :parent) ?prop-v]
|
|
||||||
[true]], :count 3}
|
|
||||||
|
|
||||||
"(page-property parent [[child page 1]])"
|
|
||||||
{:query '[[?p :block/name]
|
|
||||||
[?p :block/properties ?prop]
|
|
||||||
[(get ?prop :parent) ?v]
|
|
||||||
(or [(= ?v "child page 1")] [(contains? ?v "child page 1")])]
|
|
||||||
:count 2}
|
|
||||||
|
|
||||||
"(page-property parent \"child page 1\")"
|
|
||||||
{:query '[[?p :block/name]
|
|
||||||
[?p :block/properties ?prop]
|
|
||||||
[(get ?prop :parent) ?v]
|
|
||||||
(or
|
|
||||||
[(= ?v "child page 1")]
|
|
||||||
[(contains? ?v "child page 1")])]
|
|
||||||
:count 2}
|
|
||||||
|
|
||||||
"(and (page-property parent [[child page 1]]) (page-property parent [[child page 2]]))"
|
|
||||||
{:query '([?p :block/name]
|
|
||||||
[?p :block/properties ?prop]
|
|
||||||
[(get ?prop :parent) ?v]
|
|
||||||
(or [(= ?v "child page 1")] [(contains? ?v "child page 1")])
|
|
||||||
(or [(= ?v "child page 2")] [(contains? ?v "child page 2")]))
|
|
||||||
:count 1}
|
|
||||||
|
|
||||||
"(or (page-property parent [[child page 1]]) (page-property parent [[child page 2]]))"
|
|
||||||
{:query '(or (and
|
|
||||||
[?p :block/name]
|
|
||||||
[?p :block/properties ?prop]
|
|
||||||
[(get ?prop :parent) ?v]
|
|
||||||
(or [(= ?v "child page 1")] [(contains? ?v "child page 1")]))
|
|
||||||
(and
|
|
||||||
[?p :block/name]
|
|
||||||
[?p :block/properties ?prop]
|
|
||||||
[(get ?prop :parent) ?v]
|
|
||||||
(or [(= ?v "child page 2")] [(contains? ?v "child page 2")])))
|
|
||||||
:count 3}))
|
|
||||||
|
|
||||||
;; boolean queries
|
;; boolean queries
|
||||||
(testing "AND queries"
|
(testing "AND queries"
|
||||||
|
@ -334,7 +334,7 @@ last-modified-at:: 1609084800002"}]]
|
||||||
"(not [[page 1]])"
|
"(not [[page 1]])"
|
||||||
{:query '([?b :block/uuid]
|
{:query '([?b :block/uuid]
|
||||||
(not [?b :block/path-refs [:block/name "page 1"]]))
|
(not [?b :block/path-refs [:block/name "page 1"]]))
|
||||||
:count 37}))
|
:count 39}))
|
||||||
|
|
||||||
(testing "Between query"
|
(testing "Between query"
|
||||||
(are [x y] (= (count-only x) y)
|
(are [x y] (= (count-only x) y)
|
||||||
|
@ -382,7 +382,7 @@ last-modified-at:: 1609084800002"}]]
|
||||||
(and [?b :block/path-refs [:block/name "page 1"]])
|
(and [?b :block/path-refs [:block/name "page 1"]])
|
||||||
(and [?b :block/path-refs [:block/name "page 2"]])
|
(and [?b :block/path-refs [:block/name "page 2"]])
|
||||||
[?b])))
|
[?b])))
|
||||||
:count 40})
|
:count 42})
|
||||||
|
|
||||||
;; FIXME: not working
|
;; FIXME: not working
|
||||||
;; (are [x y] (= (q-count x) y)
|
;; (are [x y] (= (q-count x) y)
|
||||||
|
|
Loading…
Reference in New Issue