1 | import decimal |
---|
2 | from zope.catalog.interfaces import ICatalog |
---|
3 | from zope.component import queryUtility |
---|
4 | from zope.component.hooks import setSite |
---|
5 | from waeup.ikoba.app import Company |
---|
6 | from waeup.ikoba.payments.payment import Payment, PaymentItem |
---|
7 | from waeup.ikoba.payments.testing import FakePayer, FakePayable |
---|
8 | from waeup.ikoba.testing import FunctionalLayer, FunctionalTestCase |
---|
9 | from waeup.ikoba.payments.catalog import search |
---|
10 | |
---|
11 | |
---|
12 | class FunctionalPaymentCatalogTests(FunctionalTestCase): |
---|
13 | |
---|
14 | layer = FunctionalLayer |
---|
15 | |
---|
16 | def create_app(self): |
---|
17 | self.root = self.getRootFolder() |
---|
18 | self.app = Company() |
---|
19 | self.root['app'] = self.app |
---|
20 | setSite(self.app) |
---|
21 | return self.app |
---|
22 | |
---|
23 | def test_catalog_available(self): |
---|
24 | # the payments catalog will be available in ICompany objects |
---|
25 | self.create_app() |
---|
26 | catalog = queryUtility(ICatalog, name='payments_catalog') |
---|
27 | assert catalog is not None |
---|
28 | |
---|
29 | def test_catalog_catalogs_ipayments(self): |
---|
30 | # the catalog will register IPayment objects stored |
---|
31 | app = self.create_app() |
---|
32 | payment = Payment(payer=FakePayer(), payable=FakePayable()) |
---|
33 | app['p1'] = payment |
---|
34 | result = search(query=payment.payment_id, searchtype='payment_id') |
---|
35 | result = [x for x in result] |
---|
36 | self.assertEqual(len(result), 1) |
---|
37 | |
---|
38 | def test_search_for_payer_id(self): |
---|
39 | # the catalog will search in payer_id field |
---|
40 | app = self.create_app() |
---|
41 | payer = FakePayer() |
---|
42 | payer.payer_id = u'ThePayerID' |
---|
43 | payment = Payment(payer=payer, payable=FakePayable()) |
---|
44 | app['p1'] = payment |
---|
45 | result = search(query='ThePayerID', searchtype='payer_id') |
---|
46 | result = [x for x in result] |
---|
47 | self.assertEqual(len(result), 1) |
---|
48 | |
---|
49 | def test_search_payer_id_starts_with_query(self): |
---|
50 | # we can find payer_ids starting with query string. |
---|
51 | app = self.create_app() |
---|
52 | payer1, payer2, payer3 = FakePayer(), FakePayer(), FakePayer() |
---|
53 | payer1.payer_id = u'PAYER_11' |
---|
54 | payer2.payer_id = u'PAYER_12' |
---|
55 | payer3.payer_id = u'PAYER_20' |
---|
56 | payment1 = Payment(payer=payer1, payable=FakePayable()) |
---|
57 | payment2 = Payment(payer=payer2, payable=FakePayable()) |
---|
58 | payment3 = Payment(payer=payer3, payable=FakePayable()) |
---|
59 | app['p1'], app['p2'], app['p3'] = payment1, payment2, payment3 |
---|
60 | result1 = search(query='PAYER_1', searchtype='payer_id') |
---|
61 | result1 = [x for x in result1] |
---|
62 | self.assertEqual(len(result1), 2) |
---|
63 | result2 = search(query='PAYER_2', searchtype='payer_id') |
---|
64 | result2 = [x for x in result2] |
---|
65 | self.assertEqual(len(result2), 1) |
---|
66 | result3 = search(query='PAYER_3', searchtype='payer_id') |
---|
67 | result3 = [x for x in result3] |
---|
68 | self.assertEqual(len(result3), 0) |
---|
69 | result4 = search(query='PAYER_11', searchtype='payer_id') |
---|
70 | result4 = [x for x in result4] |
---|
71 | self.assertEqual(len(result4), 1) |
---|
72 | |
---|
73 | def test_search_amount(self): |
---|
74 | # we can find given amounts starting with query string. |
---|
75 | app = self.create_app() |
---|
76 | payable1, payable2 = FakePayable(), FakePayable() |
---|
77 | payable1.payment_items = ( |
---|
78 | PaymentItem(u'Item 1', decimal.Decimal("1.0")), ) |
---|
79 | payable2.payment_items = ( |
---|
80 | PaymentItem(u'Item 2', decimal.Decimal("2.0")), ) |
---|
81 | payment1 = Payment(payer=FakePayer(), payable=payable1) |
---|
82 | payment2 = Payment(payer=FakePayer(), payable=payable2) |
---|
83 | app['p1'], app['p2'] = payment1, payment2 |
---|
84 | result1 = search(query=decimal.Decimal("1.00"), searchtype='amount') |
---|
85 | result1 = [x for x in result1] |
---|
86 | self.assertEqual(len(result1), 1) |
---|
87 | result2 = search(query=decimal.Decimal("2.00"), searchtype='amount') |
---|
88 | result2 = [x for x in result2] |
---|
89 | self.assertEqual(len(result2), 1) |
---|
90 | result3 = search(query=decimal.Decimal("3.00"), searchtype='amount') |
---|
91 | result3 = [x for x in result3] |
---|
92 | self.assertEqual(len(result3), 0) |
---|