| rev |
line source |
|
corymercurial@16
|
1 """
|
|
corymercurial@16
|
2 Test hypy.lib module
|
|
corymercurial@16
|
3 """
|
|
corymercurial@16
|
4
|
|
corymercurial@2
|
5 from __future__ import with_statement
|
|
corymercurial@2
|
6
|
|
corymercurial@2
|
7 import unittest
|
|
corymercurial@2
|
8 import os
|
|
corymercurial@2
|
9 from contextlib import contextmanager
|
|
corymercurial@2
|
10
|
|
corymercurial@2
|
11 import hypy
|
|
corymercurial@2
|
12
|
|
corymercurial@2
|
13 from hypy import (HDocument, HDatabase, HHit, HResults, HCondition, OpenFailed,
|
|
corymercurial@2
|
14 PutFailed, CloseFailed, FlushFailed, EditFailed)
|
|
corymercurial@2
|
15
|
|
corymercurial@2
|
16 class TestHDocument(unittest.TestCase):
|
|
corymercurial@12
|
17 """
|
|
corymercurial@12
|
18 Test the dictionary and text properties of HDocument
|
|
corymercurial@12
|
19 """
|
|
corymercurial@2
|
20 def setUp(self):
|
|
corymercurial@12
|
21 """
|
|
corymercurial@12
|
22 Create a document
|
|
corymercurial@12
|
23 """
|
|
corymercurial@2
|
24 self.doc = HDocument(uri=u'1')
|
|
corymercurial@2
|
25
|
|
corymercurial@2
|
26 def test_dictlike(self):
|
|
corymercurial@2
|
27 """
|
|
corymercurial@2
|
28 HDocument mostly conforms to the dictionary protocol. Make sure that
|
|
corymercurial@2
|
29 works.
|
|
corymercurial@2
|
30 """
|
|
corymercurial@2
|
31 doc = self.doc
|
|
corymercurial@2
|
32
|
|
corymercurial@2
|
33 # byte strings, other types are not allowed.
|
|
corymercurial@2
|
34 self.assertRaises(TypeError, doc.__setitem__, 'foobar', 'baz')
|
|
corymercurial@2
|
35 self.assertRaises(TypeError, doc.__setitem__, 'foobar', 1)
|
|
corymercurial@2
|
36
|
|
corymercurial@2
|
37 doc[u'foobar'] = u'baz'
|
|
corymercurial@2
|
38 doc[u'foobar']
|
|
corymercurial@2
|
39 self.assertEqual(doc[u'foobar'], u'baz')
|
|
corymercurial@31
|
40 self.assertEqual(doc.get(u'foobar', 'default'), u'baz')
|
|
corymercurial@31
|
41 self.assertEqual(doc.get(u'xyz', 'default'), 'default')
|
|
corymercurial@31
|
42 self.assertEqual(doc.get(u'xyz'), None)
|
|
corymercurial@2
|
43
|
|
corymercurial@2
|
44 newattrs = {u'new1': u'lala', u'foobar': u'bazz'}
|
|
corymercurial@2
|
45 doc.update(newattrs)
|
|
corymercurial@2
|
46 self.assertEqual(sorted(doc.items()), [(u'@uri', u'1'), (u'foobar', u'bazz'), (u'new1',
|
|
corymercurial@2
|
47 u'lala')])
|
|
corymercurial@2
|
48
|
|
corymercurial@2
|
49 doc[u'ninjas'] = u'11'
|
|
corymercurial@2
|
50 self.assertEqual(sorted(doc.keys()), [u'@uri', u'foobar', u'new1', u'ninjas', ])
|
|
corymercurial@2
|
51 self.assertEqual(sorted(doc.values()), [u'1', u'11', u'bazz', u'lala'])
|
|
corymercurial@2
|
52
|
|
corymercurial@26
|
53 self.assertRaises(NotImplementedError, doc.__delitem__, u'ninjas')
|
|
corymercurial@26
|
54
|
|
corymercurial@2
|
55 def test_text(self):
|
|
corymercurial@12
|
56 """
|
|
corymercurial@12
|
57 Mess with document text
|
|
corymercurial@12
|
58 """
|
|
corymercurial@2
|
59 doc = self.doc
|
|
corymercurial@2
|
60 self.assertRaises(TypeError, doc.addText, 'xyz')
|
|
corymercurial@2
|
61 doc.addText(u'xyz')
|
|
corymercurial@2
|
62 self.assertEqual([u'xyz'], doc.getTexts())
|
|
corymercurial@2
|
63 doc.addText(u'123')
|
|
corymercurial@2
|
64 self.assertEqual([u'xyz', u'123'], doc.getTexts())
|
|
corymercurial@2
|
65 self.assertRaises(TypeError, doc.addHiddenText, 'abc')
|
|
corymercurial@2
|
66 doc.addHiddenText(u'abc')
|
|
corymercurial@2
|
67 self.assertEqual([u'xyz', u'123'], doc.getTexts())
|
|
corymercurial@2
|
68
|
|
corymercurial@23
|
69 self.assertEqual(u'xyz\n123', doc.text)
|
|
corymercurial@23
|
70
|
|
corymercurial@71
|
71 doc.addText(u'\u062b')
|
|
corymercurial@71
|
72
|
|
corymercurial@71
|
73 self.assertEqual('xyz123\xd8\xab', doc.encode('utf-8'))
|
|
corymercurial@71
|
74
|
|
corymercurial@26
|
75 def test_unicodeType(self):
|
|
corymercurial@26
|
76 """
|
|
corymercurial@26
|
77 Almost everything in hypy must be unicode
|
|
corymercurial@26
|
78 """
|
|
corymercurial@26
|
79 self.assertRaises(TypeError, HDocument, uri='notunicode')
|
|
corymercurial@26
|
80
|
|
corymercurial@2
|
81
|
|
corymercurial@2
|
82 class TestDatabase(unittest.TestCase):
|
|
corymercurial@2
|
83 """
|
|
corymercurial@2
|
84 Tests HResults, HCondition and HHit. And since you can't test these
|
|
corymercurial@2
|
85 things without a database, test HDatabase.
|
|
corymercurial@2
|
86 """
|
|
corymercurial@2
|
87 @contextmanager
|
|
corymercurial@2
|
88 def freshenDatabase(self, extras=0):
|
|
corymercurial@2
|
89 """
|
|
corymercurial@2
|
90 Use:
|
|
corymercurial@2
|
91 with self.freshenDatabase() as db:
|
|
corymercurial@2
|
92 ... stuff that should test using these three documents ...
|
|
corymercurial@2
|
93 """
|
|
corymercurial@2
|
94 db = HDatabase()
|
|
corymercurial@2
|
95 db.open('_temp_db', 'w')
|
|
corymercurial@2
|
96
|
|
corymercurial@2
|
97 doc = HDocument(uri=u'1')
|
|
corymercurial@2
|
98 doc.addText(u'word this is my document. do you like documents? this one is hi-res.')
|
|
corymercurial@2
|
99 db.putDoc(doc, clean=True)
|
|
corymercurial@2
|
100
|
|
corymercurial@2
|
101 doc = HDocument(uri=u'2')
|
|
corymercurial@2
|
102 doc.addText(u'word lorem ipsum dolor sit amet something whatever whatever i do not remember the rest')
|
|
corymercurial@2
|
103 db.putDoc(doc)
|
|
corymercurial@2
|
104
|
|
corymercurial@2
|
105 doc = HDocument(uri=u'3')
|
|
corymercurial@2
|
106 doc.addText(u'word four score and 7 years ago our forefathers brought forth upon upon something')
|
|
corymercurial@2
|
107 db.putDoc(doc)
|
|
corymercurial@2
|
108
|
|
corymercurial@2
|
109 for x in range(4, extras+4):
|
|
corymercurial@2
|
110 doc = HDocument(uri=unicode(x))
|
|
corymercurial@2
|
111 doc.addText(u'filler filler filler carrot top')
|
|
corymercurial@2
|
112 # set some attributes for attribute operator testing
|
|
corymercurial@2
|
113 doc[u'specialId'] = unicode(x)
|
|
corymercurial@2
|
114 doc[u'description'] = unicode(x) * x
|
|
corymercurial@2
|
115 doc[u'date'] = u'2008-12-%s' % (x,)
|
|
corymercurial@2
|
116 db.putDoc(doc)
|
|
corymercurial@2
|
117
|
|
corymercurial@2
|
118 db.flush()
|
|
corymercurial@2
|
119
|
|
corymercurial@2
|
120 try:
|
|
corymercurial@2
|
121 yield db
|
|
corymercurial@2
|
122 finally:
|
|
corymercurial@2
|
123 try:
|
|
corymercurial@2
|
124 db.close()
|
|
corymercurial@2
|
125 except CloseFailed:
|
|
corymercurial@2
|
126 """Some of the tests do this close on their own."""
|
|
corymercurial@2
|
127
|
|
corymercurial@2
|
128 def test_dbOptimize(self):
|
|
corymercurial@2
|
129 """
|
|
corymercurial@2
|
130 Make sure the various optimize flags do not cause a heart attack.
|
|
corymercurial@2
|
131 """
|
|
corymercurial@2
|
132 with self.freshenDatabase() as db:
|
|
corymercurial@2
|
133 db.optimize(purge=True)
|
|
corymercurial@2
|
134 with self.freshenDatabase() as db:
|
|
corymercurial@2
|
135 db.optimize(opt=True)
|
|
corymercurial@2
|
136 with self.freshenDatabase() as db:
|
|
corymercurial@2
|
137 db.optimize()
|
|
corymercurial@26
|
138 self.assertRaises(NotImplementedError, db.sync)
|
|
corymercurial@2
|
139
|
|
corymercurial@2
|
140 def test_removeUpdate(self):
|
|
corymercurial@2
|
141 """
|
|
corymercurial@2
|
142 Test for document id, update, document removal, len() of database.
|
|
corymercurial@2
|
143 """
|
|
corymercurial@2
|
144 docxx = HDocument(uri=u'xx')
|
|
corymercurial@2
|
145 docxx.addText(u'xx')
|
|
corymercurial@2
|
146
|
|
corymercurial@2
|
147 # id of a non-stored document
|
|
corymercurial@2
|
148 self.assertEqual(docxx.id, -1)
|
|
corymercurial@2
|
149
|
|
corymercurial@26
|
150 # updateAttributes on opened db?
|
|
corymercurial@26
|
151 db = HDatabase()
|
|
corymercurial@26
|
152 self.assertRaises(EditFailed, db.updateAttributes, docxx)
|
|
corymercurial@26
|
153
|
|
corymercurial@2
|
154 # removes and updates
|
|
corymercurial@2
|
155 with self.freshenDatabase() as db:
|
|
corymercurial@2
|
156 # flags; just test that these do not nuke us. no idea what they
|
|
corymercurial@2
|
157 # are supposed to do.
|
|
corymercurial@2
|
158 db.putDoc(docxx, clean=True)
|
|
corymercurial@2
|
159 del db[u'xx']
|
|
corymercurial@2
|
160 # delete same doc twice?
|
|
corymercurial@2
|
161 self.assertRaises(EditFailed, db.remove, uri=u'xx')
|
|
corymercurial@2
|
162
|
|
corymercurial@2
|
163 db.putDoc(docxx)
|
|
corymercurial@2
|
164
|
|
corymercurial@2
|
165 # __len__
|
|
corymercurial@2
|
166 self.assertEqual(len(db), 4)
|
|
corymercurial@2
|
167
|
|
corymercurial@2
|
168 # remove by uri, by id, by reference
|
|
corymercurial@2
|
169 db.remove(uri=u'1')
|
|
corymercurial@2
|
170 self.assertEqual(len(db), 3)
|
|
corymercurial@2
|
171 doc2 = db[u'2']
|
|
corymercurial@2
|
172 db.remove(doc2)
|
|
corymercurial@2
|
173 self.assertEqual(len(db), 2)
|
|
corymercurial@2
|
174 doc3id = db[u'3'].id
|
|
corymercurial@2
|
175 db.remove(id=doc3id)
|
|
corymercurial@2
|
176 self.assertEqual(len(db), 1)
|
|
corymercurial@2
|
177 # no arg?
|
|
corymercurial@2
|
178 self.assertRaises(TypeError, db.remove)
|
|
corymercurial@2
|
179 # already removed?
|
|
corymercurial@2
|
180 self.assertRaises(EditFailed, db.remove, id=doc3id)
|
|
corymercurial@2
|
181
|
|
corymercurial@26
|
182 self.assertRaises(KeyError, db.__getitem__, '1')
|
|
corymercurial@26
|
183
|
|
corymercurial@2
|
184 # fetch a document from the database, edit it, store it, compare
|
|
corymercurial@2
|
185 # it with the original (unfetched) document. Verify that they are
|
|
corymercurial@2
|
186 # different after the edit. Then verify that the document can be
|
|
corymercurial@2
|
187 # fetched (again) from the database with the edited text.
|
|
corymercurial@2
|
188 dbdocxx = db[u'xx']
|
|
corymercurial@2
|
189 # yes, these are different objects
|
|
corymercurial@2
|
190 self.assertFalse(docxx is dbdocxx)
|
|
corymercurial@2
|
191 self.assertTrue(dbdocxx.get(u'zz') is None)
|
|
corymercurial@2
|
192
|
|
corymercurial@2
|
193 dbdocxx[u'zz'] = u'hello'
|
|
corymercurial@2
|
194 db.updateAttributes(dbdocxx)
|
|
corymercurial@2
|
195 dbdocxx2 = db[u'xx']
|
|
corymercurial@2
|
196 # again, different objects
|
|
corymercurial@2
|
197 self.assertFalse(dbdocxx is dbdocxx2)
|
|
corymercurial@2
|
198 self.assertEqual(dbdocxx2.get(u'zz'), u'hello')
|
|
corymercurial@2
|
199
|
|
corymercurial@101
|
200 def test_removeURINone(self):
|
|
corymercurial@101
|
201 """
|
|
corymercurial@101
|
202 #356253: should be able to explicitly say "uri=None" when calling remove
|
|
corymercurial@101
|
203 """
|
|
corymercurial@101
|
204 with self.freshenDatabase() as db:
|
|
corymercurial@101
|
205 # this is odd, but it should work
|
|
corymercurial@101
|
206 db.remove(id=1, uri=None)
|
|
corymercurial@101
|
207
|
|
corymercurial@78
|
208 def test_removeNulls(self):
|
|
corymercurial@78
|
209 """
|
|
corymercurial@78
|
210 Bug 321579: nulls should not kill addText
|
|
corymercurial@78
|
211 """
|
|
corymercurial@78
|
212 docnulls = HDocument(uri=u'nulls\0')
|
|
corymercurial@78
|
213 docnulls.addText(u'hello there\0 children')
|
|
corymercurial@78
|
214 docnulls.addHiddenText(u'sweet\0sweet love')
|
|
corymercurial@78
|
215 docnulls[u'character\0'] = u'chef\0'
|
|
corymercurial@78
|
216 with self.freshenDatabase() as db:
|
|
corymercurial@78
|
217 db.putDoc(docnulls)
|
|
corymercurial@78
|
218 db.flush()
|
|
corymercurial@78
|
219 cond = HCondition(u'there*')
|
|
corymercurial@78
|
220 self.assertEqual(db.search(cond).pluck(u'@uri'), [u'nulls'])
|
|
corymercurial@78
|
221 cond = HCondition(u'sweet*')
|
|
corymercurial@78
|
222 self.assertEqual(db.search(cond).pluck(u'@uri'), [u'nulls'])
|
|
corymercurial@78
|
223
|
|
corymercurial@78
|
224 cond = HCondition()
|
|
corymercurial@78
|
225 cond.addAttr(u'character STREQ chef')
|
|
corymercurial@78
|
226 self.assertEqual(db.search(cond).pluck(u'@uri'), [u'nulls'])
|
|
corymercurial@78
|
227
|
|
corymercurial@78
|
228 docnulls[u'voi\0ce'] = u'Isaac Hayes'
|
|
corymercurial@78
|
229 db.updateAttributes(docnulls)
|
|
corymercurial@78
|
230
|
|
corymercurial@78
|
231 cond = HCondition()
|
|
corymercurial@78
|
232 cond.addAttr(u'voice STRRX .saac.*')
|
|
corymercurial@78
|
233 self.assertEqual(db.search(cond).pluck(u'@uri'), [u'nulls'])
|
|
corymercurial@78
|
234
|
|
corymercurial@2
|
235 def test_putFlags(self):
|
|
corymercurial@2
|
236 """
|
|
corymercurial@2
|
237 Tests for put flags, other put-related corner cases.
|
|
corymercurial@2
|
238 """
|
|
corymercurial@2
|
239 docxx = HDocument(uri=u'xx')
|
|
corymercurial@2
|
240 docxx.addText(u'xx')
|
|
corymercurial@2
|
241
|
|
corymercurial@2
|
242 with self.freshenDatabase() as db:
|
|
corymercurial@2
|
243 # flags; just test that these do not nuke us. no idea what they
|
|
corymercurial@2
|
244 # are supposed to do.
|
|
corymercurial@2
|
245 db.putDoc(docxx, clean=True)
|
|
corymercurial@2
|
246 del db[u'xx']
|
|
corymercurial@2
|
247 db.putDoc(docxx, weight=True)
|
|
corymercurial@2
|
248 del db[u'xx']
|
|
corymercurial@2
|
249
|
|
corymercurial@2
|
250 ## # put same doc twice?
|
|
corymercurial@2
|
251 ## apparently this works. huh.
|
|
corymercurial@2
|
252 ## db.putDoc(docxx); db.putDoc(docxx)
|
|
corymercurial@2
|
253
|
|
corymercurial@2
|
254 def test_condExtras(self):
|
|
corymercurial@2
|
255 """
|
|
corymercurial@2
|
256 Tests for search skip, search options, cond on attributes
|
|
corymercurial@2
|
257 """
|
|
corymercurial@2
|
258 with self.freshenDatabase(extras=10) as db:
|
|
corymercurial@2
|
259 self.assertEqual(len(db), 13)
|
|
corymercurial@2
|
260 # skip and max
|
|
corymercurial@2
|
261 cond4_8 = HCondition(u'filler', max=5)
|
|
corymercurial@2
|
262 cond9_11 = HCondition(u'filler', max=3, skip=5)
|
|
corymercurial@2
|
263 res1 = db.search(cond4_8)
|
|
corymercurial@2
|
264 self.assertEqual(res1.pluck(u'@uri'), list(u'45678'))
|
|
corymercurial@2
|
265 res2 = db.search(cond9_11)
|
|
corymercurial@2
|
266 self.assertEqual(res2.pluck(u'@uri'), [u'9', u'10', u'11'])
|
|
corymercurial@2
|
267
|
|
corymercurial@2
|
268 # union matching
|
|
corymercurial@31
|
269 result = db.search(HCondition(u'ipsum score', matching='simple'))
|
|
corymercurial@2
|
270 self.assertEqual(len(result), 0)
|
|
corymercurial@31
|
271 result = db.search(HCondition(u'ipsum score', matching='union'))
|
|
corymercurial@2
|
272 self.assertEqual(len(result), 2)
|
|
corymercurial@2
|
273
|
|
corymercurial@26
|
274 # isect matching
|
|
corymercurial@31
|
275 result = db.search(HCondition(u'lorem* ipsum*', matching='simple'))
|
|
corymercurial@26
|
276 self.assertEqual(len(result), 1)
|
|
corymercurial@31
|
277 result = db.search(HCondition(u'lorem* ipsum*', matching='isect'))
|
|
corymercurial@26
|
278 self.assertEqual(len(result), 0)
|
|
corymercurial@31
|
279 result = db.search(HCondition(u'lorem ipsum', matching='isect'))
|
|
corymercurial@26
|
280 self.assertEqual(len(result), 1)
|
|
corymercurial@26
|
281
|
|
corymercurial@26
|
282 # rough matching
|
|
corymercurial@31
|
283 result = db.search(HCondition(u'lorem* ipsum*', matching='simple'))
|
|
corymercurial@26
|
284 self.assertEqual(len(result), 1)
|
|
corymercurial@31
|
285 result = db.search(HCondition(u'lorem* ipsum*', matching='rough'))
|
|
corymercurial@26
|
286 self.assertEqual(len(result), 0)
|
|
corymercurial@31
|
287 result = db.search(HCondition(u'lorem ipsum', matching='rough'))
|
|
corymercurial@26
|
288 self.assertEqual(len(result), 1)
|
|
corymercurial@26
|
289
|
|
corymercurial@2
|
290 # fewer-than-max hits
|
|
corymercurial@31
|
291 result = db.search(HCondition(u'7*', matching='simple', max=2))
|
|
corymercurial@2
|
292 self.assertEqual(len(result), 1)
|
|
corymercurial@2
|
293 self.assertEqual(result[0][u'@uri'], u'3')
|
|
corymercurial@2
|
294
|
|
corymercurial@2
|
295 # attribute conditions
|
|
corymercurial@26
|
296 cc = HCondition(u'*.**')
|
|
corymercurial@26
|
297 self.assertRaises(TypeError, cc.addAttr, 'xxx')
|
|
corymercurial@26
|
298
|
|
corymercurial@101
|
299 # None as a kwarg should work (bug #356253)
|
|
corymercurial@101
|
300 c2 = HCondition(phrase=None)
|
|
corymercurial@101
|
301
|
|
corymercurial@72
|
302 def attrSearch(expr, order=None, phrase=None):
|
|
corymercurial@72
|
303 if phrase:
|
|
corymercurial@72
|
304 cond = HCondition(phrase)
|
|
corymercurial@72
|
305 else:
|
|
corymercurial@72
|
306 cond = HCondition()
|
|
corymercurial@2
|
307 cond.addAttr(expr)
|
|
corymercurial@2
|
308 if order:
|
|
corymercurial@26
|
309 self.assertRaises(TypeError, cond.setOrder, '*.**')
|
|
corymercurial@2
|
310 cond.setOrder(order)
|
|
corymercurial@2
|
311 return db.search(cond)
|
|
corymercurial@2
|
312 result = attrSearch(u'description STREQ 4444')
|
|
corymercurial@2
|
313 self.assertEqual(result.pluck(u'@uri'), [u'4'])
|
|
corymercurial@2
|
314 result = attrSearch(u'specialId NUMGE 10')
|
|
corymercurial@2
|
315 self.assertEqual(result.pluck(u'@uri'), [u'10', u'11', u'12', u'13'])
|
|
corymercurial@2
|
316 result = attrSearch(u'date NUMGE 2008-12-09')
|
|
corymercurial@72
|
317 self.assertEqual(sorted(map(int, result.pluck(u'@uri'))), [9, 10, 11, 12, 13])
|
|
corymercurial@2
|
318 result = attrSearch(u'description STRRX .{10,14}')
|
|
corymercurial@2
|
319 self.assertEqual(result.pluck(u'@uri'), [u'10', u'11', u'12', u'13'])
|
|
corymercurial@2
|
320 # ordering and !not
|
|
corymercurial@2
|
321 result = attrSearch(u'description !STRRX .{10,14}', u'@uri NUMA')
|
|
corymercurial@2
|
322 self.assertEqual(result.pluck(u'@uri'), list(u'123456789'))
|
|
corymercurial@2
|
323 result = attrSearch(u'description STRRX .{10,14}', u'@uri NUMD')
|
|
corymercurial@2
|
324 self.assertEqual(result.pluck(u'@uri'), [u'13', u'12', u'11', u'10'])
|
|
corymercurial@2
|
325
|
|
corymercurial@72
|
326 # one attribute search combined with a text search - regular
|
|
corymercurial@72
|
327 # expression match, all characters
|
|
corymercurial@72
|
328 result = attrSearch(u'description STRRX .{10,14}', u'@uri NUMD', u'*.**')
|
|
corymercurial@72
|
329 self.assertEqual(result.pluck(u'@uri'), [u'13', u'12', u'11', u'10'])
|
|
corymercurial@72
|
330
|
|
corymercurial@2
|
331 def test_dbOpenClosed(self):
|
|
corymercurial@2
|
332 """
|
|
corymercurial@2
|
333 Tests for all the db open/close modes
|
|
corymercurial@2
|
334 """
|
|
corymercurial@2
|
335 docyy = HDocument(uri=u'yy')
|
|
corymercurial@2
|
336 docyy.addText(u'yy')
|
|
corymercurial@2
|
337 docxx = HDocument(uri=u'xx')
|
|
corymercurial@2
|
338 docxx.addText(u'xx')
|
|
corymercurial@2
|
339 condxx = HCondition(u'xx')
|
|
corymercurial@2
|
340 condyy = HCondition(u'yy')
|
|
corymercurial@2
|
341
|
|
corymercurial@2
|
342 db = HDatabase()
|
|
corymercurial@2
|
343 # open of unreachable directory
|
|
corymercurial@2
|
344 self.assertRaises(OpenFailed, db.open, 'does/not/exist', 'a')
|
|
corymercurial@2
|
345 # close before successful open
|
|
corymercurial@2
|
346 self.assertRaises(CloseFailed, db.close)
|
|
corymercurial@2
|
347
|
|
corymercurial@2
|
348 # w mode
|
|
corymercurial@2
|
349 db.open('_temp_db', 'w')
|
|
corymercurial@2
|
350 self.assert_(os.path.exists('_temp_db/_idx'))
|
|
corymercurial@2
|
351 db.putDoc(docyy)
|
|
corymercurial@2
|
352 db.close()
|
|
corymercurial@2
|
353
|
|
corymercurial@2
|
354 # r mode
|
|
corymercurial@2
|
355 db.open('_temp_db', 'r')
|
|
corymercurial@2
|
356 # write to read-only db
|
|
corymercurial@2
|
357 self.assertRaises(PutFailed, db.putDoc, docxx)
|
|
corymercurial@2
|
358 self.assertEqual(len(db.search(condyy)), 1)
|
|
corymercurial@2
|
359 db.close()
|
|
corymercurial@2
|
360
|
|
corymercurial@2
|
361 # a mode
|
|
corymercurial@2
|
362 db.open('_temp_db', 'a')
|
|
corymercurial@2
|
363 db.putDoc(docxx)
|
|
corymercurial@2
|
364 db.flush()
|
|
corymercurial@2
|
365 self.assertEqual(len(db.search(condxx)), 1)
|
|
corymercurial@2
|
366 self.assertEqual(len(db.search(condyy)), 1)
|
|
corymercurial@2
|
367 db.close()
|
|
corymercurial@2
|
368
|
|
corymercurial@2
|
369 # w mode again - check that the db is clobbered
|
|
corymercurial@2
|
370 db.open('_temp_db', 'w')
|
|
corymercurial@2
|
371 self.assertEqual(len(db.search(condxx)), 0)
|
|
corymercurial@2
|
372 db.close()
|
|
corymercurial@2
|
373
|
|
corymercurial@2
|
374 # close after successful close
|
|
corymercurial@2
|
375 self.assertRaises(CloseFailed, db.close)
|
|
corymercurial@26
|
376 self.assertRaises(FlushFailed, db.flush)
|
|
corymercurial@2
|
377
|
|
corymercurial@2
|
378 def test_queries(self):
|
|
corymercurial@2
|
379 """
|
|
corymercurial@2
|
380 Test various conditions against an index to make sure search works.
|
|
corymercurial@2
|
381 """
|
|
corymercurial@2
|
382 with self.freshenDatabase() as db:
|
|
corymercurial@2
|
383 # plain search, 8-bit str
|
|
corymercurial@31
|
384 result = db.search(HCondition(u'wor*', matching='simple'))
|
|
corymercurial@2
|
385 self.assertEqual(len(result), 3)
|
|
corymercurial@2
|
386
|
|
corymercurial@2
|
387 # unicode searches
|
|
corymercurial@2
|
388 result = db.search(HCondition(u'wor*', matching='simple'))
|
|
corymercurial@2
|
389 self.assertEqual(len(result), 3)
|
|
corymercurial@2
|
390
|
|
corymercurial@2
|
391 # test simple query with multiple hits
|
|
corymercurial@31
|
392 result = db.search(HCondition(u'res*', matching='simple'))
|
|
corymercurial@31
|
393 self.assertEqual(result.pluck(u'@uri'), [u'1', u'2'])
|
|
corymercurial@2
|
394
|
|
corymercurial@2
|
395 # vary query terms to check result scoring
|
|
corymercurial@31
|
396 result = db.search(HCondition(u'someth* | whatever*', matching='simple', max=2))
|
|
corymercurial@2
|
397 self.assertEqual(result.pluck(u'@uri'), [u'2', u'3'])
|
|
corymercurial@31
|
398 result = db.search(HCondition(u'someth* | upon*', matching='simple', max=2))
|
|
corymercurial@2
|
399 self.assertEqual(result.pluck(u'@uri'), [u'3', u'2']) # FIXME
|
|
corymercurial@2
|
400
|
|
corymercurial@26
|
401 self.assertEqual(result.hintWords(), [u'someth', u'upon'])
|
|
corymercurial@26
|
402
|
|
corymercurial@26
|
403 def test_hits(self):
|
|
corymercurial@26
|
404 """
|
|
corymercurial@26
|
405 Poke at the hits returned by a search and see if document data and
|
|
corymercurial@26
|
406 teaser text come out right.
|
|
corymercurial@26
|
407 """
|
|
corymercurial@26
|
408 with self.freshenDatabase() as db:
|
|
corymercurial@26
|
409 cc = HCondition(u'hi-res')
|
|
corymercurial@26
|
410 for hit in db.search(cc):
|
|
corymercurial@26
|
411 self.assertEqual(str(hit),
|
|
corymercurial@26
|
412 '@digest=17de33c57e358f0fc5d57cd26a08b48e\n@id=1\n@uri=1\n\nword this is my document. do you like documents? this one is hi-res.\n')
|
|
corymercurial@26
|
413 self.assertEqual(hit.teaser([u'document']), u'word this is my <strong>document</strong>. do you li ... ke <strong>document</strong>s? this one is hi-re ... ')
|
|
corymercurial@70
|
414 self.assertEqual(hit.teaser([u'document'], 'rst'), u'word this is my **document**. do you li ... ke **document**s? this one is hi-re ... ')
|
|
corymercurial@26
|
415 self.assertRaises(TypeError, hit.teaser, ['document'])
|
|
corymercurial@26
|
416 self.assertRaises(NotImplementedError, hit.teaser,
|
|
corymercurial@70
|
417 [u'document'], 'pdf')
|
|
corymercurial@26
|
418
|
|
corymercurial@26
|
419 def test_autoflush(self):
|
|
corymercurial@26
|
420 """
|
|
corymercurial@26
|
421 Verify that autoflush is not on when it's not turned on, and that
|
|
corymercurial@26
|
422 words are autoindexed when it is turned on
|
|
corymercurial@26
|
423 """
|
|
corymercurial@26
|
424 cond = HCondition(u'doc*')
|
|
corymercurial@26
|
425
|
|
corymercurial@26
|
426 @contextmanager
|
|
corymercurial@26
|
427 def setup(**kw):
|
|
corymercurial@26
|
428 db1 = HDatabase(**kw)
|
|
corymercurial@26
|
429 db1.open('_temp_db', 'w')
|
|
corymercurial@26
|
430
|
|
corymercurial@26
|
431 doc = HDocument(uri=u'1')
|
|
corymercurial@26
|
432 doc.addText(u'word this is my document. do you like documents? this one is hi-res.')
|
|
corymercurial@26
|
433 db1.putDoc(doc, clean=True)
|
|
corymercurial@26
|
434
|
|
corymercurial@26
|
435 try:
|
|
corymercurial@26
|
436 yield db1
|
|
corymercurial@26
|
437 finally:
|
|
corymercurial@26
|
438 db1.close()
|
|
corymercurial@26
|
439
|
|
corymercurial@26
|
440 # non-autoflush: can't search for doc*
|
|
corymercurial@26
|
441 with setup() as db:
|
|
corymercurial@26
|
442 self.assertEqual(len(db.search(cond)), 0)
|
|
corymercurial@26
|
443
|
|
corymercurial@26
|
444 # autoflush: now you can.
|
|
corymercurial@26
|
445 with setup(autoflush=True) as db:
|
|
corymercurial@26
|
446 self.assertEqual(len(db.search(cond)), 1)
|
|
corymercurial@26
|
447
|
|
corymercurial@26
|
448
|
|
corymercurial@104
|
449 class TestMisc(unittest.TestCase):
|
|
corymercurial@104
|
450 """
|
|
corymercurial@104
|
451 Test misc. features of the package such as version string
|
|
corymercurial@104
|
452 """
|
|
corymercurial@104
|
453 def test_version(self):
|
|
corymercurial@104
|
454 """
|
|
corymercurial@104
|
455 __version__ and other release info can be found in copyright.py and
|
|
corymercurial@104
|
456 __init__.py
|
|
corymercurial@104
|
457
|
|
corymercurial@104
|
458 This doesn't really verify that copyright.py is generated correctly,
|
|
corymercurial@104
|
459 just that it exists and has the right contents.
|
|
corymercurial@104
|
460 """
|
|
corymercurial@104
|
461 from hypy import __version__ as version1
|
|
corymercurial@104
|
462 from hypy.copyright import __version__ as version2
|
|
corymercurial@104
|
463 self.assertEqual(version1, version2)
|
|
corymercurial@104
|
464
|
|
corymercurial@104
|
465
|
|
corymercurial@23
|
466 if __name__ == '__main__':
|
|
corymercurial@23
|
467 unittest.main()
|