diff --git a/src/datahike/db/utils.cljc b/src/datahike/db/utils.cljc index 3612cab6..59baebb7 100644 --- a/src/datahike/db/utils.cljc +++ b/src/datahike/db/utils.cljc @@ -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] diff --git a/src/datahike/schema.cljc b/src/datahike/schema.cljc index 50cbd82b..1266e67d 100644 --- a/src/datahike/schema.cljc +++ b/src/datahike/schema.cljc @@ -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))) diff --git a/test/datahike/test/attribute_refs/datoms_test.cljc b/test/datahike/test/attribute_refs/datoms_test.cljc new file mode 100644 index 00000000..edcf370a --- /dev/null +++ b/test/datahike/test/attribute_refs/datoms_test.cljc @@ -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)]}))))))) diff --git a/test/datahike/test/utils.cljc b/test/datahike/test/utils.cljc index 09645072..354bcc9b 100644 --- a/test/datahike/test/utils.cljc +++ b/test/datahike/test/utils.cljc @@ -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." ([] @@ -49,9 +67,8 @@ (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)) @@ -59,9 +76,8 @@ (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))