Răsfoiți Sursa

fix: property tests

Tienson Qin 1 an în urmă
părinte
comite
1ea6b6fff3

+ 4 - 3
scripts/src/logseq/tasks/db_graph/create_graph.cljs

@@ -169,12 +169,13 @@
                              (into {}))
         new-properties-tx (vec
                            (mapcat
-                            (fn [[prop-name uuid]]
+                            (fn [[prop-name]]
                               (if (get-in properties [prop-name :closed-values])
                                 (db-property-util/build-closed-values
                                  prop-name
-                                 (assoc (get properties prop-name) :block/uuid uuid)
-                                 {:icon-id
+                                 (get properties prop-name)
+                                 {:db-ident (keyword "user.property" (name prop-name))
+                                  :icon-id
                                   (get-in options [:property-uuids :icon])
                                   :translate-closed-page-value-fn
                                   #(hash-map :block/uuid (translate-property-value (:value %) uuid-maps))

+ 44 - 49
src/main/frontend/handler/db_based/property.cljs

@@ -21,7 +21,8 @@
             [frontend.handler.property.util :as pu]
             [promesa.core :as p]
             [frontend.db.async :as db-async]
-            [logseq.db :as ldb]))
+            [logseq.db :as ldb]
+            [datascript.impl.entity :as de]))
 
 ;; schema -> type, cardinality, object's class
 ;;           min, max -> string length, number range, cardinality size limit
@@ -192,11 +193,13 @@
                      m))]
           (:db/id e'))))))
 
+(defn- ->eid
+  [id]
+  (if (uuid? id) [:block/uuid id] id))
+
 (defn set-block-property!
-  [repo block-eid property-id v {:keys [old-value] :as opts}]
-  (let [block-eid (if (uuid? block-eid)
-                    [:block/uuid block-eid]
-                    block-eid)
+  [repo block-eid property-id v {:keys [property-name] :as opts}]
+  (let [block-eid (->eid block-eid)
         property-id (if (string? property-id)
                       (db-property/get-db-ident-from-name property-id)
                       property-id)
@@ -235,19 +238,10 @@
                 (if-let [msg (when-not (= v* :property/empty-placeholder) (validate-property-value schema v*))]
                   (let [msg' (str "\"" k-name "\"" " " (if (coll? msg) (first msg) msg))]
                     (notification/show! msg' :warning))
-                  (let [_ (upsert-property! repo property-id (assoc property-schema :type property-type) {})
+                  (let [_ (upsert-property! repo property-id (assoc property-schema :type property-type) {:property-name property-name})
                         status? (= :logseq.task/status (:db/ident property))
                         value (if (= value :property/empty-placeholder) [] value)
                         new-value (cond
-                                    (and multiple-values? old-value
-                                         (not= old-value :frontend.components.property/new-value-placeholder))
-                                    (if (coll? v*)
-                                      (vec (distinct (concat value v*)))
-                                      (let [v (mapv (fn [x] (if (= x old-value) v* x)) value)]
-                                        (if (contains? (set v) v*)
-                                          v
-                                          (conj v v*))))
-
                                     multiple-values?
                                     (let [f (if (coll? v*) concat conj)]
                                       (f value v*))
@@ -299,37 +293,36 @@
                            :block/schema schema}]
                     {:outliner-op :save-block}))))
 
-(defn- ->eid
-  [id]
-  (if (uuid? id) [:block/uuid id] id))
-
 (defn batch-set-property!
   "Notice that this works only for properties with cardinality equals to `one`."
   [repo block-ids property-id v]
   (assert property-id "property-id is nil")
   (let [block-eids (map ->eid block-ids)
-        property (db/entity property-id)
-        type (:type (:block/schema property))
-        infer-schema (when-not type (infer-schema-from-input-string v))
-        property-type (or type infer-schema :default)
-        {:keys [cardinality]} (:block/schema property)
-        status? (= :logseq.task/status (:db/ident property))
-        txs (mapcat
-             (fn [eid]
-               (when-let [block (db/entity eid)]
-                 (when (and (some? v) (not= cardinality :many))
-                   (when-let [v* (try
-                                   (convert-property-input-string property-type v)
-                                   (catch :default e
-                                     (notification/show! (str e) :error false)
-                                     nil))]
-                     [{:block/uuid (:block/uuid block)
-                       property-id v*}
-                      (when status?
-                        [:db/add (:db/id block) :block/tags :logseq.class/task])]))))
-             block-eids)]
-    (when (seq txs)
-      (db/transact! repo txs {:outliner-op :save-block}))))
+        property (db/entity property-id)]
+    (when property
+      (let [type (:type (:block/schema property))
+            infer-schema (when-not type (infer-schema-from-input-string v))
+            property-type (or type infer-schema :default)
+            {:keys [cardinality]} (:block/schema property)
+            status? (= :logseq.task/status (:db/ident property))
+            txs (->>
+                 (mapcat
+                  (fn [eid]
+                    (when-let [block (db/entity eid)]
+                      (when (and (some? v) (not= cardinality :many))
+                        (when-let [v* (try
+                                        (convert-property-input-string property-type v)
+                                        (catch :default e
+                                          (notification/show! (str e) :error false)
+                                          nil))]
+                          [{:block/uuid (:block/uuid block)
+                            property-id v*}
+                           (when status?
+                             [:db/add (:db/id block) :block/tags :logseq.class/task])]))))
+                  block-eids)
+                 (remove nil?))]
+        (when (seq txs)
+          (db/transact! repo txs {:outliner-op :save-block}))))))
 
 (defn batch-remove-property!
   [repo block-ids property-id]
@@ -362,12 +355,13 @@
 
 (defn remove-block-property!
   [repo eid property-id]
-  (if (contains? #{:block/alias :block/tags} property-id)
-    (when-let [block (db/entity eid)]
-      (db/transact! repo
-                    [[:db/retract (:db/id block) property-id]]
-                    {:outliner-op :save-block}))
-    (batch-remove-property! repo [eid] property-id)))
+  (let [eid (->eid eid)]
+    (if (contains? #{:block/alias :block/tags} property-id)
+     (when-let [block (db/entity eid)]
+       (db/transact! repo
+                     [[:db/retract (:db/id block) property-id]]
+                     {:outliner-op :save-block}))
+     (batch-remove-property! repo [eid] property-id))))
 
 (defn delete-property-value!
   "Delete value if a property has multiple values"
@@ -471,8 +465,8 @@
                     :block/parent page-id
                     :block/left (or (when page-entity (model/get-block-last-direct-child-id (db/get-db) (:db/id page-entity)))
                                     page-id)
-                    :logseq.property/created-from-block [:block/uuid (:block/uuid block)]
-                    :logseq.property/created-from-property (:db/id property)}
+                    :logseq.property/created-from-block block
+                    :logseq.property/created-from-property property}
                    sqlite-util/block-with-timestamps)
         child-1-id (db/new-block-id)
         child-1 (-> {:block/uuid child-1-id
@@ -488,6 +482,7 @@
 
 (defn create-property-text-block!
   [block property value parse-block {:keys [class-schema?]}]
+  (assert (de/entity? property))
   (let [repo (state/get-current-repo)
         {:keys [page blocks]} (property-create-new-block block property value parse-block)
         first-block (first blocks)

+ 24 - 41
src/test/frontend/handler/db_based/property_test.cljs

@@ -37,7 +37,7 @@
     (db-property-handler/set-block-property! repo fbid :user.property/property-1 "value" {})
     (let [block (db/entity [:block/uuid fbid])
           properties (:block/properties block)
-          property (db/entity [:block/name :user.property/property-1])]
+          property (db/entity :user.property/property-1)]
       ;; ensure property exists
       (are [x y] (= x y)
         (:block/schema property)
@@ -48,7 +48,7 @@
       (are [x y] (= x y)
         (count properties)
         1
-        (uuid? (ffirst properties))
+        (keyword? (ffirst properties))
         true
         (second (first properties))
         "value")))
@@ -57,7 +57,7 @@
     (db-property-handler/set-block-property! repo fbid :user.property/property-2 "1" {})
     (let [block (db/entity [:block/uuid fbid])
           properties (:block/properties block)
-          property (db/entity [:block/name :user.property/property-2])]
+          property (db/entity :user.property/property-2)]
       ;; ensure property exists
       (are [x y] (= x y)
         (:block/schema property)
@@ -68,7 +68,7 @@
       (are [x y] (= x y)
         (count properties)
         2
-        (every? uuid? (map first properties))
+        (every? keyword? (map first properties))
         true
         (second (second properties))
         1)))
@@ -96,13 +96,13 @@
         2)))
 
   (testing "Add a multi-values property"
-    (db-property-handler/upsert-property! repo "property-3" {:type :number :cardinality :many} {})
-    (db-property-handler/set-block-property! repo fbid "property-3" 1 {})
-    (db-property-handler/set-block-property! repo fbid "property-3" 2 {})
-    (db-property-handler/set-block-property! repo fbid "property-3" 3 {})
+    (db-property-handler/upsert-property! repo :user.property/property-3 {:type :number :cardinality :many} {})
+    (db-property-handler/set-block-property! repo fbid :user.property/property-3 1 {})
+    (db-property-handler/set-block-property! repo fbid :user.property/property-3 2 {})
+    (db-property-handler/set-block-property! repo fbid :user.property/property-3 3 {})
     (let [block (db/entity [:block/uuid fbid])
           properties (:block/properties block)
-          property (db/entity [:block/name "property-3"])]
+          property (db/entity :user.property/property-3)]
       ;; ensure property exists
       (are [x y] (= x y)
         (:block/schema property)
@@ -113,52 +113,35 @@
       (are [x y] (= x y)
         (count properties)
         3
-        (get properties (:block/uuid property))
-        #{1 2 3}))
-
-    ;; update property value from 1 to 4
-    (db-property-handler/set-block-property! repo fbid "property-3" 4 {:old-value 1})
-    (let [block (db/entity [:block/uuid fbid])
-          properties (:block/properties block)
-          property (db/entity [:block/name "property-3"])]
-      ;; check block's properties
-      (are [x y] (= x y)
-        (count properties)
-        3
-        (get properties (:block/uuid property))
-        #{4 2 3})
-
-      (db-property-handler/delete-property-value! repo block (:block/uuid property) "value 4")
-      (let [properties (:block/properties block)]
-        (is (get properties (:block/uuid property))
-            #{"value 2" "value 3"}))))
+        (get properties :user.property/property-3)
+        #{1 2 3})))
 
   (testing "Remove a property"
-    (db-property-handler/remove-block-property! repo fbid "property-3")
+    (db-property-handler/remove-block-property! repo fbid :user.property/property-3)
     (let [block (db/entity [:block/uuid fbid])
-          properties (:block/properties block)
-          property (db/entity [:block/name "property-3"])]
+          properties (:block/properties block)]
       ;; check block's properties
       (are [x y] (= x y)
         (count properties)
         2
-        (contains? (set (keys properties)) (:block/uuid property))
+        (contains? (set (keys properties)) :user.property/property-3)
         false)))
 
   (testing "Batch set properties"
-    (let [k "property-4"
+    (let [k :user.property/property-4
           v "batch value"]
+      (db-property-handler/upsert-property! repo :user.property/property-4 {:type :default} {})
       (db-property-handler/batch-set-property! repo [fbid sbid] k v)
       (let [fb (db/entity [:block/uuid fbid])
             sb (db/entity [:block/uuid sbid])]
         (are [x y] (= x y)
-          (get (:block/properties fb) (:block/uuid (db/entity [:block/name k])))
+          (get (:block/properties fb) k)
           v
-          (get (:block/properties sb) (:block/uuid (db/entity [:block/name k])))
+          (get (:block/properties sb) k)
           v))))
 
   (testing "Batch remove properties"
-    (let [k "property-4"]
+    (let [k :user.property/property-4]
       (db-property-handler/batch-remove-property! repo [fbid sbid] k)
       (let [fb (db/entity [:block/uuid fbid])
             sb (db/entity [:block/uuid sbid])]
@@ -198,7 +181,7 @@
       (is (= 2 (count (:class/schema.properties (db/entity (:db/id c1)))))))
 
     (testing "Class remove property"
-      (db-property-handler/class-remove-property! repo c1id (:block/uuid (db/entity [:block/name :user.property/property-1])))
+      (db-property-handler/class-remove-property! repo c1id :user.property/property-1)
       (is (= 1 (count (:class/schema.properties (db/entity (:db/id c1)))))))
     (testing "Add classes to a block"
       (test-helper/save-block! repo fbid "Block 1" {:tags ["class1" "class2" "class3"]})
@@ -214,8 +197,8 @@
       (let [c3 (db/entity [:block/name "class3"])]
         (db/transact! [{:db/id (:db/id c3)
                         :class/parent (:db/id c2)}]))
-      (db-property-handler/class-add-property! repo c2id "property-3")
-      (db-property-handler/class-add-property! repo c2id "property-4")
+      (db-property-handler/class-add-property! repo c2id :user.property/property-3)
+      (db-property-handler/class-add-property! repo c2id :user.property/property-4)
       (is (= 3 (count (:classes-properties
                        (db-property-handler/get-block-classes-properties (:db/id (db/entity [:block/uuid fbid]))))))))))
 
@@ -229,7 +212,7 @@
           k :user.property/property-1]
       ;; add property
       (db-property-handler/upsert-property! repo k {:type :default} {})
-      (let [property (db/entity [:block/name k])
+      (let [property (db/entity k)
             {:keys [last-block-id]} (db-property-handler/create-property-text-block! fb property "Block content" editor-handler/wrap-parse-block {})
             {:keys [from-block-id from-property-id]} (db-property-handler/get-property-block-created-block [:block/uuid last-block-id])]
         (is (= from-block-id (:db/id fb)))
@@ -258,7 +241,7 @@
           k :user.property/property-1]
       ;; add property
       (db-property-handler/upsert-property! repo k {:type :default} {})
-      (let [property (db/entity [:block/name k])]
+      (let [property (db/entity k)]
         (db-property-handler/create-property-text-block! fb property "Block content" editor-handler/wrap-parse-block {})
         ;; collapse property-1
         (db-property-handler/collapse-expand-property! repo fb property true)