Explorar el Código

Allow dsl-query tests to run from existing :block/content

Generate :file/blocks from content thus allowing more tests to run
under db graph. Also loosened a couple query-dsl tests to pass
with db graphs without sacrificing test accuracy
Gabriel Horner hace 2 años
padre
commit
4a5dd5bd83
Se han modificado 2 ficheros con 36 adiciones y 17 borrados
  1. 8 13
      src/test/frontend/db/query_dsl_test.cljs
  2. 28 4
      src/test/frontend/test/helper.cljs

+ 8 - 13
src/test/frontend/db/query_dsl_test.cljs

@@ -86,13 +86,7 @@ prop-c:: [[page a]], [[page b]], [[page c]]
 prop-linked-num:: [[3000]]
 prop-d:: [[no-space-link]]
 - b4
-prop-d:: nada"
-                     :file/blocks [["b1" {:prop-a "val-a" :prop-num 2000}]
-                                   ["b2" {:prop-a "val-a" :prop-b "val-b"}]
-                                   ["b3" {:prop-c #{"page a" "page b" "page c"}
-                                           :prop-linked-num #{"3000"}
-                                           :prop-d #{"no-space-link"}}]
-                                   ["b4" {:prop-d "nada"}]]}])
+prop-d:: nada"}])
   (testing "Blocks have given property value"
     (is (= #{"b1" "b2"}
            (set (map (comp first str/split-lines :block/content)
@@ -154,9 +148,9 @@ prop-d:: nada"
                    (map-indexed (fn [idx {:keys [tags]}]
                                   {:file/path (str "pages/page" idx ".md")
                                    :file/content (if (seq tags)
-                                                   (str "tags:: " (str/join ", " (map page-ref/->page-ref tags)))
-                                                   "")
-                                   :file/blocks [[(str "block for page" idx) {:tags (set tags)}]]})))
+                                                   (str "page-prop:: b\n- block for page" idx
+                                                        "\ntags:: " (str/join ", " (map page-ref/->page-ref tags)))
+                                                   "")})))
         _ (load-test-files pages)
         {:keys [result time]}
         (util/with-time (dsl-query "(and (property tags tag1) (property tags tag2))"))]
@@ -329,10 +323,11 @@ prop-d:: nada"
          (set (map :block/content
                    (dsl-query "(and (todo now later done) (or [[page 1]] (not [[page 1]])))")))))
 
-  (is (= #{"foo:: bar\n" "DONE b1 [[page 1]] [[page 3]]"
+  (is (= #{"foo:: bar" "DONE b1 [[page 1]] [[page 3]]"
            "DONE b2 [[page 1]]"}
          (->> (dsl-query "(not (and (todo now later) (or [[page 1]] [[page 2]])))")
               (keep :block/content)
+              (map str/trimr)
               set)))
 
   ;; FIXME: not working
@@ -462,11 +457,11 @@ tags: [[other]]
                 (dsl-query "(or [[tag2]] [[page 3]])")))
         "OR query with nonexistent page should return meaningful results")
 
-    (is (= ["b1 [[page 1]] #tag2" "foo:: bar\n" "b3"]
+    (is (= ["b1 [[page 1]] #tag2" "foo:: bar" "b3"]
            (->> (dsl-query "(not [[page 2]])")
                 ;; Only filter to page1 to get meaningful results
                 (filter #(= "page1" (get-in % [:block/page :block/name])))
-                (map :block/content)))
+                (map (comp str/trimr :block/content))))
         "NOT query")))
 
 (deftest nested-page-ref-queries

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

@@ -7,7 +7,8 @@
             [clojure.set :as set]
             [frontend.modules.outliner.core :as outliner-core]
             [frontend.db :as db]
-            [datascript.core :as d]))
+            [datascript.core :as d]
+            [logseq.graph-parser.text :as text]))
 
 (defonce test-db (if (some? js/process.env.DB_GRAPH) "logseq_db_test-db" "test-db"))
 
@@ -24,12 +25,35 @@
   (destroy-test-db!)
   (start-test-db!))
 
+(defn- parse-property-value [value]
+  (if-let [refs (seq (map second (re-seq #"\[\[(.*?)\]\]" value)))]
+    (set refs)
+    (if-some [new-val (text/parse-non-string-property-value value)]
+      new-val
+      value)))
+
+(defn- build-blocks
+  "Parses properties and content from a markdown block"
+  [file-content]
+  (if (string/includes? file-content "\n-")
+    (->> (string/split file-content #"\n-\s*")
+         (mapv (fn [s]
+                 (let [[content & props] (string/split-lines s)]
+                   [content (->> props
+                                 (map #(let [[k v] (string/split % #"::\s*" 2)]
+                                         [(keyword k) (parse-property-value v)]))
+                                 (into {}))]))))
+    ;; TODO: page-properties
+    []))
+
 ;; Currently this only works for load-test-files that have added a :file/blocks for each file arg
 (defn- load-test-files-for-db-graph
   [files*]
-  (let [files (mapv #(assoc % :file/content
-                            (string/join "\n"
-                                         (map (fn [x] (str "- " (first x))) (:file/blocks %))))
+  (let [files (mapv #(let [blocks (build-blocks (:file/content %))]
+                       (merge %
+                              {:file/blocks blocks
+                               :file/content (string/join "\n"
+                                                          (map (fn [x] (str "- " (first x))) blocks))}))
                     files*)]
     ;; TODO: Use sqlite instead of file graph to create client db
     (repo-handler/parse-files-and-load-to-db!