/
compliance_test.cljc
120 lines (111 loc) · 5.38 KB
/
compliance_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
(ns konserve.compliance-test
(:require [clojure.core.async :refer [#?(:clj <!!) <! go]]
[konserve.core :as k]
#?(:cljs [cljs.test :refer [is]])
#?(:clj [clojure.test :refer [are is testing]])))
#_(deftype UnknownType [])
#_(:clj (defn exception? [thing]
(instance? Throwable thing)))
#?(:clj
(defn compliance-test [store]
(doseq [opts [{:sync? false} {:sync? true}]
:let [<!! (if (:sync? opts) identity <!!)]]
(testing "Testing the append store functionality."
(<!! (k/append store :foolog {:bar 42} opts))
(<!! (k/append store :foolog {:bar 43} opts))
(is (= (<!! (k/log store :foolog opts))
'({:bar 42}
{:bar 43})))
(is (= (<!! (k/reduce-log store
:foolog
(fn [acc elem]
(conj acc elem))
[]
opts))
[{:bar 42} {:bar 43}]))
(let [{:keys [key type last-write]} (<!! (k/get-meta store :foolog nil opts))]
(are [x y] (= x y)
:foolog key
:append-log type
java.util.Date (clojure.core/type last-write))))
(testing "Test the core API."
(is (= nil (<!! (k/get store :foo nil opts))))
(is (false? (<!! (k/exists? store :foo opts))))
(<!! (k/assoc store :foo :bar opts))
(is (<!! (k/exists? store :foo opts)))
(is (= :bar (<!! (k/get store :foo nil opts))))
(<!! (k/assoc-in store [:foo] :bar2 opts))
(is (= :bar2 (<!! (k/get store :foo nil opts))))
(is (= :default (<!! (k/get-in store [:fuu] :default opts))))
(is (= :bar2 (<!! (k/get store :foo nil opts))))
(is (= :default (<!! (k/get-in store [:fuu] :default opts))))
(<!! (k/update-in store [:foo] name opts))
(is (= "bar2" (<!! (k/get store :foo nil opts))))
(<!! (k/assoc-in store [:baz] {:bar 42} opts))
(is (= 42 (<!! (k/get-in store [:baz :bar] nil opts))))
(<!! (k/update-in store [:baz :bar] inc opts))
(is (= 43 (<!! (k/get-in store [:baz :bar] nil opts))))
(<!! (k/update-in store [:baz :bar] (fn [x] (+ x 2 3)) opts))
(is (= 48 (<!! (k/get-in store [:baz :bar] nil opts))))
(is (= true (<!! (k/dissoc store :foo opts))))
(is (= false (<!! (k/dissoc store :not-there opts))))
(is (= nil (<!! (k/get-in store [:foo] nil opts))))
(<!! (k/bassoc store :binbar (byte-array (range 10)) opts))
(<!! (k/bget store :binbar (fn [{:keys [input-stream]}]
(go
(is (= (map byte (slurp input-stream))
(range 10)))
true))
opts))
(let [list-keys (<!! (k/keys store opts))]
(are [x y] (= x y)
#{{:key :baz
:type :edn}
{:key :binbar
:type :binary}
{:key :foolog
:type :append-log}}
(->> list-keys (map #(clojure.core/dissoc % :last-write)) set)
true
(every?
(fn [{:keys [:last-write]}]
(= (type (java.util.Date.)) (type last-write)))
list-keys)))
(doseq [to-delete [:baz :binbar :foolog]]
(<!! (k/dissoc store to-delete opts)))
;; TODO fix by adding spec to core and cache namespace
#_(let [params (clojure.core/keys store)
corruptor (fn [s k]
(if (= (type (k s)) clojure.lang.Atom)
(clojure.core/assoc-in s [k] (atom {}))
(clojure.core/assoc-in s [k] (UnknownType.))))
corrupt (reduce corruptor store params)]
(is (exception? (<!! (get corrupt :bad))))
(is (exception? (<!! (get-meta corrupt :bad))))
(is (exception? (<!! (assoc corrupt :bad 10))))
(is (exception? (<!! (dissoc corrupt :bad))))
(is (exception? (<!! (assoc-in corrupt [:bad :robot] 10))))
(is (exception? (<!! (update-in corrupt [:bad :robot] inc))))
(is (exception? (<!! (exists? corrupt :bad))))
(is (exception? (<!! (keys corrupt))))
(is (exception? (<!! (bget corrupt :bad (fn [_] nil)))))
(is (exception? (<!! (bassoc corrupt :binbar (byte-array (range 10)))))))))))
(defn async-compliance-test [store]
(go
(and
(is (= nil (<! (k/get store :foo))))
(is (= [nil :bar] (<! (k/assoc store :foo :bar))))
(is (= :bar (<! (k/get store :foo))))
(is (= [nil :bar2] (<! (k/assoc-in store [:foo] :bar2))))
(is (= :bar2 (<! (k/get store :foo))))
(is (= :default (<! (k/get-in store [:fuu] :default))))
(<! (k/update-in store [:foo] name))
(is (= "bar2" (<! (k/get store :foo))))
(<! (k/assoc-in store [:baz] {:bar 42}))
(is (= (<! (k/get-in store [:baz :bar])) 42))
(<! (k/update-in store [:baz :bar] inc))
(is (= (<! (k/get-in store [:baz :bar])) 43))
(<! (k/update-in store [:baz :bar] #(+ % 2 3)))
(is (= (<! (k/get-in store [:baz :bar])) 48))
(<! (k/dissoc store :foo))
(is (= (<! (k/get-in store [:foo])) nil)))))