1
0
Эх сурвалжийг харах

fix: convert model tests to use db graphs

Tweaked sqlite.build to allow :block/alias. Also modified
one test's assertions since there aren't pre-blocks anymore.
model tests are running as DB_GRAPH=1 with a hack for now
Gabriel Horner 1 долоо хоног өмнө
parent
commit
937742df61

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

@@ -662,7 +662,7 @@
         classes' (merge new-classes classes)
         used-properties (get-used-properties-from-options options)
         new-properties (->> (set/difference (set (keys used-properties)) (set (keys properties)))
-                            (remove db-property/logseq-property?)
+                            (remove db-property/internal-property?)
                             (map (fn [prop]
                                    [prop (infer-property-schema (get used-properties prop))]))
                             (into {}))

+ 95 - 63
src/test/frontend/db/model_test.cljs

@@ -1,72 +1,105 @@
 (ns frontend.db.model-test
   (:require [cljs.test :refer [use-fixtures deftest is are]]
-            [frontend.db.model :as model]
+            [datascript.core :as d]
             [frontend.db :as db]
             [frontend.db.conn :as conn]
-            [frontend.test.helper :as test-helper :refer [load-test-files]]
-            [datascript.core :as d]))
+            [frontend.db.model :as model]
+            [frontend.test.helper :as test-helper :refer [load-test-files]]))
 
-(use-fixtures :each {:before test-helper/start-test-db!
+(use-fixtures :each {:before #(test-helper/start-test-db! {:db-graph? true})
                      :after test-helper/destroy-test-db!})
 
+(def test-db test-helper/test-db-name-db-version)
+
 (deftest test-page-alias-with-multiple-alias
-  (load-test-files [{:file/path "aa.md"
-                     :file/content "alias:: ab, ac"}
-                    {:file/path "ab.md"
-                     :file/content "alias:: aa, ad"}
-                    {:file/path "ae.md"
-                     :file/content "## ref to [[ab]]"}])
-  (let [aid (:db/id (db/entity [:block/name "aa"]))
-        bid (:db/id (db/entity [:block/name "ab"]))
-        a-aliases (model/page-alias-set test-helper/test-db aid)
-        b-aliases (model/page-alias-set test-helper/test-db bid)
-        alias-names (model/get-page-alias-names test-helper/test-db aid)
+  (let [ab-uuid (random-uuid)
+        aa-uuid (random-uuid)]
+    (load-test-files
+     [{:page {:block/title "ab"
+              :build/keep-uuid? true
+              :block/uuid ab-uuid
+              :build/properties {:block/alias #{[:block/uuid aa-uuid]
+                                                [:build/page {:block/title "ad"}]}}}}
+      {:page {:block/title "aa"
+              :build/keep-uuid? true
+              :block/uuid aa-uuid
+              :build/properties {:block/alias #{[:block/uuid ab-uuid]
+                                                [:build/page {:block/title "ac"}]}}}}
+      {:page {:block/title "ae"}
+       :blocks [{:block/title (str "## ref to [[" ab-uuid "]]")}]}]))
+
+  (let [aid (:db/id (test-helper/find-page-by-title "aa"))
+        bid (:db/id (test-helper/find-page-by-title "ab"))
+        a-aliases (model/page-alias-set test-db aid)
+        b-aliases (model/page-alias-set test-db bid)
+        alias-names (model/get-page-alias-names test-db aid)
         b-ref-blocks (model/get-referenced-blocks bid)
         a-ref-blocks (model/get-referenced-blocks aid)]
 
     (are [x y] (= x y)
       4 (count a-aliases)
       4 (count b-aliases)
-      2 (count b-ref-blocks)
-      2 (count a-ref-blocks)
+      1 (count b-ref-blocks)
+      1 (count a-ref-blocks)
       #{"ab" "ac" "ad"} (set alias-names))))
 
 (deftest test-page-alias-set
-  (load-test-files [{:file/path "aa.md"
-                     :file/content "alias:: ab"}
-                    {:file/path "ab.md"
-                     :file/content "alias:: ac"}
-                    {:file/path "ad.md"
-                     :file/content "## ref to [[ab]]"}])
-  (let [page-id (:db/id (db/entity [:block/name "aa"]))
-        a-aliases (model/page-alias-set test-helper/test-db page-id)
-        alias-names (model/get-page-alias-names test-helper/test-db page-id)
+  (let [ab-uuid (random-uuid)]
+    (load-test-files
+     [{:page {:block/title "ab"
+              :build/keep-uuid? true
+              :block/uuid ab-uuid
+              :build/properties {:block/alias #{[:build/page {:block/title "ac"}]}}}}
+      {:page {:block/title "aa"
+              :build/properties {:block/alias #{[:block/uuid ab-uuid]}}}}
+      {:page {:block/title "ae"}
+       :blocks [{:block/title (str "## ref to [[" ab-uuid "]]")}]}]))
+
+  (let [page-id (:db/id (test-helper/find-page-by-title "aa"))
+        a-aliases (model/page-alias-set test-db page-id)
+        alias-names (model/get-page-alias-names test-db page-id)
         a-ref-blocks (model/get-referenced-blocks page-id)]
     (are [x y] (= x y)
       3 (count a-aliases)
-      2 (count a-ref-blocks)
+      1 (count a-ref-blocks)
       #{"ab" "ac"} (set alias-names))))
 
 (defn- page-mention-check?
   [page-name include-journals?]
-  (->> (model/get-pages-that-mentioned-page test-helper/test-db (:db/id (db/entity [:block/name page-name])) include-journals?)
+  (->> (model/get-pages-that-mentioned-page test-db (:db/id (test-helper/find-page-by-title page-name)) include-journals?)
        (map (fn [id] (:block/name (db/entity id))))))
 
 (deftest get-pages-that-mentioned-page-with-show-journal
-  (load-test-files [{:file/path "journals/2020_08_15.md"
-                     :file/content "link 1 to [[page ONE]] and link to [[generic page]]"}
-                    {:file/path "journals/2020_09_18.md"
-                     :file/content "link 2 to [[page ONE]]"}
-                    {:file/path "pages/page ONE.md"
-                     :file/content "tags:: a tag
-- page one has link to [[Dec 26th, 2020]] journal page"}
-                    {:file/path "pages/a tag.md"
-                     :file/content "i'm a tag"}
-                    {:file/path "pages/generic page.md"
-                     :file/content "- link to page one [[page ONE]]"}])
-
-  (is (= '("sep 18th, 2020" "aug 15th, 2020" "generic page")
-         (page-mention-check? "page one" true))
+  (let [page-one-uuid (random-uuid)
+        generic-page-uuid (random-uuid)
+        journal-2020-12-26-uuid (random-uuid)]
+    (load-test-files
+     [;; -------- Pages --------
+      {:page {:block/title "page one"
+              :build/keep-uuid? true
+              :block/uuid page-one-uuid}
+       :blocks
+       [{:block/title (str "- page one has link to [["
+                           journal-2020-12-26-uuid
+                           "]] journal page")}]}
+      {:page {:block/title "generic page"
+              :build/keep-uuid? true
+              :block/uuid generic-page-uuid}
+       :blocks
+       [{:block/title (str "- link to page one [[" page-one-uuid "]]")}]}
+      ;; -------- Journals --------
+      {:page {:build/journal 20201226
+              :build/keep-uuid? true
+              :block/uuid journal-2020-12-26-uuid}}
+      {:page {:build/journal 20200815}
+       :blocks
+       [{:block/title (str "link 1 to [[" page-one-uuid "]] and link to [[" generic-page-uuid "]]")}]}
+      {:page {:build/journal 20200918}
+       :blocks
+       [{:block/title (str "link 2 to [[" page-one-uuid "]]")}]}]))
+
+  (is (= #{"sep 18th, 2020" "aug 15th, 2020" "generic page"}
+         (set (page-mention-check? "page one" true)))
       "Must be 'generic page' + 2 journals")
 
   (is (= '("generic page")
@@ -85,40 +118,39 @@
   (is (nil? (db/entity 1000000))))
 
 (deftest entity-query-should-support-both-graph-string-and-db
-  (db/transact! test-helper/test-db [{:db/id 1 :value "test"}])
-  (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)))))
+  (db/transact! test-db [{:db/id 1 :value "test"}])
+  (is (= 1 (:db/id (db/entity test-db 1))))
+  (is (= 1 (:db/id (db/entity (conn/get-db test-db) 1)))))
 
 (deftest get-block-by-page-name-and-block-route-name
-  (load-test-files [{:file/path "foo.md"
-                     :file/content "foo:: bar
-- b2
-- ### Header 2
-foo:: bar"}])
+  (load-test-files
+   [{:page {:block/title "foo"}
+     :blocks [{:block/title "b2"}
+              {:block/title "Header 2"
+               :build/properties {:logseq.property/heading 3}}]}])
   (is (uuid?
        (:block/uuid
         (let [page (db/get-page "foo")]
-          (model/get-block-by-page-name-and-block-route-name test-helper/test-db (str (:block/uuid page)) "header 2"))))
+          (model/get-block-by-page-name-and-block-route-name test-db (str (:block/uuid page)) "header 2"))))
       "Header block's content returns map with :block/uuid")
 
   (is (nil?
        (let [page (db/get-page "foo")]
-         (model/get-block-by-page-name-and-block-route-name test-helper/test-db (str (:block/uuid page)) "b2")))
+         (model/get-block-by-page-name-and-block-route-name test-db (str (:block/uuid page)) "b2")))
       "Non header block's content returns nil"))
 
-
 (deftest get-block-immediate-children
-  (load-test-files [{:file/path "pages/page1.md"
-                     :file/content "\n
-- parent
-  - child 1
-    - grandchild 1
-  - child 2
-    - grandchild 2
-  - child 3"}])
+  (load-test-files
+   [{:page {:block/title "page1"}
+     :blocks [{:block/title "parent"
+               :build/children [{:block/title "child 1"
+                                 :build/children [{:block/title "grandchild 1"}]}
+                                {:block/title "child 2"
+                                 :build/children [{:block/title "grandchild 2"}]}
+                                {:block/title "child 3"}]}]}])
   (let [parent (-> (d/q '[:find (pull ?b [*]) :where [?b :block/title "parent"]]
-                        (conn/get-db test-helper/test-db))
+                        (conn/get-db test-db))
                    ffirst)]
     (is (= ["child 1" "child 2" "child 3"]
            (map :block/title
-                (model/get-block-immediate-children test-helper/test-db (:block/uuid parent)))))))
+                (model/get-block-immediate-children test-db (:block/uuid parent)))))))

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

@@ -17,6 +17,7 @@
             [logseq.db.sqlite.build :as sqlite-build]
             [logseq.db.sqlite.create-graph :as sqlite-create-graph]
             [logseq.db.sqlite.util :as sqlite-util]
+            [logseq.db.test.helper :as db-test]
             [logseq.graph-parser.text :as text]))
 
 (def bare-marker-pattern
@@ -164,16 +165,21 @@
                       (assoc options* :auto-create-ontology? true)
                       :else
                       (build-blocks-tx-options options*))
-        {:keys [init-tx block-props-tx]} (sqlite-build/build-blocks-tx options)]
-    (db/transact! test-db init-tx)
+        {:keys [init-tx block-props-tx] :as _txs} (sqlite-build/build-blocks-tx options)
+        ;; Allow pages to reference each other via uuid and for unordered init-tx
+        init-index (map #(select-keys % [:block/uuid]) init-tx)]
+    ;; (cljs.pprint/pprint _txs)
+    (db/transact! test-db-name-db-version (concat init-index init-tx))
     (when (seq block-props-tx)
-      (db/transact! test-db block-props-tx))))
+      (db/transact! test-db-name-db-version block-props-tx))))
 
 (defn load-test-files
   "Given a collection of file maps, loads them into the current test-db.
 This can be called in synchronous contexts as no async fns should be invoked"
   [files]
-  (if (and node? js/process.env.DB_GRAPH)
+  (if (and node? (or js/process.env.DB_GRAPH
+                     ;; TODO: Remove once tests are converted
+                     (-> files first :page)))
     (load-test-files-for-db-graph files)
     (file-repo-handler/parse-files-and-load-to-db!
      test-db
@@ -251,3 +257,9 @@ This can be called in synchronous contexts as no async fns should be invoked"
         conn (db/get-db repo false)
         [page-name _page-uuid] (worker-page/create! conn title opts)]
     page-name))
+
+(defn find-page-by-title [page-title]
+  (db-test/find-page-by-title (conn/get-db) page-title))
+
+(defn find-block-by-content [block-title]
+  (db-test/find-block-by-content (conn/get-db) block-title))