From efd53b45912af1f8ac64b95ce2a11ab2f03a2c66 Mon Sep 17 00:00:00 2001 From: Gabriel Horner Date: Mon, 2 May 2022 16:44:40 -0400 Subject: [PATCH] Basic test for parsing docs graph --- .gitignore | 2 + src/test/frontend/handler/repo_test.cljs | 61 ++++++++++++++++++++++++ 2 files changed, 63 insertions(+) create mode 100644 src/test/frontend/handler/repo_test.cljs diff --git a/.gitignore b/.gitignore index d15a753453..e13619356f 100644 --- a/.gitignore +++ b/.gitignore @@ -42,3 +42,5 @@ android/app/src/main/assets/capacitor.plugin.json ios/App/App/capacitor.config.json startup.png + +/src/test/docs diff --git a/src/test/frontend/handler/repo_test.cljs b/src/test/frontend/handler/repo_test.cljs new file mode 100644 index 0000000000..eb21e0c6a5 --- /dev/null +++ b/src/test/frontend/handler/repo_test.cljs @@ -0,0 +1,61 @@ +(ns frontend.handler.repo-test + (:require [cljs.test :refer [deftest use-fixtures is]] + [clojure.string :as string] + ["fs" :as fs] + ["child_process" :as child-process] + [frontend.handler.repo :as repo-handler] + [frontend.test.helper :as test-helper] + [datascript.core :as d] + [frontend.db.conn :as conn] + [frontend.db.utils :as db-utils])) + +(use-fixtures :each {:before test-helper/start-test-db! + :after test-helper/destroy-test-db!}) + +(defn- slurp + "Like clojure.core/slurp" + [file] + (str (fs/readFileSync file))) + +(defn- sh + "Run shell cmd synchronously and print to inherited streams by default. Aims + to be similar to babashka.tasks/shell" + [cmd opts] + (child-process/spawnSync (first cmd) + (clj->js (rest cmd)) + (clj->js (merge {:stdio "inherit"} opts)))) + +(defn- build-graph-files + [dir] + (let [files (->> (str (.-stdout (sh ["git" "ls-files"] + {:cwd dir :stdio nil}))) + string/split-lines + (filter #(re-find #"^(pages|journals)" %)) + (map #(str dir "/" %)))] + (mapv #(hash-map :file/path % :file/content (slurp %)) files))) + +(defn- clone-docs-repo-if-not-exists + [dir] + (when-not (.existsSync fs dir) + (sh ["git" "clone" "--depth" "1" "-b" "v0.6.7" "-c" "advice.detachedHead=false" + "https://github.com/logseq/docs" dir] {}))) + +;; Integration test that test parsing a large graph like docs +(deftest ^:integration parse-and-load-files-to-db + (let [graph-dir "src/test/docs" + _ (clone-docs-repo-if-not-exists graph-dir) + files (build-graph-files graph-dir)] + (repo-handler/parse-files-and-load-to-db! test-helper/test-db files {:re-render? false}) + (let [db-pages (map first (db-utils/q '[:find (pull ?b [* {:block/file [:file/path]}]) :where [?b :block/name] [?b :block/file]]))] + (is (= 206 (count files)) "Correct file count") + (is (= 40888 (count (d/datoms (conn/get-db test-helper/test-db) :eavt))) + "Correct datoms count") + + (is (= (set (map :file/path files)) + (set (map #(get-in % [:block/file :file/path]) db-pages))) + "Journal and pages files on disk should equal ones in db") + (is (= #{"term" "setting" "book" "templates" "Query" "Query/table" "page"} + (->> (db-utils/q '[:find (pull ?n [*]) :where [?b :block/namespace ?n]]) + (map (comp :block/original-name first)) + set)) + "Has correct namespaces"))))