blob: a718d64b30301531b4b8c45e52d7a64bb42f8387 (
plain)
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
|
;;; guile-xapian --- Guile bindings for Xapian
;;; Copyright © 2026 Arun Isaac <arunisaac@systemreboot.net>
;;;
;;; This file is part of guile-xapian.
;;;
;;; guile-xapian is free software: you can redistribute it and/or
;;; modify it under the terms of the GNU General Public License as
;;; published by the Free Software Foundation, either version 2 of the
;;; License, or (at your option) any later version.
;;;
;;; guile-xapian is distributed in the hope that it will be useful,
;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
;;; General Public License for more details.
;;;
;;; You should have received a copy of the GNU General Public License
;;; along with guile-xapian. If not, see
;;; <https://www.gnu.org/licenses/>.
;;; This test suite is a more or less faithful port of
;;; xapian-bindings/python3/smoketest.py from v1.4.32 of the xapian
;;; repo. Keep this updated with new releases of xapian.
(use-modules (srfi srfi-26)
(srfi srfi-64)
(ice-9 ftw)
(xapian xapian)
;; Prefix with xapian: to avoid overriding guile core
;; bindings.
((xapian wrap) #:select (major-version minor-version) #:prefix xapian:)
((xapian wrap) #:select (revision
version-string
BAD-VALUENO
new-Query
Query-MatchAll
Query-MatchNothing
Query-OP-AND
Query-OP-ELITE-SET
Query-OP-VALUE-RANGE
Query-OP-WILDCARD
Query-OP-SCALE-WEIGHT
Stem-apply
Stem-get-description
Query-get-description
Database-get-metadata
WritableDatabase-set-metadata
Document-add-posting
Enquire-set-collapse-key
Enquire-set-sort-by-value)))
(define (fold-count item result)
(1+ result))
;; TODO: Do this with an in-memory database so that we have less
;; cleanup to do.
(define (call-with-temporary-writable-database path proc)
"Like @code{call-with-writable-database}, but delete the database
after."
(dynamic-wind
(const #t)
(cut call-with-writable-database path proc)
(lambda ()
(for-each (lambda (file)
(delete-file (string-append path "/" file)))
(scandir path (negate (cut member <> (list "." "..")))))
(rmdir path))))
(test-begin "smoketest")
(test-equal "version number reporting"
(string-join (map number->string (list (xapian:major-version)
(xapian:minor-version)
(revision)))
".")
(version-string))
(test-equal "stemmer description"
"Xapian::Stem(english)"
(Stem-get-description (make-stem "english")))
(test-expect-fail "document does not transparently handle a zero byte")
(let* ((data "a\0b")
(doc (make-document #:data data)))
(test-equal "document does not transparently handle a zero byte"
data
(document-data (make-document #:data data))))
(call-with-temporary-writable-database "/tmp/xapian-db"
(lambda (db)
(let ((doc (make-document #:data "is there anybody out there?")))
(add-document! db doc)
(test-equal "document count"
1
(database-document-count db)))))
(test-equal "OR query description 1"
"Query((smoke OR test OR terms))"
(Query-get-description (query-or (query "smoke") (query "test") (query "terms"))))
(test-equal "OR query description 2"
"Query((a OR b))"
(Query-get-description (query-or (query "a") (query "b"))))
(test-equal "value range query description"
"Query(VALUE_RANGE 0 1 4)"
(Query-get-description (new-Query (Query-OP-VALUE-RANGE) 0 "1" "4")))
(let* ((query1 (query-phrase (query "smoke")
(query "test")
(query "tuple")))
(query2 (query-xor (query "smoke")
query1
(query "string"))))
(test-equal "phrase query description"
"Query((smoke PHRASE 3 test PHRASE 3 tuple))"
(Query-get-description query1))
(test-equal "XOR query description"
"Query((smoke XOR (smoke PHRASE 3 test PHRASE 3 tuple) XOR string))"
(Query-get-description query2))
(test-equal "number of terms"
4
(termlist-fold fold-count
0
(query-terms query2))))
;; TODO: Check error reporting of database opening functions as in
;; smoketest.py.
(test-equal "match all query description"
"Query(<alldocuments>)"
(Query-get-description (Query-MatchAll)))
(test-equal "match nothing query description"
"Query()"
(Query-get-description (Query-MatchNothing)))
(test-equal "wildcard query description 1"
"Query(WILDCARD SYNONYM wild)"
(Query-get-description (new-Query (Query-OP-WILDCARD) "wild")))
(test-equal "wildcard query description 2"
"Query(WILDCARD SYNONYM wild)"
(Query-get-description (new-Query (Query-OP-WILDCARD) "wild" 0)))
(call-with-temporary-writable-database "/tmp/xapian-db"
(lambda (db)
(let ((enq (enquire db (query-or (query "there") (query "is"))))
(doc (make-document #:data "is there anybody out there?")))
(test-assert "set BAD_VALUENO collapse key"
(Enquire-set-collapse-key enq (BAD-VALUENO)))
(Document-add-posting doc "is" 1)
(Document-add-posting doc "there" 2)
(Document-add-posting doc "anybody" 3)
(Document-add-posting doc "out" 4)
(Document-add-posting doc "there" 5)
(add-document! db doc)
(test-equal "mset size"
1
(mset-fold fold-count
0
(enquire-mset enq #:maximum-items 10)))
;; TODO: Test matching terms.
;; TODO: Test mset size.
;; TODO: Test expand terms.
;; TODO: Test number of terms in database.
;; TODO: Test number of terms in database allterms.
;; TODO: Test database postlist.
;; TODO: Test database postlist with empty term.
;; TODO: Test database termlist.
;; TODO: Test term iterator skip_to.
;; TODO: Test number of document values.
;; TODO: Check exception handling for Xapian::DocNotFoundError.
)))
(test-equal "value of OP_ELITE_SET"
10
(Query-OP-ELITE-SET))
;; TODO: Test match decider.
;; TODO: Test expand decider.
;; TODO: Check min_wt argument to get_eset() works.
;; TODO: Check QueryParser parsing error.
(test-equal "parse pure NOT query"
"Query((<alldocuments> AND_NOT test@1))"
(Query-get-description (parse-query "NOT test"
#:stemmer (make-stem "none")
#:boolean? #t
#:pure-not? #t)))
(test-equal "parse query with partial matching 1"
"Query((Zfoo@1 AND (WILDCARD SYNONYM o OR Zo@2)))"
(Query-get-description (parse-query "foo o"
#:stemmer (make-stem "en")
#:default-operator (Query-OP-AND)
#:partial? #t)))
(test-equal "parse query with partial matching 2"
"Query((Zfoo@1 AND (WILDCARD SYNONYM outside OR Zoutsid@2)))"
(Query-get-description (parse-query "foo outside"
#:stemmer (make-stem "en")
#:default-operator (Query-OP-AND)
#:partial? #t)))
(test-equal "unicode query description"
"Query((foo OR bar£))"
(Query-get-description (query-or (query "foo") (query "bar£"))))
(test-equal "parse query with unicode characters"
"Query((<alldocuments> AND_NOT Zt\u00e9st@1))"
(Query-get-description (parse-query "NOT tést"
#:stemmer (make-stem "en")
#:boolean? #t
#:pure-not? #t)))
(let* ((data "Unicode with an accént")
(doc (make-document #:data data)))
(Document-add-posting doc
(Stem-apply (make-stem "en") "outér")
1)
(test-equal "unicode document data"
data
(document-data doc))
(test-equal "unicode document termlist"
(list "outér")
(termlist-fold (lambda (term result)
(cons (term-string term)
result))
'()
(document-termlist doc))))
;; TODO: Check simple stopper.
;; TODO: custom Stopper
(test-equal "index text"
'(("foo" 2 (1 4))
("baz" 1 (3))
("bar" 1 (2)))
(let* ((doc (make-document))
(term-generator (make-term-generator #:stem (make-stem "none")
#:document doc)))
(index-text! term-generator "foo bar baz foo")
(termlist-fold (lambda (term result)
(cons (list (term-string term)
(term-wdf term)
(reverse
(positionlist-fold (lambda (position result)
(cons (position-termpos position)
result))
'()
(term-positionlist term))))
result))
'()
(document-termlist doc))))
(test-equal "date value range query description"
"Query(VALUE_RANGE 1 19991203 20011204)"
(Query-get-description
(parse-query "12/03/99..12/04/01"
#:stemmer (make-stem "none")
#:range-processors (list (prefixed-date-range-processor 1 #:prefer-mdy? #t)))))
;; TODO: Test NumberValueRangeProcessor.
(let* ((test-field-processor
(field-processor (lambda (str)
(if (string= str "spam")
(error "already spam")
(query "spam")))))
(prefixes `(("spam" . ,test-field-processor)))
(boolean-prefixes `(("boolspam" . ,test-field-processor))))
(test-equal "field processor"
"Query(spam)"
(Query-get-description
(parse-query "spam:ignored"
#:stemmer (make-stem "none")
#:prefixes prefixes
;; TODO: Add boolean prefixes with other options
;; too.
#:boolean-prefixes boolean-prefixes)))
(test-error "failing field processor"
&error
(parse-query "spam:spam"
#:stemmer (make-stem "none")
#:prefixes prefixes
#:boolean-prefixes boolean-prefixes)))
(call-with-temporary-writable-database "/tmp/xapian-db"
(lambda (db)
(test-equal "get empty database metadata"
""
(Database-get-metadata db "foo"))
(WritableDatabase-set-metadata db "foo" "foo")
(test-equal "get database metadata"
"foo"
(Database-get-metadata db "foo"))
;; TODO: Test empty metadata keys.
))
(test-equal "scale weight query description"
"Query(5 * foo)"
(Query-get-description (new-Query (Query-OP-SCALE-WEIGHT)
(query "foo")
5)))
;; TODO: Test custom stemmer.
(test-end "smoketest")
|