chore: convert last of frontend tests to db graphs

Now able to remove unused frontend.test.* namespaces.
Also removed file-graph specific handling of old attrs like
:block/ref-pages
This commit is contained in:
Gabriel Horner
2026-01-07 15:15:23 -05:00
parent 96a9f95622
commit 6c876a0be2
30 changed files with 108 additions and 434 deletions

View File

@@ -53,13 +53,4 @@
(:init-db? tx-meta))
(conn/transact! repo tx-data tx-meta))
(ui-outliner-tx/transact! tx-meta
(outliner-op/transact! tx-data tx-meta)))))
(defn set-file-content!
([repo path content]
(set-file-content! repo path content {}))
([repo path content opts]
(when (and repo path)
(let [tx-data {:file/path path
:file/content content}]
(transact! repo [tx-data] opts)))))
(outliner-op/transact! tx-data tx-meta)))))

View File

@@ -10,7 +10,6 @@
[frontend.util.text :as text-util]
[logseq.common.util :as common-util]
[logseq.db :as ldb]
[logseq.db.file-based.schema :as file-schema]
[logseq.db.frontend.schema :as db-schema]
[logseq.graph-parser.text :as text]))
@@ -81,9 +80,7 @@
(start! repo {}))
([repo {:keys [listen-handler]}]
(let [db-name (db-conn-state/get-repo-path repo)
db-conn (if (config/db-based-graph? repo)
(d/create-conn db-schema/schema)
(d/create-conn file-schema/schema))]
db-conn (d/create-conn db-schema/schema)]
(destroy-all!)
(swap! conns assoc db-name db-conn)
(when listen-handler

View File

@@ -66,14 +66,6 @@
(walk/postwalk
(fn [f]
(cond
;; backward compatible
;; 1. replace :page/ => :block/
(and (keyword? f) (= "page" (namespace f)))
(keyword "block" (name f))
(and (keyword? f) (contains? #{:block/ref-pages :block/ref-blocks} f))
:block/refs
(and (list? f)
(= (first f) '=)
(= 3 (count f))

View File

@@ -126,9 +126,6 @@
_ (<ensure-dir! containing-dir)
_ (js/window.pfs.writeFile fpath content)]
;; TODO: store file metadata
;; (db/set-file-content! repo rpath content)
;; (db/set-file-last-modified-at! repo rpath (js/Date.))
))
(rename! [_this _repo old-path new-path]
(let [old-path (path/url-to-path old-path)

View File

@@ -21,11 +21,6 @@
[]
(path/path-join @root-dir "config"))
(defn safe-global-config-dir
"Fetch config dir in a general context, not just for global config"
[]
(when @root-dir (global-config-dir)))
(defn global-config-path
"Fetch config path in a global config context"
[]

View File

@@ -1978,13 +1978,6 @@ Similar to re-frame subscriptions"
(defn get-auth-refresh-token []
(:auth/refresh-token @state))
(defn set-parsing-state!
"Leave for tests"
[m]
(update-state! [:graph/parsing-state (get-current-repo)]
(if (fn? m) m
(fn [old-value] (merge old-value m)))))
(defn http-proxy-enabled-or-val? []
(when-let [{:keys [type protocol host port] :as agent-opts} (sub [:electron/user-cfgs :settings/agent])]
(when (and (not (contains? #{"system"} type))

View File

@@ -7,12 +7,12 @@
[logseq.db :as ldb]
[logseq.db.frontend.class :as db-class]))
(def repo test-helper/test-db-name-db-version)
(def repo test-helper/test-db)
(def init-data (test-helper/initial-test-page-and-blocks))
(defn start-and-destroy-db
[f]
(test-helper/db-based-start-and-destroy-db
(test-helper/start-and-destroy-db
f
{:init-data (fn [conn] (d/transact! conn init-data))}))

View File

@@ -6,10 +6,10 @@
[frontend.db.model :as model]
[frontend.test.helper :as test-helper :refer [load-test-files]]))
(use-fixtures :each {:before #(test-helper/start-test-db! {:db-graph? true})
(use-fixtures :each {:before test-helper/start-test-db!
:after test-helper/destroy-test-db!})
(def test-db test-helper/test-db-name-db-version)
(def test-db test-helper/test-db)
(deftest test-page-alias-with-multiple-alias
(let [ab-uuid (random-uuid)

View File

@@ -4,7 +4,7 @@
[frontend.db :as db]
[frontend.db.query-dsl :as query-dsl]
[frontend.db.react :as react]
[frontend.test.helper :as test-helper :include-macros true :refer [load-test-files load-test-files-for-db-graph]]
[frontend.test.helper :as test-helper :include-macros true :refer [load-test-files]]
[frontend.util :as util]))
;; TODO: quickcheck
@@ -12,7 +12,7 @@
;; 2. find illegal queries which can't be executed by datascript
;; 3. find filters combinations which might break the current query implementation
(use-fixtures :each {:before #(test-helper/start-test-db! {:db-graph? true})
(use-fixtures :each {:before test-helper/start-test-db!
:after test-helper/destroy-test-db!})
;; Test helpers
@@ -65,14 +65,14 @@
(defn- dsl-query
[s]
(react/clear-query-state!)
(when-let [result (dsl-query* test-helper/test-db-name-db-version s)]
(when-let [result (dsl-query* test-helper/test-db s)]
(map first (deref result))))
(defn- custom-query
[query]
(react/clear-query-state!)
(when-let [result (with-redefs [query-dsl/db-block-attrs db-block-attrs]
(query-dsl/custom-query test-helper/test-db-name-db-version query {}))]
(query-dsl/custom-query test-helper/test-db query {}))]
(map first (deref result))))
;; Tests
@@ -177,7 +177,7 @@
(block-property-queries-test))))
(deftest db-only-block-property-queries
(load-test-files-for-db-graph
(load-test-files
{:properties
{:zzz {:logseq.property/type :default
:block/title "zzz name!"}}
@@ -202,7 +202,7 @@
(when (not js/process.env.DB_QUERY_TYPE)
(deftest property-default-type-default-value-queries
(load-test-files-for-db-graph
(load-test-files
{:properties
{:default {:logseq.property/type :default
:build/properties
@@ -229,7 +229,7 @@
"Blocks with :default property value and not tagged with a tag that has that default-value property value"))
(deftest property-checkbox-type-default-value-queries
(load-test-files-for-db-graph
(load-test-files
{:properties
{:checkbox {:logseq.property/type :checkbox
:build/properties
@@ -255,7 +255,7 @@
"Blocks with :checkbox property value and not tagged with a tag that has that default-value property value"))
(deftest closed-property-default-value-queries
(load-test-files-for-db-graph
(load-test-files
{:properties
{:status {:logseq.property/type :default
:build/closed-values

View File

@@ -1,10 +1,8 @@
(ns frontend.db.query-react-test
(:require [cljs-time.core :as t]
[cljs.test :refer [deftest is use-fixtures]]
[clojure.string :as string]
[frontend.db.query-custom :as query-custom]
[frontend.test.helper :as test-helper :refer [load-test-files]]
[goog.string :as gstring]
[logseq.db.frontend.inputs :as db-inputs]))
(use-fixtures :each {:before test-helper/start-test-db!
@@ -19,7 +17,7 @@ adds rules that users often use"
(defn- blocks-created-between-inputs [a b]
(sort
(map #(-> % :block/title string/split-lines first)
(map :block/title
(custom-query {:inputs [a b]
:query '[:find (pull ?b [*])
:in $ ?start ?end
@@ -28,6 +26,7 @@ adds rules that users often use"
[?b :block/parent ?p]
[?b :block/title]
[?b :block/created-at ?timestamp]
[(missing? $ ?b :logseq.property/built-in?)]
[(>= ?timestamp ?start)]
[(<= ?timestamp ?end)]]}))))
@@ -38,46 +37,34 @@ adds rules that users often use"
:in $ ?current-page ?tag-name
:where [?b :block/page ?bp]
[?bp :block/name ?current-page]
[?b :block/ref-pages ?t]
[?b :block/refs ?t]
[?t :block/name ?tag-name]]}
{:current-page-fn (constantly current-page)})))
;; These tests rely on seeding timestamps with properties. If this ability goes
;; away we could still test page-level timestamps
;; TODO: Move this test to inputs-test
(deftest resolve-input-for-timestamp-inputs
(load-test-files [{:file/path "pages/page1.md"
:file/content (gstring/format "foo::bar
- -1y
created-at:: %s
- -1m
created-at:: %s
- -1w
created-at:: %s
- -1d
created-at:: %s
- today
created-at:: %s
- tonight
created-at:: %s
- +1d
created-at:: %s
- +1w
created-at:: %s
- +1m
created-at:: %s
- +1y
created-at:: %s"
(db-inputs/date-at-local-ms (t/minus (t/today) (t/years 1)) 0 0 0 0)
(db-inputs/date-at-local-ms (t/minus (t/today) (t/months 1)) 0 0 0 0)
(db-inputs/date-at-local-ms (t/minus (t/today) (t/weeks 1)) 0 0 0 0)
(db-inputs/date-at-local-ms (t/minus (t/today) (t/days 1)) 0 0 0 0)
(db-inputs/date-at-local-ms (t/today) 12 0 0 0)
(db-inputs/date-at-local-ms (t/today) 18 0 0 0)
(db-inputs/date-at-local-ms (t/plus (t/today) (t/days 1)) 0 0 0 0)
(db-inputs/date-at-local-ms (t/plus (t/today) (t/weeks 1)) 0 0 0 0)
(db-inputs/date-at-local-ms (t/plus (t/today) (t/months 1)) 0 0 0 0)
(db-inputs/date-at-local-ms (t/plus (t/today) (t/years 1)) 0 0 0 0))}])
(load-test-files
[{:page {:block/title "page1"}
:blocks [{:block/title "-1y"
:block/created-at (db-inputs/date-at-local-ms (t/minus (t/today) (t/years 1)) 0 0 0 0)}
{:block/title "-1m"
:block/created-at (db-inputs/date-at-local-ms (t/minus (t/today) (t/months 1)) 0 0 0 0)}
{:block/title "-1w"
:block/created-at (db-inputs/date-at-local-ms (t/minus (t/today) (t/weeks 1)) 0 0 0 0)}
{:block/title "-1d"
:block/created-at (db-inputs/date-at-local-ms (t/minus (t/today) (t/days 1)) 0 0 0 0)}
{:block/title "today"
:block/created-at (db-inputs/date-at-local-ms (t/today) 12 0 0 0)}
{:block/title "tonight"
:block/created-at (db-inputs/date-at-local-ms (t/today) 18 0 0 0)}
{:block/title "+1d"
:block/created-at (db-inputs/date-at-local-ms (t/plus (t/today) (t/days 1)) 0 0 0 0)}
{:block/title "+1w"
:block/created-at (db-inputs/date-at-local-ms (t/plus (t/today) (t/weeks 1)) 0 0 0 0)}
{:block/title "+1m"
:block/created-at (db-inputs/date-at-local-ms (t/plus (t/today) (t/months 1)) 0 0 0 0)}
{:block/title "+1y"
:block/created-at (db-inputs/date-at-local-ms (t/plus (t/today) (t/years 1)) 0 0 0 0)}]}])
(is (= ["today" "tonight"] (blocks-created-between-inputs :-0d-ms :+0d-ms))
":+0d-ms and :-0d-ms resolve to correct datetime range")
@@ -134,9 +121,12 @@ created-at:: %s"
":-XT-HHMM and :+XT-HHMM will not reoslve with invalid time formats but will fail gracefully"))
(deftest cache-input-for-page-inputs
(load-test-files [{:file/path "pages/a.md" :file/content "- a #shared-tag"}
{:file/path "pages/b.md" :file/content "- b #shared-tag"}])
(load-test-files [{:page {:block/title "a"}
:blocks [{:block/title "a [[shared-tag]]"
:build/tags [:shared-tag]}]}
{:page {:block/title "b"}
:blocks [{:block/title "b [[shared-tag]]"
:build/tags [:shared-tag]}]}])
(is (not= (blocks-with-tag-on-specified-current-page :current-page "a" :tag "shared-tag")
(blocks-with-tag-on-specified-current-page :current-page "b" :tag "shared-tag")
[])

View File

@@ -6,7 +6,7 @@
[shadow.resource :as rc]))
(def test-transit (rc/inline "fixtures/references.transit"))
;; (use-fixtures :each test-helper/db-based-start-and-destroy-db)
;; (use-fixtures :each test-helper/start-and-destroy-db)
(defn- create-conn!
[]

View File

@@ -8,7 +8,7 @@
(def init-data (test-helper/initial-test-page-and-blocks))
(defn start-and-destroy-db
[f]
(test-helper/db-based-start-and-destroy-db
(test-helper/start-and-destroy-db
f
{:init-data (fn [conn] (d/transact! conn init-data))}))

View File

@@ -13,7 +13,7 @@
(use-fixtures :each
{:before (fn []
(async done
(test-helper/start-test-db! {:db-graph? true})
(test-helper/start-test-db!)
(done)))
:after test-helper/destroy-test-db!})
@@ -45,7 +45,7 @@
{:id block-dom-id
:block-uuid (:block/uuid block)}]))]
(p/do!
(editor/delete-block! test-helper/test-db-name-db-version)
(editor/delete-block! test-helper/test-db)
(when (fn? on-delete)
(on-delete))))))
@@ -57,7 +57,7 @@
[{:block/title "b1"}
{:block/title "b2"}
{:block/title ""}]}])
(p/let [conn (db/get-db test-helper/test-db-name-db-version false)
(p/let [conn (db/get-db test-helper/test-db false)
block (->> (d/q '[:find (pull ?b [*])
:where [?b :block/title ""]
[?p :block/name "page1"]
@@ -78,7 +78,7 @@
(testing "backspace deletes empty block in embedded context"
;; testing embed at this layer doesn't require an embed block since
;; delete-block handles all the embed setup
(p/let [conn (db/get-db test-helper/test-db-name-db-version false)
(p/let [conn (db/get-db test-helper/test-db false)
block (->> (d/q '[:find (pull ?b [*])
:where [?b :block/title ""]
[?p :block/name "page1"]

View File

@@ -7,7 +7,7 @@
[frontend.test.helper :as test-helper]
[frontend.util.cursor :as cursor]))
(use-fixtures :each #(test-helper/start-and-destroy-db % {:db-graph? true}))
(use-fixtures :each test-helper/start-and-destroy-db)
(deftest extract-nearest-link-from-text-test
(testing "Page, block and tag links"
@@ -199,7 +199,7 @@
(test-helper/load-test-files [{:page {:block/title "foo"}
:blocks [{:block/title "foo"
:build/properties {:logseq.property/heading 1}}]}])
(let [repo test-helper/test-db-name-db-version
(let [repo test-helper/test-db
page-uuid (:block/uuid (db/get-page "foo"))
block-uuid (:block/uuid (model/get-block-by-page-name-and-block-route-name repo (str page-uuid) "foo"))]
(editor/save-block! repo block-uuid "# bar")

View File

@@ -41,7 +41,7 @@
(use-fixtures :once
{:before (fn []
(async done
(test-helper/start-test-db! {:db-graph? true})
(test-helper/start-test-db!)
(p/let [_ (test-helper/load-test-files test-files)]
(done))))
:after test-helper/destroy-test-db!})

View File

@@ -4,7 +4,7 @@
[frontend.db.utils :as db-utils]
[clojure.test :refer [deftest is use-fixtures testing]]))
(use-fixtures :each {:before #(test-helper/start-test-db! {:db-graph? true})
(use-fixtures :each {:before test-helper/start-test-db!
:after test-helper/destroy-test-db!})
(deftest default-page-route

View File

@@ -9,7 +9,6 @@
[frontend.db.model :as db-model]
[frontend.modules.outliner.tree :as tree]
[frontend.state :as state]
[frontend.test.fixtures :as fixtures]
[frontend.test.helper :as test-helper]
[frontend.worker.db-listener :as worker-db-listener]
[logseq.db :as ldb]
@@ -37,8 +36,8 @@
(use-fixtures :each
disable-browser-fns
fixtures/react-components
fixtures/reset-db
test-helper/react-components
#(test-helper/start-and-destroy-db % {:build-init-data? false})
listen-db-fixture)
(defn get-block
@@ -789,14 +788,4 @@
:block/uuid #uuid "62f4b8c6-072e-4133-90e2-0591021a7fea",
:block/parent #:db{:id 2333},
:block/page #:db{:id 2313},
:block/level 3}]}]})))))
(comment
(dotimes [i 5]
(do
(frontend.test.fixtures/reset-datascript test-db)
(cljs.test/run-tests)))
(do
(frontend.test.fixtures/reset-datascript test-db)
(cljs.test/test-vars [#'test-paste-first-empty-block])))
:block/level 3}]}]})))))

View File

@@ -8,7 +8,7 @@
[logseq.db.test.helper :as db-test]
[logseq.outliner.core :as outliner-core]))
(use-fixtures :each test-helper/db-based-start-and-destroy-db)
(use-fixtures :each test-helper/start-and-destroy-db)
(deftest save-inline-tag
(let [conn (db-test/create-conn-with-blocks

View File

@@ -1,89 +0,0 @@
(ns frontend.test.file
"Provides util handler fns for file graph files"
(:refer-clojure :exclude [load-file])
(:require [datascript.core :as d]
[frontend.db :as db]
[frontend.db.conn :as conn]
[frontend.db.utils :as db-utils]
[frontend.handler.common.config-edn :as config-edn-common-handler]
[frontend.handler.global-config :as global-config-handler]
[frontend.schema.handler.global-config :as global-config-schema]
[frontend.schema.handler.repo-config :as repo-config-schema]
[frontend.state :as state]
[frontend.test.file.reset :as file-reset]
[frontend.util :as util]
[logseq.common.path :as path]
[logseq.common.util :as common-util]))
(defn- get-file-page-id
[file-path]
(when-let [repo (state/get-current-repo)]
(when-let [db (conn/get-db repo)]
(some->
(d/q
'[:find ?page
:in $ ?path
:where
[?file :file/path ?path]
[?page :block/name]
[?page :block/file ?file]]
db file-path)
db-utils/seq-flatten
first))))
(defn reset-file!
[repo file-path content opts]
(when util/node-test?
(file-reset/reset-file! repo (db/get-db repo false) file-path content opts)))
(defn- detect-deprecations
[path content]
(when (or (= path "logseq/config.edn")
(= (path/dirname path) (global-config-handler/safe-global-config-dir)))
(config-edn-common-handler/detect-deprecations path content {:db-graph? false})))
(defn- validate-file
"Returns true if valid and if false validator displays error message. Files
that are not validated just return true"
[path content]
(cond
(= path "logseq/config.edn")
(config-edn-common-handler/validate-config-edn path content repo-config-schema/Config-edn)
(= (path/dirname path) (global-config-handler/safe-global-config-dir))
(config-edn-common-handler/validate-config-edn path content global-config-schema/Config-edn)
:else
true))
(defn alter-file-test-version
"Test version of alter-file that is synchronous"
[repo path content {:keys [reset? from-disk? new-graph? verbose
ctime mtime]
:fs/keys [event]
:or {reset? true
from-disk? false}}]
(let [path (common-util/path-normalize path)
config-file? (= path "logseq/config.edn")
_ (when config-file?
(detect-deprecations path content))
config-valid? (and config-file? (validate-file path content))]
(when (or config-valid? (not config-file?)) ; non-config file or valid config
(let [opts {:new-graph? new-graph?
:from-disk? from-disk?
:fs/event event
:ctime ctime
:mtime mtime}
result (if reset?
(do
(when-let [page-id (get-file-page-id path)]
(db/transact! repo
[[:db/retract page-id :block/alias]
[:db/retract page-id :block/tags]]
opts))
(reset-file!
repo path content (merge opts
;; To avoid skipping the `:or` bounds for keyword destructuring
(when (some? verbose) {:verbose verbose}))))
(db/set-file-content! repo path content opts))]
result))))

View File

@@ -1,83 +0,0 @@
(ns frontend.test.file.reset
"Fns for resetting a db file with parsed file content"
(:require [clojure.string :as string]
[datascript.core :as d]
[frontend.worker-common.util :as worker-util]
[frontend.worker.state :as worker-state]
[logseq.common.config :as common-config]
[logseq.common.util :as common-util]
[logseq.db :as ldb]
[logseq.graph-parser :as graph-parser]))
(defn get-page-file
[db page-name]
(some-> (ldb/get-page db page-name)
:block/file))
(defn- page-exists-in-another-file
"Conflict of files towards same page"
[db page file]
(when-let [page-name (:block/name page)]
(let [current-file (:file/path (get-page-file db page-name))]
(when (not= file current-file)
current-file))))
(defn- validate-existing-file
"Handle the case when the file is already exists in db
Likely caused by renaming between caps and non-caps, then cause file system
bugs on some OS
e.g. on macOS, it doesn't fire the file change event when renaming between
caps and non-caps"
[repo conn file-page file-path]
(when-let [current-file-path (page-exists-in-another-file @conn file-page file-path)]
(when (not= file-path current-file-path)
(cond
;; TODO: handle case sensitive file system
(= (common-util/path-normalize (string/lower-case current-file-path))
(common-util/path-normalize (string/lower-case file-path)))
;; case renamed
(when-let [file (d/entity @conn [:file/path current-file-path])]
(worker-util/post-message :backup-file
[repo current-file-path (:file/content file)])
(ldb/transact! conn [{:db/id (:db/id file)
:file/path file-path}]))
:else
(worker-util/post-message :notify-existing-file
[{:current-file-path current-file-path
:file-path file-path}])))))
(defn- validate-and-get-blocks-to-delete
"An implementation for the delete-blocks-fn in graph-parser/parse-file"
[repo conn file-page file-path retain-uuid-blocks]
(validate-existing-file repo conn file-page file-path)
(graph-parser/get-blocks-to-delete @conn file-page file-path retain-uuid-blocks))
(defn- reset-file!*
"Parse file.
Decide how to treat the parsed file based on the file's triggering event
options -
:fs/reset-event - the event that triggered the file update
:fs/local-file-change - file changed on local disk
:fs/remote-file-change - file changed on remote"
[db-conn file-path content options]
(graph-parser/parse-file db-conn file-path content options))
(defn reset-file!
"Main fn for updating a db with the results of a parsed file"
([repo conn file-path content]
(reset-file! repo conn file-path content {}))
([repo conn file-path content {:keys [verbose _ctime _mtime] :as options}]
(let [config (worker-state/get-config repo)
options (merge (dissoc options :verbose)
{:delete-blocks-fn (partial validate-and-get-blocks-to-delete repo conn)
;; Options here should also be present in gp-cli/parse-graph
:extract-options (merge
{:user-config config
:date-formatter (worker-state/get-date-formatter repo)
:block-pattern (common-config/get-block-pattern
(or (common-util/get-format file-path) :markdown))
:filename-format (:file/name-format config)}
;; To avoid skipping the `:or` bounds for keyword destructuring
(when (some? verbose) {:verbose verbose}))})]
(:tx (reset-file!* conn file-path content options)))))

View File

@@ -1,28 +0,0 @@
(ns frontend.test.fixtures
(:require [datascript.core :as d]
[frontend.db.conn :as conn]
[frontend.db.react :as react]
[frontend.state :as state]
[frontend.test.helper :as test-helper]
[logseq.db.file-based.schema :as file-schema]))
(defn react-components
[f]
(reset! react/*query-state {})
(let [r (f)]
(reset! react/*query-state {})
r))
(defn- reset-datascript
[repo]
(let [db-name (conn/get-repo-path repo)
db-conn (d/create-conn file-schema/schema)]
(state/set-current-repo! repo)
(swap! conn/conns assoc db-name db-conn)))
(defn reset-db
[f]
(let [repo test-helper/test-db]
(reset-datascript repo)
(let [r (f)]
(reset-datascript repo) r)))

View File

@@ -6,10 +6,10 @@
[frontend.config :as config]
[frontend.db :as db]
[frontend.db.conn :as conn]
[frontend.db.react :as react]
[frontend.handler.db-based.property :as db-property-handler]
[frontend.handler.editor :as editor-handler]
[frontend.state :as state]
[frontend.test.repo :as file-repo-handler]
[frontend.worker.handler.page :as worker-page]
[frontend.worker.pipeline :as worker-pipeline]
[logseq.db :as ldb]
@@ -19,31 +19,30 @@
[logseq.db.sqlite.util :as sqlite-util]
[logseq.db.test.helper :as db-test]))
(defn react-components
[f]
(reset! react/*query-state {})
(let [r (f)]
(reset! react/*query-state {})
r))
(def bare-marker-pattern
#"(TODO|DOING|DONE|WAIT|CANCELED|CANCELLED){1}\s+")
(def node? (exists? js/process))
(def test-db-name "test-db")
(def test-db-name-db-version "logseq_db_test-db")
(def test-db
(if (and node? (some? js/process.env.DB_GRAPH)) test-db-name-db-version test-db-name))
(def test-db "logseq_db_test-db")
(defn start-test-db!
[& {:as opts}]
(let [db-graph? (or (:db-graph? opts) (and node? (some? js/process.env.DB_GRAPH)))
test-db' (if db-graph? test-db-name-db-version test-db-name)]
(state/set-current-repo! test-db')
(conn/start! test-db' opts)
(ldb/register-transact-pipeline-fn!
(fn [tx-report]
(worker-pipeline/transact-pipeline test-db' tx-report)))
(let [conn (conn/get-db test-db' false)]
(when db-graph?
(d/transact! conn (sqlite-create-graph/build-db-initial-data "")))
(d/listen! conn ::listen-db-changes!
(fn [tx-report]
(worker-pipeline/invoke-hooks conn tx-report {}))))))
[& {:keys [build-init-data?] :or {build-init-data? true} :as opts}]
(state/set-current-repo! test-db)
(conn/start! test-db opts)
(ldb/register-transact-pipeline-fn!
(fn [tx-report]
(worker-pipeline/transact-pipeline test-db tx-report)))
(let [conn (conn/get-db test-db false)]
(when build-init-data? (d/transact! conn (sqlite-create-graph/build-db-initial-data config/config-default-content)))
(d/listen! conn ::listen-db-changes!
(fn [tx-report]
(worker-pipeline/invoke-hooks conn tx-report {})))))
(defn destroy-test-db!
[]
@@ -68,8 +67,9 @@
(update :build/properties merge {:logseq.property/status status}))
block))
(defn load-test-files-for-db-graph
"Wrapper around sqlite-build/build-blocks-tx with frontend defaults. Also supports
(defn load-test-files
"Builds the given data into the current test-db.
Wrapper around sqlite-build/build-blocks-tx with frontend defaults. Also supports
the following special keys:
* :build.test/title - Only available to top-level blocks. Convenient for writing tasks quickly"
[options*]
@@ -90,19 +90,7 @@
;; 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 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? (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
files)))
(db/transact! test-db (concat init-index init-tx block-props-tx))))
(defn initial-test-page-and-blocks
[& {:keys [page-uuid]}]
@@ -136,30 +124,19 @@ This can be called in synchronous contexts as no async fns should be invoked"
also seeds the db with the same default data that the app does and destroys a db
connection when done with it."
[f & {:as start-opts}]
;; Set current-repo explicitly since it's not the default
(let [db-graph? (or (:db-graph? start-opts) (and node? (some? js/process.env.DB_GRAPH)))
repo (if db-graph? test-db-name-db-version test-db-name)]
(state/set-current-repo! repo)
(start-test-db! start-opts)
(when db-graph?
(let [built-in-data (sqlite-create-graph/build-db-initial-data
config/config-default-content)]
(db/transact! repo built-in-data)))
(when-let [init-f (:init-data start-opts)]
(assert (fn? f) "init-data should be a fn")
(init-f (db/get-db repo false)))
(f)
(state/set-current-repo! nil)
(destroy-test-db!)))
(state/set-current-repo! test-db)
(start-test-db! start-opts)
(when-let [init-f (:init-data start-opts)]
(assert (fn? f) "init-data should be a fn")
(init-f (db/get-db test-db false)))
(f)
(state/set-current-repo! nil)
(destroy-test-db!))
(defn db-based-start-and-destroy-db
[f & {:as start-opts}]
(start-and-destroy-db f (assoc start-opts :db-graph? true)))
(def db-based-start-and-destroy-db-map-fixture
(def start-and-destroy-db-map-fixture
"To avoid 'Fixtures may not be of mixed types' error
when use together with other map-type fixtures"
{:before #(start-test-db! {:db-graph? true})
{:before start-test-db!
:after #(destroy-test-db!)})
(defn save-block!

View File

@@ -1,46 +0,0 @@
(ns frontend.test.repo
"Repo fns for creating, loading and parsing file graphs"
(:require [frontend.state :as state]
[frontend.test.file :as file-handler]
[logseq.graph-parser :as graph-parser]))
(defn- parse-and-load-file-test-version!
"Accept: .md, .org, .edn, .css"
[repo-url file {:keys [new-graph? verbose]}]
(try
(let [result (file-handler/alter-file-test-version
repo-url
(:file/path file)
(:file/content file)
(merge (:stat file)
{:new-graph? new-graph?
:re-render-root? false
:from-disk? true}
;; To avoid skipping the `:or` bounds for keyword destructuring
(when (some? verbose) {:verbose verbose})))]
(state/set-parsing-state! (fn [m]
(update m :finished inc)))
result)
(catch :default e
(println "Parse and load file failed: " (str (:file/path file)))
(js/console.error e)
(state/set-parsing-state! (fn [m]
(update m :failed-parsing-files conj [(:file/path file) e])))
(state/set-parsing-state! (fn [m]
(update m :finished inc))))))
(defn- parse-files-and-create-default-files-inner!
[repo-url files]
(let [supported-files (graph-parser/filter-files files)]
(state/set-current-repo! repo-url)
(state/set-parsing-state! {:total (count supported-files)})
;; Synchronous for tests for not breaking anything
(doseq [file supported-files]
(state/set-parsing-state! (fn [m]
(assoc m
:current-parsing-file (:file/path file))))
(parse-and-load-file-test-version! repo-url file {}))))
(defn parse-files-and-load-to-db!
[repo-url files]
(parse-files-and-create-default-files-inner! repo-url files))

View File

@@ -4,7 +4,6 @@
[frontend.db :as db]
[frontend.modules.outliner.core-test :as outliner-test]
[frontend.state :as state]
[frontend.test.fixtures :as fixtures]
[frontend.test.helper :as test-helper]
[frontend.undo-redo :as undo-redo]
[frontend.worker.db-listener :as worker-db-listener]))
@@ -35,8 +34,8 @@
(use-fixtures :each
disable-browser-fns
fixtures/react-components
fixtures/reset-db
test-helper/react-components
#(test-helper/start-and-destroy-db % {:build-init-data? false})
listen-db-fixture)
(defn- undo-all!

View File

@@ -7,9 +7,9 @@
(defn listen-test-db-fixture
[handler-keys]
(fn [f]
(let [test-db-conn (conn/get-db test-helper/test-db-name-db-version false)]
(let [test-db-conn (conn/get-db test-helper/test-db false)]
(assert (some? test-db-conn))
(worker-db-listener/listen-db-changes! test-helper/test-db-name-db-version test-db-conn
(worker-db-listener/listen-db-changes! test-helper/test-db test-db-conn
{:handler-keys handler-keys})
(f)
@@ -20,7 +20,7 @@
(defn listen-test-db-to-write-tx-log-json-file
"Write {:tx-log <tx-data-coll> :init-db <init-db>} to file 'tx-log-<index>.json'"
[f]
(let [test-db-conn (conn/get-db test-helper/test-db-name-db-version false)
(let [test-db-conn (conn/get-db test-helper/test-db false)
init-db @test-db-conn
*tx-log (atom [])]
(d/listen! test-db-conn :collect-tx-data

View File

@@ -42,7 +42,7 @@
(set (#'worker-pipeline/remove-conflict-datoms datoms)))))))
(deftest test-built-in-page-updates-that-should-be-reverted
(let [graph test-helper/test-db-name-db-version
(let [graph test-helper/test-db
conn (db-test/create-conn-with-blocks
[{:page {:block/title "page1"}
:blocks [{:block/title "b1"}
@@ -95,7 +95,7 @@
(ldb/register-transact-pipeline-fn! identity)))
(deftest ensure-query-property-on-tag-additions-test
(let [graph test-helper/test-db-name-db-version
(let [graph test-helper/test-db
conn (db-test/create-conn-with-blocks
{:pages-and-blocks [{:page {:block/title "page1"}
:blocks [{:block/title "b1"}

View File

@@ -9,13 +9,13 @@
(def listen-test-db-to-gen-rtc-ops-fixture
{:before
#(let [test-db-conn (conn/get-db test-helper/test-db-name-db-version false)]
#(let [test-db-conn (conn/get-db test-helper/test-db false)]
(assert (some? test-db-conn))
(worker-db-listener/listen-db-changes! test-helper/test-db-name-db-version test-db-conn
(worker-db-listener/listen-db-changes! test-helper/test-db test-db-conn
{:handler-keys [:gen-rtc-ops]})
(swap! worker-state/*client-ops-conns
assoc test-helper/test-db-name-db-version (d/create-conn client-op/schema-in-db)))
assoc test-helper/test-db (d/create-conn client-op/schema-in-db)))
:after
#(when-let [test-db-conn (conn/get-db test-helper/test-db-name-db-version false)]
#(when-let [test-db-conn (conn/get-db test-helper/test-db false)]
(d/unlisten! test-db-conn :frontend.worker.db-listener/listen-db-changes!)
(swap! worker-state/*client-ops-conns dissoc test-helper/test-db-name-db-version))})
(swap! worker-state/*client-ops-conns dissoc test-helper/test-db))})

View File

@@ -15,7 +15,7 @@
[meander.epsilon :as me]))
(t/use-fixtures :each
test-helper/db-based-start-and-destroy-db-map-fixture
test-helper/start-and-destroy-db-map-fixture
r.fixture/listen-test-db-to-gen-rtc-ops-fixture)
(defn- tx-data=>e->a->add?->v->t

View File

@@ -12,7 +12,7 @@
[logseq.outliner.transaction :as outliner-tx]))
(use-fixtures :each
test-helper/db-based-start-and-destroy-db
test-helper/start-and-destroy-db
(worker-fixtures/listen-test-db-fixture [:sync-db-to-main-thread]))
(deftest ^:large-vars/cleanup-todo update-remote-data-by-local-unpushed-ops-test

View File

@@ -6,7 +6,7 @@
[frontend.test.helper :as test-helper]
[logseq.api.block :as api-block]))
(use-fixtures :each {:before test-helper/start-test-db!
(use-fixtures :each {:before #(test-helper/start-test-db! {:build-init-data? false})
:after test-helper/destroy-test-db!})
(deftest get-block