Browse Source

fix: property tests

Tienson Qin 1 year ago
parent
commit
5a595b1626

+ 2 - 1
deps/db/src/logseq/db/frontend/malli_schema.cljs

@@ -69,7 +69,8 @@
                                           (:block/uuid (d/entity db val)))))
                         validate-fn')]
     (if (= (get-in property [:block/schema :cardinality]) :many)
-      (every? validate-fn'' property-val)
+      (let [property-val (if (coll? property-val) property-val [property-val])]
+        (every? validate-fn'' property-val))
       (or (validate-fn'' property-val) (= :logseq.property/empty-placeholder property-val)))))
 
 (defn update-properties-in-schema

+ 7 - 7
src/main/frontend/handler/db_based/property.cljs

@@ -247,7 +247,7 @@
 (defn set-block-property!
   "Updates a block property's value for the an existing property-id. If possibly
   creating a new property, use upsert-property!"
-  [repo block-eid property-id v {:keys [property-name] :as opts}]
+  [repo block-eid property-id v {:keys [property-name property-type] :as opts}]
   (let [block-eid (->eid block-eid)
         _ (assert (qualified-keyword? property-id) "property-id should be a keyword")
         block (db/entity repo block-eid)
@@ -261,7 +261,7 @@
     (cond
       db-attribute?
       (db/transact! repo [{:db/id (:db/id block) property-id v}]
-        {:outliner-op :save-block})
+                    {:outliner-op :save-block})
 
       (and multiple-values? (coll? v))
       (reset-block-property-multiple-values! repo block-eid property-id v opts)
@@ -270,14 +270,14 @@
       (let [v (if property v (or v ""))]
         (when (some? v)
           (let [infer-schema (when-not type (infer-schema-from-input-string v))
-                property-type (or type infer-schema :default)
-                schema (get-property-value-schema property-type property)
+                property-type' (or type property-type infer-schema :default)
+                schema (get-property-value-schema property-type' property)
                 value (when-let [id (:db/ident property)]
                         (get block id))
                 v* (if (= v :logseq.property/empty-placeholder)
                      v
                      (try
-                       (convert-property-input-string property-type v)
+                       (convert-property-input-string property-type' v)
                        (catch :default e
                          (js/console.error e)
                          (notification/show! (str e) :error false)
@@ -286,7 +286,7 @@
               (if-let [msg (when-not (= v* :logseq.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) {:property-name property-name})
+                (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 :logseq.property/empty-placeholder) [] value)
                       new-value (cond
@@ -298,7 +298,7 @@
                                   v*)
                         ;; don't modify maps
                       new-value (if (or (sequential? new-value) (set? new-value))
-                                  (if (= :coll property-type)
+                                  (if (= :coll property-type')
                                     (vec (remove string/blank? new-value))
                                     (set (remove string/blank? new-value)))
                                   new-value)

+ 9 - 8
src/test/frontend/handler/db_based/property_test.cljs

@@ -6,7 +6,8 @@
             [datascript.core :as d]
             [frontend.state :as state]
             [frontend.handler.page :as page-handler]
-            [frontend.handler.editor :as editor-handler]))
+            [frontend.handler.editor :as editor-handler]
+            [promesa.core :as p]))
 
 (def repo test-helper/test-db-name-db-version)
 
@@ -32,16 +33,16 @@
 ;; batch-remove-property!
 ;; upsert-property!
 ;; update-property!
-(deftest ^:large-vars/cleanup-todo block-property-test
+(deftest ^:large-vars/cleanup-todo ^:wip block-property-test
   (testing "Add a property to a block"
-    (db-property-handler/set-block-property! repo fbid :user.property/property-1 "value" {})
+    (db-property-handler/set-block-property! repo fbid :user.property/property-1 "value" {:property-type :string})
     (let [block (db/entity [:block/uuid fbid])
           properties (:block/properties block)
           property (db/entity :user.property/property-1)]
       ;; ensure property exists
       (are [x y] (= x y)
         (:block/schema property)
-        {:type :default}
+        {:type :string}
         (:block/type property)
         #{"property"})
       ;; check block's properties
@@ -130,7 +131,7 @@
   (testing "Batch set properties"
     (let [k :user.property/property-4
           v "batch value"]
-      (db-property-handler/upsert-property! repo :user.property/property-4 {:type :default} {})
+      (db-property-handler/upsert-property! repo :user.property/property-4 {:type :string} {})
       (db-property-handler/batch-set-property! repo [fbid sbid] k v)
       (let [fb (db/entity [:block/uuid fbid])
             sb (db/entity [:block/uuid sbid])]
@@ -214,9 +215,9 @@
           k :user.property/property-1]
       ;; add property
       (db-property-handler/upsert-property! repo k {:type :default} {})
-      (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])]
+      (p/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)))
         (is (= from-property-id (:db/id property)))))))