PageRenderTime 27ms CodeModel.GetById 15ms app.highlight 9ms RepoModel.GetById 1ms app.codeStats 0ms

/src/djapian/tests/commiter.py

http://djapian.googlecode.com/
Python | 98 lines | 79 code | 18 blank | 1 comment | 4 complexity | ec9d47465c8389b9cf68a751f0ee303e MD5 | raw file
 1from django.core.paginator import Paginator
 2
 3from djapian.tests.utils import BaseTestCase, Entry, Person
 4from djapian.utils.commiter import Commiter
 5
 6class CommiterTest(BaseTestCase):
 7    num_entries = 30
 8    per_page = 10
 9    num_pages = num_entries / per_page
10
11    def test_concrete_commiter_page(self):
12        def _begin():
13            return 'begin'
14        def _commit():
15            return 'commit'
16        def _rollback():
17            return 'rollback'
18
19        commiter = Commiter.create(False)(
20            _begin,
21            _commit,
22            _rollback
23        )
24
25        self.assertEqual(commiter.begin_object(), None)
26        self.assertEqual(commiter.commit_object(), None)
27        self.assertEqual(commiter.cancel_object(), None)
28        self.assertEqual(commiter.begin_page(), 'begin')
29        self.assertEqual(commiter.commit_page(), 'commit')
30        self.assertEqual(commiter.cancel_page(), 'rollback')
31
32    def test_concrete_commiter_object(self):
33        def _begin():
34            return 'begin'
35        def _commit():
36            return 'commit'
37        def _rollback():
38            return 'rollback'
39
40        commiter = Commiter.create(True)(
41            _begin,
42            _commit,
43            _rollback
44        )
45
46        self.assertEqual(commiter.begin_page(), None)
47        self.assertEqual(commiter.commit_page(), None)
48        self.assertEqual(commiter.cancel_page(), None)
49        self.assertEqual(commiter.begin_object(), 'begin')
50        self.assertEqual(commiter.commit_object(), 'commit')
51        self.assertEqual(commiter.cancel_object(), 'rollback')
52
53    def test_bulk_delete(self):
54        p = Person.objects.create(name="Alex")
55        for i in range(self.num_entries):
56            Entry.objects.create(
57                author=p,
58                title="Entry with number %s" % i,
59                text="foobar " * i
60            )
61        Entry.indexer.update()
62
63        result = Entry.indexer.search("title:number")
64
65        database = Entry.indexer._db.open(write=True)
66
67        commiter = Commiter.create(False)(
68            database.begin_transaction,
69            database.commit_transaction,
70            database.cancel_transaction
71        )
72
73        result_count = result.count()
74        paginator = Paginator(result, self.per_page)
75
76        self.assertEqual(paginator.num_pages, self.num_pages)
77
78        page = paginator.page(2)
79
80        commiter.begin_page()
81        try:
82            for obj in page.object_list:
83                commiter.begin_object()
84                try:
85                    Entry.indexer.delete(obj.pk, database=database)
86                    commiter.commit_object()
87                except Exception:
88                    commiter.cancel_object()
89                    raise
90
91            commiter.commit_page()
92        except Exception:
93            commiter.cancel_page()
94            raise
95
96        # database.commit() if hasattr(database, 'commit') else database.flush()
97        self.assertEqual(Entry.indexer.search("title:number").count(),
98                         result_count - self.per_page)