Skip to content

Commit 9ebd2cb

Browse files
committed
spandex: Improve tests, exact results for chan and search-after
Also ensure we cleanup the testing index when the tests finished
1 parent 447e7a7 commit 9ebd2cb

File tree

1 file changed

+52
-32
lines changed

1 file changed

+52
-32
lines changed

test/qbits/spandex/test/core_test.clj

Lines changed: 52 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
(ns qbits.spandex.test.core-test
22
(:refer-clojure :exclude [type])
3-
(:use clojure.test)
43
(:require
4+
[clojure.test :refer :all]
55
[clojure.core.async :as async]
66
[qbits.spandex :as s]
77
[qbits.spandex.url :as url]
88
[qbits.spandex.utils :as utils])
9-
(:import (qbits.spandex Response)))
9+
(:import (qbits.spandex Response)
10+
(clojure.lang ExceptionInfo)))
1011

1112
(try
1213
(require 'qbits.spandex.spec)
@@ -18,25 +19,22 @@
1819

1920
(def server "http://127.0.0.1:9200")
2021
(def index (java.util.UUID/randomUUID))
21-
(def type (java.util.UUID/randomUUID))
22+
(def type :_doc)
2223

2324
(def doc {:some {:fancy "thing"}})
2425
(def doc-id (java.util.UUID/randomUUID))
2526
(def client (s/client {:hosts [server]}))
2627
(def sniffer (s/sniffer client))
2728

28-
(defn wait! []
29-
(Thread/sleep 3000))
30-
3129
(use-fixtures
3230
:each
3331
(fn [t]
32+
(t)
3433
(try
3534
(s/request client
3635
{:method :delete
37-
:url [index type]})
38-
(catch Exception _ nil))
39-
(t)))
36+
:url [index]})
37+
(catch Exception _ nil))))
4038

4139
(deftest test-url
4240
(is (= (url/encode [:foo 1 "bar"]) "/foo/1/bar"))
@@ -78,11 +76,13 @@
7876
(= (clojure.walk/stringify-keys doc)))))
7977

8078
(deftest test-head-req
81-
(is (-> (s/request client
82-
{:url [index type doc-id]
83-
:method :head})
84-
:body
85-
nil?)))
79+
(try
80+
(s/request client
81+
{:url [index type doc-id]
82+
:method :head})
83+
(is false)
84+
(catch ExceptionInfo ex
85+
(is (-> ex ex-data :status (= 404))))))
8686

8787
(deftest test-async-sync-query
8888
(s/request client
@@ -99,33 +99,37 @@
9999
(deliver p response))})
100100
(is (contains? #{200 201} (:status @p)))))
101101

102+
(defn- fill-in-docs!
103+
[i]
104+
(s/request client
105+
{:url [index type "_bulk"]
106+
:method :post
107+
:query-string {:refresh true}
108+
:body (->> (for [i (range i)]
109+
[{:index {:_id i}}
110+
{:value i}])
111+
(mapcat identity)
112+
(s/chunks->body))}))
113+
102114
(deftest test-scrolling-chan
103-
(dotimes [i 99]
104-
(s/request client
105-
{:url [index type (str i)]
106-
:method :post
107-
:body doc}))
108-
(wait!)
109-
(let [ch (s/scroll-chan client {:url [index type :_search]})]
115+
(fill-in-docs! 100)
116+
(let [ch (s/scroll-chan client {:url [index type :_search]
117+
:body {:sort {:value :asc}}})]
110118
(-> (async/go
111119
(loop [docs []]
112120
(if-let [docs' (-> (async/<! ch) :body :hits :hits seq)]
113121
(recur (concat docs docs'))
114122
docs)))
115123
async/<!!
116-
count
117-
(= 100)
124+
(->> (map :_source))
125+
(= (for [i (range 100)] {:value i}))
118126
is)))
119127

120128
(deftest test-scrolling-chan-interupted
121-
(dotimes [i 99]
122-
(s/request client
123-
{:url [index type (str i)]
124-
:method :post
125-
:body doc}))
126-
(wait!)
129+
(fill-in-docs! 100)
127130
(let [ch (s/scroll-chan client {:url [index type :_search]
128-
:body {:size 1}})]
131+
:body {:size 1
132+
:sort {:value :asc}}})]
129133
(-> (async/go
130134
(loop [docs []
131135
countdown 33]
@@ -137,8 +141,24 @@
137141
(dec countdown)))
138142
docs)))
139143
async/<!!
140-
count
141-
(= 33)
144+
(->> (map :_source))
145+
(= (for [i (range 33)] {:value i}))
146+
is)))
147+
148+
(deftest test-search-after-chan
149+
(fill-in-docs! 100)
150+
(let [ch (s/search-after-chan client
151+
{:url [index type :_search]
152+
:body {:size 1
153+
:sort {:value :asc}}})]
154+
(-> (async/go
155+
(loop [docs []]
156+
(if-let [docs' (-> (async/<! ch) :body :hits :hits seq)]
157+
(recur (concat docs docs'))
158+
docs)))
159+
async/<!!
160+
(->> (map :_source))
161+
(= (for [i (range 100)] {:value i}))
142162
is)))
143163

144164
(deftest test-exceptions []

0 commit comments

Comments
 (0)