mirror of https://github.com/logseq/logseq
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-puexperiment/tanstack-table
parent
d574cc3b0e
commit
04b25a9240
|
@ -231,12 +231,21 @@
|
||||||
(:block/content ent)))
|
(:block/content ent)))
|
||||||
|
|
||||||
(defn get-property-value-name
|
(defn get-property-value-name
|
||||||
"Gets a readable name for the property value of a ref type property. Different
|
"Given an entity, gets a readable name for the property value of a ref type
|
||||||
than closed-value-name as there implementation will likely differ"
|
property. Different than closed-value-name as there implementation will likely
|
||||||
|
differ"
|
||||||
[ent]
|
[ent]
|
||||||
(or (:block/original-name ent)
|
(or (:block/original-name ent)
|
||||||
(:block/content 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
|
(defn get-closed-value-entity-by-name
|
||||||
"Given a property, finds one of its closed values by name or nil if none
|
"Given a property, finds one of its closed values by name or nil if none
|
||||||
found. Works for all closed value types"
|
found. Works for all closed value types"
|
||||||
|
@ -276,3 +285,12 @@
|
||||||
[ent]
|
[ent]
|
||||||
(when (contains? (:block/type ent) "closed value")
|
(when (contains? (:block/type ent) "closed value")
|
||||||
(:block/content ent)))
|
(: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 {})))
|
|
@ -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")))
|
|
@ -15,7 +15,7 @@
|
||||||
[frontend.util.cursor :as cursor]
|
[frontend.util.cursor :as cursor]
|
||||||
[frontend.util.priority :as priority]
|
[frontend.util.priority :as priority]
|
||||||
[frontend.handler.file-based.property :as file-property-handler]
|
[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]
|
[frontend.handler.property.file :as property-file]
|
||||||
[goog.dom :as gdom]
|
[goog.dom :as gdom]
|
||||||
[goog.object :as gobj]
|
[goog.object :as gobj]
|
||||||
|
@ -126,7 +126,7 @@
|
||||||
(defn db-based-statuses
|
(defn db-based-statuses
|
||||||
[]
|
[]
|
||||||
(map (fn [e] (:block/content e))
|
(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
|
(defn db-based-embed-page
|
||||||
[]
|
[]
|
||||||
|
@ -162,7 +162,7 @@
|
||||||
(defn db-based-priorities
|
(defn db-based-priorities
|
||||||
[]
|
[]
|
||||||
(map (fn [e] (:block/content e))
|
(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
|
(defn get-priorities
|
||||||
[]
|
[]
|
||||||
|
|
|
@ -3,7 +3,7 @@
|
||||||
(:require [clojure.walk :as walk]
|
(:require [clojure.walk :as walk]
|
||||||
[frontend.extensions.sci :as sci]
|
[frontend.extensions.sci :as sci]
|
||||||
[frontend.handler.common :as common-handler]
|
[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 :as gstring]
|
||||||
[goog.string.format]
|
[goog.string.format]
|
||||||
[frontend.state :as state]
|
[frontend.state :as state]
|
||||||
|
@ -66,7 +66,7 @@
|
||||||
query-result*)
|
query-result*)
|
||||||
repo (state/get-current-repo)
|
repo (state/get-current-repo)
|
||||||
query-result' (if (config/db-based-graph? 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)
|
query-result)
|
||||||
fn-string (-> (gstring/format "(fn [result] %s)" (first arguments))
|
fn-string (-> (gstring/format "(fn [result] %s)" (first arguments))
|
||||||
(common-handler/safe-read-string "failed to parse function")
|
(common-handler/safe-read-string "failed to parse function")
|
||||||
|
|
|
@ -21,7 +21,7 @@
|
||||||
[frontend.config :as config]
|
[frontend.config :as config]
|
||||||
[logseq.db.frontend.property :as db-property]
|
[logseq.db.frontend.property :as db-property]
|
||||||
[frontend.state :as state]
|
[frontend.state :as state]
|
||||||
[frontend.handler.property.util :as pu]))
|
[frontend.handler.db-based.property.util :as db-pu]))
|
||||||
|
|
||||||
|
|
||||||
;; Query fields:
|
;; Query fields:
|
||||||
|
@ -634,7 +634,7 @@ Some bindings in this fn:
|
||||||
(let [repo (state/get-current-repo)]
|
(let [repo (state/get-current-repo)]
|
||||||
(map (fn [blocks]
|
(map (fn [blocks]
|
||||||
(mapv (fn [block]
|
(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))
|
blocks))
|
||||||
col))
|
col))
|
||||||
col))
|
col))
|
||||||
|
|
|
@ -1,7 +1,8 @@
|
||||||
(ns frontend.handler.db-based.property.util
|
(ns frontend.handler.db-based.property.util
|
||||||
"DB-graph only utility fns for properties"
|
"DB-graph only utility fns for properties"
|
||||||
(:require [frontend.db.utils :as db-utils]
|
(: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]))
|
[logseq.db.frontend.property :as db-property]))
|
||||||
|
|
||||||
(defn get-property-name
|
(defn get-property-name
|
||||||
|
@ -14,14 +15,20 @@
|
||||||
[e]
|
[e]
|
||||||
(if-let [e (if (number? e) (db-utils/pull e) e)]
|
(if-let [e (if (number? e) (db-utils/pull e) e)]
|
||||||
(or (:block/content e)
|
(or (:block/content e)
|
||||||
(:block/original-name e))
|
(:block/original-name e))
|
||||||
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?
|
(defn all-hidden-properties?
|
||||||
"Checks if the given properties are all hidden properties"
|
"Checks if the given properties are all hidden properties"
|
||||||
[properties]
|
[properties]
|
||||||
(every? (fn [id]
|
(every? (fn [id]
|
||||||
(:hide? (:block/schema (db/entity id)))) properties))
|
(:hide? (:block/schema (db-utils/entity id)))) properties))
|
||||||
|
|
||||||
(defn readable-properties
|
(defn readable-properties
|
||||||
"Given a DB graph's properties, returns a readable properties map with keys as
|
"Given a DB graph's properties, returns a readable properties map with keys as
|
||||||
|
@ -46,3 +53,9 @@
|
||||||
:else
|
:else
|
||||||
v)])))
|
v)])))
|
||||||
(into {}))))
|
(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)))
|
|
@ -4,8 +4,6 @@
|
||||||
compatible with file graphs"
|
compatible with file graphs"
|
||||||
(:require [frontend.state :as state]
|
(:require [frontend.state :as state]
|
||||||
[frontend.db.conn :as conn]
|
[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]))
|
[logseq.db.frontend.property.util :as db-property-util]))
|
||||||
|
|
||||||
(defn lookup
|
(defn lookup
|
||||||
|
@ -14,18 +12,6 @@
|
||||||
(let [repo (state/get-current-repo)]
|
(let [repo (state/get-current-repo)]
|
||||||
(db-property-util/lookup repo coll key)))
|
(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
|
(defn get-block-property-value
|
||||||
"Get the value of a built-in block's property by its db-ident"
|
"Get the value of a built-in block's property by its db-ident"
|
||||||
[block db-ident]
|
[block db-ident]
|
||||||
|
@ -49,10 +35,4 @@
|
||||||
[block]
|
[block]
|
||||||
(let [repo (state/get-current-repo)
|
(let [repo (state/get-current-repo)
|
||||||
db (conn/get-db repo)]
|
db (conn/get-db repo)]
|
||||||
(db-property-util/shape-block? repo db block)))
|
(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)))
|
|
|
@ -5,7 +5,7 @@
|
||||||
[frontend.db :as db]
|
[frontend.db :as db]
|
||||||
[frontend.util :as util]
|
[frontend.util :as util]
|
||||||
[frontend.db.query-dsl :as query-dsl]
|
[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
|
;; TODO: quickcheck
|
||||||
;; 1. generate query filters
|
;; 1. generate query filters
|
||||||
|
@ -142,15 +142,32 @@ prop-d:: [[nada]]"}])
|
||||||
"Blocks have property value with no space")
|
"Blocks have property value with no space")
|
||||||
|
|
||||||
(is (= ["b3" "b4"]
|
(is (= ["b3" "b4"]
|
||||||
(map (comp first str/split-lines :block/content)
|
(map (comp first str/split-lines :block/content)
|
||||||
(dsl-query "(property prop-d)")))
|
(dsl-query "(property prop-d)")))
|
||||||
"Blocks that have a property"))
|
"Blocks that have a property"))
|
||||||
|
|
||||||
(deftest block-property-queries
|
(deftest block-property-queries
|
||||||
(testing "block property tests with default config"
|
(testing "block property tests with default config"
|
||||||
(test-helper/with-config {}
|
(test-helper/with-config {}
|
||||||
(block-property-queries-test))))
|
(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
|
(deftest block-property-query-performance
|
||||||
(let [pages (->> (repeat 10 {:tags ["tag1" "tag2"]})
|
(let [pages (->> (repeat 10 {:tags ["tag1" "tag2"]})
|
||||||
(map-indexed (fn [idx {:keys [tags]}]
|
(map-indexed (fn [idx {:keys [tags]}]
|
||||||
|
|
|
@ -155,7 +155,9 @@
|
||||||
(defn load-test-files-for-db-graph
|
(defn load-test-files-for-db-graph
|
||||||
[options*]
|
[options*]
|
||||||
(let [;; Builds options from markdown :file/content unless given explicit build-blocks config
|
(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)]
|
{:keys [init-tx block-props-tx]} (sqlite-build/build-blocks-tx options)]
|
||||||
(db/transact! test-db init-tx)
|
(db/transact! test-db init-tx)
|
||||||
(when (seq block-props-tx)
|
(when (seq block-props-tx)
|
||||||
|
|
Loading…
Reference in New Issue