-
-
Notifications
You must be signed in to change notification settings - Fork 97
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* allow local kaocha test config * add hashp for dev environment * add query middleware * fix formatting * move to list of middlewares * rename middleware, add q db check * Fix formatting, again * add historcial db middleware tests * moved and adjusted middleware tests * moved middleware tests into test folder * used print instead of logging for timed-q * fixed formatting * removed :middleware test id * add newline to gitignore
- Loading branch information
Showing
12 changed files
with
260 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -28,3 +28,4 @@ out/ | |
/.settings | ||
.classpath | ||
/.clj-kondo | ||
tests.user.edn |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
(ns user | ||
(:require [datahike.api :as d])) | ||
|
||
(comment | ||
|
||
(def schema [{:db/ident :name | ||
:db/cardinality :db.cardinality/one | ||
:db/index true | ||
:db/unique :db.unique/identity | ||
:db/valueType :db.type/string} | ||
{:db/ident :sibling | ||
:db/cardinality :db.cardinality/many | ||
:db/valueType :db.type/ref} | ||
{:db/ident :age | ||
:db/cardinality :db.cardinality/one | ||
:db/valueType :db.type/long}]) | ||
|
||
(def cfg {:store {:backend :mem :id "sandbox"} | ||
:keep-history? true | ||
:schema-flexibility :write | ||
:middleware {:query ['datahike.middleware.query/timed]} | ||
:attribute-refs? true}) | ||
|
||
(def conn (do | ||
(d/delete-database cfg) | ||
(d/create-database cfg) | ||
(d/connect cfg))) | ||
|
||
(d/transact conn schema) | ||
|
||
(d/datoms @conn :avet) | ||
(d/datoms @conn :aevt) | ||
(d/datoms @conn :eavt) | ||
|
||
(:max-eid @conn) | ||
|
||
(d/transact conn [{:name "Alice" | ||
:age 25}]) | ||
|
||
(d/transact conn [{:name "Bob" | ||
:age 25}]) | ||
|
||
(d/transact conn [{:name "Charlie" | ||
:age 45 | ||
:sibling [[:name "Alice"] [:name "Bob"]]}]) | ||
|
||
(d/q '[:find ?e ?a ?v ?t | ||
:in $ ?a | ||
:where | ||
[?e :name ?v ?t] | ||
[?e :age ?a]] | ||
@conn | ||
25) | ||
|
||
(d/q '[:find ?e ?at ?v | ||
:where | ||
[?e ?a ?v] | ||
[?a :db/ident ?at]] | ||
@conn) | ||
|
||
|
||
(d/q '[:find ?e :where [?e :name "Alice"]] @conn) | ||
|
||
(do (d/transact conn (vec (repeatedly 5000 (fn [] {:age (long (rand-int 1000)) | ||
:name (str (rand-int 1000))})))) | ||
true) | ||
|
||
(d/q {:query '[:find ?e ?v | ||
:in $ | ||
:where [?e :name ?v]] | ||
:args [@conn] | ||
:offset 0 | ||
:limit 10}) | ||
|
||
(do (d/q {:query '[:find ?v1 ?v2 | ||
:in $ | ||
:where [?e1 :name ?v1] [?e2 :name ?v2]] | ||
:args [@conn]}) | ||
nil)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
(ns datahike.middleware.query | ||
(:require [clojure.pprint :as pprint])) | ||
|
||
(defn timed-query [query-handler] | ||
(fn [query & inputs] | ||
(let [start (. System (nanoTime)) | ||
result (apply query-handler query inputs) | ||
t (/ (double (- (. System (nanoTime)) start)) 1000000.0)] | ||
(println "Query time:") | ||
(pprint/pprint {:t t | ||
:q (update query :args str) | ||
:inputs (str inputs)}) | ||
result))) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
(ns datahike.middleware.utils) | ||
|
||
(defn apply-middlewares | ||
"Combines a list of middleware functions into one." | ||
[middlewares handler] | ||
(reduce | ||
(fn [acc f-sym] | ||
(if-let [f (resolve f-sym)] | ||
(f acc) | ||
(throw (ex-info "Invalid middleware.😱" {:fn f-sym})))) | ||
handler | ||
middlewares)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
(ns datahike.test.middleware.query-test | ||
(:require | ||
[clojure.test :refer [deftest is]] | ||
[datahike.api :as d] | ||
[datahike.middleware.query] | ||
[datahike.test.utils :as utils]) | ||
(:import | ||
[clojure.lang ExceptionInfo] | ||
[java.util Date])) | ||
|
||
(deftest timed-query-should-log-time-for-query-to-run | ||
(let [cfg {:store {:backend :mem | ||
:id "query-middleware"} | ||
:keep-history? false | ||
:schema-flexibility :read | ||
:middleware {:query ['datahike.middleware.query/timed-query]}} | ||
conn (utils/setup-db cfg)] | ||
(d/transact conn {:tx-data [{:name "Anna"} | ||
{:name "Boris"}]}) | ||
(let [out-str (with-out-str (d/q '[:find ?e :where [?e :name "Anna"]] @conn))] | ||
(is (re-find #"Query time" out-str)) | ||
(is (re-find #":query" out-str)) | ||
(is (re-find #":args" out-str)) | ||
(is (re-find #"DB" out-str)) | ||
(is (re-find #"[:find ?e :where [?e :name \"Anna\"]]" out-str)) | ||
(is (re-find #":t" out-str))))) | ||
|
||
(deftest invalid-middleware-should-be-caught-on-connection | ||
(let [cfg {:store {:backend :mem | ||
:id "query-middleware"} | ||
:keep-history? false | ||
:schema-flexibility :read | ||
:middleware {:query "this is neither a function nor a vector!"}}] | ||
(is (thrown-with-msg? ExceptionInfo #"Invalid Datahike configuration." (utils/setup-db cfg))))) | ||
|
||
(deftest middleware-should-work-with-as-of-db | ||
(let [cfg {:store {:backend :mem | ||
:id "query-middleware"} | ||
:keep-history? true | ||
:schema-flexibility :read | ||
:middleware {:query ['datahike.middleware.query/timed-query]}} | ||
conn (utils/setup-db cfg)] | ||
(d/transact conn {:tx-data [{:name "Anna"} | ||
{:name "Boris"}]}) | ||
(let [before (Date.) | ||
_ (d/transact conn {:tx-data [{:name "Charlize"}]}) | ||
out-str (with-out-str (d/q '[:find ?e :where [?e :name "Anna"]] (d/as-of @conn before)))] | ||
(is (re-find #"Query time" out-str)) | ||
(is (re-find #":query" out-str)) | ||
(is (re-find #":args" out-str)) | ||
(is (re-find #"AsOfDB" out-str)) | ||
(is (re-find #"[:find ?e :where [?e :name \"Anna\"]]" out-str)) | ||
(is (re-find #":t" out-str))))) | ||
|
||
(deftest middleware-should-work-with-since | ||
(let [cfg {:store {:backend :mem | ||
:id "query-middleware"} | ||
:keep-history? true | ||
:schema-flexibility :read | ||
:middleware {:query ['datahike.middleware.query/timed-query]}} | ||
conn (utils/setup-db cfg)] | ||
(d/transact conn {:tx-data [{:name "Anna"} | ||
{:name "Boris"}]}) | ||
(let [before (Date.) | ||
_ (d/transact conn {:tx-data [{:name "Charlize"}]}) | ||
out-str (with-out-str (d/q '[:find ?e :where [?e :name "Anna"]] (d/since @conn before)))] | ||
(is (re-find #"Query time" out-str)) | ||
(is (re-find #":query" out-str)) | ||
(is (re-find #":args" out-str)) | ||
(is (re-find #"SinceDB" out-str)) | ||
(is (re-find #"[:find ?e :where [?e :name \"Anna\"]]" out-str)) | ||
(is (re-find #":t" out-str))))) | ||
|
||
(deftest middleware-should-work-with-history | ||
(let [cfg {:store {:backend :mem | ||
:id "query-middleware"} | ||
:keep-history? true | ||
:schema-flexibility :read | ||
:middleware {:query ['datahike.middleware.query/timed-query]}} | ||
conn (utils/setup-db cfg)] | ||
(d/transact conn {:tx-data [{:name "Anna"} | ||
{:name "Boris"}]}) | ||
(let [_ (d/transact conn {:tx-data [{:name "Charlize"}]}) | ||
out-str (with-out-str (d/q '[:find ?e :where [?e :name "Anna"]] (d/history @conn)))] | ||
(is (re-find #"Query time" out-str)) | ||
(is (re-find #":query" out-str)) | ||
(is (re-find #":args" out-str)) | ||
(is (re-find #"HistoricalDB" out-str)) | ||
(is (re-find #"[:find ?e :where [?e :name \"Anna\"]]" out-str)) | ||
(is (re-find #":t" out-str))))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
(ns datahike.test.middleware.utils-test | ||
(:require [clojure.test :refer [deftest is testing]] | ||
[datahike.middleware.utils :as utils])) | ||
|
||
(defn test-handler-inc-first [handler] (fn [a b] (handler (+ a 5) b))) | ||
(defn test-handler-inc-second [handler] (fn [a b] (handler a (+ b 10)))) | ||
|
||
(deftest apply-middlewares-should-combine-symbols-to-new-function | ||
(let [combined (utils/apply-middlewares ['datahike.test.middleware.utils-test/test-handler-inc-first | ||
'datahike.test.middleware.utils-test/test-handler-inc-second] | ||
(fn [a b] (+ a b)))] | ||
(is (= 15 | ||
(combined 0 0))))) | ||
|
||
(deftest apply-middlewares-should-throw-exception-on-non-resolved-symbols | ||
(is (thrown-with-msg? clojure.lang.ExceptionInfo #"Invalid middleware.😱" | ||
(utils/apply-middlewares ['is-not-there | ||
'datahike.middleware.utils-test/test-handler-inc-second] | ||
(fn [a b] (+ a b)))))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,19 @@ | ||
#kaocha/v1 {:tests [{:id :clj | ||
:ns-patterns ["datahike.test."]} | ||
#_{:id :cljs | ||
:type :kaocha.type/cljs | ||
:ns-patterns ["datahike.test."]} | ||
{:id :quick | ||
:ns-patterns ["datahike.test."] | ||
:bindings {datahike.test.config/*user-filter-map* [{:index :datahike.index/persistent-set | ||
:backend :mem | ||
:schema-flexibility :read | ||
}]}} | ||
{:id :integration | ||
:test-paths ["test/datahike/integration_test"]}] | ||
:reporter kaocha.report/documentation} | ||
#kaocha/v1 | ||
#meta-merge [{:tests [{:id :clj | ||
:focus-meta [:focused] | ||
:ns-patterns ["datahike.test."]} | ||
#_{:id :cljs | ||
:type :kaocha.type/cljs | ||
:ns-patterns ["datahike.test."]} | ||
{:id :quick | ||
:focus-meta [:focused] | ||
:ns-patterns ["datahike.test."] | ||
:bindings {datahike.test.config/*user-filter-map* [{:index :datahike.index/persistent-set | ||
:backend :mem | ||
:schema-flexibility :read | ||
}]}} | ||
{:id :integration | ||
:focus-meta [:focused] | ||
:test-paths ["test/datahike/integration_test"]}] | ||
:reporter kaocha.report/documentation} | ||
#include "tests.user.edn"] |