浏览代码

wip: fix property-related tests

Tienson Qin 1 年之前
父节点
当前提交
8d6916cfd7

+ 2 - 11
deps/db/src/logseq/db.cljs

@@ -417,17 +417,8 @@
 
 (defn get-classes-with-property
   "Get classes which have given property as a class property"
-  [db property-uuid]
-  (d/q
-   '[:find [?b ...]
-     :in $ ?property-uuid
-     :where
-     [?b :block/schema ?schema]
-     [(get ?schema :properties) ?schema-properties*]
-     [(set ?schema-properties*) ?schema-properties]
-     [(contains? ?schema-properties ?property-uuid)]]
-   db
-   property-uuid))
+  [db property-id]
+  (:class/_schema.properties (d/entity db property-id)))
 
 (defn get-block-property-values
   "Get blocks which have this property."

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

@@ -74,8 +74,7 @@
                (:closed-values property)))
         property-schema (assoc (:block/schema property)
                                :values (mapv :block/uuid closed-value-blocks-tx))
-        property-tx (merge (sqlite-util/build-new-property prop-name property-schema
-                                                           {:db-ident db-ident})
+        property-tx (merge (sqlite-util/build-new-property db-ident prop-name property-schema)
                            property-attributes)]
     (into [property-tx page-tx]
           (when-not closed-value-page-uuids? closed-value-blocks-tx))))

+ 2 - 2
deps/db/src/logseq/db/sqlite/create_graph.cljs

@@ -27,9 +27,9 @@
                        {:block/schema schema :closed-values closed-values}
                        {:db-ident db-ident})
                       [(sqlite-util/build-new-property
+                        db-ident
                         name
-                        schema
-                        {:db-ident db-ident})])]
+                        schema)])]
          (update blocks 0 default-db/mark-block-as-built-in)))
      built-in-properties)))
 

+ 20 - 16
deps/db/src/logseq/db/sqlite/util.cljs

@@ -67,22 +67,26 @@
 
 (defn build-new-property
   "Build a standard new property so that it is is consistent across contexts"
-  [prop-name prop-schema & {:keys [db-ident]}]
-  (block-with-timestamps
-   (cond->
-    {:block/type "property"
-     :block/schema (merge {:type :default} prop-schema)
-     :block/original-name (name prop-name)}
-     (and db-ident (keyword? db-ident))
-     (assoc :db/ident db-ident)
-     (= :many (:cardinality prop-schema))
-     (assoc :db/cardinality :db.cardinality/many)
-     (not= :many (:cardinality prop-schema))
-     (assoc :db/cardinality :db.cardinality/one)
-     (contains? property-ref-types (:type prop-schema))
-     (assoc :db/valueType :db.type/ref)
-     true
-     (assoc :db/index true))))
+  [db-ident prop-name prop-schema]
+  (assert (keyword? db-ident))
+  (let [db-ident' (if (or (= "user.property" (namespace db-ident))
+                          (string/starts-with? (namespace db-ident) "logseq."))
+                    db-ident
+                    (keyword "user.property" (name db-ident)))]
+    (block-with-timestamps
+     (cond->
+      {:db/ident db-ident'
+       :block/type "property"
+       :block/schema (merge {:type :default} prop-schema)
+       :block/original-name (name prop-name)}
+       (= :many (:cardinality prop-schema))
+       (assoc :db/cardinality :db.cardinality/many)
+       (not= :many (:cardinality prop-schema))
+       (assoc :db/cardinality :db.cardinality/one)
+       (contains? property-ref-types (:type prop-schema))
+       (assoc :db/valueType :db.type/ref)
+       true
+       (assoc :db/index true)))))
 
 
 (defn build-new-class

+ 8 - 7
scripts/src/logseq/tasks/db_graph/create_graph.cljs

@@ -181,13 +181,14 @@
                                   :property-attributes
                                   {:db/id (or (property-db-ids (name prop-name))
                                               (throw (ex-info "No :db/id for property" {:property prop-name})))}})
-                                [(merge
-                                  (sqlite-util/build-new-property prop-name (get-in properties [prop-name :block/schema]) uuid)
-                                  {:db/id (or (property-db-ids (name prop-name))
-                                                      (throw (ex-info "No :db/id for property" {:property prop-name})))}
-                                  (when-let [props (not-empty (get-in properties [prop-name :properties]))]
-                                            {:block/properties (->block-properties-tx props uuid-maps)
-                                             :block/refs (build-property-refs props property-db-ids)}))]))
+                                [(let [db-ident (keyword "user.property" (name prop-name))]
+                                   (merge
+                                    (sqlite-util/build-new-property db-ident prop-name (get-in properties [prop-name :block/schema]))
+                                    {:db/id (or (property-db-ids (name prop-name))
+                                                (throw (ex-info "No :db/id for property" {:property prop-name})))}
+                                    (when-let [props (not-empty (get-in properties [prop-name :properties]))]
+                                      {:block/properties (->block-properties-tx props uuid-maps)
+                                       :block/refs (build-property-refs props property-db-ids)})))]))
                             property-uuids))
         pages-and-blocks-tx
         (vec

+ 2 - 9
src/main/frontend/components/block/macros.cljs

@@ -45,14 +45,7 @@
            :updated-at
            :block/updated-at
 
-           (let [vals (map #(pu/lookup-by-name (:block/properties %) f) result)
-                 int? (some integer? vals)
-                 repo (state/get-current-repo)
-                 prop-key (if (config/db-based-graph? repo)
-                            (or (db-pu/get-user-property-uuid repo f)
-                                ;; Fall back to the keyword for queries that set named properties through :result-transform
-                                f)
-                            f)]
+           (let [prop-key f]
              `(~'fn [~'b]
                     (~'let [~'result-str (~'get-in ~'b [:block/properties ~prop-key])
                             ~'result-num (~'parseFloat ~'result-str)
@@ -78,4 +71,4 @@
     (when (fn? f)
       (try (f query-result)
            (catch :default e
-             (js/console.error e))))))
+             (js/console.error e))))))

+ 1 - 2
src/main/frontend/components/query_table.cljs

@@ -112,8 +112,7 @@
         hidden-properties (if db-graph?
                             ;; TODO: Support additional hidden properties e.g. from user config
                             ;; or gp-property/built-in-extended properties
-                            (set (map #(db-pu/get-built-in-property-uuid repo %)
-                                      (keys db-property/built-in-properties)))
+                            (set (keys db-property/built-in-properties))
                             (conj (file-property-handler/built-in-properties) :template))
         prop-keys* (->> (distinct (mapcat keys (map :block/properties result)))
                         (remove hidden-properties))

+ 5 - 4
src/main/frontend/db/model.cljs

@@ -824,13 +824,14 @@ independent of format as format specific heading characters are stripped"
 
 (defn get-block-property-values
   "Get blocks which have this property."
-  [property-uuid]
-  (ldb/get-block-property-values (conn/get-db) property-uuid))
+  [property-id]
+  (let [db (conn/get-db)]
+    (map :v (d/datoms db :avet property-id))))
 
 (defn get-classes-with-property
   "Get classes which have given property as a class property"
-  [property-uuid]
-  (ldb/get-classes-with-property (conn/get-db) property-uuid))
+  [property-id]
+  (ldb/get-classes-with-property (conn/get-db) property-id))
 
 (defn get-all-referenced-blocks-uuid
   "Get all uuids of blocks with any back link exists."

+ 7 - 9
src/main/frontend/extensions/pdf/assets.cljs

@@ -195,19 +195,17 @@
            (let [text       (:text content)
                  wrap-props #(if-let [stamp (:image content)]
                                (assoc %
-                                      (pu/get-pid :logseq.property/hl-type) :area
-                                      (pu/get-pid :logseq.property/hl-stamp) stamp)
+                                      :logseq.property/hl-type :area
+                                      :logseq.property/hl-stamp stamp)
                                %)
                  props (cond->
-                        {(pu/get-pid :logseq.property/ls-type)  :annotation
-                         (pu/get-pid :logseq.property/hl-page)  page
-                         (pu/get-pid :logseq.property/hl-color) (:color properties)}
+                        {:logseq.property/ls-type  :annotation
+                         :logseq.property/hl-page  page
+                         :logseq.property/hl-color (:color properties)}
                          (not (config/db-based-graph? (state/get-current-repo)))
-                       ;; force custom uuid
+                          ;; force custom uuid
                          (assoc :id (str id)))
-                 properties (->>
-                             (wrap-props props)
-                             (property-handler/replace-key-with-id (state/get-current-repo)))]
+                 properties (wrap-props props)]
              (when (string? text)
                (editor-handler/api-insert-new-block!
                 text (merge {:page        (:block/name ref-page)

+ 10 - 18
src/main/frontend/handler/db_based/property.cljs

@@ -124,8 +124,12 @@
         (db/transact! repo tx-data {:outliner-op :save-block}))
 
       :else
-      (let [k-name (or (:block/original-name property) (and property-name (name property-name)))]
-        (db/transact! repo [(sqlite-util/build-new-property k-name schema {:db-ident db-ident})]
+      (let [k-name (or (:block/original-name property)
+                       (and property-name (name property-name))
+                       (name property-id))]
+        (assert (some? k-name)
+                (prn "property-id: " property-id ", property-name: " property-name))
+        (db/transact! repo [(sqlite-util/build-new-property db-ident k-name schema)]
                       {:outliner-op :new-property})))))
 
 (defn validate-property-value
@@ -190,7 +194,10 @@
 
 (defn set-block-property!
   [repo block-eid property-id v {:keys [old-value] :as opts}]
-  (let [property-id (if (string? property-id)
+  (let [block-eid (if (uuid? block-eid)
+                    [:block/uuid block-eid]
+                    block-eid)
+        property-id (if (string? property-id)
                       (db-property/get-db-ident-from-name property-id)
                       property-id)
         _ (assert (keyword? property-id) "property-id should be a keyword")
@@ -380,21 +387,6 @@
                                    {})
               (remove-block-property! repo (:db/id block) property-id))))))))
 
-(defn replace-key-with-id
-  "Notice: properties need to be created first"
-  [m]
-  (zipmap
-   (map (fn [k]
-          (if (uuid? k)
-            k
-            (let [property-id (db-pu/get-user-property-uuid k)]
-              (when-not property-id
-                (throw (ex-info "Property not exists yet"
-                                {:key k})))
-              property-id)))
-        (keys m))
-   (vals m)))
-
 (defn collapse-expand-property!
   "Notice this works only if the value itself if a block (property type should be either :default or :template)"
   [repo block property collapse?]

+ 0 - 15
src/main/frontend/handler/db_based/property/util.cljs

@@ -1,8 +1,6 @@
 (ns frontend.handler.db-based.property.util
   "DB-graph only utility fns for properties"
   (:require [frontend.db.utils :as db-utils]
-            [frontend.state :as state]
-            [logseq.common.util :as common-util]
             [frontend.db :as db]
             [logseq.db.frontend.property :as db-property]))
 
@@ -11,19 +9,6 @@
   [id]
   (:block/original-name (db-utils/entity id)))
 
-;; FIXME: property no long has `:block/name` attribute
-(defn get-built-in-property-uuid
-  "Get a built-in property's uuid given its db-ident"
-  ([db-ident] (get-built-in-property-uuid (state/get-current-repo) db-ident))
-  ([repo db-ident] (:block/uuid (db-utils/entity repo db-ident))))
-
-;; FIXME: property no long has `:block/name` attribute
-(defn get-user-property-uuid
-  "Get a user property's uuid given its unsanitized name"
-  ([property-name] (get-user-property-uuid (state/get-current-repo) property-name))
-  ([repo property-name]
-   (:block/uuid (db-utils/entity repo [:block/name (common-util/page-name-sanity-lc (name property-name))]))))
-
 (defn all-hidden-properties?
   "Checks if the given properties are all hidden properties"
   [properties]

+ 0 - 3
src/main/frontend/handler/editor.cljs

@@ -518,9 +518,6 @@
               new-block (-> new-block
                             (wrap-parse-block)
                             (assoc :block/uuid (or custom-uuid (db/new-block-id))))
-              new-block (if (and db-based? (seq properties))
-                          (assoc new-block :block/properties (db-property-handler/replace-key-with-id properties))
-                          new-block)
               new-block (merge new-block other-attrs)
               [block-m sibling?] (cond
                                    before?

+ 0 - 6
src/main/frontend/handler/property.cljs

@@ -68,9 +68,3 @@
       (db-property-handler/batch-remove-property! repo block-ids key)
       (db-property-handler/batch-set-property! repo block-ids key value))
     (file-property-handler/batch-set-block-property! block-ids key value)))
-
-(defn replace-key-with-id
-  [repo m]
-  (if (config/db-based-graph? repo)
-    (db-property-handler/replace-key-with-id m)
-    m))

+ 15 - 15
src/test/frontend/db/db_based_model_test.cljs

@@ -22,16 +22,15 @@
 (use-fixtures :each start-and-destroy-db)
 
 (deftest get-block-property-values-test
-  (db-property-handler/set-block-property! repo fbid "property-1" "value 1" {})
-  (db-property-handler/set-block-property! repo sbid "property-1" "value 2" {})
-  (let [property (db/entity [:block/name "property-1"])]
-    (is (= (map second (model/get-block-property-values (:block/uuid property)))
-           ["value 1" "value 2"]))))
+  (db-property-handler/set-block-property! repo fbid :user.property/property-1 "value 1" {})
+  (db-property-handler/set-block-property! repo sbid :user.property/property-1 "value 2" {})
+  (is (= (model/get-block-property-values :user.property/property-1)
+         ["value 1" "value 2"])))
 
-;; (deftest get-db-property-values-test
-;;   (db-property-handler/set-block-property! repo fbid "property-1" "1" {})
-;;   (db-property-handler/set-block-property! repo sbid "property-1" "2" {})
-;;   (is (= [1 2] (model/get-db-property-values repo "property-1"))))
+(deftest get-db-property-values-test
+  (db-property-handler/set-block-property! repo fbid :user.property/property-1 "1" {})
+  (db-property-handler/set-block-property! repo sbid :user.property/property-1 "2" {})
+  (is (= [1 2] (model/get-block-property-values :user.property/property-1))))
 
 ;; (deftest get-db-property-values-test-with-pages
 ;;   (let [opts {:redirect? false :create-first-block? false}
@@ -75,12 +74,13 @@
         _ (page-handler/create! "class2" opts)
         class1 (db/entity [:block/name "class1"])
         class2 (db/entity [:block/name "class2"])]
-    (db-property-handler/upsert-property! repo "property-1" {:type :page} {})
-    (db-property-handler/class-add-property! repo (:block/uuid class1) "property-1")
-    (db-property-handler/class-add-property! repo (:block/uuid class2) "property-1")
-    (let [property (db/entity [:block/name "property-1"])
-          class-ids (model/get-classes-with-property (:block/uuid property))]
-      (is (= class-ids [(:db/id class1) (:db/id class2)])))))
+    (db-property-handler/upsert-property! repo :user.property/property-1 {:type :page} {})
+    (db-property-handler/class-add-property! repo (:block/uuid class1) :user.property/property-1)
+    (db-property-handler/class-add-property! repo (:block/uuid class2) :user.property/property-1)
+    (let [property (db/entity :user.property/property-1)
+          classes (model/get-classes-with-property (:block/uuid property))]
+      (is (= (set (map :db/id classes))
+             #{(:db/id class1) (:db/id class2)})))))
 
 (deftest get-tag-blocks-test
   (let [opts {:redirect? false :create-first-block? false :class? true}

+ 4 - 4
src/test/frontend/handler/db_based/property_closed_value_test.cljs

@@ -40,10 +40,10 @@
 ;; delete-closed-value
 (deftest-async closed-values-test
   (testing "Create properties and closed values"
-    (db-property-handler/set-block-property! repo fbid "property-1" "1" {})
-    (db-property-handler/set-block-property! repo sbid "property-1" "2" {})
-    (let [k "property-1"
-          property (db/entity [:block/name k])]
+    (db-property-handler/set-block-property! repo fbid :user.property/property-1 "1" {})
+    (db-property-handler/set-block-property! repo sbid :user.property/property-1 "2" {})
+    (let [k :user.property/property-1
+          property (db/entity k)]
       (p/do!
        (db-property-handler/<add-existing-values-to-closed-values! property [1 2])
        (testing "Add existing values to closed values"

+ 12 - 23
src/test/frontend/handler/db_based/property_test.cljs

@@ -34,10 +34,10 @@
 ;; update-property!
 (deftest ^:large-vars/cleanup-todo block-property-test
   (testing "Add a property to a block"
-    (db-property-handler/set-block-property! repo fbid "property-1" "value" {})
+    (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 "property-1"])]
+          property (db/entity [:block/name :user.property/property-1])]
       ;; ensure property exists
       (are [x y] (= x y)
         (:block/schema property)
@@ -54,10 +54,10 @@
         "value")))
 
   (testing "Add another property"
-    (db-property-handler/set-block-property! repo fbid "property-2" "1" {})
+    (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 "property-2"])]
+          property (db/entity [:block/name :user.property/property-2])]
       ;; ensure property exists
       (are [x y] (= x y)
         (:block/schema property)
@@ -74,7 +74,7 @@
         1)))
 
   (testing "Update property value"
-    (db-property-handler/set-block-property! repo fbid "property-2" 2 {})
+    (db-property-handler/set-block-property! repo fbid :user.property/property-2 2 {})
     (let [block (db/entity [:block/uuid fbid])
           properties (:block/properties block)]
       ;; check block's properties
@@ -85,7 +85,7 @@
         2)))
 
   (testing "Wrong type property value shouldn't transacted"
-    (db-property-handler/set-block-property! repo fbid "property-2" "Not a number" {})
+    (db-property-handler/set-block-property! repo fbid :user.property/property-2 "Not a number" {})
     (let [block (db/entity [:block/uuid fbid])
           properties (:block/properties block)]
       ;; check block's properties
@@ -191,14 +191,14 @@
         #{"class"}))
 
     (testing "Class add property"
-      (db-property-handler/class-add-property! repo c1id "property-1")
-      (db-property-handler/class-add-property! repo c1id "property-2")
+      (db-property-handler/class-add-property! repo c1id :user.property/property-1)
+      (db-property-handler/class-add-property! repo c1id :user.property/property-2)
       ;; repeated adding property-2
-      (db-property-handler/class-add-property! repo c1id "property-2")
+      (db-property-handler/class-add-property! repo c1id :user.property/property-2)
       (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 "property-1"])))
+      (db-property-handler/class-remove-property! repo c1id (:block/uuid (db/entity [:block/name :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"]})
@@ -226,7 +226,7 @@
   (testing "Add property and create a block value"
     (let [repo (state/get-current-repo)
           fb (db/entity [:block/uuid fbid])
-          k "property-1"]
+          k :user.property/property-1]
       ;; add property
       (db-property-handler/upsert-property! repo k {:type :default} {})
       (let [property (db/entity [:block/name k])
@@ -250,23 +250,12 @@
         [:any test-uuid] test-uuid
         [nil test-uuid] test-uuid))))
 
-;; replace-key-with-id
-(deftest replace-key-with-id-test
-  (db-property-handler/upsert-property! repo "property 1" {:type :default} {})
-  (db-property-handler/upsert-property! repo "property 2" {:type :default} {})
-  (testing "Replace property key with its uuid"
-    (let [result (db-property-handler/replace-key-with-id {"property 1" "value 1"
-                                                           "property 2" "value 2"})]
-      (is (every? uuid? (keys result)))))
-  (testing "Throw an error if a property doesn't exists"
-    (is (thrown? js/Error (db-property-handler/replace-key-with-id {"property not exists yet" "value 1"})))))
-
 ;; collapse-expand-property!
 (deftest collapse-expand-property-test
   (testing "Collapse and expand property"
     (let [repo (state/get-current-repo)
           fb (db/entity [:block/uuid fbid])
-          k "property-1"]
+          k :user.property/property-1]
       ;; add property
       (db-property-handler/upsert-property! repo k {:type :default} {})
       (let [property (db/entity [:block/name k])]