mirror of
https://github.com/logseq/logseq.git
synced 2026-04-24 22:25:01 +00:00
add tests
This commit is contained in:
29
deps/db/src/logseq/db.cljs
vendored
29
deps/db/src/logseq/db.cljs
vendored
@@ -743,18 +743,19 @@
|
||||
(reduce (fn [acc [class-ent entity]]
|
||||
(add-entity acc class-ent entity))
|
||||
{})
|
||||
(map (fn [[class-id entities]]
|
||||
(let [class (d/entity db class-id)
|
||||
custom-title (when-let [custom (:logseq.property.class/title-plural class)]
|
||||
(if (string? custom)
|
||||
custom
|
||||
(db-property/property-value-content custom)))
|
||||
title (if (string/blank? custom-title)
|
||||
(pluralize-class-title (:block/title class))
|
||||
custom-title)]
|
||||
{:title title
|
||||
:class (-> (into {} class)
|
||||
(assoc :db/id (:db/id class)))
|
||||
:entities (->> entities
|
||||
(sort-by :block/created-at))})))
|
||||
(keep (fn [[class-id entities]]
|
||||
(let [class (d/entity db class-id)]
|
||||
(when (true? (:logseq.property.class/enable-bidirectional? class))
|
||||
(let [custom-title (when-let [custom (:logseq.property.class/title-plural class)]
|
||||
(if (string? custom)
|
||||
custom
|
||||
(db-property/property-value-content custom)))
|
||||
title (if (string/blank? custom-title)
|
||||
(pluralize-class-title (:block/title class))
|
||||
custom-title)]
|
||||
{:title title
|
||||
:class (-> (into {} class)
|
||||
(assoc :db/id (:db/id class)))
|
||||
:entities (->> entities
|
||||
(sort-by :block/created-at))})))))
|
||||
(sort-by (comp :block/created-at :class))))))
|
||||
|
||||
4
deps/db/src/logseq/db/frontend/property.cljs
vendored
4
deps/db/src/logseq/db/frontend/property.cljs
vendored
@@ -186,6 +186,10 @@
|
||||
:schema {:type :string
|
||||
:public? true
|
||||
:view-context :class}}
|
||||
:logseq.property.class/enable-bidirectional? {:title "Enable bi-directional properties"
|
||||
:schema {:type :checkbox
|
||||
:public? true
|
||||
:view-context :class}}
|
||||
:logseq.property/hide-empty-value {:title "Hide empty value"
|
||||
:schema {:type :checkbox
|
||||
:public? true
|
||||
|
||||
41
deps/db/test/logseq/db_test.cljs
vendored
41
deps/db/test/logseq/db_test.cljs
vendored
@@ -108,4 +108,43 @@
|
||||
(fn [temp-conn]
|
||||
(ldb/transact! temp-conn [{:db/ident :logseq.class/Task
|
||||
:block/tags :logseq.class/Property}])
|
||||
(ldb/transact! temp-conn [[:db/retract :logseq.class/Task :block/tags :logseq.class/Property]]))))))
|
||||
(ldb/transact! temp-conn [[:db/retract :logseq.class/Task :block/tags :logseq.class/Property]]))))))
|
||||
|
||||
(deftest get-bidirectional-properties
|
||||
(testing "disabled by default"
|
||||
(let [conn (db-test/create-conn-with-blocks
|
||||
{:properties {:friend {:logseq.property/type :node
|
||||
:build/property-classes [:Person]}}
|
||||
:classes {:Person {}
|
||||
:Project {}}
|
||||
:pages-and-blocks
|
||||
[{:page {:block/title "Alice"
|
||||
:build/tags [:Person]
|
||||
:build/properties {:friend [:build/page {:block/title "Bob"}]}}}
|
||||
{:page {:block/title "Bob"}}
|
||||
{:page {:block/title "Charlie"
|
||||
:build/tags [:Project]
|
||||
:build/properties {:friend [:build/page {:block/title "Bob"}]}}}]})
|
||||
target (db-test/find-page-by-title @conn "Bob")]
|
||||
(is (empty? (ldb/get-bidirectional-properties @conn (:db/id target))))))
|
||||
|
||||
(testing "enabled per class"
|
||||
(let [conn (db-test/create-conn-with-blocks
|
||||
{:properties {:friend {:logseq.property/type :node
|
||||
:build/property-classes [:Person]}}
|
||||
:classes {:Person {:build/properties {:logseq.property.class/enable-bidirectional? true}}
|
||||
:Project {}}
|
||||
:pages-and-blocks
|
||||
[{:page {:block/title "Alice"
|
||||
:build/tags [:Person]
|
||||
:build/properties {:friend [:build/page {:block/title "Bob"}]}}}
|
||||
{:page {:block/title "Bob"}}
|
||||
{:page {:block/title "Charlie"
|
||||
:build/tags [:Project]
|
||||
:build/properties {:friend [:build/page {:block/title "Bob"}]}}}]})
|
||||
target (db-test/find-page-by-title @conn "Bob")
|
||||
results (ldb/get-bidirectional-properties @conn (:db/id target))]
|
||||
(is (= 1 (count results)))
|
||||
(is (= "Persons" (:title (first results))))
|
||||
(is (= ["Alice"]
|
||||
(map :block/title (:entities (first results))))))))
|
||||
|
||||
Reference in New Issue
Block a user