|
1 | 1 | (ns qbits.spandex.test.core-test |
2 | 2 | (:refer-clojure :exclude [type]) |
3 | | - (:use clojure.test) |
4 | 3 | (:require |
| 4 | + [clojure.test :refer :all] |
5 | 5 | [clojure.core.async :as async] |
6 | 6 | [qbits.spandex :as s] |
7 | 7 | [qbits.spandex.url :as url] |
8 | 8 | [qbits.spandex.utils :as utils]) |
9 | | - (:import (qbits.spandex Response))) |
| 9 | + (:import (qbits.spandex Response) |
| 10 | + (clojure.lang ExceptionInfo))) |
10 | 11 |
|
11 | 12 | (try |
12 | 13 | (require 'qbits.spandex.spec) |
|
18 | 19 |
|
19 | 20 | (def server "http://127.0.0.1:9200") |
20 | 21 | (def index (java.util.UUID/randomUUID)) |
21 | | -(def type (java.util.UUID/randomUUID)) |
| 22 | +(def type :_doc) |
22 | 23 |
|
23 | 24 | (def doc {:some {:fancy "thing"}}) |
24 | 25 | (def doc-id (java.util.UUID/randomUUID)) |
25 | 26 | (def client (s/client {:hosts [server]})) |
26 | 27 | (def sniffer (s/sniffer client)) |
27 | 28 |
|
28 | | -(defn wait! [] |
29 | | - (Thread/sleep 3000)) |
30 | | - |
31 | 29 | (use-fixtures |
32 | 30 | :each |
33 | 31 | (fn [t] |
| 32 | + (t) |
34 | 33 | (try |
35 | 34 | (s/request client |
36 | 35 | {:method :delete |
37 | | - :url [index type]}) |
38 | | - (catch Exception _ nil)) |
39 | | - (t))) |
| 36 | + :url [index]}) |
| 37 | + (catch Exception _ nil)))) |
40 | 38 |
|
41 | 39 | (deftest test-url |
42 | 40 | (is (= (url/encode [:foo 1 "bar"]) "/foo/1/bar")) |
|
78 | 76 | (= (clojure.walk/stringify-keys doc))))) |
79 | 77 |
|
80 | 78 | (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)))))) |
86 | 86 |
|
87 | 87 | (deftest test-async-sync-query |
88 | 88 | (s/request client |
|
99 | 99 | (deliver p response))}) |
100 | 100 | (is (contains? #{200 201} (:status @p))))) |
101 | 101 |
|
| 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 | + |
102 | 114 | (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}}})] |
110 | 118 | (-> (async/go |
111 | 119 | (loop [docs []] |
112 | 120 | (if-let [docs' (-> (async/<! ch) :body :hits :hits seq)] |
113 | 121 | (recur (concat docs docs')) |
114 | 122 | docs))) |
115 | 123 | async/<!! |
116 | | - count |
117 | | - (= 100) |
| 124 | + (->> (map :_source)) |
| 125 | + (= (for [i (range 100)] {:value i})) |
118 | 126 | is))) |
119 | 127 |
|
120 | 128 | (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) |
127 | 130 | (let [ch (s/scroll-chan client {:url [index type :_search] |
128 | | - :body {:size 1}})] |
| 131 | + :body {:size 1 |
| 132 | + :sort {:value :asc}}})] |
129 | 133 | (-> (async/go |
130 | 134 | (loop [docs [] |
131 | 135 | countdown 33] |
|
137 | 141 | (dec countdown))) |
138 | 142 | docs))) |
139 | 143 | 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})) |
142 | 162 | is))) |
143 | 163 |
|
144 | 164 | (deftest test-exceptions [] |
|
0 commit comments