Add tests for namespaces in Library

This commit is contained in:
Tienson Qin
2025-05-16 16:27:28 +08:00
parent b9fedf2eb9
commit 0822e455f8
3 changed files with 16 additions and 8 deletions

View File

@@ -122,7 +122,8 @@
(and
parent-title
(= (util/page-name-sanity-lc parent-title)
(util/page-name-sanity-lc (nth (reverse (string/split input "/")) 1)))))))
(some-> (util/nth-safe (reverse (string/split input "/")) 1)
util/page-name-sanity-lc))))))
(some (fn [block]
(and
(:block/tags block)

View File

@@ -86,23 +86,24 @@
:block/title ""})]))
(defn- get-page-by-parent-name
[db parent-title child-title]
[db parent-title child-title class?]
(some->>
(d/q
'[:find [?b ...]
:in $ ?parent-name ?child-name
:in $ ?attribute ?parent-name ?child-name
:where
[?b :logseq.property.class/extends ?p]
[?b ?attribute ?p]
[?b :block/name ?child-name]
[?p :block/name ?parent-name]]
db
(if class? :logseq.property.class/extends :block/parent)
(common-util/page-name-sanity-lc parent-title)
(common-util/page-name-sanity-lc child-title))
first
(d/entity db)))
(defn- split-namespace-pages
[db page date-formatter]
[db page date-formatter create-class?]
(let [{:block/keys [title] block-uuid :block/uuid} page]
(->>
(if (and (or (entity-util/class? page)
@@ -117,7 +118,7 @@
(let [last-part? (= idx (dec (count parts)))
page (if (zero? idx)
(ldb/get-page db part)
(get-page-by-parent-name db (nth parts (dec idx)) part))
(get-page-by-parent-name db (nth parts (dec idx)) part create-class?))
result (or page
(gp-block/page-name->map part db true date-formatter
{:page-uuid (when last-part? block-uuid)
@@ -205,7 +206,7 @@
:page-uuid (when (uuid? uuid) uuid)
:skip-existing-page-check? true})
[page parents] (if (and (text/namespace-page? title) split-namespace?)
(let [pages (split-namespace-pages db page date-formatter)]
(let [pages (split-namespace-pages db page date-formatter class?)]
[(last pages) (butlast pages)])
[page nil])]
(when (and page (or (nil? (:db/ident page))

View File

@@ -30,7 +30,13 @@
child-page2 (d/entity @conn [:block/uuid child-uuid2])
;; Create a child page for a class
[_ child-uuid3] (worker-db-page/create! conn "c1/c2" {:split-namespace? true :class? true})
child-page3 (d/entity @conn [:block/uuid child-uuid3])]
child-page3 (d/entity @conn [:block/uuid child-uuid3])
library (ldb/get-built-in-page @conn "Library")
bar (ldb/get-page @conn "bar")]
(is (= ["foo"] (map :block/title (:block/_parent library)))
"Namespace (non-class) pages are added to the Library page")
(is (= ["baz" "baz2"] (map :block/title (:block/_parent bar)))
"Child pages are created under the same parent")
(is (= ["foo" "bar"] (map :block/title [(:block/parent (:block/parent child-page))
(:block/parent child-page)]))
"Child page with new parent has correct parents")