Skip to content

Commit

Permalink
Make datoms function work with system attribute components (#704)
Browse files Browse the repository at this point in the history
* Make datoms function work with system attribute components

* FIXUP

---------

Co-authored-by: Jonas Östlund <[email protected]>
  • Loading branch information
jonasseglare and Jonas Östlund authored Sep 25, 2024
1 parent acd9d4b commit e1cb9b0
Show file tree
Hide file tree
Showing 4 changed files with 138 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/datahike/db/utils.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@

(defn attr-has-ref? [db attr]
(and (not (nil? attr))
(not (ds/is-system-keyword? attr))
(not (ds/built-in-attribute? attr))
(:attribute-refs? (dbi/-config db))))

(defn attr-ref-or-ident [db attr]
Expand Down
5 changes: 5 additions & 0 deletions src/datahike/schema.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -214,5 +214,10 @@
(= "db" (first (clojure.string/split ns #"\.")))
false)))

(def built-in-attributes #{:db/ident})

(defn built-in-attribute? [x]
(contains? built-in-attributes x))

(defn get-user-schema [{:keys [schema] :as db}]
(into {} (filter #(not (is-system-keyword? (key %))) schema)))
110 changes: 110 additions & 0 deletions test/datahike/test/attribute_refs/datoms_test.cljc
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
(ns datahike.test.attribute-refs.datoms-test
(:require
#?(:cljs [cljs.test :as t :refer-macros [is deftest testing]]
:clj [clojure.test :as t :refer [is deftest testing]])
[datahike.api :as d]
[datahike.db :as db :refer [ref-datoms]]
[datahike.test.utils :refer [with-connect provide-unique-id
recreate-database]]))

(def cfg
{:store {:backend :mem}
:keep-history? true
:attribute-refs? true
:schema-flexibility :write})

(deftest test-datoms-with-components
(with-connect [conn (-> cfg
provide-unique-id
recreate-database)]
(d/transact conn [{:db/ident :name
:db/cardinality :db.cardinality/one
:db/index true
:db/valueType :db.type/string}
{:db/ident :age
:db/cardinality :db.cardinality/one
:db/valueType :db.type/long}])
(d/transact conn [{:name "Alice"
:age 10}])
(let [all-datoms (d/datoms @conn {:index :avet :components []})
all-tx-inst-datoms (filter (fn [datom]
(and (= (:e datom)
(:tx datom))
(instance? java.util.Date
(:v datom))))
all-datoms)
name-datoms (filter (fn [datom] (= "Alice" (:v datom)))
all-datoms)]
(is (= 1 (count name-datoms)))
(doseq [datom name-datoms]
(is (= [datom]
(d/datoms
@conn
{:index :avet
:components [(:a datom)]})))
(is (= [datom]
(d/datoms
@conn
{:index :avet
:components [(:a datom)
(:v datom)]})))
(is (= [datom]
(d/datoms
@conn
{:index :avet
:components [(:a datom)
(:v datom)
(:e datom)]})))
(is (= [datom]
(d/datoms
@conn
{:index :avet
:components [(:a datom)
(:v datom)
(:e datom)
(:tx datom)]}))))

(is (= 3 (count all-tx-inst-datoms)))
(is (= (set all-tx-inst-datoms)
(set (d/datoms @conn {:index :avet
:components [:db/txInstant]}))))
(is (= (set all-tx-inst-datoms)
(set (d/datoms @conn {:index :aevt
:components [:db/txInstant]}))))
(doseq [datom all-tx-inst-datoms]
(is (= [datom]
(d/datoms
@conn
{:index :avet
:components [:db/txInstant
(:v datom)
(:e datom)]})))
(is (= [datom]
(d/datoms
@conn
{:index :avet
:components [:db/txInstant
(:v datom)
(:e datom)
(:tx datom)]})))
(is (= [datom]
(d/datoms
@conn
{:index :aevt
:components [:db/txInstant
(:e datom)]})))
(is (= [datom]
(d/datoms
@conn
{:index :aevt
:components [:db/txInstant
(:e datom)
(:v datom)]})))
(is (= [datom]
(d/datoms
@conn
{:index :aevt
:components [:db/txInstant
(:e datom)
(:v datom)
(:tx datom)]})))))))
28 changes: 22 additions & 6 deletions test/datahike/test/utils.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,24 @@
:keep-history? true
:schema-flexibility :read})

(defn recreate-database [cfg]
(d/delete-database cfg)
(d/create-database cfg)
cfg)

(defn with-connect-fn [cfg body-fn]
(let [conn (d/connect cfg)]
(try
(body-fn conn)
(finally
(d/release conn)))))

(defmacro with-connect [[conn cfg] & body]
`(with-connect-fn ~cfg (fn [~conn] ~@body)))

(defn provide-unique-id [cfg]
(assoc-in cfg [:store :id] (str (UUID/randomUUID))))

(defn setup-db
"Setting up a test-db in memory by default. Deep-merges the passed config into the defaults."
([]
Expand All @@ -49,19 +67,17 @@
(setup-db cfg (not (get-in cfg [:store :id]))))
([cfg gen-uuid?]
(let [cfg (cond-> (tools/deep-merge (cfg-template) cfg)
gen-uuid? (assoc-in [:store :id] (str (UUID/randomUUID))))]
(d/delete-database cfg)
(d/create-database cfg)
gen-uuid? (provide-unique-id))]
(recreate-database cfg)
(d/connect cfg))))

(defn all-true? [c] (every? true? c))

(defn all-eq? [c1 c2] (all-true? (map = c1 c2)))

(defn setup-default-db [config test-data]
(let [_ (d/delete-database config)
_ (d/create-database config)
conn (d/connect config)]
(recreate-database config)
(let [conn (d/connect config)]
(d/transact conn test-data)
conn))

Expand Down

0 comments on commit e1cb9b0

Please sign in to comment.