|
| 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)]}))))))) |
0 commit comments