Просмотр исходного кода

fix: export tests with large graph have duplicate built-in pages

Fixed the setup of these tests and then was able to confirm that
built-in pages are idempotent with import-export process. Also fixed
related docstrings
Gabriel Horner 1 день назад
Родитель
Сommit
16beb18c92

+ 12 - 4
deps/db/src/logseq/db/sqlite/export.cljs

@@ -19,7 +19,7 @@
             [logseq.db.frontend.schema :as db-schema]
             [logseq.db.frontend.validate :as db-validate]
             [logseq.db.sqlite.build :as sqlite-build]
-            [logseq.db.test.helper :as db-test]
+            [logseq.db.sqlite.create-graph :as sqlite-create-graph]
             [medley.core :as medley]))
 
 ;; Export fns
@@ -1062,7 +1062,7 @@
     (merge-export-maps export-map {:pages-and-blocks pages-and-blocks})))
 
 (defn build-import
-  "Given an entity's export map, build the import tx to create it. In addition to standard sqlite.build keys,
+  "Given an export map, build the import tx to create it. In addition to standard sqlite.build keys,
    an export map can have the following namespaced keys:
    * ::export-type - Keyword indicating export type
    * ::block - Block map for a :block export
@@ -1071,7 +1071,7 @@
    * ::auto-include-namespaces - A set of parent namespaces to include from properties and classes
      for a :graph export. See :exclude-namespaces in build-graph-export for a similar option
    * ::import-options - A map of options that alters importing behavior. Has the following keys:
-     * :existing-pages-keep-properties? - Boolean which disables upsert of :build/properties on
+     * :existing-pages-keep-properties? - Boolean which allows existing pages to keep existing properties
 
    This fn then returns a map of txs to transact with the following keys:
    * :init-tx - Txs that must be transacted first, usually because they define new properties
@@ -1098,12 +1098,20 @@
                                          (::kv-values export-map'')))))
         (sqlite-build/build-blocks-tx (remove-namespaced-keys export-map''))))))
 
+(defn create-conn
+  "Create a conn for a DB graph seeded with initial data"
+  []
+  (let [conn (d/create-conn db-schema/schema)
+        _ (d/transact! conn (sqlite-create-graph/build-db-initial-data "{}"))]
+    (entity-plus/reset-immutable-entities-cache!)
+    conn))
+
 (defn validate-export
   "Validates an export by creating an in-memory DB graph, importing the EDN and validating the graph.
    Returns a map with a readable :error key if any error occurs"
   [export-edn]
   (try
-    (let [import-conn (db-test/create-conn)
+    (let [import-conn (create-conn)
           {:keys [init-tx block-props-tx misc-tx] :as _txs} (build-import export-edn @import-conn {})
           _ (d/transact! import-conn (concat init-tx block-props-tx misc-tx))
           validation (db-validate/validate-local-db! @import-conn)]

+ 14 - 11
deps/db/src/logseq/db/test/helper.cljs

@@ -2,11 +2,9 @@
   "Main ns for providing test fns for DB graphs"
   (:require [datascript.core :as d]
             [datascript.impl.entity :as de]
-            [logseq.db.common.entity-plus :as entity-plus]
             [logseq.db.frontend.property :as db-property]
-            [logseq.db.frontend.schema :as db-schema]
             [logseq.db.sqlite.build :as sqlite-build]
-            [logseq.db.sqlite.create-graph :as sqlite-create-graph]))
+            [logseq.db.sqlite.export :as sqlite-export]))
 
 (defn find-block-by-content
   "Find first block by exact block string or by fuzzier regex"
@@ -73,17 +71,22 @@
                   v)]))
        (into {})))
 
-(defn create-conn
-  "Create a conn for a DB graph seeded with initial data"
-  []
-  (let [conn (d/create-conn db-schema/schema)
-        _ (d/transact! conn (sqlite-create-graph/build-db-initial-data "{}"))]
-    (entity-plus/reset-immutable-entities-cache!)
-    conn))
+(def create-conn sqlite-export/create-conn)
 
 (defn create-conn-with-blocks
-  "Create a conn with create-db-conn and then create blocks using sqlite-build"
+  "Create a conn with create-conn and then create blocks using sqlite-build"
   [opts]
   (let [conn (create-conn)
         _ (sqlite-build/create-blocks conn opts)]
     conn))
+
+(defn create-conn-with-upsertable-blocks
+  "Create conn like create-conn-with-blocks but also handles upserting existing built-in entities"
+  [export-map]
+  (let [conn (create-conn)
+        {:keys [init-tx block-props-tx misc-tx]}
+        ;; Handle graph-files separately b/c build-import can't upsert them
+        (sqlite-export/build-import (dissoc export-map ::sqlite-export/graph-files) @conn {})
+        _ (d/transact! conn (concat init-tx block-props-tx misc-tx))
+        _ (d/transact! conn (::sqlite-export/graph-files export-map))]
+    conn))

+ 6 - 10
deps/db/test/logseq/db/sqlite/export_test.cljs

@@ -733,6 +733,7 @@
           {:file/path "logseq/publish.js"
            :file/content ""}]
          :build-existing-tx? true}
+        ;; Some of these built-ins are only here to make assertions pass
         built-in-pages
         [{:page {:block/title "Library" :build/properties {:logseq.property/built-in? true}}
           :blocks []}
@@ -762,12 +763,11 @@
 
 (deftest ^:long import-graph
   (let [original-data (build-original-graph-data)
-        conn (db-test/create-conn-with-blocks (dissoc original-data ::sqlite-export/graph-files))
+        conn (db-test/create-conn-with-upsertable-blocks original-data)
         ;; set to an unobtainable version to test this ident
         _ (d/transact! conn [{:db/ident :logseq.kv/schema-version :kv/value {:major 1 :minor 0}}])
         original-kv-values (remove #(= :logseq.kv/schema-version (:db/ident %))
                                    (d/q '[:find [(pull ?b [:db/ident :kv/value]) ...] :where [?b :kv/value]] @conn))
-        _ (d/transact! conn (::sqlite-export/graph-files original-data))
         conn2 (db-test/create-conn)
         imported-graph (export-graph-and-import-to-another-graph conn conn2 {})]
 
@@ -805,8 +805,7 @@
                                     (mapv #(let [now (js/Date.)]
                                              (merge % {:file/created-at now :file/last-modified-at now}))
                                           files))))
-        conn (db-test/create-conn-with-blocks (dissoc original-data ::sqlite-export/graph-files))
-        _ (d/transact! conn (::sqlite-export/graph-files original-data))
+        conn (db-test/create-conn-with-upsertable-blocks original-data)
         conn2 (db-test/create-conn)
         imported-graph (export-graph-and-import-to-another-graph conn conn2 {:include-timestamps? true})]
 
@@ -820,8 +819,7 @@
 
 (deftest ^:long import-graph-with-exclude-namespaces
   (let [original-data (build-original-graph-data {:exclude-namespaces? true})
-        conn (db-test/create-conn-with-blocks (dissoc original-data ::sqlite-export/graph-files))
-        _ (d/transact! conn (::sqlite-export/graph-files original-data))
+        conn (db-test/create-conn-with-upsertable-blocks original-data)
         conn2 (db-test/create-conn-with-blocks
                {:properties (update-vals (:properties original-data) #(dissoc % :build/properties))
                 :classes (update-vals (:classes original-data) #(dissoc % :build/properties))})
@@ -834,10 +832,8 @@
         "All :file/path entities are imported")))
 
 (deftest ^:long graph-is-idempotent-across-import-and-export
-  ;; FIXME: built-in pages should be idempotent across export and import
-  (let [original-data (build-original-graph-data {:add-built-in-pages? false})
-        conn (db-test/create-conn-with-blocks (dissoc original-data ::sqlite-export/graph-files))
-        _ (d/transact! conn (::sqlite-export/graph-files original-data))
+  (let [original-data (build-original-graph-data)
+        conn (db-test/create-conn-with-upsertable-blocks original-data)
         export-map (sqlite-export/build-export @conn {:export-type :graph})
         valid-result (sqlite-export/validate-export export-map)
         _ (assert (not (:error valid-result)) "No error when importing export-map into new graph")