root/projects/whoosh/trunk/tests/test_writing.py @ 415

Revision 415, 1.5 KB (checked in by matt, 7 months ago)

Work on NUMERIC, DATETIME, and BOOLEAN field types.
Changes instances of test_index to testindex.

Line 
1import unittest
2
3import os, random
4
5from whoosh import fields, index, writing
6
7
8class TestWriting(unittest.TestCase):
9    def make_dir(self, name):
10        if not os.path.exists(name):
11            os.mkdir(name)
12   
13    def destroy_dir(self, name):
14        try:
15            os.rmdir("testindex")
16        except:
17            pass
18   
19    def clean_file(self, path):
20        if os.path.exists(path):
21            os.remove(path)
22   
23    def test_asyncwriter(self):
24        self.make_dir("testindex")
25        schema = fields.Schema(id=fields.ID, text=fields.TEXT)
26        ix = index.create_in("testindex", schema)
27       
28        domain = (u"alfa", u"bravo", u"charlie", u"delta", u"echo", u"foxtrot", u"golf", u"hotel", u"india")
29       
30        writers = []
31        for i in xrange(20):
32            w = writing.AsyncWriter(ix.writer)
33            # Simulate doing 20 (near-)simultaneous commits. If we weren't using
34            # AsyncWriter, at least some of these would fail because the first
35            # writer wouldn't be finished yet.
36            writers.append(w)
37            w.add_document(id=unicode(i), text=u" ".join(random.sample(domain, 5)))
38            w.commit()
39       
40        # Wait for all writers to finish before checking the results
41        for w in writers:
42            if w.running:
43                w.join()
44       
45        # Check whether all documents made it into the index.
46        r = ix.reader()
47        self.assertEqual(sorted([int(id) for id in r.lexicon("id")]), range(20))
48
49
50
51if __name__ == '__main__':
52    unittest.main()
Note: See TracBrowser for help on using the browser.