Add db-based model tests

This commit is contained in:
Tienson Qin
2023-12-04 20:07:52 +08:00
parent aff14668fc
commit 00b11d6cb0
6 changed files with 178 additions and 57 deletions

View File

@@ -9,7 +9,8 @@
[logseq.graph-parser.text :as text]
[logseq.db :as ldb]
[logseq.db.frontend.schema :as db-schema]
[logseq.graph-parser.util :as gp-util]))
[logseq.graph-parser.util :as gp-util]
[datascript.core :as d]))
(defonce conns (atom {}))
@@ -84,6 +85,12 @@
[repo]
(swap! conns dissoc (datascript-db repo)))
(defn kv
[key value]
{:db/id -1
:db/ident key
key value})
(defn start!
([repo]
(start! repo {}))
@@ -91,6 +98,9 @@
(let [db-name (datascript-db repo)
db-conn (ldb/start-conn :schema (get-schema repo) :create-default-pages? false)]
(swap! conns assoc db-name db-conn)
(when db-graph?
(d/transact! db-conn [(kv :db/type "db")])
(d/transact! db-conn [(kv :schema/version db-schema/version)]))
(when listen-handler
(listen-handler repo))
(ldb/create-default-pages! db-conn {:db-graph? db-graph?}))))

View File

@@ -79,7 +79,7 @@
(defn get-tag-blocks
[repo tag-name]
(d/q '[:find ?b
(d/q '[:find [?b ...]
:in $ ?tag
:where
[?e :block/name ?tag]
@@ -1237,38 +1237,38 @@ independent of format as format specific heading characters are stripped"
"Returns all property values of a given property for use in a simple query.
Property values that are references are displayed as page references"
[repo property]
(->> (d/q
'[:find ?prop-type ?v
:in $ ?prop-name
:where
[?b :block/properties ?bp]
[?prop-b :block/name ?prop-name]
[?prop-b :block/uuid ?prop-uuid]
[?prop-b :block/schema ?prop-schema]
[(get ?prop-schema :type) ?prop-type]
[(get ?bp ?prop-uuid) ?v]]
(conn/get-db repo)
(name property))
(map (fn [[prop-type v]] [prop-type (if (coll? v) v [v])]))
(mapcat (fn [[prop-type vals]]
(case prop-type
:enum
(map #(:block/content (db-utils/entity repo [:block/uuid %])) vals)
:default
(let [property-name (if (keyword? property)
(name property)
(util/page-name-sanity-lc property))]
(->> (d/q
'[:find ?prop-type ?v
:in $ ?prop-name
:where
[?b :block/properties ?bp]
[?prop-b :block/name ?prop-name]
[?prop-b :block/uuid ?prop-uuid]
[?prop-b :block/schema ?prop-schema]
[(get ?prop-schema :type) ?prop-type]
[(get ?bp ?prop-uuid) ?v]]
(conn/get-db repo)
property-name)
(map (fn [[prop-type v]] [prop-type (if (coll? v) v [v])]))
(mapcat (fn [[prop-type vals]]
(case prop-type
:default
;; Remove multi-block properties as there isn't a supported approach to query them yet
(map str (remove uuid? vals))
(:page :date)
(map #(page-ref/->page-ref (:block/original-name (db-utils/entity repo [:block/uuid %])))
vals)
:number
vals
(map str (remove uuid? vals))
(:page :date)
(map #(page-ref/->page-ref (:block/original-name (db-utils/entity repo [:block/uuid %])))
vals)
:number
vals
;; Checkboxes returned as strings as builder doesn't display boolean values correctly
(map str vals))))
(map str vals))))
;; Remove blanks as they match on everything
(remove string/blank?)
(distinct)
(sort)))
(remove string/blank?)
(distinct)
(sort))))
(defn get-block-property-values
"Get blocks which have this property."
@@ -1287,15 +1287,15 @@ independent of format as format specific heading characters are stripped"
"Get classes which have given property as a class property"
[property-uuid]
(d/q
'[:find ?b
:in $ ?property-uuid
:where
[?b :block/schema ?schema]
[(get ?schema :properties) ?schema-properties*]
[(set ?schema-properties*) ?schema-properties]
[(contains? ?schema-properties ?property-uuid)]]
(conn/get-db)
property-uuid))
'[:find [?b ...]
:in $ ?property-uuid
:where
[?b :block/schema ?schema]
[(get ?schema :properties) ?schema-properties*]
[(set ?schema-properties*) ?schema-properties]
[(contains? ?schema-properties ?property-uuid)]]
(conn/get-db)
property-uuid))
(defn get-template-by-name
[name]
@@ -1574,17 +1574,16 @@ independent of format as format specific heading characters are stripped"
(conn/get-db repo)))
(defn get-namespace-children
[repo-url eid]
[repo eid]
(->>
(d/q '[:find ?children
(d/q '[:find [?children ...]
:in $ ?parent %
:where
(namespace ?parent ?children)]
(conn/get-db repo-url)
(conn/get-db repo)
eid
(:namespace rules/rules))
db-utils/seq-flatten
(set)))
distinct))
(defn get-class-objects
[repo class-id]

View File

@@ -62,11 +62,7 @@
(when-let [result-atom (get-in @query-state [k :result])]
(reset! result-atom new-result)))
(defn kv
[key value]
{:db/id -1
:db/ident key
key value})
(def kv conn/kv)
(defn remove-key!
[repo-url key]
@@ -383,4 +379,4 @@
(defn db-graph?
"Whether the current graph is db-only"
[]
(= "db" (sub-key-value :db/type)))
(= "db" (:db/type (db-utils/entity :db/type))))

View File

@@ -133,10 +133,6 @@
;; TODO: Store schema in sqlite
;; (db-migrate/migrate attached-db)
(d/transact! conn [(react/kv :db/type "db")
{:schema/version db-schema/version}]
{:skip-persist? true})
(js/setTimeout
(fn []
(p/let [other-data (persist-db/<fetch-blocks-excluding repo (map :uuid journal-blocks))

View File

@@ -7,6 +7,7 @@
[frontend.date :as date]
[frontend.db :as db]
[frontend.db.restore :as db-restore]
[logseq.db.frontend.schema :as db-schema]
[frontend.fs :as fs]
[frontend.fs.nfs :as nfs]
[frontend.handler.file :as file-handler]
@@ -546,8 +547,6 @@
_ (start-repo-db-if-not-exists! full-graph-name)
_ (state/add-repo! {:url full-graph-name})
_ (route-handler/redirect-to-home!)
_ (db/transact! full-graph-name [(react/kv :db/type "db")]
{:skip-persist? true})
initial-data (sqlite-util/build-db-initial-data config/config-default-content)
_ (db/transact! full-graph-name initial-data)
_ (repo-config-handler/set-repo-config-state! full-graph-name config/config-default-content)

View File

@@ -0,0 +1,121 @@
(ns frontend.db.db-based-model-test
(:require [cljs.test :refer [use-fixtures deftest is testing]]
[frontend.db.model :as model]
[frontend.db :as db]
[frontend.test.helper :as test-helper]
[datascript.core :as d]
[frontend.handler.db-based.property :as db-property-handler]
[frontend.handler.page :as page-handler]
[frontend.handler.editor :as editor-handler]))
(def repo test-helper/test-db-name-db-version)
(def init-data (test-helper/initial-test-page-and-blocks))
(defn start-and-destroy-db
[f]
(test-helper/db-based-start-and-destroy-db
f
{:init-data (fn [conn] (d/transact! conn init-data))}))
(def fbid (:block/uuid (second init-data)))
(def sbid (:block/uuid (nth init-data 2)))
(use-fixtures :each start-and-destroy-db)
(deftest get-all-properties-test
(db-property-handler/set-block-property! repo fbid "property-1" "value" {})
(db-property-handler/set-block-property! repo fbid "property-2" "1" {})
(is (= '("property-1" "property-2") (model/get-all-properties))))
(deftest get-block-property-values-test
(db-property-handler/set-block-property! repo fbid "property-1" "value 1" {})
(db-property-handler/set-block-property! repo sbid "property-1" "value 2" {})
(let [property (db/entity [:block/name "property-1"])]
(is (= (model/get-block-property-values (:block/uuid property))
#{[21 "value 1"] [22 "value 2"]}))))
(deftest get-db-property-values-test
(db-property-handler/set-block-property! repo fbid "property-1" "1" {})
(db-property-handler/set-block-property! repo sbid "property-1" "2" {})
(is (= [1 2] (model/get-db-property-values repo "property-1"))))
(deftest get-db-property-values-test-with-pages
(let [opts {:redirect? false :create-first-block? false}
_ (page-handler/create! "page1" opts)
_ (page-handler/create! "page2" opts)
p1id (:block/uuid (db/entity [:block/name "page1"]))
p2id (:block/uuid (db/entity [:block/name "page2"]))]
(db-property-handler/upsert-property! repo "property-1" {:type :page} {})
(db-property-handler/set-block-property! repo fbid "property-1" p1id {})
(db-property-handler/set-block-property! repo sbid "property-1" p2id {})
(is (= '("[[page1]]" "[[page2]]") (model/get-db-property-values repo "property-1")))))
(deftest get-all-classes-test
(let [opts {:redirect? false :create-first-block? false :class? true}
_ (page-handler/create! "class1" opts)
_ (page-handler/create! "class2" opts)]
(is (= ["class1" "class2"] (map first (model/get-all-classes repo))))))
(deftest get-class-objects-test
(let [opts {:redirect? false :create-first-block? false :class? true}
_ (page-handler/create! "class1" opts)
class (db/entity [:block/name "class1"])
_ (editor-handler/save-block! repo fbid "Block 1 #class1")]
(is (= (model/get-class-objects repo (:db/id class))
[(:db/id (db/entity [:block/uuid fbid]))]))
(testing "namespace classes"
(page-handler/create! "class2" opts)
;; set class2's parent to class1
(let [class2 (db/entity [:block/name "class2"])]
(db/transact! [{:db/id (:db/id class2)
:block/namespace (:db/id class)}]))
(editor-handler/save-block! repo sbid "Block 2 #class2")
(is (= (model/get-class-objects repo (:db/id class))
[(:db/id (db/entity [:block/uuid fbid]))
(:db/id (db/entity [:block/uuid sbid]))])))))
(deftest get-classes-with-property-test
(let [opts {:redirect? false :create-first-block? false :class? true}
_ (page-handler/create! "class1" opts)
_ (page-handler/create! "class2" opts)
class1 (db/entity [:block/name "class1"])
class2 (db/entity [:block/name "class2"])]
(db-property-handler/upsert-property! repo "property-1" {:type :page} {})
(db-property-handler/class-add-property! repo (:block/uuid class1) "property-1")
(db-property-handler/class-add-property! repo (:block/uuid class2) "property-1")
(let [property (db/entity [:block/name "property-1"])
class-ids (model/get-classes-with-property (:block/uuid property))]
(is (= class-ids [(:db/id class1) (:db/id class2)])))))
(deftest get-tag-blocks-test
(let [opts {:redirect? false :create-first-block? false :class? true}
_ (page-handler/create! "class1" opts)
_ (editor-handler/save-block! repo fbid "Block 1 #class1")
_ (editor-handler/save-block! repo sbid "Block 2 #class1")]
(is
(= (model/get-tag-blocks repo "class1")
[(:db/id (db/entity [:block/uuid fbid]))
(:db/id (db/entity [:block/uuid sbid]))]))))
(deftest hidden-page-test
(let [opts {:redirect? false :create-first-block? false}
_ (page-handler/create! "page 1" opts)]
(is (false? (model/hidden-page? (db/entity [:block/name "page 1"]))))
(is (false? (model/hidden-page? "$$$test")))
(is (true? (model/hidden-page? (str "$$$" (random-uuid)))))))
(deftest get-namespace-children-test
(let [opts {:redirect? false :create-first-block? false :class? true}
_ (page-handler/create! "class1" opts)
_ (page-handler/create! "class2" opts)
_ (page-handler/create! "class3" opts)
class1 (db/entity [:block/name "class1"])
class2 (db/entity [:block/name "class2"])
class3 (db/entity [:block/name "class3"])
_ (db/transact! [{:db/id (:db/id class2)
:block/namespace (:db/id class1)}
{:db/id (:db/id class3)
:block/namespace (:db/id class2)}])]
(= (model/get-namespace-children repo (:db/id (db/entity [:block/name "class1"])))
[(:db/id class2) (:db/id class3)])))