Ver Fonte

add testcase for get-block-children-ids-in-db

rcmerci há 2 anos atrás
pai
commit
5287c24cf2

+ 20 - 17
src/main/frontend/db/model.cljs

@@ -889,25 +889,28 @@ independent of format as format specific heading characters are stripped"
           (db-utils/pull-many repo
                               '[:db/id :block/name :block/original-name]
                               ids))))))
+(defn get-block-children-ids-in-db
+  [db block-uuid]
+  (when-let [eid (:db/id (db-utils/entity db [:block/uuid block-uuid]))]
+    (let [seen   (volatile! [])]
+      (loop [steps          100      ;check result every 100 steps
+             eids-to-expand [eid]]
+        (when (seq eids-to-expand)
+          (let [eids-to-expand*
+                (mapcat (fn [eid] (map first (d/datoms db :avet :block/parent eid))) eids-to-expand)
+                uuids-to-add (remove nil? (map #(:block/uuid (db-utils/entity db %)) eids-to-expand*))]
+            (when (and (zero? steps)
+                       (seq (set/intersection (set @seen) (set uuids-to-add))))
+              (throw (ex-info "bad outliner data, need to re-index to fix"
+                              {:seen @seen :eids-to-expand eids-to-expand})))
+            (vswap! seen (partial apply conj) uuids-to-add)
+            (recur (if (zero? steps) 100 (dec steps)) eids-to-expand*))))
+      @seen)))
 
 (defn get-block-children-ids
-  [repo block-uuid]
-  (when-let [db (conn/get-db repo)]
-    (when-let [eid (:db/id (db-utils/entity repo [:block/uuid block-uuid]))]
-      (let [seen   (volatile! [])]
-        (loop [steps          100               ;check result every 100 steps
-               eids-to-expand [eid]]
-          (when (seq eids-to-expand)
-            (let [eids-to-expand*
-                  (mapcat (fn [eid] (map first (d/datoms db :avet :block/parent eid))) eids-to-expand)
-                  uuids-to-add (remove nil? (map #(:block/uuid (db-utils/entity db %)) eids-to-expand*))]
-              (when (and (zero? steps)
-                         (seq (set/intersection (set @seen) (set uuids-to-add))))
-                (throw (ex-info "bad outliner data, need to re-index to fix"
-                                {:seen @seen :eids-to-expand eids-to-expand})))
-              (vswap! seen (partial apply conj) uuids-to-add)
-              (recur (if (zero? steps) 100 (dec steps)) eids-to-expand*))))
-        @seen))))
+  ([repo block-uuid]
+   (when-let [db (conn/get-db repo)]
+     (get-block-children-ids-in-db db block-uuid))))
 
 (defn get-block-immediate-children
   "Doesn't include nested children."

+ 14 - 0
src/test/fixtures/broken-outliner-data-with-cycle.edn

@@ -0,0 +1,14 @@
+;;; datoms
+;;; - 1 <----+
+;;;   - 2    |
+;;;     - 3 -+
+[{:db/id 1
+  :block/uuid #uuid"e538d319-48d4-4a6d-ae70-c03bb55b6fe4"
+  :block/parent 3}
+ {:db/id 2
+  :block/uuid #uuid"c46664c0-ea45-4998-adf0-4c36486bb2e5"
+  :block/parent 1}
+ {:db/id 3
+  :block/uuid #uuid"2b736ac4-fd49-4e04-b00f-48997d2c61a2"
+  :block/parent 2}
+ ]

+ 19 - 2
src/test/frontend/db/model_test.cljs

@@ -3,7 +3,11 @@
             [frontend.db.model :as model]
             [frontend.db :as db]
             [frontend.db.conn :as conn]
-            [frontend.test.helper :as test-helper :refer [load-test-files]]))
+            [logseq.db.schema :as schema]
+            [frontend.test.helper :as test-helper :refer [load-test-files]]
+            [datascript.core :as d]
+            [shadow.resource :as rc]
+            [clojure.edn :as edn]))
 
 (use-fixtures :each {:before test-helper/start-test-db!
                      :after test-helper/destroy-test-db!})
@@ -129,7 +133,7 @@
 (deftest entity-query-should-support-both-graph-string-and-db
   (is (= 1 (:db/id (db/entity test-helper/test-db 1))))
   (is (= 1 (:db/id (db/entity (conn/get-db test-helper/test-db) 1)))))
-  
+
 (deftest get-block-by-page-name-and-block-route-name
   (load-test-files [{:file/path "foo.md"
                      :file/content "foo:: bar
@@ -144,3 +148,16 @@ foo:: bar"}])
   (is (nil?
        (model/get-block-by-page-name-and-block-route-name test-helper/test-db "foo" "b2"))
       "Non header block's content returns nil"))
+
+
+(def broken-outliner-data-with-cycle (-> (rc/inline "fixtures/broken-outliner-data-with-cycle.edn")
+                                         edn/read-string))
+
+(deftest get-block-children-ids-on-bad-outliner-data
+  (let [db (d/db-with (d/empty-db logseq.db.schema/schema)
+                      broken-outliner-data-with-cycle)]
+
+    (is (= "bad outliner data, need to re-index to fix"
+           (try (model/get-block-children-ids-in-db db #uuid"e538d319-48d4-4a6d-ae70-c03bb55b6fe4")
+                (catch :default e
+                  (ex-message e)))))))