Browse Source

fix: property unit tests

Tienson Qin 1 year ago
parent
commit
4a0736f62b

+ 1 - 1
deps/db/src/logseq/db/sqlite/util.cljs

@@ -92,7 +92,7 @@
         :db/cardinality (if (= :many (:cardinality prop-schema))
                           :db.cardinality/many
                           :db.cardinality/one)}
-        (or ref-type? (contains? db-property-type/ref-property-types (:type prop-schema)))
+        (or ref-type? (contains? (conj db-property-type/ref-property-types :entity) (:type prop-schema)))
         (assoc :db/valueType :db.type/ref))))))
 
 

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

@@ -546,7 +546,7 @@
         last-block-id (:block/uuid (last blocks))
         class? (contains? (:block/type block) "class")
         property-id (:db/ident property)]
-    (p/let [_ (db/transact! repo (if page (cons page blocks) blocks) {:outliner-op :insert-blocks})]
+    (p/let [_tx-report (db/transact! repo (if page (cons page blocks) blocks) {:outliner-op :insert-blocks})]
       (let [result (when property-id
                      (if (and class? class-schema?)
                        (class-add-property! repo (:db/id block) property-id)
@@ -790,7 +790,7 @@
                       from-property (:logseq.property/created-from-property block)]
                   (when (and from-block from-property)
                     [from-block from-property]))) (reverse parents))]
-    {:from-block-id (or (:db/id created-from-block) (:db/id b))
+    {:from-block-id (:db/id created-from-block)
      :from-property-id (:db/id created-from-property)}))
 
 (defn batch-set-property-closed-value!

+ 63 - 0
src/test/frontend/handler/db_based/property_async_test.cljs

@@ -0,0 +1,63 @@
+(ns frontend.handler.db-based.property-async-test
+  (:require [frontend.handler.db-based.property :as db-property-handler]
+            [frontend.db :as db]
+            [clojure.test :refer [is testing async use-fixtures]]
+            [frontend.test.helper :as test-helper :include-macros true :refer [deftest-async]]
+            [datascript.core :as d]
+            [promesa.core :as p]
+            [frontend.db.conn :as conn]
+            [frontend.state :as state]
+            [frontend.handler.editor :as editor-handler]))
+
+(def repo test-helper/test-db-name-db-version)
+
+(def init-data (test-helper/initial-test-page-and-blocks))
+
+(def fbid (:block/uuid (second init-data)))
+
+(use-fixtures :each
+  {:before (fn []
+             (async done
+                    (test-helper/start-test-db! {:db-graph? true})
+                    (p/let [_tx-report (d/transact! (conn/get-db repo false) init-data)]
+                      (done))))
+   :after test-helper/destroy-test-db!})
+
+;; property-create-new-block
+;; get-property-block-created-block
+(deftest-async text-block-test
+  (testing "Add property and create a block value"
+    (let [repo (state/get-current-repo)
+          fb (db/entity [:block/uuid fbid])
+          k :user.property/property-1]
+      (p/do!
+       ;; add property
+       (db-property-handler/upsert-property! repo k {:type :default} {})
+       (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))))))))
+
+;; collapse-expand-property!
+(deftest-async collapse-expand-property-test
+  (testing "Collapse and expand property"
+    (let [repo (state/get-current-repo)
+          fb (db/entity [:block/uuid fbid])
+          k :user.property/property-1]
+      (let []
+        (p/do!
+         ;; add property
+         (db-property-handler/upsert-property! repo k {:type :default} {})
+         (let [property (db/entity k)]
+           (p/do!
+            (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)
+            (is (=
+                 [(:db/id property)]
+                 (map :db/id (:block/collapsed-properties (db/entity [:block/uuid fbid])))))
+
+            ;; expand property-1
+            (db-property-handler/collapse-expand-property! repo fb property false)
+            (is (nil? (:block/collapsed-properties (db/entity [:block/uuid fbid])))))))))))

+ 16 - 17
src/test/frontend/handler/db_based/property_closed_value_test.cljs

@@ -67,22 +67,21 @@
            (db/transact! tx-data)
            (let [b (db/entity [:block/uuid block-id])]
              (is (= 3 (:value (:block/schema b))))
-             (is (contains? (:block/type b) "closed value")))
-           (let [values (get-value-ids k)]
-             (is (= #{1 2 3} (get-closed-values values))))
+             (is (contains? (:block/type b) "closed value"))
+             (let [values (get-value-ids k)]
+               (is (= #{1 2 3} (get-closed-values values))))
 
-           (testing "Update closed value"
-             (p/let [{:keys [tx-data]} (db-property-handler/upsert-closed-value property {:id block-id
-                                                                                        :value 4
-                                                                                        :description "choice 4"})]
-               (db/transact! tx-data)
-               (let [b (db/entity [:block/uuid block-id])]
-                 (is (= 4 (:value (:block/schema b))))
-                 (is (= "choice 4" (:description (:block/schema b))))
-                 (is (contains? (:block/type b) "closed value")))))
+             (testing "Update closed value"
+               (p/let [{:keys [tx-data]} (db-property-handler/upsert-closed-value property {:id block-id
+                                                                                            :value 4
+                                                                                            :description "choice 4"})]
+                 (db/transact! tx-data)
+                 (let [b (db/entity [:block/uuid block-id])]
+                   (is (= 4 (:value (:block/schema b))))
+                   (is (= "choice 4" (:description (:block/schema b))))
+                   (is (contains? (:block/type b) "closed value"))
+                   (p/let [_ (db-property-handler/delete-closed-value! property (db/entity [:block/uuid block-id]))]
 
-           (p/do!
-            (db-property-handler/delete-closed-value! property (db/entity [:block/uuid block-id]))
-            (testing "Delete closed value"
-              (is (nil? (db/entity [:block/uuid block-id])))
-              (is (= 2 (count (:values (:block/schema (db/entity k))))))))))))))
+                     (testing "Delete closed value"
+                       (is (nil? (db/entity [:block/uuid block-id])))
+                       (is (= 2 (count (:values (:block/schema (db/entity k))))))))))))))))))

+ 1 - 38
src/test/frontend/handler/db_based/property_test.cljs

@@ -5,9 +5,7 @@
             [frontend.test.helper :as test-helper]
             [datascript.core :as d]
             [frontend.state :as state]
-            [frontend.handler.page :as page-handler]
-            [frontend.handler.editor :as editor-handler]
-            [promesa.core :as p]))
+            [frontend.handler.page :as page-handler]))
 
 (def repo test-helper/test-db-name-db-version)
 
@@ -206,21 +204,6 @@
                        (db-property-handler/get-block-classes-properties (:db/id (db/entity [:block/uuid fbid]))))))))))
 
 
-;; property-create-new-block
-;; get-property-block-created-block
-(deftest text-block-test
-  (testing "Add property and create a block value"
-    (let [repo (state/get-current-repo)
-          fb (db/entity [:block/uuid fbid])
-          k :user.property/property-1]
-      ;; add property
-      (db-property-handler/upsert-property! repo k {:type :default} {})
-      (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)))))))
-
 ;; convert-property-input-string
 (deftest convert-property-input-string
   (testing "Convert property input string according to its schema type"
@@ -236,26 +219,6 @@
         [:any test-uuid] test-uuid
         [nil test-uuid] test-uuid))))
 
-;; 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 :user.property/property-1]
-      ;; add property
-      (db-property-handler/upsert-property! repo k {:type :default} {})
-      (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)
-        (is (=
-             [(:db/id property)]
-             (map :db/id (:block/collapsed-properties (db/entity [:block/uuid fbid])))))
-
-        ;; expand property-1
-        (db-property-handler/collapse-expand-property! repo fb property false)
-        (is (nil? (:block/collapsed-properties (db/entity [:block/uuid fbid]))))))))
-
 (deftest upsert-property!
   (testing "Update an existing property"
     (let [repo (state/get-current-repo)]

+ 5 - 4
src/test/frontend/test/helper.cljs

@@ -12,7 +12,7 @@
             [frontend.handler.page :as page-handler]
             [datascript.core :as d]
             [logseq.graph-parser.text :as text]
-            [logseq.db.sqlite.create-graph :as sqlite-create-graph]
+            [logseq.db.sqlite.create-graph :as sqlite-create-graph :refer [kv]]
             [frontend.config :as config]
             [frontend.worker.pipeline :as worker-pipeline]))
 
@@ -25,12 +25,13 @@
 
 (defn start-test-db!
   [& {:as opts}]
-  (let [test-db (if (or (:db-graph? opts) (and node? (some? js/process.env.DB_GRAPH)))
-                  test-db-name-db-version
-                  test-db-name)]
+  (let [db-graph? (or (:db-graph? opts) (and node? (some? js/process.env.DB_GRAPH)))
+        test-db (if db-graph? test-db-name-db-version test-db-name)]
     (state/set-current-repo! test-db)
     (conn/start! test-db opts)
     (let [conn (conn/get-db test-db false)]
+      (when db-graph?
+        (d/transact! conn (sqlite-create-graph/build-db-initial-data "")))
       (d/listen! conn ::listen-db-changes!
                  (fn [tx-report]
                    (worker-pipeline/invoke-hooks test-db conn tx-report {}))))))