-
-
Notifications
You must be signed in to change notification settings - Fork 298
/
components.cljc
89 lines (77 loc) · 3.36 KB
/
components.cljc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
(ns datascript.test.components
(:require
[#?(:cljs cljs.reader :clj clojure.edn) :as edn]
#?(:cljs [cljs.test :as t :refer-macros [is are deftest testing]]
:clj [clojure.test :as t :refer [is are deftest testing]])
[datascript.core :as d]
[datascript.db :as db]
[datascript.test.core :as tdc]))
(t/use-fixtures :once tdc/no-namespace-maps)
#?(:cljs
(def Throwable js/Error))
(deftest test-components
(is (thrown-msg? "Bad attribute specification for :profile: {:db/isComponent true} should also have {:db/valueType :db.type/ref}"
(d/empty-db {:profile {:db/isComponent true}})))
(is (thrown-msg? "Bad attribute specification for {:profile {:db/isComponent \"aaa\"}}, expected one of #{true false}"
(d/empty-db {:profile {:db/isComponent "aaa" :db/valueType :db.type/ref}})))
(let [db (d/db-with
(d/empty-db {:profile {:db/valueType :db.type/ref
:db/isComponent true}})
[{:db/id 1 :name "Ivan" :profile 3}
{:db/id 3 :email "@3"}
{:db/id 4 :email "@4"}])
visible #(edn/read-string (pr-str %))
touched #(visible (d/touch %))]
(testing "touch"
(is (= (touched (d/entity db 1))
{:db/id 1
:name "Ivan"
:profile {:db/id 3
:email "@3"}}))
(is (= (touched (d/entity (d/db-with db [[:db/add 3 :profile 4]]) 1))
{:db/id 1
:name "Ivan"
:profile {:db/id 3
:email "@3"
:profile {:db/id 4
:email "@4"}}})))
(testing "retractEntity"
(let [db (d/db-with db [[:db.fn/retractEntity 1]])]
(is (= (d/q '[:find ?a ?v :where [1 ?a ?v]] db)
#{}))
(is (= (d/q '[:find ?a ?v :where [3 ?a ?v]] db)
#{}))))
(testing "retractAttribute"
(let [db (d/db-with db [[:db.fn/retractAttribute 1 :profile]])]
(is (= (d/q '[:find ?a ?v :where [3 ?a ?v]] db)
#{}))))
(testing "reverse navigation"
(is (= (visible (:_profile (d/entity db 3)))
{:db/id 1})))))
(deftest test-components-multival
(let [db (d/db-with
(d/empty-db {:profile {:db/valueType :db.type/ref
:db/cardinality :db.cardinality/many
:db/isComponent true}})
[{:db/id 1 :name "Ivan" :profile [3 4]}
{:db/id 3 :email "@3"}
{:db/id 4 :email "@4"}])
visible #(edn/read-string (pr-str %))
touched #(visible (d/touch %))]
(testing "touch"
(is (= (touched (d/entity db 1))
{:db/id 1
:name "Ivan"
:profile #{{:db/id 3 :email "@3"}
{:db/id 4 :email "@4"}}})))
(testing "retractEntity"
(let [db (d/db-with db [[:db.fn/retractEntity 1]])]
(is (= (d/q '[:find ?a ?v :in $ [?e ...] :where [?e ?a ?v]] db [1 3 4])
#{}))))
(testing "retractAttribute"
(let [db (d/db-with db [[:db.fn/retractAttribute 1 :profile]])]
(is (= (d/q '[:find ?a ?v :in $ [?e ...] :where [?e ?a ?v]] db [3 4])
#{}))))
(testing "reverse navigation"
(is (= (visible (:_profile (d/entity db 3)))
{:db/id 1})))))