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

View File

@@ -355,7 +355,9 @@
container-id (::container-id state)
config {:id (str "bidirectional-" container-id)
:container-id container-id
:editor-box (state/get-component :editor/box)}]
:editor-box (state/get-component :editor/box)
:default-collapsed? true
:ref? true}]
(if (and blocks-container (seq entities))
[:div.property-block-container.content.w-full
(blocks-container config entities)]
@@ -653,7 +655,11 @@
(and show?
(or (= mode :global)
(and (set? ids) (contains? ids (:block/uuid block))))))
properties (:block/properties block)
properties (cond-> (:block/properties block)
(and (ldb/class? block)
(not (ldb/built-in? block)))
(assoc :logseq.property.class/enable-bidirectional?
(:logseq.property.class/enable-bidirectional? block)))
remove-built-in-or-other-position-properties
(fn [properties show-in-hidden-properties?]
(remove (fn [property]

View File

@@ -3452,6 +3452,7 @@
(or (:block/_parent block) (:block.temp/has-children? block))
(integer? (:block-level config))
(>= (:block-level config) (state/get-ref-open-blocks-level)))
(:default-collapsed? config)
(and (or (:view? config) (:popup? config))
(or (ldb/page? block)
(:table-block-title? config))))))

View File

@@ -186,7 +186,7 @@
["65.17" {:properties [:logseq.property.publish/published-url]}]
["65.18" {:fix deprecated-ensure-graph-uuid}]
["65.19" {:properties [:logseq.property/choice-classes :logseq.property/choice-exclusions]}]
["65.20" {:properties [:logseq.property.class/title-plural]}]])
["65.20" {:properties [:logseq.property.class/title-plural :logseq.property.class/enable-bidirectional?]}]])
(let [[major minor] (last (sort (map (comp (juxt :major :minor) db-schema/parse-schema-version first)
schema-version->updates)))]