This repository has been archived by the owner on Jun 23, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmungbean_test.clj
127 lines (106 loc) · 3.49 KB
/
mungbean_test.clj
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
(ns mungbean_test
(:use [clojure.test])
(:require [mungbean :as mongo]
[mungbean.aggregation :as aggregation]
[mungbean.admin :as admin]
)
)
(declare ^:dynamic coll)
(defn insert-test-data [count & values]
(doseq [n (take count (cycle values))] (mongo/insert coll {:foo n}))
)
(defmacro with-mungo [name & body]
`(let [db# (mongo/get-db (.toHex (new mungbean.ObjectId)))]
(let [collection# (mongo/get-collection db# ~name)]
(binding [coll collection#]
(do ~@body)
)
)
(.dropDatabase (.dbAdmin db#))
)
)
(defstruct teststruct :name :age :city :country)
(deftest save-and-load-struct
(with-mungo "struct"
(mongo/insert coll (struct teststruct "James Bond" 42 "London" "UK"))
(is (= "James Bond" ((mongo/query-one coll {:name "James Bond"}) :name)))
)
)
(deftest save-and-load-structural-map
(with-mungo "foo"
(mongo/insert coll {:foo {:bar 2, :zoo 3}, :name "James Bond"})
(is (= {:foo {:bar 2, :zoo 3}, :name "James Bond"} (dissoc (mongo/query-one coll {:name "James Bond"}) :_id)))
)
)
(deftest save-item-read-back-and-delete
(with-mungo "foo"
(let [id (mongo/get-id (mongo/insert coll {:foo "bar"}))]
(is (= ((mongo/find-one coll id) :foo) "bar"))
(mongo/delete-one coll id)
)
)
)
(deftest generate-items-and-query
(with-mungo "foo"
(insert-test-data 10 "foo" "bar" "zoo")
(is (= 3 (count (mongo/query coll :where {:foo "bar"}))))
)
)
(deftest generate-items-and-get-count
(with-mungo "foo"
(insert-test-data 10 "foo" "bar" "zoo")
(is (= 3 (mongo/query coll :operation (aggregation/get-count) :where {:foo "bar"})))
)
)
(deftest generate-items-and-get-distinct
(with-mungo "foo"
(insert-test-data 10 "foo" "bar" "zoo")
(is (= ["bar" "foo" "zoo"] (sort (mongo/query coll :operation (aggregation/get-distinct :foo)))))
)
)
(deftest more-advanced-query
(with-mungo "foo"
(insert-test-data 10 1 3 5)
(is (= 6 (count (mongo/query coll :where {:foo {:$gt 2}}))))
)
)
(deftest admin-interface
(with-mungo "foo"
(admin/ensure-index coll [:name] :unique true :dropDups true)
)
)
(deftest function-can-be-given-to-iterate-items
(with-mungo "foo"
(insert-test-data 10 1 3 5)
(is (= [1 1 1 1 3 3 3 5 5 5] (mongo/query coll :order {:foo :asc} :function (fn [item] (item :foo)))))
)
)
(deftest ordered-items-can-be-queried
(with-mungo "foo"
(admin/ensure-index coll [:foo])
(insert-test-data 10 1 3 5)
(is (= [1 1 1 1 3 3 3 5 5 5] (mongo/query coll :order {:foo :asc} :function (fn [item] (item :foo)))))
(is (= [5 5 5 3 3 3 1 1 1 1] (mongo/query coll :order {:foo :desc} :function (fn [item] (item :foo)))))
)
)
(deftest update-in-place
(with-mungo "foo"
(insert-test-data 10 1 3 5)
(mongo/update coll {:$inc {:foo 3}, :$set {:bar "zoo"}} :multiple true)
(is (= 3 (count (mongo/query coll :where {:foo 8}))))
(is (= 10 (count (mongo/query coll :where {:bar "zoo"}))))
)
)
(deftest do-upsert
(with-mungo "foo"
(mongo/update coll {:$set {:bar "zoo"}} :where {:zoo 1} :upsert true)
(is (= 1 (count (mongo/query coll :where {:zoo 1, :bar "zoo"}))))
)
)
(deftest insert-structured-object
(with-mungo "foo"
(mongo/insert coll {"id" "123"})
(mongo/update coll {"id" "123", "zzz" [1,2]} :where {"id" "123"})
(is (= 1 (count (mongo/query coll :where {"id" "123"}))))
)
)