-
Notifications
You must be signed in to change notification settings - Fork 1
/
cl-elastic-test.lisp
148 lines (130 loc) · 5.51 KB
/
cl-elastic-test.lisp
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
(defpackage cl-elastic-test
(:use :cl
:cl-elastic
:parachute)
(:import-from :named-readtables
:in-readtable)
(:shadow #:run)
(:export cl-elastic-test))
(in-package :cl-elastic-test)
;; NOTE: To run this test file, execute `(asdf:test-system :cl-elastic)' in your Lisp.
(in-readtable hashtable-syntax)
(define-test enable-keywords-test
(let* ((*enable-keywords* t)
(res (cl-elastic::keywords-to-strings #{:settings #{:foo "bar"}})))
(true (nth-value 1 (gethash "settings" res)))
(true (nth-value 1 (gethash "foo" (gethash "settings" res))))
(is equal "bar" (gethash "foo" (gethash "settings" res)))))
(in-readtable :standard)
(defvar *client* (make-instance '<client>))
(define-test simple-es-test
(multiple-value-bind (res status) (send-request *client* "")
(of-type hash-table res)
(is = 200 status)))
(defun get-version ()
(let* ((*enable-keywords* t)
(res (send-request *client* "")))
(nth-value 0 (gethash :number (gethash :version res)))))
(defun major-version ()
(let* ((version (get-version))
(pos (position #\. version)))
(nth-value 0 (parse-integer (subseq version 0 pos)))))
;; TODO check if index exists
(define-test create-and-delete-index-test
:depends-on (simple-es-test)
(multiple-value-bind (res status)
(send-request *client* :elasticsearch-test :method :put)
(true (gethash "acknowledged" res))
(is = 200 status))
(multiple-value-bind (res status)
(send-request *client* :elasticsearch-test :method :delete)
(true (gethash "acknowledged" res))
(is = 200 status)))
(defun create-index (data)
(send-request *client* :elasticsearch-test :method :put :data data))
(defun delete-index ()
(send-request *client* :elasticsearch-test :method :delete))
;; (send-request *client* "elasticsearch-test" :method :delete)
(in-readtable hashtable-syntax)
(define-test index-find-delete-document-test
(if (< 6 (major-version))
(let ((index-settings #{:settings #{:number_of_shards 1}
:mappings #{:properties #{:test #{:type "text"}}}})
(*enable-keywords* t))
(create-index index-settings)
(multiple-value-bind (res status)
(send-request *client* '(:elasticsearch-test :_doc 3) :method :put
:data #{:test "toto"})
(declare (ignore res))
(is = 201 status))
(multiple-value-bind (res status)
(send-request *client* '(:elasticsearch-test :_doc 3) :method :get)
(is = 200 status)
(is equal "toto" (gethash :test (gethash :_source res))))
(multiple-value-bind (res status)
(send-request *client* '(:elasticsearch-test :_doc 3) :method :delete)
(declare (ignore res))
(is = 200 status))
(multiple-value-bind (res status)
(send-request *client* '(:elasticsearch-test :_doc 3) :method :delete)
(declare (ignore res))
(is = 404 status))
(delete-index))
(progn)))
(define-test index-and-search-document-test
(if (< 6 (major-version))
(let ((index-settings #{:settings #{:number_of_shards 1}
:mappings #{:properties #{:test #{:type "text"}}}})
(*enable-keywords* t))
(create-index index-settings)
(multiple-value-bind (res status)
(send-request *client* '(:elasticsearch-test :_doc 3) :method :put
:data #{:test "toto" })
(declare (ignore res))
(is = 201 status))
;; necessary for indexing to finish
(sleep 1)
(multiple-value-bind (res status)
(send-request *client* '(:elasticsearch-test :_search) :method :get
:data #{:query #{:term #{:test "toto"}}})
(is = 200 status)
(is equal
"toto"
(gethash :test (gethash :_source
(car (gethash :hits (gethash :hits res)))))))
(delete-index))
(progn)))
(define-test encode-json-test
(let* ((*enable-keywords* t)
(res (cl-elastic::encode-json (list #{:foo 1} #{:bar 2}))))
(is equal (format nil "{\"foo\":1}~%{\"bar\":2}~%") res)))
(define-test bulk-indexing-test
:depends-on (encode-json-test)
(if (< 6 (major-version))
(let ((index-settings #{:settings #{:number_of_shards 1}
:mappings #{:properties #{:test #{:type :text}}}})
(*enable-keywords* t))
(create-index index-settings)
(multiple-value-bind (res status)
(send-request *client* :_bulk :method :post
:data (list
#{:index #{:_index :elasticsearch-test :_id 3}}
#{:test "foo"}
#{:index #{:_index :elasticsearch-test :_id 2}}
#{:test "bar"}
#{:delete #{:_index :elasticsearch-test :_id 3}}))
(declare (ignore res))
(is = 200 status))
;; necessary for indexing to finish
(sleep 1)
(multiple-value-bind (res status)
(send-request *client* '(:elasticsearch-test :_search) :method :get
:data #{:query #{:term #{:test "bar"}}})
(is = 200 status)
(is equal
"bar"
(gethash :test (gethash :_source
(car (gethash :hits (gethash :hits res)))))))
(delete-index))
(progn)))
(in-readtable :standard)