enhance(dev): add test helper for ent properties

Was using them across namespaces and they were needlessly different
This commit is contained in:
Gabriel Horner
2025-01-28 16:00:38 -05:00
parent 5983bd0473
commit 8550b48f17
3 changed files with 56 additions and 70 deletions

View File

@@ -11,7 +11,6 @@
[logseq.db :as ldb]
[logseq.db.frontend.content :as db-content]
[logseq.db.frontend.malli-schema :as db-malli-schema]
[logseq.db.frontend.property :as db-property]
[logseq.db.frontend.rules :as rules]
[logseq.db.frontend.validate :as db-validate]
[logseq.db.test.helper :as db-test]
@@ -21,7 +20,6 @@
[logseq.graph-parser.test.helper :as test-helper :include-macros true :refer [deftest-async]]
[logseq.outliner.db-pipeline :as db-pipeline]
[promesa.core :as p]
[datascript.impl.entity :as de]
[logseq.db.frontend.entity-plus :as entity-plus]))
;; Helpers
@@ -127,22 +125,6 @@
(p/finally (fn [_]
(reset! gp-block/*export-to-db-graph? false)))))
(defn- readable-properties
[ent]
(->> (db-property/properties ent)
(mapv (fn [[k v]]
[k
(cond
(= :block/tags k)
(mapv :db/ident v)
(and (set? v) (every? de/entity? v))
(set (map db-property/property-value-content v))
(de/entity? v)
(db-property/property-value-content v)
:else
v)]))
(into {})))
;; Tests
;; =====
@@ -255,8 +237,7 @@
(is (= {:user.property/prop-bool true
:user.property/prop-num 5
:user.property/prop-string "woot"}
(update-vals (db-property/properties (db-test/find-block-by-content @conn "b1"))
(fn [v] (if (de/entity? v) (db-property/property-value-content v) v))))
(db-test/readable-properties (db-test/find-block-by-content @conn "b1")))
"Basic block has correct properties")
(is (= #{"prop-num" "prop-string" "prop-bool"}
(->> (db-test/find-block-by-content @conn "b1")
@@ -267,17 +248,17 @@
(is (= {:user.property/prop-num2 10
:block/tags [:logseq.class/Page]}
(readable-properties (db-test/find-page-by-title @conn "new page")))
(db-test/readable-properties (db-test/find-page-by-title @conn "new page")))
"New page has correct properties")
(is (= {:user.property/prop-bool true
:user.property/prop-num 5
:user.property/prop-string "yeehaw"
:block/tags [:logseq.class/Page :user.class/Some---Namespace]}
(readable-properties (db-test/find-page-by-title @conn "some page")))
(db-test/readable-properties (db-test/find-page-by-title @conn "some page")))
"Existing page has correct properties")
(is (= {:user.property/rating 5.5}
(readable-properties (db-test/find-block-by-content @conn ":rating float")))
(db-test/readable-properties (db-test/find-block-by-content @conn ":rating float")))
"Block with float property imports as a float")
(is (= []
@@ -295,17 +276,17 @@
(let [b (db-test/find-block-by-content @conn #"MEETING TITLE")]
(is (= {}
(and b (readable-properties b)))
(and b (db-test/readable-properties b)))
":template properties are ignored to not invalidate its property types"))
(is (= 20221126
(-> (readable-properties (db-test/find-block-by-content @conn "only deadline"))
(-> (db-test/readable-properties (db-test/find-block-by-content @conn "only deadline"))
:logseq.task/deadline
date-time-util/ms->journal-day))
"deadline block has correct journal as property value")
(is (= 20221125
(-> (readable-properties (db-test/find-block-by-content @conn "only scheduled"))
(-> (db-test/readable-properties (db-test/find-block-by-content @conn "only scheduled"))
:logseq.task/scheduled
date-time-util/ms->journal-day))
"scheduled block converted to correct deadline")
@@ -316,24 +297,26 @@
@conn "Apr 1st, 2024")))
"Only one journal page exists when deadline is on same day as journal")
(is (= {:logseq.task/priority "High"}
(readable-properties (db-test/find-block-by-content @conn "high priority")))
(is (= {:logseq.task/priority :logseq.task/priority.high}
(db-test/readable-properties (db-test/find-block-by-content @conn "high priority")))
"priority block has correct property")
(is (= {:logseq.task/status "Doing" :logseq.task/priority "Medium" :block/tags [:logseq.class/Task]}
(readable-properties (db-test/find-block-by-content @conn "status test")))
(is (= {:logseq.task/status :logseq.task/status.doing
:logseq.task/priority :logseq.task/priority.medium
:block/tags [:logseq.class/Task]}
(db-test/readable-properties (db-test/find-block-by-content @conn "status test")))
"status block has correct task properties and class")
(is (= #{:logseq.task/status :block/tags}
(set (keys (readable-properties (db-test/find-block-by-content @conn "old todo block")))))
(set (keys (db-test/readable-properties (db-test/find-block-by-content @conn "old todo block")))))
"old task properties like 'todo' are ignored")
(is (= {:logseq.property/order-list-type "number"}
(readable-properties (db-test/find-block-by-content @conn "list one")))
(db-test/readable-properties (db-test/find-block-by-content @conn "list one")))
"numered block has correct property")
(is (= #{"gpt"}
(:block/alias (readable-properties (db-test/find-page-by-title @conn "chat-gpt"))))
(:block/alias (db-test/readable-properties (db-test/find-page-by-title @conn "chat-gpt"))))
"alias set correctly")
(is (= ["y"]
(->> (d/q '[:find [?b ...] :where [?b :block/title "y"] [?b :logseq.property/parent]]
@@ -346,27 +329,27 @@
(is (= {:logseq.property.linked-references/includes #{"Oct 9th, 2024"}
:logseq.property.linked-references/excludes #{"ref2"}}
(select-keys (readable-properties (db-test/find-page-by-title @conn "chat-gpt"))
(select-keys (db-test/readable-properties (db-test/find-page-by-title @conn "chat-gpt"))
[:logseq.property.linked-references/excludes :logseq.property.linked-references/includes]))
"linked ref filters set correctly"))
(testing "built-in classes and their properties"
;; Queries
(is (= {:logseq.property.table/sorting [{:id :user.property/prop-num, :asc? false}]
:logseq.property.view/type "Table View"
:logseq.property.view/type :logseq.property.view/type.table
:logseq.property.table/ordered-columns [:block/title :user.property/prop-string :user.property/prop-num]
:logseq.property/query "(property :prop-string)"
:block/tags [:logseq.class/Query]}
(readable-properties (find-block-by-property-value @conn :logseq.property/query "(property :prop-string)")))
(db-test/readable-properties (find-block-by-property-value @conn :logseq.property/query "(property :prop-string)")))
"simple query block has correct query properties")
(is (= "For example, here's a query with title text:"
(:block/title (db-test/find-block-by-content @conn #"query with title text")))
"Text around a simple query block is set as a query's title")
(is (= {:logseq.property.view/type "List View"
(is (= {:logseq.property.view/type :logseq.property.view/type.list
:logseq.property/query "{:query (task todo doing)}"
:block/tags [:logseq.class/Query]
:logseq.property.table/ordered-columns [:block/title]}
(readable-properties (db-test/find-block-by-content @conn #"tasks with")))
(db-test/readable-properties (db-test/find-block-by-content @conn #"tasks with")))
"Advanced query has correct query properties")
(is (= "tasks with todo and doing"
(:block/title (db-test/find-block-by-content @conn #"tasks with")))
@@ -374,7 +357,7 @@
;; Cards
(is (= {:block/tags [:logseq.class/Card]}
(readable-properties (db-test/find-block-by-content @conn "card 1")))
(db-test/readable-properties (db-test/find-block-by-content @conn "card 1")))
"None of the card properties are imported since they are deprecated"))
(testing "tags convert to classes"
@@ -440,7 +423,7 @@
(:logseq.property/type (d/entity @conn :user.property/description)))
":default property to :node (or any non :default value) remains :default")
(is (= "[[Jakob]]"
(:user.property/description (readable-properties (db-test/find-block-by-content @conn #":default to :node"))))
(:user.property/description (db-test/readable-properties (db-test/find-block-by-content @conn #":default to :node"))))
":default to :node property saves :default property value default with full text")
(testing "with changes to upstream/existing property value"
@@ -448,17 +431,17 @@
(:logseq.property/type (d/entity @conn :user.property/duration)))
":number property to :default value changes to :default")
(is (= "20"
(:user.property/duration (readable-properties (db-test/find-block-by-content @conn "existing :number to :default"))))
(:user.property/duration (db-test/readable-properties (db-test/find-block-by-content @conn "existing :number to :default"))))
"existing :number property value correctly saved as :default")
(is (= {:logseq.property/type :default :db/cardinality :db.cardinality/many}
(select-keys (d/entity @conn :user.property/people) [:logseq.property/type :db/cardinality]))
":node property to :default value changes to :default and keeps existing cardinality")
(is (= #{"[[Jakob]] [[Gabriel]]"}
(:user.property/people (readable-properties (db-test/find-block-by-content @conn ":node people"))))
(:user.property/people (db-test/readable-properties (db-test/find-block-by-content @conn ":node people"))))
"existing :node property value correctly saved as :default with full text")
(is (= #{"[[Gabriel]] [[Jakob]]"}
(:user.property/people (readable-properties (db-test/find-block-by-content @conn #"pending block for :node"))))
(:user.property/people (db-test/readable-properties (db-test/find-block-by-content @conn #"pending block for :node"))))
"pending :node property value correctly saved as :default with full text")
(is (some? (db-test/find-page-by-title @conn "Jakob"))
"Previous :node property value still exists")
@@ -511,7 +494,7 @@
(testing "whiteboards"
(let [block-with-props (db-test/find-block-by-content @conn #"block with props")]
(is (= {:user.property/prop-num 10}
(readable-properties block-with-props)))
(db-test/readable-properties block-with-props)))
(is (= "block with props" (:block/title block-with-props)))))))
(deftest-async export-basic-graph-with-convert-all-tags-option-disabled
@@ -564,10 +547,10 @@
"tag page is not a class")
(is (= #{"Movie"}
(:logseq.property/page-tags (readable-properties tagged-page)))
(:logseq.property/page-tags (db-test/readable-properties tagged-page)))
"tagged page has existing page imported as a tag to page-tags")
(is (= #{"LargeLanguageModel" "fun" "ai"}
(:logseq.property/page-tags (readable-properties (db-test/find-page-by-title @conn "chat-gpt"))))
(:logseq.property/page-tags (db-test/readable-properties (db-test/find-page-by-title @conn "chat-gpt"))))
"tagged page has new page and other pages marked with '#' and '[[]]` imported as tags to page-tags")))))
(deftest-async export-files-with-tag-classes-option
@@ -584,7 +567,7 @@
(is (= (:block/title block) "Inception")
"tagged block with configured tag strips tag from content")
(is (= [:user.class/Movie]
(:block/tags (readable-properties block)))
(:block/tags (db-test/readable-properties block)))
"tagged block has configured tag imported as a class")
(is (= [:logseq.class/Tag] (mapv :db/ident (:block/tags tag-page)))
@@ -593,7 +576,7 @@
"unconfigured tag page is not a class")
(is (= {:block/tags [:logseq.class/Page :user.class/Movie]}
(readable-properties (db-test/find-page-by-title @conn "Interstellar")))
(db-test/readable-properties (db-test/find-page-by-title @conn "Interstellar")))
"tagged page has configured tag imported as a class"))))
(deftest-async export-files-with-property-classes-option
@@ -627,7 +610,7 @@
(is (= (:block/title block) "The Creator")
"tagged block with configured tag strips tag from content")
(is (= [:user.class/Movie]
(:block/tags (readable-properties block)))
(:block/tags (db-test/readable-properties block)))
"tagged block has configured tag imported as a class")
(is (= (:user.property/testtagclass block) (:block/tags block))
"tagged block can have another property that references the same class it is tagged with,
@@ -639,7 +622,7 @@
"No page exists for configured property")
(is (= #{:user.class/Property :logseq.class/Property}
(set (:block/tags (readable-properties (db-test/find-page-by-title @conn "url")))))
(set (:block/tags (db-test/readable-properties (db-test/find-page-by-title @conn "url")))))
"tagged page has correct tags including one from option"))))
(deftest-async export-files-with-remove-inline-tags