3 # smoketest.rb - test Xapian bindings for Ruby
4 # Original version by Paul Legato (plegato@nks.net), 4/17/2006
6 # Originally based on smoketest.php from the PHP4 bindings.
8 # Copyright (C) 2006 Networked Knowledge Systems, Inc.
9 # Copyright (C) 2008,2009,2010,2011,2016 Olly Betts
10 # Copyright (C) 2010 Richard Boulton
12 # This program is free software; you can redistribute it and/or
13 # modify it under the terms of the GNU General Public License as
14 # published by the Free Software Foundation; either version 2 of the
15 # License, or (at your option) any later version.
17 # This program is distributed in the hope that it will be useful,
18 # but WITHOUT ANY WARRANTY; without even the implied warranty of
19 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 # GNU General Public License for more details.
22 # You should have received a copy of the GNU General Public License
23 # along with this program; if not, write to the Free Software
24 # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
31 class TestMatchDecider < Xapian::MatchDecider
33 return doc.value(0) == "yes"
37 class XapianSmoketest < Test::Unit::TestCase
40 @stem = Xapian::Stem.new("english")
42 @doc = Xapian::Document.new()
43 @doc.data = "is there anybody out there?"
44 @doc.add_posting(@stem.call("is"), 1)
45 @doc.add_posting(@stem.call("there"), 2)
46 @doc.add_posting(@stem.call("anybody"), 3)
47 @doc.add_posting(@stem.call("out"), 4)
48 @doc.add_posting(@stem.call("there"), 5)
49 @doc.add_term("XYzzy")
51 @db = Xapian::inmemory_open()
52 @db.add_document(@doc)
54 @enq = Xapian::Enquire.new(@db)
58 # Test the version number reporting functions give plausible results.
59 @v = sprintf("%d.%d.%d", Xapian::major_version(), Xapian::minor_version(),
61 @v2 = Xapian::version_string()
66 assert_equal("Xapian::Stem(english)", @stem.description())
68 assert_equal("is", @stem.call("is"))
69 assert_equal("go", @stem.call("going"))
70 assert_equal("want", @stem.call("wanted"))
71 assert_equal("refer", @stem.call("reference"))
74 # subtests are those on which some test_foo() method depends.
78 assert_equal("is there anybody out there?", @doc.data())
80 assert_equal(@doc.termlist_count(), 5)
81 assert_equal("XYzzy", @doc.terms().first.term)
84 assert_equal(6, @doc.termlist_count())
85 assert_equal(@doc.terms.size(), @doc.termlist_count())
91 assert_equal("WritableDatabase()", @db.description())
92 assert_equal(1, @db.doccount())
96 assert_equal("Query((smoke OR test OR terms))",
97 Xapian::Query.new(Xapian::Query::OP_OR ,["smoke", "test", "terms"]).description())
99 phraseQuery = Xapian::Query.new(Xapian::Query::OP_PHRASE ,["smoke", "test", "tuple"])
100 xorQuery = Xapian::Query.new(Xapian::Query::OP_XOR, [ Xapian::Query.new("smoke"), phraseQuery, "string" ])
102 assert_equal("Query((smoke PHRASE 3 test PHRASE 3 tuple))", phraseQuery.description())
103 assert_equal("Query((smoke XOR (smoke PHRASE 3 test PHRASE 3 tuple) XOR string))", xorQuery.description())
105 assert_equal([Xapian::Term.new("smoke", 1),
106 Xapian::Term.new("string", 1),
107 Xapian::Term.new("test", 1),
108 Xapian::Term.new("tuple", 1)], xorQuery.terms())
110 assert_equal(Xapian::Query::OP_ELITE_SET, 10)
112 assert_equal("Query(<alldocuments>)", Xapian::Query::MatchAll.description())
113 assert_equal("Query()", Xapian::Query::MatchNothing.description())
117 @enq = Xapian::Enquire.new(@db)
120 @enq.query = Xapian::Query.new(Xapian::Query::OP_OR, "there", "is")
121 mset = @enq.mset(0, 10)
123 assert_equal(1, mset.size())
125 # Feature test for Enquire.matching_terms()
126 assert_equal(2, @enq.matching_terms(mset.hit(0)).size())
127 assert_equal([Xapian::Term.new("is", 1), Xapian::Term.new("there", 1)],
128 @enq.matching_terms(mset.hit(0)))
131 def test_004_mset_iterator
132 @enq = Xapian::Enquire.new(@db)
135 @enq.query = Xapian::Query.new(Xapian::Query::OP_OR, "there", "is")
136 mset = @enq.mset(0, 10)
138 assert_equal(mset.matches().size(), mset.size())
142 def test_005_eset_iterator
143 rset = Xapian::RSet.new
147 @enq = Xapian::Enquire.new(@db)
148 @enq.query = Xapian::Query.new(Xapian::Query::OP_OR, "there", "is")
150 eset = @enq.eset(10, rset)
153 assert_equal(3, eset.terms.size())
156 # Feature test for Database.allterms
157 def test_006_database_allterms
158 assert_equal(5, @db.allterms.size())
159 ou_terms = @db.allterms('ou')
160 assert_equal(1, ou_terms.size())
161 assert_equal('out', ou_terms[0].term)
164 # Feature test for Database.postlist
165 def test_007_database_postlist
166 assert_equal(1, @db.postlist("there").size())
169 # Feature test for Database.termlist
170 def test_008_database_termlist
171 assert_equal(5, @db.termlist(1).size())
174 # Feature test for Database.positionlist
175 def test_009_database_positionlist
176 assert_equal(2, @db.positionlist(1, "there").size())
179 # Feature test for Document.values
180 def test_010_document_values
181 assert_equal(0, @doc.values().size())
184 def test_011_matchdecider
185 @doc = Xapian::Document.new()
187 @doc.add_posting(@stem.call("out"), 1)
188 @doc.add_posting(@stem.call("source"), 2)
189 @doc.add_value(0, "yes")
190 @db.add_document(@doc)
192 @query = Xapian::Query.new(@stem.call("out"))
193 enquire = Xapian::Enquire.new(@db)
194 enquire.query = @query
195 mset = enquire.mset(0, 10, nil, TestMatchDecider.new)
196 assert_equal(mset.size(), 1)
197 assert_equal(mset.docid(0), 2)
200 def test_012_metadata
201 assert_equal(@db.get_metadata('Foo'), '')
202 @db.set_metadata('Foo', 'Foo')
203 assert_equal(@db.get_metadata('Foo'), 'Foo')
206 def test_013_scaleweight
207 query = Xapian::Query.new("foo")
208 query2 = Xapian::Query.new(Xapian::Query::OP_SCALE_WEIGHT, query, 5);
209 assert_equal(query2.description(), "Query(5 * foo)")
212 def test_014_sortable_serialise
213 # In Xapian 1.0.13/1.1.1 and earlier, the SWIG generated wrapper code
214 # didn't handle integer values > MAXINT for double parameters.
216 assert_equal(v, Xapian::sortable_unserialise(Xapian::sortable_serialise(v)))
219 def test_015_valuecount_matchspy
220 spy = Xapian::ValueCountMatchSpy.new(0)
221 doc = Xapian::Document.new()
222 doc.add_posting("term", 1)
223 doc.add_value(0, "yes")
224 @db.add_document(doc)
225 @db.add_document(doc)
226 doc.add_value(0, "maybe")
227 @db.add_document(doc)
228 doc.add_value(0, "no")
229 @db.add_document(doc)
230 query = Xapian::Query.new("term")
231 enquire = Xapian::Enquire.new(@db)
232 enquire.query = query
233 enquire.add_matchspy(spy)
234 mset = enquire.mset(0, 10)
235 assert_equal(mset.size(), 4)
236 assert_equal(spy.values.map{|i| "%s:%d"%[i.term, i.termfreq]} * ",",
237 "maybe:1,no:1,yes:2")
238 assert_equal(spy.top_values(1).map{|i| "%s:%d"%[i.term, i.termfreq]} * ",",
240 assert_equal(spy.top_values(2).map{|i| "%s:%d"%[i.term, i.termfreq]} * ",",
242 assert_equal(spy.top_values(3).map{|i| "%s:%d"%[i.term, i.termfreq]} * ",",
243 "yes:2,maybe:1,no:1")
245 # Test the valuestream iterator, while we've got some data
246 assert_equal(@db.valuestream(1).size(), 0)
247 assert_equal(@db.valuestream(0).map{|i| "%d:%s"%[i.docid, i.value]}*",",
248 "2:yes,3:yes,4:maybe,5:no")
251 def test_016_compactor
252 Dir.mktmpdir("smokerb") {|tmpdir|
253 db1path = "#{tmpdir}db1"
254 db2path = "#{tmpdir}db2"
255 db3path = "#{tmpdir}db3"
257 # Set up a couple of sample input databases
258 db1 = Xapian::WritableDatabase.new(db1path, Xapian::DB_CREATE_OR_OVERWRITE)
259 doc1 = Xapian::Document.new()
260 doc1.add_term('Hello')
261 doc1.add_term('Hello1')
262 doc1.add_value(0, 'Val1')
263 db1.set_metadata('key', '1')
264 db1.set_metadata('key1', '1')
265 db1.add_document(doc1)
268 db2 = Xapian::WritableDatabase.new(db2path, Xapian::DB_CREATE_OR_OVERWRITE)
269 doc2 = Xapian::Document.new()
270 doc2.add_term('Hello')
271 doc2.add_term('Hello2')
272 doc2.add_value(0, 'Val2')
273 db2.set_metadata('key', '2')
274 db2.set_metadata('key2', '2')
275 db2.add_document(doc2)
278 # Compact with the default compactor
279 # Metadata conflicts are resolved by picking the first value
280 c = Xapian::Compactor.new()
281 c.add_source(db1path)
282 c.add_source(db2path)
283 c.set_destdir(db3path)
286 db3 = Xapian::Database.new(db3path)
287 #assert_equal([(item.term, item.termfreq) for item in db3.allterms()],
288 # [('Hello', 2), ('Hello1', 1), ('Hello2', 1)])
289 assert_equal(db3.document(1).value(0), 'Val1')
290 assert_equal(db3.document(2).value(0), 'Val2')
291 assert_equal(db3.get_metadata('key'), '1')
292 assert_equal(db3.get_metadata('key1'), '1')
293 assert_equal(db3.get_metadata('key2'), '2')
297 def test_016_latlongcoords_iterator
298 coords = Xapian::LatLongCoords.new()
299 coords.append(Xapian::LatLongCoord.new(0, 0))
300 assert_equal(coords.size(), 1)
301 assert_equal(coords.all.map{|i| "%s"%i.description}*",",
302 "Xapian::LatLongCoord(0, 0)")
305 end # class XapianSmoketest