Skip to content

Commit e1cb9b0

Browse files
jonasseglareJonas Östlund
and
Jonas Östlund
authored
Make datoms function work with system attribute components (#704)
* Make datoms function work with system attribute components * FIXUP --------- Co-authored-by: Jonas Östlund <[email protected]>
1 parent acd9d4b commit e1cb9b0

File tree

4 files changed

+138
-7
lines changed

4 files changed

+138
-7
lines changed

src/datahike/db/utils.cljc

+1-1
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@
148148

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

154154
(defn attr-ref-or-ident [db attr]

src/datahike/schema.cljc

+5
Original file line numberDiff line numberDiff line change
@@ -214,5 +214,10 @@
214214
(= "db" (first (clojure.string/split ns #"\.")))
215215
false)))
216216

217+
(def built-in-attributes #{:db/ident})
218+
219+
(defn built-in-attribute? [x]
220+
(contains? built-in-attributes x))
221+
217222
(defn get-user-schema [{:keys [schema] :as db}]
218223
(into {} (filter #(not (is-system-keyword? (key %))) schema)))
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
(ns datahike.test.attribute-refs.datoms-test
2+
(:require
3+
#?(:cljs [cljs.test :as t :refer-macros [is deftest testing]]
4+
:clj [clojure.test :as t :refer [is deftest testing]])
5+
[datahike.api :as d]
6+
[datahike.db :as db :refer [ref-datoms]]
7+
[datahike.test.utils :refer [with-connect provide-unique-id
8+
recreate-database]]))
9+
10+
(def cfg
11+
{:store {:backend :mem}
12+
:keep-history? true
13+
:attribute-refs? true
14+
:schema-flexibility :write})
15+
16+
(deftest test-datoms-with-components
17+
(with-connect [conn (-> cfg
18+
provide-unique-id
19+
recreate-database)]
20+
(d/transact conn [{:db/ident :name
21+
:db/cardinality :db.cardinality/one
22+
:db/index true
23+
:db/valueType :db.type/string}
24+
{:db/ident :age
25+
:db/cardinality :db.cardinality/one
26+
:db/valueType :db.type/long}])
27+
(d/transact conn [{:name "Alice"
28+
:age 10}])
29+
(let [all-datoms (d/datoms @conn {:index :avet :components []})
30+
all-tx-inst-datoms (filter (fn [datom]
31+
(and (= (:e datom)
32+
(:tx datom))
33+
(instance? java.util.Date
34+
(:v datom))))
35+
all-datoms)
36+
name-datoms (filter (fn [datom] (= "Alice" (:v datom)))
37+
all-datoms)]
38+
(is (= 1 (count name-datoms)))
39+
(doseq [datom name-datoms]
40+
(is (= [datom]
41+
(d/datoms
42+
@conn
43+
{:index :avet
44+
:components [(:a datom)]})))
45+
(is (= [datom]
46+
(d/datoms
47+
@conn
48+
{:index :avet
49+
:components [(:a datom)
50+
(:v datom)]})))
51+
(is (= [datom]
52+
(d/datoms
53+
@conn
54+
{:index :avet
55+
:components [(:a datom)
56+
(:v datom)
57+
(:e datom)]})))
58+
(is (= [datom]
59+
(d/datoms
60+
@conn
61+
{:index :avet
62+
:components [(:a datom)
63+
(:v datom)
64+
(:e datom)
65+
(:tx datom)]}))))
66+
67+
(is (= 3 (count all-tx-inst-datoms)))
68+
(is (= (set all-tx-inst-datoms)
69+
(set (d/datoms @conn {:index :avet
70+
:components [:db/txInstant]}))))
71+
(is (= (set all-tx-inst-datoms)
72+
(set (d/datoms @conn {:index :aevt
73+
:components [:db/txInstant]}))))
74+
(doseq [datom all-tx-inst-datoms]
75+
(is (= [datom]
76+
(d/datoms
77+
@conn
78+
{:index :avet
79+
:components [:db/txInstant
80+
(:v datom)
81+
(:e datom)]})))
82+
(is (= [datom]
83+
(d/datoms
84+
@conn
85+
{:index :avet
86+
:components [:db/txInstant
87+
(:v datom)
88+
(:e datom)
89+
(:tx datom)]})))
90+
(is (= [datom]
91+
(d/datoms
92+
@conn
93+
{:index :aevt
94+
:components [:db/txInstant
95+
(:e datom)]})))
96+
(is (= [datom]
97+
(d/datoms
98+
@conn
99+
{:index :aevt
100+
:components [:db/txInstant
101+
(:e datom)
102+
(:v datom)]})))
103+
(is (= [datom]
104+
(d/datoms
105+
@conn
106+
{:index :aevt
107+
:components [:db/txInstant
108+
(:e datom)
109+
(:v datom)
110+
(:tx datom)]})))))))

test/datahike/test/utils.cljc

+22-6
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,24 @@
4141
:keep-history? true
4242
:schema-flexibility :read})
4343

44+
(defn recreate-database [cfg]
45+
(d/delete-database cfg)
46+
(d/create-database cfg)
47+
cfg)
48+
49+
(defn with-connect-fn [cfg body-fn]
50+
(let [conn (d/connect cfg)]
51+
(try
52+
(body-fn conn)
53+
(finally
54+
(d/release conn)))))
55+
56+
(defmacro with-connect [[conn cfg] & body]
57+
`(with-connect-fn ~cfg (fn [~conn] ~@body)))
58+
59+
(defn provide-unique-id [cfg]
60+
(assoc-in cfg [:store :id] (str (UUID/randomUUID))))
61+
4462
(defn setup-db
4563
"Setting up a test-db in memory by default. Deep-merges the passed config into the defaults."
4664
([]
@@ -49,19 +67,17 @@
4967
(setup-db cfg (not (get-in cfg [:store :id]))))
5068
([cfg gen-uuid?]
5169
(let [cfg (cond-> (tools/deep-merge (cfg-template) cfg)
52-
gen-uuid? (assoc-in [:store :id] (str (UUID/randomUUID))))]
53-
(d/delete-database cfg)
54-
(d/create-database cfg)
70+
gen-uuid? (provide-unique-id))]
71+
(recreate-database cfg)
5572
(d/connect cfg))))
5673

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

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

6178
(defn setup-default-db [config test-data]
62-
(let [_ (d/delete-database config)
63-
_ (d/create-database config)
64-
conn (d/connect config)]
79+
(recreate-database config)
80+
(let [conn (d/connect config)]
6581
(d/transact conn test-data)
6682
conn))
6783

0 commit comments

Comments
 (0)