chore: add db tests and start a logseq.db testing ns

Add acceptance test for newer validations and some useful unit tests for
untested fns at different layers
pull/11550/head
Gabriel Horner 2024-09-27 14:28:05 -04:00
parent 476e641c1a
commit 2b782c601b
5 changed files with 100 additions and 17 deletions

20
deps/db/src/logseq/db/test/helper.cljs vendored Normal file
View File

@ -0,0 +1,20 @@
(ns ^:node-only logseq.db.test.helper
"Main ns for providing test fns for DB graphs"
(:require [datascript.core :as d]
[logseq.db.sqlite.build :as sqlite-build]
[logseq.db.sqlite.create-graph :as sqlite-create-graph]
[logseq.db.frontend.schema :as db-schema]))
(defn create-conn
"Create a conn for a DB graph seeded with initial data"
[]
(let [conn (d/create-conn db-schema/schema-for-db-based-graph)
_ (d/transact! conn (sqlite-create-graph/build-db-initial-data "{}"))]
conn))
(defn create-conn-with-blocks
"Create a conn with create-db-conn and then create blocks using sqlite-build"
[opts]
(let [conn (create-conn)
_ (sqlite-build/create-blocks conn opts)]
conn))

View File

@ -3,7 +3,8 @@
[logseq.db.frontend.schema :as db-schema]
[datascript.core :as d]
[logseq.db :as ldb]
[logseq.db.sqlite.create-graph :as sqlite-create-graph]))
[logseq.db.sqlite.create-graph :as sqlite-create-graph]
[logseq.db.test.helper :as db-test]))
;;; datoms
@ -53,3 +54,16 @@
(->> (ldb/get-page-parents (ldb/get-page @conn "z") {:node-class? true})
(map :block/title)
set)))))
(deftest get-case-page
(let [conn (db-test/create-conn-with-blocks
{:properties
{:foo {:block/schema {:type :default}}
:Foo {:block/schema {:type :default}}}
:classes {:movie {} :Movie {}}})]
;; Case sensitive properties
(is (= "foo" (:block/title (ldb/get-case-page @conn "foo"))))
(is (= "Foo" (:block/title (ldb/get-case-page @conn "Foo"))))
;; Case sensitive classes
(is (= "movie" (:block/title (ldb/get-case-page @conn "movie"))))
(is (= "Movie" (:block/title (ldb/get-case-page @conn "Movie"))))))

View File

@ -41,6 +41,30 @@
:payload {:message "Built-in pages can't be edited"
:type :warning}}))))
(defn- validate-unique-for-property-page
[entity db new-title]
(when-let [_res (seq (d/q (if (:logseq.property/built-in? entity)
'[:find [?b ...]
:in $ ?eid ?title
:where
[?b :block/title ?title]
[?b :block/type "property"]
[(not= ?b ?eid)]]
'[:find [?b ...]
:in $ ?eid ?title
:where
[?b :block/title ?title]
[?b :block/type "property"]
[(missing? $ ?b :logseq.property/built-in?)]
[(not= ?b ?eid)]])
db
(:db/id entity)
new-title))]
(throw (ex-info "Duplicate property"
{:type :notification
:payload {:message (str "Another property named " (pr-str new-title) " already exists")
:type :warning}}))))
(defn- validate-unique-for-page
[db new-title {:block/keys [tags] :as entity}]
(cond
@ -62,21 +86,7 @@
:type :warning}})))
(ldb/property? entity)
(when-let [_res (seq (d/q '[:find [?b ...]
:in $ ?eid ?type ?title
:where
[?b :block/title ?title]
[?b :block/type ?type]
[(missing? $ ?b :logseq.property/built-in?)]
[(not= ?b ?eid)]]
db
(:db/id entity)
(:block/type entity)
new-title))]
(throw (ex-info "Duplicate property"
{:type :notification
:payload {:message (str "Another property named " (pr-str new-title) " already exists")
:type :warning}})))
(validate-unique-for-property-page entity db new-title)
:else
(when-let [_res (seq (d/q '[:find [?b ...]

View File

@ -16,7 +16,7 @@
(->> content
(d/q '[:find [(pull ?b [*]) ...]
:in $ ?content
:where [?b :block/title ?content]]
:where [?b :block/title ?content] [(missing? $ ?b :logseq.property/built-in?)]]
@conn)
first))
@ -77,3 +77,21 @@
"Apple"
(find-block-by-content conn "Fruit")))
"Allow class to have same name as a page")))
(deftest new-graph-should-be-valid
(let [conn (create-conn-with-blocks {})
pages (d/q '[:find [(pull ?b [*]) ...] :where [?b :block/title] [?b :block/type]] @conn)
validation-errors (atom {})]
(doseq [page pages]
(try
;; Try as many of the relevant validations
(outliner-validate/validate-unique-by-name-tag-and-block-type @conn (:block/title page) page)
(outliner-validate/validate-page-title (:block/title page) {:node page})
(outliner-validate/validate-page-title-characters (:block/title page) {:node page})
(catch :default e
(if (= :notification (:type (ex-data e)))
(swap! validation-errors update (select-keys page [:block/title :db/ident :block/uuid]) (fnil conj []) e)
(throw e)))))
(is (= {} @validation-errors)
"Default pages shouldn't have any validation errors")))

View File

@ -0,0 +1,21 @@
(ns frontend.worker.handler.page.db-based.page-test
(:require [cljs.test :refer [deftest is]]
[datascript.core :as d]
[logseq.db.test.helper :as db-test]
[logseq.db :as ldb]
[frontend.worker.handler.page.db-based.page :as worker-db-page]))
(deftest create-class
(let [conn (db-test/create-conn)
_ (worker-db-page/create! conn "movie" {:class? true})
_ (worker-db-page/create! conn "Movie" {:class? true})
movie-class (->> (d/q '[:find [(pull ?b [*]) ...] :in $ ?title :where [?b :block/title ?title]]
@conn "movie")
first)
Movie-class (->> (d/q '[:find [(pull ?b [*]) ...] :in $ ?title :where [?b :block/title ?title]]
@conn "Movie")
first)]
(is (ldb/class? movie-class) "Creates a class")
(is (ldb/class? Movie-class) "Creates another class with a different case sensitive name")
(is (not= movie-class Movie-class) "The two classes are not the same")))