chore: move and add create-property-text-block tests

experiment/tanstack-table
Gabriel Horner 2024-06-19 23:01:44 -04:00
parent c23513eaa8
commit eae2156f6c
3 changed files with 55 additions and 43 deletions

View File

@ -522,13 +522,6 @@
{:db/id property-id})]] {:db/id property-id})]]
(ldb/transact! conn tx-data))))) (ldb/transact! conn tx-data)))))
(defn get-property-block-created-block
"Get the root block and property that created this property block."
[db eid]
(let [block (d/entity db eid)
created-from-property (:logseq.property/created-from-property block)]
{:from-property-id (:db/id created-from-property)}))
(defn class-add-property! (defn class-add-property!
[conn class-id property-id] [conn class-id property-id]
(when-let [class (d/entity @conn class-id)] (when-let [class (d/entity @conn class-id)]

View File

@ -4,7 +4,8 @@
[datascript.core :as d] [datascript.core :as d]
[logseq.db.sqlite.create-graph :as sqlite-create-graph] [logseq.db.sqlite.create-graph :as sqlite-create-graph]
[logseq.db.sqlite.build :as sqlite-build] [logseq.db.sqlite.build :as sqlite-build]
[logseq.outliner.property :as outliner-property])) [logseq.outliner.property :as outliner-property]
[logseq.db.frontend.property :as db-property]))
(defn- find-block-by-content [conn content] (defn- find-block-by-content [conn content]
(->> content (->> content
@ -14,15 +15,53 @@
@conn) @conn)
first)) first))
(defn- create-conn-with-blocks [opts]
(let [conn (d/create-conn db-schema/schema-for-db-based-graph)
_ (d/transact! conn (sqlite-create-graph/build-db-initial-data "{}"))
_ (sqlite-build/create-blocks conn opts)]
conn))
(deftest create-property-text-block!
(testing "Create a new :default property value"
(let [conn (create-conn-with-blocks
[{:page {:block/original-name "page1"}
:blocks [{:block/content "b1" :build/properties {:default "foo"}}
{:block/content "b2"}]}])
block (find-block-by-content conn "b2")
;; Use same args as outliner.op
_ (outliner-property/create-property-text-block! conn (:db/id block) :user.property/default "" {})
new-property-value (:user.property/default (find-block-by-content conn "b2"))]
(is (some? (:db/id new-property-value)) "New property value created")
(is (= "" (db-property/ref->property-value-content @conn new-property-value))
"Property value has correct content")
(is (= :user.property/default
(get-in (d/entity @conn (:db/id new-property-value)) [:logseq.property/created-from-property :db/ident]))
"Has correct created-from-property")))
(testing "Create a new :number property value"
(let [conn (create-conn-with-blocks
[{:page {:block/original-name "page1"}
:blocks [{:block/content "b1" :build/properties {:num 2}}
{:block/content "b2"}]}])
block (find-block-by-content conn "b2")
;; Use same args as outliner.op
_ (outliner-property/create-property-text-block! conn (:db/id block) :user.property/num "3" {})
new-property-value (:user.property/num (find-block-by-content conn "b2"))]
(is (some? (:db/id new-property-value)) "New property value created")
(is (= 3 (db-property/ref->property-value-content @conn new-property-value))
"Property value has correct content")
(is (= :user.property/num
(get-in (d/entity @conn (:db/id new-property-value)) [:logseq.property/created-from-property :db/ident]))
"Has correct created-from-property"))))
(deftest set-block-property-with-ref-values (deftest set-block-property-with-ref-values
(testing "Select a :number value from existing values" (testing "Select a :number value from existing values"
(let [conn (d/create-conn db-schema/schema-for-db-based-graph) (let [conn (create-conn-with-blocks
_ (d/transact! conn (sqlite-create-graph/build-db-initial-data "{}")) [{:page {:block/original-name "page1"}
_ (sqlite-build/create-blocks :blocks [{:block/content "b1" :build/properties {:num 2}}
conn {:block/content "b2"}]}])
[{:page {:block/original-name "page1"}
:blocks [{:block/content "b1" :build/properties {:num 2}}
{:block/content "b2"}]}])
property-value (:user.property/num (find-block-by-content conn "b1")) property-value (:user.property/num (find-block-by-content conn "b1"))
_ (assert (:db/id property-value)) _ (assert (:db/id property-value))
block-uuid (:block/uuid (find-block-by-content conn "b2")) block-uuid (:block/uuid (find-block-by-content conn "b2"))
@ -33,13 +72,10 @@
(deftest set-block-property-with-raw-values (deftest set-block-property-with-raw-values
(testing "Setting :default with same property value reuses existing entity" (testing "Setting :default with same property value reuses existing entity"
(let [conn (d/create-conn db-schema/schema-for-db-based-graph) (let [conn (create-conn-with-blocks
_ (d/transact! conn (sqlite-create-graph/build-db-initial-data "{}")) [{:page {:block/original-name "page1"}
_ (sqlite-build/create-blocks :blocks [{:block/content "b1" :build/properties {:logseq.property/order-list-type "number"}}
conn {:block/content "b2"}]}])
[{:page {:block/original-name "page1"}
:blocks [{:block/content "b1" :build/properties {:logseq.property/order-list-type "number"}}
{:block/content "b2"}]}])
property-value (:logseq.property/order-list-type (find-block-by-content conn "b1")) property-value (:logseq.property/order-list-type (find-block-by-content conn "b1"))
block-uuid (:block/uuid (find-block-by-content conn "b2")) block-uuid (:block/uuid (find-block-by-content conn "b2"))
;; Use same args as outliner.op ;; Use same args as outliner.op
@ -50,13 +86,10 @@
(:db/id (:logseq.property/order-list-type (find-block-by-content conn "b2"))))))) (:db/id (:logseq.property/order-list-type (find-block-by-content conn "b2")))))))
(testing "Setting :checkbox with same property value reuses existing entity" (testing "Setting :checkbox with same property value reuses existing entity"
(let [conn (d/create-conn db-schema/schema-for-db-based-graph) (let [conn (create-conn-with-blocks
_ (d/transact! conn (sqlite-create-graph/build-db-initial-data "{}")) [{:page {:block/original-name "page1"}
_ (sqlite-build/create-blocks :blocks [{:block/content "b1" :build/properties {:checkbox true}}
conn {:block/content "b2"}]}])
[{:page {:block/original-name "page1"}
:blocks [{:block/content "b1" :build/properties {:checkbox true}}
{:block/content "b2"}]}])
property-value (:user.property/checkbox (find-block-by-content conn "b1")) property-value (:user.property/checkbox (find-block-by-content conn "b1"))
block-uuid (:block/uuid (find-block-by-content conn "b2")) block-uuid (:block/uuid (find-block-by-content conn "b2"))
;; Use same args as outliner.op ;; Use same args as outliner.op

View File

@ -333,20 +333,6 @@
(is (nil? (db/entity [:block/uuid block-id]))) (is (nil? (db/entity [:block/uuid block-id])))
(is (= 2 (count (:property/closed-values (db/entity k))))))))))))) (is (= 2 (count (:property/closed-values (db/entity k)))))))))))))
;; property-create-new-block
;; get-property-block-created-block
(deftest text-block-test
(testing "Add property and create a block value"
(let [fb (db/entity [:block/uuid fbid])
k :user.property/property-1
conn (db/get-db false)]
;; add property
(outliner-property/upsert-property! conn k {:type :default} {})
(let [property (db/entity k)
block-id (outliner-property/create-property-text-block! conn (:db/id fb) (:db/id property) "Block content" {})
{:keys [from-property-id]} (outliner-property/get-property-block-created-block @conn [:block/uuid block-id])]
(is (= from-property-id (:db/id property)))))))
;; collapse-expand-property! ;; collapse-expand-property!
(deftest collapse-expand-property-test (deftest collapse-expand-property-test
(testing "Collapse and expand property" (testing "Collapse and expand property"