chore: add basic tests for sqlite.build

and added a couple db-graph specific for query-dsl. Also
moved fns in pu that should be in db-pu
experiment/tanstack-table
Gabriel Horner 2024-06-10 11:51:47 -04:00
parent d574cc3b0e
commit 04b25a9240
9 changed files with 121 additions and 38 deletions

View File

@ -231,12 +231,21 @@
(:block/content ent)))
(defn get-property-value-name
"Gets a readable name for the property value of a ref type property. Different
than closed-value-name as there implementation will likely differ"
"Given an entity, gets a readable name for the property value of a ref type
property. Different than closed-value-name as there implementation will likely
differ"
[ent]
(or (:block/original-name ent)
(:block/content ent)))
(defn get-property-value-name-from-ref
"Given a ref from a pulled query e.g. `{:db/id X}`, gets a readable name for
the property value of a ref type property"
[db ref]
(some->> (:db/id ref)
(d/entity db)
get-property-value-name))
(defn get-closed-value-entity-by-name
"Given a property, finds one of its closed values by name or nil if none
found. Works for all closed value types"
@ -276,3 +285,12 @@
[ent]
(when (contains? (:block/type ent) "closed value")
(:block/content ent)))
(defn properties-by-name
"Given a block from a query result, returns a map of its properties indexed by property names"
[db block]
(->> (properties block)
(map (fn [[k v]]
[(:block/original-name (d/entity db k))
(get-property-value-name-from-ref db v)]))
(into {})))

View File

@ -0,0 +1,53 @@
(ns logseq.db.sqlite.build-test
(:require [cljs.test :refer [deftest is]]
[datascript.core :as d]
[logseq.db.frontend.schema :as db-schema]
[logseq.db.sqlite.create-graph :as sqlite-create-graph]
[logseq.db.sqlite.build :as sqlite-build]
[logseq.db.frontend.property :as db-property]))
(deftest build-tags
(let [conn (d/create-conn db-schema/schema-for-db-based-graph)
_ (d/transact! conn (sqlite-create-graph/build-db-initial-data "{}"))
_ (sqlite-build/create-blocks
conn
[{:page {:block/original-name "page1"}
:blocks [{:block/content "Jrue Holiday" :build/tags [:Person]}]}
{:page {:block/original-name "Jayson Tatum" :build/tags [:Person]}}])]
(is (= {:block/tags [{:block/original-name "Person", :block/type ["class"]}]}
(first (d/q '[:find [(pull ?b [{:block/tags [:block/original-name :block/type]}]) ...]
:where [?b :block/content "Jrue Holiday"]]
@conn)))
"Person class is created and correctly associated to a block")
(is (= {:block/tags [{:block/original-name "Person", :block/type ["class"]}]}
(first (d/q '[:find [(pull ?b [{:block/tags [:block/original-name :block/type]}]) ...]
:where [?b :block/original-name "Jayson Tatum"]]
@conn)))
"Person class is created and correctly associated to a page")))
(deftest build-properties
(let [conn (d/create-conn db-schema/schema-for-db-based-graph)
_ (d/transact! conn (sqlite-create-graph/build-db-initial-data "{}"))
_ (sqlite-build/create-blocks
conn
[{:page {:block/original-name "page1"}
:blocks [{:block/content "Jrue Holiday" :build/properties {:description "Clutch defense"}}]}
{:page {:block/original-name "Jayson Tatum" :build/properties {:description "Awesome selfless basketball"}}}])]
(is (= "Clutch defense"
(->> @conn
(d/q '[:find [(pull ?b [*]) ...]
:where [?b :block/content "Jrue Holiday"]])
first
:user.property/description
(db-property/get-property-value-name-from-ref @conn)))
"description property is created and correctly associated to a block")
(is (= "Awesome selfless basketball"
(->> @conn
(d/q '[:find [(pull ?b [*]) ...]
:where [?b :block/original-name "Jayson Tatum"]])
first
:user.property/description
(db-property/get-property-value-name-from-ref @conn)))
"description property is created and correctly associated to a page")))

View File

@ -15,7 +15,7 @@
[frontend.util.cursor :as cursor]
[frontend.util.priority :as priority]
[frontend.handler.file-based.property :as file-property-handler]
[frontend.handler.property.util :as pu]
[frontend.handler.db-based.property.util :as db-pu]
[frontend.handler.property.file :as property-file]
[goog.dom :as gdom]
[goog.object :as gobj]
@ -126,7 +126,7 @@
(defn db-based-statuses
[]
(map (fn [e] (:block/content e))
(pu/get-closed-property-values :logseq.task/status)))
(db-pu/get-closed-property-values :logseq.task/status)))
(defn db-based-embed-page
[]
@ -162,7 +162,7 @@
(defn db-based-priorities
[]
(map (fn [e] (:block/content e))
(pu/get-closed-property-values :logseq.task/priority)))
(db-pu/get-closed-property-values :logseq.task/priority)))
(defn get-priorities
[]

View File

@ -3,7 +3,7 @@
(:require [clojure.walk :as walk]
[frontend.extensions.sci :as sci]
[frontend.handler.common :as common-handler]
[frontend.handler.property.util :as pu]
[frontend.handler.db-based.property.util :as db-pu]
[goog.string :as gstring]
[goog.string.format]
[frontend.state :as state]
@ -66,7 +66,7 @@
query-result*)
repo (state/get-current-repo)
query-result' (if (config/db-based-graph? repo)
(map #(assoc % :block/properties (pu/properties-by-name repo %)) query-result)
(map #(assoc % :block/properties (db-pu/properties-by-name repo %)) query-result)
query-result)
fn-string (-> (gstring/format "(fn [result] %s)" (first arguments))
(common-handler/safe-read-string "failed to parse function")

View File

@ -21,7 +21,7 @@
[frontend.config :as config]
[logseq.db.frontend.property :as db-property]
[frontend.state :as state]
[frontend.handler.property.util :as pu]))
[frontend.handler.db-based.property.util :as db-pu]))
;; Query fields:
@ -634,7 +634,7 @@ Some bindings in this fn:
(let [repo (state/get-current-repo)]
(map (fn [blocks]
(mapv (fn [block]
(assoc block :block/properties-by-name (pu/properties-by-name repo block)))
(assoc block :block/properties-by-name (db-pu/properties-by-name repo block)))
blocks))
col))
col))

View File

@ -1,7 +1,8 @@
(ns frontend.handler.db-based.property.util
"DB-graph only utility fns for properties"
(:require [frontend.db.utils :as db-utils]
[frontend.db :as db]
[frontend.db.conn :as conn]
[frontend.state :as state]
[logseq.db.frontend.property :as db-property]))
(defn get-property-name
@ -14,14 +15,20 @@
[e]
(if-let [e (if (number? e) (db-utils/pull e) e)]
(or (:block/content e)
(:block/original-name e))
(:block/original-name e))
e))
(defn properties-by-name
"Given a block from a query result, returns a map of its properties indexed by property names"
[repo block]
(let [db (conn/get-db repo)]
(db-property/properties-by-name db block)))
(defn all-hidden-properties?
"Checks if the given properties are all hidden properties"
[properties]
(every? (fn [id]
(:hide? (:block/schema (db/entity id)))) properties))
(:hide? (:block/schema (db-utils/entity id)))) properties))
(defn readable-properties
"Given a DB graph's properties, returns a readable properties map with keys as
@ -46,3 +53,9 @@
:else
v)])))
(into {}))))
(defn get-closed-property-values
[property-id]
(let [repo (state/get-current-repo)
db (conn/get-db repo)]
(db-property/get-closed-property-values db property-id)))

View File

@ -4,8 +4,6 @@
compatible with file graphs"
(:require [frontend.state :as state]
[frontend.db.conn :as conn]
[frontend.db.utils :as db-utils]
[logseq.db.frontend.property :as db-property]
[logseq.db.frontend.property.util :as db-property-util]))
(defn lookup
@ -14,18 +12,6 @@
(let [repo (state/get-current-repo)]
(db-property-util/lookup repo coll key)))
(defn properties-by-name
"Given a block from a query result, returns a map of its properties indexed by property names"
[repo block]
(->> (db-property/properties block)
(map (fn [[k v]]
[(:block/original-name (db-utils/entity k))
(or (some->> (:db/id v)
(db-utils/entity repo)
db-property/get-property-value-name)
v)]))
(into {})))
(defn get-block-property-value
"Get the value of a built-in block's property by its db-ident"
[block db-ident]
@ -50,9 +36,3 @@
(let [repo (state/get-current-repo)
db (conn/get-db repo)]
(db-property-util/shape-block? repo db block)))
(defn get-closed-property-values
[property-id]
(let [repo (state/get-current-repo)
db (conn/get-db repo)]
(db-property/get-closed-property-values db property-id)))

View File

@ -5,7 +5,7 @@
[frontend.db :as db]
[frontend.util :as util]
[frontend.db.query-dsl :as query-dsl]
[frontend.test.helper :as test-helper :include-macros true :refer [load-test-files]]))
[frontend.test.helper :as test-helper :include-macros true :refer [load-test-files load-test-files-for-db-graph]]))
;; TODO: quickcheck
;; 1. generate query filters
@ -142,15 +142,32 @@ prop-d:: [[nada]]"}])
"Blocks have property value with no space")
(is (= ["b3" "b4"]
(map (comp first str/split-lines :block/content)
(dsl-query "(property prop-d)")))
"Blocks that have a property"))
(map (comp first str/split-lines :block/content)
(dsl-query "(property prop-d)")))
"Blocks that have a property"))
(deftest block-property-queries
(testing "block property tests with default config"
(test-helper/with-config {}
(block-property-queries-test))))
(when js/process.env.DB_GRAPH
(deftest db-only-block-property-queries
(load-test-files-for-db-graph
[{:page {:block/original-name "page1"}
:blocks [{:block/content "b1"
:build/properties {:Foo "bar"}}
{:block/content "b2"
:build/properties {:foo "bar"}}]}])
(is (= ["b1"]
(map :block/content (dsl-query "(property Foo)")))
"filter is case sensitive")
(is (= ["b2"]
(map :block/content (dsl-query "(property :user.property/foo)")))
"filter can handle qualified keyword properties")))
(deftest block-property-query-performance
(let [pages (->> (repeat 10 {:tags ["tag1" "tag2"]})
(map-indexed (fn [idx {:keys [tags]}]

View File

@ -155,7 +155,9 @@
(defn load-test-files-for-db-graph
[options*]
(let [;; Builds options from markdown :file/content unless given explicit build-blocks config
options (if (:page (first options*)) {:pages-and-blocks options*} (build-blocks-tx-options options*))
options (if (:page (first options*))
{:pages-and-blocks options* :auto-create-ontology? true}
(build-blocks-tx-options options*))
{:keys [init-tx block-props-tx]} (sqlite-build/build-blocks-tx options)]
(db/transact! test-db init-tx)
(when (seq block-props-tx)