-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcore_test.cljc
2277 lines (2177 loc) · 118 KB
/
core_test.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
(ns entity-graph.core-test
(:require
#?(:clj [clojure.pprint :refer [pprint]]
:cljs [cljs.pprint :refer [pprint]])
#?(:clj [clojure.test :as t :refer [is are deftest testing]]
:cljs [cljs.test :as t :refer-macros [is are deftest testing]])
[entity-graph.core :refer [create-db check-attr transact pull pull-many cardinality-many?] :as eg]))
(comment
(clojure.test/run-tests)
(cljs.test/run-tests))
;; For Clojure Cursive plugin to be able to resolve these symbols
(declare thrown? thrown-with-msg?)
;; Gain access to non-public functions
(def prepare-tx-data #'entity-graph.core/prepare-tx-data)
(def resolve-lookup-ref-or-throw #'entity-graph.core/resolve-lookup-ref-or-throw)
(def wrap-id #'entity-graph.core/wrap-id)
(def wrap-ids #'entity-graph.core/wrap-ids)
;; =========
;; Test Database Schema
(defn by-global-id [db id]
((:db/eav db) (resolve-lookup-ref-or-throw db [:global-id id])))
(defn by-tempid [db tempids tempid]
(get (:db/eav db) (tempids tempid)))
;; Schema for testing the code of entity-graph.
;; It contains attributes with various properties covering all or almost all possibilities
(def test-schema
{:global-id {:db/doc "Used for easily fetching entities by lookup refs."
:db/unique :db.unique/identity}
;; no AVE index
:person/name
{:db/cardinality :db.cardinality/one}
:person/aliases
{:db/cardinality :db.cardinality/many}
;; AVE index
:person/email
{:db/cardinality :db.cardinality/one
:db/unique :db.unique/identity}
:person/ssn
{:db/cardinality :db.cardinality/one
:db/unique :db.unique/identity}
:person/city
{:db/cardinality :db.cardinality/one
:db/index {:db/map-type :db.map-type/sorted-map}}
:person/past-cities
{:db/cardinality :db.cardinality/many
:db/index {:db/map-type :db.map-type/hash-map}}
:person/salary
{:db/cardinality :db.cardinality/one
:db/index {:db/map-type :db.map-type/avl-map}}
:person/past-salaries
{:db/cardinality :db.cardinality/many
:db/index {:db/map-type :db.map-type/avl-map}}
;; For sorted set test START
:person/past-salaries-sorted
{:db/cardinality :db.cardinality/many
:db/index {:db/map-type :db.map-type/avl-map}
:db/sort {:db/set-type :db.set-type/sorted-set :db/comparator <}}
:person/past-salaries-avl
{:db/cardinality :db.cardinality/many
:db/index {:db/map-type :db.map-type/avl-map}
:db/sort {:db/set-type :db.set-type/avl-set :db/comparator <}}
:person/past-salaries-sorted-no-comparator
{:db/cardinality :db.cardinality/many
:db/index {:db/map-type :db.map-type/avl-map}
:db/sort {:db/set-type :db.set-type/sorted-set}}
:person/past-salaries-avl-no-comparator
{:db/cardinality :db.cardinality/many
:db/index {:db/map-type :db.map-type/avl-map}
:db/sort {:db/set-type :db.set-type/avl-set}}
;; For sorted set test END
:person/friend
{:db/cardinality :db.cardinality/many
:db/valueType :db.type/ref}
:person/best-friend
{:db/cardinality :db.cardinality/one
:db/valueType :db.type/ref}
:person/drivers-license
{:db/cardinality :db.cardinality/one
:db/valueType :db.type/ref
:db/isComponent true}
;; Component attribute cardinality many
:person/passports
{:db/cardinality :db.cardinality/many
:db/valueType :db.type/ref
:db/isComponent true}
;; drivers license
:drivers-license/number
{:db/cardinality :db.cardinality/one
:db/unique :db.unique/value}
:drivers-license/state
{:db/cardinality :db.cardinality/one}
:drivers-license/organ-donor
{:db/cardinality :db.cardinality/one}
;; passport
:passport/number
{:db/cardinality :db.cardinality/one
:db/unique :db.unique/identity}
:passport/date-issued
{:db/cardinality :db.cardinality/one}
;; boolean value
:passport/annulled
{:db/cardinality :db.cardinality/one}
;; :db.unique/value
:reservation/code
{:db/cardinality :db.cardinality/one
:db/unique :db.unique/value}
:person/soulmate
{:db/cardinality :db.cardinality/one
:db/valueType :db.type/ref
:db/unique :db.unique/identity}})
(def db-empty (create-db test-schema))
(def schema (:db/schema db-empty))
(deftest test-ave-index-maps
(let [extra-schema
{:person/city-comparator
{:db/cardinality :db.cardinality/one
:db/index {:db/map-type :db.map-type/sorted-map :db/comparator #(pos? (compare %1 %2))}}
:person/city-avl-comparator
{:db/cardinality :db.cardinality/one
:db/index {:db/map-type :db.map-type/avl-map :db/comparator #(pos? (compare %1 %2))}}}
db-empty (create-db (merge test-schema extra-schema))
ave-city (get-in db-empty [:db/ave :person/city])
ave-past-cities (get-in db-empty [:db/ave :person/past-cities])
ave-salary (get-in db-empty [:db/ave :person/salary])
ave-city-comparator (get-in db-empty [:db/ave :person/city-comparator])
ave-city-avl-comparator (get-in db-empty [:db/ave :person/city-avl-comparator])]
;; :person/city
(is (map? ave-city))
(is (sorted? ave-city))
(is (not= (type ave-city) #?(:clj clojure.data.avl.AVLMap :cljs clojure.data.avl.AVLMap)))
;; :person/past-cities
(is (map? ave-past-cities))
(is (not (sorted? ave-past-cities)))
;; :person/salary
(is (map? ave-salary))
(is (sorted? ave-salary))
(is (= (type ave-salary) #?(:clj clojure.data.avl.AVLMap :cljs clojure.data.avl.AVLMap)))
;; :person/city-comparator
(is (map? ave-city-comparator))
(is (sorted? ave-city-comparator))
(is (not= (type ave-city-comparator) #?(:clj clojure.data.avl.AVLMap :cljs clojure.data.avl.AVLMap)))
(let [{:keys [db-after]} (transact db-empty [{:person/city-comparator "Atlanta"}
{:person/city-comparator "Zaragoza"}])]
(is (= ["Zaragoza" "Atlanta"] (-> db-after :db/ave :person/city-comparator keys))))
;; :person/city-avl-comparator
(is (map? ave-city-avl-comparator))
(is (sorted? ave-city-avl-comparator))
(is (= (type ave-city-avl-comparator) #?(:clj clojure.data.avl.AVLMap :cljs clojure.data.avl.AVLMap)))
(let [{:keys [db-after]} (transact db-empty [{:person/city-avl-comparator "Atlanta"}
{:person/city-avl-comparator "Zaragoza"}])]
(is (= ["Zaragoza" "Atlanta"] (-> db-after :db/ave :person/city-avl-comparator keys))))))
(deftest test-check-attr
(is (= (map #(check-attr db-empty % :db/isRef) [:person/name :person/friend :person/drivers-license])
[false true true]))
(is (= (map #(check-attr db-empty % :db/isComponent) [:person/name :person/friend :person/drivers-license])
[false false true]))
(is (= (map #(check-attr db-empty % :db/cardinality) [:global-id :person/name :person/friend])
[:db.cardinality/one :db.cardinality/one :db.cardinality/many]))
(is (= (map #(check-attr db-empty % :db/unique) [:person/name :global-id :reservation/code])
[:db.unique/false :db.unique/identity :db.unique/value]))
(is (= (map #(check-attr db-empty % :db/sort) [:person/name :person/past-salaries :person/past-salaries-sorted :person/past-salaries-avl])
[:db.sort/false :db.sort/false :db.sort/sorted-set :db.sort/avl-set]))
(is (= (map #(check-attr db-empty % :db/index)
[:person/name :person/email :person/friend :person/city :person/past-cities :person/past-salaries])
[:db.index/false :db.index/hash-map :db.index/hash-map
:db.index/sorted-map :db.index/hash-map :db.index/avl-map]))
(is (= (map #(check-attr db-empty % :db/ave-form) [:person/name :person/email :person/city])
[:db.ave-form/false :db.ave-form/single-e :db.ave-form/eset])))
;; =========
;; Basic Testing
(deftest simple-add-retract
(let [tx-data [{:db/id "mary" :person/name "Mary Todd"}
{:person/name "Abraham Lincoln"
:person/aliases ["Abe"]
:person/email "[email protected]"
:person/city "Washington, D.C."
:person/past-cities ["New York" "Chicago"]
:person/salary 777
:person/past-salaries [111 333 555]
:person/soulmate "mary"
:person/drivers-license {:db/id "license"
:drivers-license/number "123"
:drivers-license/state :NY
:drivers-license/organ-donor false}
:person/passports [{:db/id "passport1"
:passport/number "234"
:passport/date-issued "11-15-2019"}]}
[:db/add "tom" :person/name "Thomas Jefferson"]
[:db/add "tom" :person/aliases "Tommy"]
[:db/add "tom" :person/aliases "TJ"]
[:db/add "tom" :person/email "[email protected]"]
[:db/add "tom" :person/city "Washington, D.C."]
[:db/add "tom" :person/past-cities "Shadwell Plantation"]
[:db/add "tom" :person/past-cities "Gordonsville"]
[:db/add "tom" :person/salary 666]
[:db/add "tom" :person/past-salaries 222]
[:db/add "tom" :person/past-salaries 444]]
{:keys [tx-data db-after tempids]} (transact db-empty tx-data)
abe-id (-> tx-data :map-add :no-id first :db/id)
tom-id (get tempids "tom")
mary-id (get tempids "mary")
license-id (get tempids "license")
passport1-id (get tempids "passport1")]
(is (= (db-after :db/eav)
{tom-id
{:db/id tom-id,
:person/name "Thomas Jefferson",
:person/aliases #{"TJ" "Tommy"},
:person/email "[email protected]",
:person/city "Washington, D.C.",
:person/past-cities #{"Gordonsville" "Shadwell Plantation"},
:person/salary 666,
:person/past-salaries #{222 444}}
mary-id {:db/id mary-id
:person/name "Mary Todd"}
abe-id
{:db/id abe-id
:person/name "Abraham Lincoln",
:person/aliases #{"Abe"},
:person/email "[email protected]",
:person/city "Washington, D.C.",
:person/past-cities #{"New York" "Chicago"},
:person/salary 777,
:person/past-salaries #{111 333 555}
:person/soulmate mary-id
:person/drivers-license license-id
:person/passports #{passport1-id}}
license-id
{:db/id license-id
:drivers-license/number "123"
:drivers-license/organ-donor false
:drivers-license/state :NY}
passport1-id
{:db/id passport1-id
:passport/date-issued "11-15-2019"
:passport/number "234"}}))
(is (= (db-after :db/ave)
{:person/drivers-license {license-id abe-id},
:drivers-license/number {"123" 4}
:person/past-salaries
{111 #{abe-id}, 222 #{tom-id}, 333 #{abe-id}, 444 #{tom-id}, 555 #{abe-id}},
:person/past-salaries-avl-no-comparator {},
:person/past-salaries-avl {},
:person/salary {666 #{tom-id}, 777 #{abe-id}},
:person/city {"Washington, D.C." #{abe-id tom-id}},
:person/past-salaries-sorted-no-comparator {},
:person/past-salaries-sorted {},
:person/soulmate {mary-id abe-id},
:passport/number {"234" passport1-id},
:person/best-friend {},
:reservation/code {},
:person/passports {passport1-id abe-id},
:global-id {},
:person/past-cities
{"Shadwell Plantation" #{tom-id},
"Gordonsville" #{tom-id},
"Chicago" #{abe-id},
"New York" #{abe-id}},
:person/friend {},
:person/email {"[email protected]" tom-id, "[email protected]" abe-id},
:person/ssn {}}))
;; Retract
(let [tx-data
[[:db/retract abe-id :person/name "Abraham Lincoln"]
[:db/retract abe-id :person/aliases "Abe"]
[:db/retract abe-id :person/email "[email protected]"]
[:db/retract abe-id :person/city "Washington, D.C."]
[:db/retract abe-id :person/past-cities "New York"]
[:db/retract abe-id :person/past-cities "Chicago"]
[:db/retract abe-id :person/salary 777]
;; retracting only two of three previously added past salaries
[:db/retract abe-id :person/past-salaries 111]
[:db/retract abe-id :person/past-salaries 333]
;; non-existent past-salary
[:db/retract abe-id :person/past-salaries 3000]
;; elided value
[:db/retract abe-id :person/soulmate]
;; component entity with elided value
[:db/retract abe-id :person/drivers-license]
[:db/retract abe-id :person/passports]
;; retract passport and license
[:db/retract license-id :drivers-license/number "123"]
[:db/retract license-id :drivers-license/state :NY]
[:db/retract license-id :drivers-license/organ-donor false]
[:db/retract passport1-id :passport/number "234"]
[:db/retract passport1-id :passport/date-issued "11-15-2019"]
;; retract Thomas Jefferson
[:db/retract tom-id :person/name "Thomas Jefferson"]
[:db/retract tom-id :person/aliases "Tommy"]
[:db/retract tom-id :person/aliases "TJ"]
[:db/retract tom-id :person/email "[email protected]"]
[:db/retract tom-id :person/city "Washington, D.C."]
[:db/retract tom-id :person/past-cities "Shadwell Plantation"]
[:db/retract tom-id :person/past-cities "Gordonsville"]
[:db/retract tom-id :person/salary 666]
[:db/retract tom-id :person/past-salaries 222]
[:db/retract tom-id :person/past-salaries 444]
[:db/retract mary-id :person/name]]
{:keys [db-after]} (transact db-after tx-data)]
(is (= (db-after :db/eav)
{abe-id {:db/id abe-id
:person/past-salaries #{555}}}))
(is (= (db-after :db/ave)
{:global-id {}
:drivers-license/number {}
:passport/number {}
:person/best-friend {}
:person/city {}
:person/drivers-license {}
:person/email {}
:person/friend {}
:person/passports {}
:person/past-cities {}
:person/past-salaries {555 #{5}}
:person/past-salaries-avl {}
:person/past-salaries-avl-no-comparator {}
:person/past-salaries-sorted {}
:person/past-salaries-sorted-no-comparator {}
:person/salary {}
:person/soulmate {}
:person/ssn {}
:reservation/code {}})))))
;; test to ensure if-some is used in the code, not if-let, since if-let fails for 'false' values
(deftest boolean-value
(let [tx-data [[:db/add "1" :passport/annulled true]
[:db/add "2" :passport/annulled false]
{:person/email "email3" :passport/annulled true}
{:person/email "email4" :passport/annulled false}]
{:keys [db-after tempids]} (transact db-empty tx-data)
id1 (get tempids "1")
id2 (get tempids "2")
id3 (get-in (:db/ave db-after) [:person/email "email3"])
id4 (get-in (:db/ave db-after) [:person/email "email4"])]
(is (= (:db/eav db-after)
{id1 {:db/id id1, :passport/annulled true},
id2 {:db/id id2, :passport/annulled false},
id3 {:person/email "email3" :passport/annulled true, :db/id id3},
id4 {:person/email "email4" :passport/annulled false, :db/id id4}}))
(let [tx-data [[:db/add id1 :passport/annulled false]
[:db/add id2 :passport/annulled true]
{:db/id id3 :passport/annulled false}
{:db/id id4 :passport/annulled true}]
{:keys [db-after tempids]} (transact db-after tx-data)]
(is (= (:db/eav db-after)
{id1 {:db/id id1, :passport/annulled false},
id2 {:db/id id2, :passport/annulled true},
id3 {:person/email "email3" :passport/annulled false , :db/id id3},
id4 {:person/email "email4" :passport/annulled true, :db/id id4}})))))
(deftest lookup-ref-txs
(let [tx-data [{:db/id "person-id"
:person/ssn "111-22-3344"
:person/name "name1"}]
{:keys [db-after tempids]} (transact db-empty tx-data)
person-id (get tempids "person-id")]
(is (= person-id (resolve-lookup-ref-or-throw db-after [:person/ssn "111-22-3344"])))
(let [{:keys [db-after]} (transact db-after [{:db/id [:person/ssn "111-22-3344"] :person/name "name2"}])]
(is (= "name2" (-> db-after :db/eav (get person-id) :person/name))))
(let [{:keys [db-after]} (transact db-after [[:db/add [:person/ssn "111-22-3344"] :person/name "name2"]])]
(is (= "name2" (-> db-after :db/eav (get person-id) :person/name))))
(let [{:keys [db-after]} (transact db-after [[:db/retract [:person/ssn "111-22-3344"] :person/name "name1"]])]
(is (nil? (-> db-after :db/eav (get person-id) :person/name))))
(let [{:keys [db-after]} (transact db-after [[:db/retract [:person/ssn "111-22-3344"] :person/name]])]
(is (nil? (-> db-after :db/eav (get person-id) :person/name))))
(is (thrown-with-msg? #?(:clj java.lang.AssertionError :cljs js/Error.) #":db.error/nil-value"
(transact db-after [[:db/retract [:person/ssn "111-22-3344"] :person/name nil]])))
(let [{:keys [db-after]} (transact db-after [[:db/retractEntity [:person/ssn "111-22-3344"]]])]
(is (nil? (-> db-after :db/eav (get person-id)))))))
(deftest empty-coll-values-map-form
(let [tx-data [{:db/id "empty-set" :person/past-salaries #{}}
{:db/id "empty-sequential" :person/past-salaries []}
{:person/past-salaries #{}}
{:person/past-salaries []}
{:db/id :empty-set :person/past-salaries #{}}
{:db/id :empty-sequential :person/past-salaries []}]
{:keys [db-after tempids tx-data]} (transact db-empty tx-data)]
(is (= {} tempids))
(is (empty? (-> tx-data :map-add :tempids)))
(is (empty? (-> tx-data :map-add :no-ids)))
(is (empty? (-> tx-data :map-add :entity-id)))
(is (empty? (:db/eav db-after)))))
(deftest test-sorted-sets
(let [tx-data [{:global-id :not-sorted :person/past-salaries 100}
{:global-id :sorted :person/past-salaries-sorted 100}
{:global-id :sorted-nocomp :person/past-salaries-sorted-no-comparator 100}
{:global-id :avl :person/past-salaries-avl 100}
{:global-id :avl-nocomp :person/past-salaries-avl-no-comparator 100}]
{:keys [db-after]} (transact db-empty tx-data)]
;; Not sorted
(is (= #?(:clj clojure.lang.PersistentHashSet
:cljs cljs.core/PersistentHashSet)
(type (get (by-global-id db-after :not-sorted) :person/past-salaries))))
;; Sorted
(is (= #?(:clj clojure.lang.PersistentTreeSet
:cljs cljs.core/PersistentTreeSet)
(type (get (by-global-id db-after :sorted) :person/past-salaries-sorted))))
(is (= #?(:clj clojure.lang.PersistentTreeSet
:cljs cljs.core/PersistentTreeSet)
(type (get (by-global-id db-after :sorted-nocomp) :person/past-salaries-sorted-no-comparator))))
;; Sorted avl
(is (= clojure.data.avl.AVLSet
(type (get (by-global-id db-after :avl) :person/past-salaries-avl))))
(is (= clojure.data.avl.AVLSet
(type (get (by-global-id db-after :avl-nocomp) :person/past-salaries-avl-no-comparator))))
;; Test adding data to sorted attributes
(let [tx-data [{:global-id :not-sorted :person/past-salaries 200}
{:global-id :sorted :person/past-salaries-sorted 200}
{:global-id :sorted-nocomp :person/past-salaries-sorted-no-comparator 200}
{:global-id :avl :person/past-salaries-avl 200}
{:global-id :avl-nocomp :person/past-salaries-avl-no-comparator 200}]
{:keys [db-after]} (transact db-after tx-data)]
;; Not sorted
(is (= #?(:clj clojure.lang.PersistentHashSet
:cljs cljs.core/PersistentHashSet)
(type (get (by-global-id db-after :not-sorted) :person/past-salaries))))
;; Sorted
(is (= #?(:clj clojure.lang.PersistentTreeSet
:cljs cljs.core/PersistentTreeSet)
(type (get (by-global-id db-after :sorted)
:person/past-salaries-sorted))))
(is (= #?(:clj clojure.lang.PersistentTreeSet
:cljs cljs.core/PersistentTreeSet)
(type (get (by-global-id db-after :sorted-nocomp) :person/past-salaries-sorted-no-comparator))))
;; Sorted avl
(is (= clojure.data.avl.AVLSet
(type (get (by-global-id db-after :avl) :person/past-salaries-avl))))
(is (= clojure.data.avl.AVLSet
(type (get (by-global-id db-after :avl-nocomp) :person/past-salaries-avl-no-comparator)))))))
;; =========
;; Cardinality Many Values
(deftest cardinality-many-values-in-map-form
"Tests handling of different forms of :db.cardinality/many values in map form tx data"
(let [tx-data [{:db/id "int" :person/past-salaries 5}
{:db/id "string" :person/past-cities "Paris"}
{:db/id "vector" :person/past-cities ["Paris"]}
{:db/id "set" :person/past-cities #{"Paris"}}
{:db/id "vec-vec" :person/past-cities [["Paris"]]}
{:db/id "map" :person/past-cities {:a 1}}]
{:keys [db-after tempids]} (transact db-empty tx-data)
int-id (tempids "int")
string-id (tempids "string")
vector-id (tempids "vector")
set-id (tempids "set")
vec-vec-id (tempids "vec-vec")
map-id (tempids "map")]
(is (= #{5} (-> (:db/eav db-after) (get int-id) :person/past-salaries)))
(is (= #{"Paris"} (-> (:db/eav db-after) (get string-id) :person/past-cities)))
(is (= #{"Paris"} (-> (:db/eav db-after) (get vector-id) :person/past-cities)))
(is (= #{"Paris"} (-> (:db/eav db-after) (get set-id) :person/past-cities)))
(is (= #{["Paris"]} (-> (:db/eav db-after) (get vec-vec-id) :person/past-cities)))
(is (= #{{:a 1}} (-> (:db/eav db-after) (get map-id) :person/past-cities)))
(is (= (:db/ave db-after)
{:global-id {}
:drivers-license/number {}
:passport/number {}
:person/best-friend {}
:person/city {}
:person/drivers-license {}
:person/email {}
:person/friend {}
:person/passports {}
:person/past-cities {"Paris" #{2
3
4}
["Paris"] #{5}
{:a 1} #{6}}
:person/past-salaries {5 #{1}}
:person/past-salaries-avl {}
:person/past-salaries-avl-no-comparator {}
:person/past-salaries-sorted {}
:person/past-salaries-sorted-no-comparator {}
:person/salary {}
:person/soulmate {}
:person/ssn {}
:reservation/code {}}))
;; test union, when there are existing values already
(let [tx-data
[{:db/id (tempids "int") :person/past-salaries 6}
{:db/id (tempids "string") :person/past-cities "London"}
{:db/id (tempids "vector") :person/past-cities ["London"]}
{:db/id (tempids "set") :person/past-cities #{"London"}}
{:db/id (tempids "vec-vec") :person/past-cities [["London"]]}
{:db/id (tempids "map") :person/past-cities {:b 2}}
]
{:keys [db-after]} (transact db-after tx-data)]
(is (= #{5 6} (-> (:db/eav db-after) (get int-id) :person/past-salaries)))
(is (= #{"Paris" "London"} (-> (:db/eav db-after) (get string-id) :person/past-cities)))
(is (= #{"Paris" "London"} (-> (:db/eav db-after) (get vector-id) :person/past-cities)))
(is (= #{"Paris" "London"} (-> (:db/eav db-after) (get set-id) :person/past-cities)))
(is (= #{["Paris"] ["London"]} (-> (:db/eav db-after) (get vec-vec-id) :person/past-cities)))
(is (= #{{:a 1} {:b 2}} (-> (:db/eav db-after) (get map-id) :person/past-cities))))
(let [tx-data
[[:db/retract (tempids "int") :person/past-salaries 5]
[:db/retract (tempids "string") :person/past-cities "Paris"]
[:db/retract (tempids "vector") :person/past-cities "Paris"]
[:db/retract (tempids "set") :person/past-cities "Paris"]
[:db/retract (tempids "vec-vec") :person/past-cities ["Paris"]]
[:db/retract (tempids "map") :person/past-cities {:a 1}]]
{:keys [tx-data db-before db-after tempids]} (transact db-after tx-data)]
(is (= (:db/eav db-after) {}))
(is (= (:db/ave db-after)
{:global-id {}
:drivers-license/number {}
:passport/number {}
:person/best-friend {}
:person/city {}
:person/drivers-license {}
:person/email {}
:person/friend {}
:person/passports {}
:person/past-cities {}
:person/past-salaries {}
:person/past-salaries-avl {}
:person/past-salaries-avl-no-comparator {}
:person/past-salaries-sorted {}
:person/past-salaries-sorted-no-comparator {}
:person/salary {}
:person/soulmate {}
:person/ssn {}
:reservation/code {}})))))
(deftest cardinality-many-values-in-list-form
"Tests handling of different forms of :db.cardinality/many values in list form tx data"
(let [tx-data [[:db/add "int" :person/past-salaries 5]
[:db/add "string" :person/past-cities "Paris"]
[:db/add "string-vec" :person/past-cities ["Paris"]]
[:db/add "string-set" :person/past-cities #{"Paris"}]
[:db/add "vec-vec" :person/past-cities [["Paris"]]]
[:db/add "map-single-value" :person/past-cities {:a 1}]]
{:keys [db-after tempids]} (transact db-empty tx-data)]
(is (= #{5} (-> (:db/eav db-after) (get (tempids "int")) :person/past-salaries)))
(is (= #{"Paris"} (-> (:db/eav db-after) (get (tempids "string")) :person/past-cities)))
;; datomic map form accepts "multivals", list form does NOT
(is (= #{["Paris"]} (-> (:db/eav db-after) (get (tempids "string-vec")) :person/past-cities)))
(is (= #{#{"Paris"}} (-> (:db/eav db-after) (get (tempids "string-set")) :person/past-cities)))
(is (= #{[["Paris"]]} (-> (:db/eav db-after) (get (tempids "vec-vec")) :person/past-cities)))
(is (= #{{:a 1}} (-> (:db/eav db-after) (get (tempids "map-single-value")) :person/past-cities)))
(is (= (:db/ave db-after)
{:global-id {}
:drivers-license/number {}
:passport/number {}
:person/best-friend {}
:person/city {}
:person/drivers-license {}
:person/email {}
:person/friend {}
:person/passports {}
:person/past-cities {"Paris" #{2}
#{"Paris"} #{4}
["Paris"] #{3}
[["Paris"]] #{5}
{:a 1} #{6}}
:person/past-salaries {5 #{1}}
:person/past-salaries-avl {}
:person/past-salaries-avl-no-comparator {}
:person/past-salaries-sorted {}
:person/past-salaries-sorted-no-comparator {}
:person/salary {}
:person/soulmate {}
:person/ssn {}
:reservation/code {}}))
(let [tx-data
[[:db/retract (tempids "int") :person/past-salaries 5]
[:db/retract (tempids "string") :person/past-cities "Paris"]
[:db/retract (tempids "string-vec") :person/past-cities ["Paris"]]
[:db/retract (tempids "string-set") :person/past-cities #{"Paris"}]
[:db/retract (tempids "vec-vec") :person/past-cities [["Paris"]]]
[:db/retract (tempids "map-single-value") :person/past-cities {:a 1}]]
{:keys [db-after]} (transact db-after tx-data)]
(is (= (:db/eav db-after) {}))
(is (= (:db/ave db-after)
{:global-id {}
:drivers-license/number {}
:passport/number {}
:person/best-friend {}
:person/city {}
:person/drivers-license {}
:person/email {}
:person/friend {}
:person/passports {}
:person/past-cities {}
:person/past-salaries {}
:person/past-salaries-avl {}
:person/past-salaries-avl-no-comparator {}
:person/past-salaries-sorted {}
:person/past-salaries-sorted-no-comparator {}
:person/salary {}
:person/soulmate {}
:person/ssn {}
:reservation/code {}})))))
;; =========
;; Expanding Nested Entities
(deftest test-nested-entities
(let [db-empty (create-db {:global-id {:db/unique :db.unique/identity}
:user/favorite-color
{:db/valueType :db.type/ref
:db/isComponent true}
:user/friends
{:db/cardinality :db.cardinality/many
:db/valueType :db.type/ref
:db/isComponent true}})
nested-maps
[{:db/id "pat"
:global-id "pat"
:user/name "Pat"
:user/favorite-color {:db/id "purple" :global-id "purple"
:color/hex "9C27B0" :color/name "Purple"}
:user/friends [{:db/id "reese"
:global-id "reese"
:user/name "Reese"
:user/favorite-color {:db/id "red" :global-id "red"
:color/hex "D50000" :color/name "Red"}}
;; assign id if not provided
{:global-id "joe"
:user/name "Joe"
:user/favorite-color {:global-id "white"
:color/hex "FFFFFF" :color/name "White"}}]}]
{:keys [db-after tempids]} (transact db-empty nested-maps)]
(is (= 6 (count (:db/eav db-after))))
(is (= (by-global-id db-after "pat")
{:db/id (tempids "pat"),
:global-id "pat",
:user/name "Pat",
:user/favorite-color (:db/id (by-global-id db-after "purple")),
:user/friends #{(:db/id (by-global-id db-after "reese"))
(:db/id (by-global-id db-after "joe"))}}))
(is (= (by-global-id db-after "reese")
{:db/id (tempids "reese"),
:global-id "reese",
:user/name "Reese",
:user/favorite-color (:db/id (by-global-id db-after "red"))}))
(is (= (by-global-id db-after "red")
{:db/id (tempids "red"), :global-id "red", :color/hex "D50000", :color/name "Red"}))
(is (= (dissoc (by-global-id db-after "joe") :db/id)
{:global-id "joe",
:user/name "Joe",
:user/favorite-color (:db/id (by-global-id db-after "white"))}))
(is (= (dissoc (by-global-id db-after "white") :db/id)
{:global-id "white",
:color/hex "FFFFFF",
:color/name "White"}))
(is (= (by-global-id db-after "purple")
{:db/id (tempids "purple"),
:global-id "purple",
:color/hex "9C27B0",
:color/name "Purple"}))))
;; =========
;; Tempid Resolution
(deftest test-retract-tempid
(let [tx-data
[{:person/name "Abraham Lincoln"
:person/email "[email protected]"}
{:person/name "John Adams"
:person/email "[email protected]"}
{:person/name "Thomas Jefferson"
:person/email "[email protected]"}]
{:keys [db-after]} (transact db-empty tx-data)
abe-id (resolve-lookup-ref-or-throw db-after [:person/email "[email protected]"])
jonny-id (resolve-lookup-ref-or-throw db-after [:person/email "[email protected]"])
tommy-id (resolve-lookup-ref-or-throw db-after [:person/email "[email protected]"])]
(is (= 3 (count (pull-many db-after [:db/id] [[:person/email "[email protected]"]
[:person/email "[email protected]"]
[:person/email "[email protected]"]]))))
(let [tx-data
[;; resolve tempid via :person/email
[:db/retract "abe" :person/name "Abraham Lincoln"]
[:db/retract "abe" :person/email "[email protected]"]
;; resolve id via :person/email, includes tempid
[:db/retract "jonny-adams" :person/email "[email protected]"]]
{:keys [tx-data db-before db-after tempids]} (transact db-after tx-data)]
(is (= (get tempids "abe") abe-id))
(is (= (get tempids "jonny-adams") jonny-id))
(is (= (db-after :db/eav)
{jonny-id {:person/name "John Adams", :db/id jonny-id},
tommy-id
{:person/name "Thomas Jefferson",
:person/email "[email protected]",
:db/id tommy-id}}))
(is (= (db-after :db/ave)
{:global-id {}
:drivers-license/number {}
:passport/number {}
:person/best-friend {}
:person/city {}
:person/drivers-license {}
:person/email {"[email protected]" 3}
:person/friend {}
:person/passports {}
:person/past-cities {}
:person/past-salaries {}
:person/past-salaries-avl {}
:person/past-salaries-avl-no-comparator {}
:person/past-salaries-sorted {}
:person/past-salaries-sorted-no-comparator {}
:person/salary {}
:person/soulmate {}
:person/ssn {}
:reservation/code {}})))))
(deftest test-unique-identity
(let [tx-data
[{:db/id "unique-identity"
:person/email "[email protected]"
:person/name "unique-identity"}
{:db/id "non-unique"
:person/name "non-unique"}
;; :db.unique/identity comes before non-unique attribute
[:db/add "unq-id-within-tx" :person/email "[email protected]"]
[:db/add "unq-id-within-tx" :person/name "unq-id-within-tx"]
;; :db.unique/identity comes after non-unique attribute
[:db/add "unq-id-within-tx2" :person/name "unq-id-within-tx2"]
[:db/add "unq-id-within-tx2" :person/email "[email protected]"]]
{:keys [db-after tempids]} (transact db-empty tx-data)
id-unique-identity (get tempids "unique-identity")
id-non-unique (get tempids "non-unique")
id-unq-id-within-tx (get tempids "unq-id-within-tx")
id-unq-id-within-tx2 (get tempids "unq-id-within-tx2")]
(is (= (:db/eav db-after)
{id-unique-identity
{:db/id id-unique-identity, :person/email "[email protected]", :person/name "unique-identity"},
id-non-unique
{:db/id id-non-unique, :person/name "non-unique"}
id-unq-id-within-tx
{:db/id id-unq-id-within-tx,
:person/email "[email protected]",
:person/name "unq-id-within-tx"}
id-unq-id-within-tx2
{:db/id id-unq-id-within-tx2,
:person/email "[email protected]",
:person/name "unq-id-within-tx2"}}))
(is (= "[email protected]" (-> (:db/eav db-after) (get id-unique-identity) :person/email)))
(is (= "non-unique" (-> (:db/eav db-after) (get id-non-unique) :person/name)))
;; Test additional assertions
(let [tx-data
[[:db/add "[email protected]" :person/email "[email protected]"]
[:db/add "[email protected]" :person/name "unq-id-within-tx-CHANGED"]
[:db/add "[email protected]" :person/name "unq-id-within-tx2-CHANGED"]
[:db/add "[email protected]" :person/email "[email protected]"]]
{:keys [db-after]} (transact db-after tx-data)]
(is (= "unq-id-within-tx-CHANGED" (-> (:db/eav db-after) (get id-unq-id-within-tx) :person/name)))
(is (= "unq-id-within-tx2-CHANGED" (-> (:db/eav db-after) (get id-unq-id-within-tx2) :person/name))))
;; Test retractions
(let [tx-data
[[:db/retract "unique-identity" :person/email "[email protected]"]
[:db/retract "unique-identity" :person/name "unique-identity"]
[:db/retract "non-unique" :person/name "non-unique"]]
{:keys [db-after tempids]} (transact db-after tx-data)]
;; check removal of all unq/id attrs
(is (nil? (-> (:db/eav db-after) (get (tempids "unique-identity")) :person/email)))
(is (nil? (-> (:db/eav db-after) (get (tempids "unique-identity")) :person/name)))
;; check that attr remains
(is (= "non-unique" (-> (:db/eav db-after) (get id-non-unique) :person/name)))
(is (not= id-non-unique (-> (:db/eav db-after) (get (tempids "non-unique")))))))
;; Trickier cases
(let [tx-data
[{:db/id "temp"
:person/ssn "111-22-3344"
:person/name "John"}
{:db/id "temp2"
:person/email "email"
:person/name "Tom"}]
{:keys [db-after]} (transact db-empty tx-data)]
;; same tempid resolves to different entities based on different unq/id attrs
(let [tx-data
[[:db/add "1" :person/ssn "111-22-3344"]
[:db/add "1" :person/name "Tom"]
[:db/add "1" :person/email "email"]]]
(is (thrown-with-msg? #?(:clj java.lang.AssertionError :cljs js/Error.) #":db.error/unique-conflict" (transact db-after tx-data)))))
;; "transaction matched" case, two tempids resolve to the same - newly created - entity id
(let [tx-data
[[:db/add "1" :person/email "email"]
[:db/add "2" :person/email "email"]]
{:keys [db-after tempids]} (transact db-empty tx-data)
id1 (get tempids "1")
id2 (get tempids "2")]
(is (= id1 id2))
(is (= 1 (count (:db/eav db-after))))))
(deftest simple-retraction-assertion-tempid-1
(let [tx-data [[:db/retract "t" :person/email "email"]
[:db/add "t" :person/email "email"]]]
(is (thrown-with-msg? #?(:clj java.lang.AssertionError :cljs js/Error.)
#":db.error/assertion-retraction-conflict" (transact db-empty tx-data)))))
(deftest simple-retraction-assertion-tempid-2
(let [tx-data [[:db/retract "t" :person/email "email"]
[:db/add "t" :person/ssn "ssn"]]
{:keys [db-after tempids]} (transact db-empty tx-data)]
(is (= 1 (count (:db/eav db-after))))
(is (contains? ((:db/eav db-after) (tempids "t")) :person/ssn))
(is (not (contains? ((:db/eav db-after) (tempids "t")) :person/email)))))
(deftest add-retract-order-tempid-resolution
(let [tx-data
[[:db/add "p1" :person/email "email"]
[:db/add "p1" :person/name "name"]
[:db/add "p2" :person/ssn "111-22-3344"]
[:db/add "p2" :person/name "name2"]]
{:keys [db-after tempids]} (transact db-empty tx-data)
id1 (get tempids "p1")
id2 (get tempids "p2")
tx-data
[[:db/retract "t" :person/email "email"]
[:db/add "t" :person/ssn "111-22-3344"]
[:db/retract "t2" :person/email "email-new"]
[:db/add "t2" :person/ssn "ssn-new"]]
{:keys [db-after tempids]} (transact db-after tx-data)]
;; "t" resolves to last value found in AVE (via :person/ssn "111-22-3344", not via :person/email "email")
(is (= id2 (get tempids "t")))
;; "t2" creates a new eid in db
(is (and (not= id1 id2 (get tempids "t2"))))))
;; simple case
(deftest tempid-resolution-1
(let [tx-data
[[:db/add "p1" :person/email "email"]
[:db/add "p2" :person/email "email"]
[:db/add "p3" :person/email "email2"]]
{:keys [db-after tempids]} (transact db-empty tx-data)
id1 (get tempids "p1")
id2 (get tempids "p2")
id3 (get tempids "p3")]
(is (= id1 id2))
(is (not= id1 id3))
(is (= 2 (count (:db/eav db-after)))))
;; similar to above, but split into 2 txs
(let [tx-data
[[:db/add "p1" :person/email "email"]]
{:keys [db-after tempids]} (transact db-empty tx-data)
id1 (get tempids "p1")]
(let [tx-data
[[:db/add "p2" :person/email "email"]
[:db/add "p2" :person/name "name"]
[:db/add "p3" :person/email "email2"]]
{:keys [db-after tempids]} (transact db-after tx-data)]
(is (= id1 (tempids "p2")))
(is (not= id1 (tempids "p3")))
(is (= 2 (count (:db/eav db-after)))))))
;; introduce conflicting tempid resolutions - result depends on order of forms
(deftest tempid-resolution-2
(let [tx-data
[[:db/add "p3" :person/email "email"]
[:db/add "p3" :person/ssn "111-22-3344"]
[:db/add "p1" :person/ssn "111-22-3344"]
[:db/add "p2" :person/email "email"]]
{:keys [db-after tempids]} (transact db-empty tx-data)]
(is (= (tempids "p1") (tempids "p2") (tempids "p3")))
(is (= 1 (count (:db/eav db-after)))))
;; similar to above, but split into 2 txs
(let [tx-data
[[:db/add "p3" :person/email "email"]
[:db/add "p3" :person/ssn "111-22-3344"]]
{:keys [db-after tempids]} (transact db-empty tx-data)
p3-id (get tempids "p3")]
(let [tx-data
[[:db/add "p1" :person/ssn "111-22-3344"]
[:db/add "p2" :person/email "email"]]
{:keys [db-after tempids]} (transact db-after tx-data)]
(is (= (tempids "p1") (tempids "p2") p3-id))
(is (= 1 (count (:db/eav db-after))))))
;; Now change up the order
(let [tx-data
[[:db/add "p1" :person/ssn "111-22-3344"]
[:db/add "p2" :person/email "email"]
[:db/add "p3" :person/email "email"]
[:db/add "p3" :person/ssn "111-22-3344"]]
[_ prep-tempids] (prepare-tx-data db-empty tx-data)
id1 (get prep-tempids "p1")
id2 (get prep-tempids "p2")
id3 (get prep-tempids "p3")]
(is (thrown-with-msg? #?(:clj java.lang.AssertionError :cljs js/Error.)
#":db.error/unique-conflict" (transact db-empty tx-data)))
(is (not= id1 id2))
(is (= id2 id3)))
;; similar to above, but split into 2 txs
(let [tx-data
[[:db/add "p1" :person/ssn "111-22-3344"]
[:db/add "p2" :person/email "email"]]
{:keys [db-after tempids]} (transact db-empty tx-data)
id1 (get tempids "p1")
id2 (get tempids "p2")]
(is (not= id1 id2))
(let [tx-data
[[:db/add "p3" :person/email "email"]
[:db/add "p3" :person/ssn "111-22-3344"]
[:db/add "p3" :person/name "p3-name"]]
[prep-tx-data prep-tempids] (prepare-tx-data db-after tx-data)
id3 (get prep-tempids "p3")]
;; java.lang.AssertionError: Assert failed: :db.error/unique-conflict Unique conflict:
;; :person/email, value: email already held by: 2 asserted for: 1 (= e held-by)
(is (thrown-with-msg? #?(:clj java.lang.AssertionError :cljs js/Error.)
#":db.error/unique-conflict" (transact db-after tx-data)))
(is (= id3 id1))
(is (not= id3 id2)))))
(deftest tempid-resolution-map-no-id
(let [tx-data [{:person/name "name"}]
{:keys [db-after]} (transact db-empty tx-data)]
(is (= 1 (count (:db/eav db-after))))
(let [[id entity] (first (:db/eav db-after))]
(is (contains? entity :person/name))
(is (contains? entity :db/id))
(is (= id (:db/id entity)))))
(let [tx-data [[:db/add "p1" :person/ssn "1"]
{:person/ssn "1" :person/name "name"}]
{:keys [db-after tempids]} (transact db-empty tx-data)]
(is (= (get tempids "p1") (get-in (:db/ave db-after) [:person/ssn "1"])))
(is (= 1 (count (:db/eav db-after)))))
;; same as above, but different order, which shouldn't make a difference
(let [tx-data [{:person/ssn "1" :person/name "name"}
[:db/add "p1" :person/ssn "1"]]
{:keys [db-after tempids]} (transact db-empty tx-data)]
(is (= (get tempids "p1") (get-in (:db/ave db-after) [:person/ssn "1"])))
(is (= 1 (count (:db/eav db-after)))))
;; same as above, but matching with existing db entities
(let [tx-data [[:db/add "p1" :person/ssn "1" :person/name "name"]]
{:keys [db-after]} (transact db-empty tx-data)]
(let [tx-data [{:person/ssn "1" :person/name "name-change"}]
{:keys [db-after]} (transact db-after tx-data)]
(is (= 1 (count (:db/eav db-after))))
(let [[id entity] (first (:db/eav db-after))]
(is (= "name-change" (get entity :person/name)))
(is (= "1" (get entity :person/ssn)))
(is (contains? entity :db/id))
(is (= id (:db/id entity)))))))
;; =========
;; Conflict Detection ... and Errors/Validation
(deftest test-detect-conflicts
(let [tx-data [[:db/add "geoid" :person/name "Geo"]
[:db/add "geoid" :person/name "NewName"]]]
(is (thrown-with-msg? #?(:clj java.lang.AssertionError :cljs js/Error.) #":db.error/cardinality-one-conflict" (transact db-empty tx-data)))))
(deftest test-validations
(let [tx-data [[:db/bad-op "t" :person/email "email1"]]]
(is (thrown-with-msg? #?(:clj java.lang.AssertionError :cljs js/Error.) #":db.error/invalid-op"
(transact db-empty tx-data))))
(is (thrown-with-msg? #?(:clj java.lang.AssertionError :cljs js/Error.) #":db.error/nil-value"
(transact db-empty [[:db/retract :irrelevant :irrelevant nil]]))))
;; test assertions of cardinality/one attr
(deftest test-assertions-add-one
;; nil values illegal
(let [tx-data
[[:db/add "1" :person/name nil]]]
(is (thrown-with-msg? #?(:clj java.lang.AssertionError :cljs js/Error.) #":db.error/nil-value"
(transact db-empty tx-data))))
;; can't assert and retract the same value
(let [tx-data
[[:db/add "1" :person/name "name1"]
[:db/retract "1" :person/name "name1"]]]
(is (thrown-with-msg? #?(:clj java.lang.AssertionError :cljs js/Error.) #":db.error/assertion-retraction-conflict"
(transact db-empty tx-data))))
;; asserting different values for same :db/cardinality/one [e a] should throw
;; previously no value in db for [e a]
(let [tx-data
[[:db/add "1" :person/name "name1"]
[:db/add "1" :person/name "name2"]]]
(is (thrown-with-msg? #?(:clj java.lang.AssertionError :cljs js/Error.) #":db.error/cardinality-one-conflict"
(transact db-empty tx-data))))
;; asserting different values for same :db.cardinality/one [e a] should throw
;; previously existing value in db for [e a] (different from asserted values)
(let [tx-data
[[:db/add "1" :person/name "name1"]]
{:keys [db-after tempids]} (transact db-empty tx-data)
person-id (get tempids "1")
tx-data
[[:db/add person-id :person/name "name2"]
[:db/add person-id :person/name "name3"]]]
(is (thrown-with-msg? #?(:clj java.lang.AssertionError :cljs js/Error.) #":db.error/cardinality-one-conflict"
(transact db-after tx-data))))
;; asserting different values for same :db/cardinality/one [e a] should throw
;; previously existing value in db for [e a] (existing value same as one of asserted values)
(let [tx-data [[:db/add "1" :person/name "name1"]]