add tests

This commit is contained in:
Tienson Qin
2026-01-05 13:03:15 +08:00
parent e5521da233
commit bcb9f84469
6 changed files with 69 additions and 18 deletions

View File

@@ -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))))))

View File

@@ -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

View File

@@ -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))))))))