source: main/waeup.ikoba/trunk/src/waeup/ikoba/customers/tests/test_catalog.py

Last change on this file was 12336, checked in by Henrik Bettermann, 10 years ago

Define contract title in product. Customers must not be able to edit contract titles.

  • Property svn:keywords set to Id
File size: 8.8 KB
Line 
1## $Id: test_catalog.py 12336 2014-12-29 16:12:24Z henrik $
2##
3## Copyright (C) 2014 Uli Fouquet & Henrik Bettermann
4## This program is free software; you can redistribute it and/or modify
5## it under the terms of the GNU General Public License as published by
6## the Free Software Foundation; either version 2 of the License, or
7## (at your option) any later version.
8##
9## This program is distributed in the hope that it will be useful,
10## but WITHOUT ANY WARRANTY; without even the implied warranty of
11## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12## GNU General Public License for more details.
13##
14## You should have received a copy of the GNU General Public License
15## along with this program; if not, write to the Free Software
16## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17##
18import os
19import grok
20import shutil
21import tempfile
22from zope.event import notify
23from zope.catalog.interfaces import ICatalog
24from zope.component import queryUtility, createObject
25from zope.component.hooks import setSite
26from hurry.workflow.interfaces import IWorkflowState
27from waeup.ikoba.app import Company
28from waeup.ikoba.interfaces import VERIFIED
29from waeup.ikoba.testing import FunctionalLayer, FunctionalTestCase
30from waeup.ikoba.customers.customer import Customer
31from waeup.ikoba.customers.contracts import SampleContract
32
33
34class CatalogTestSetup(FunctionalTestCase):
35    # A setup for testing catalog related stuff.
36    #
37    # sets up a site with some customer already created.
38    layer = FunctionalLayer
39
40    def setUp(self):
41        super(CatalogTestSetup, self).setUp()
42
43        # Prepopulate ZODB
44        app = Company()
45        self.dc_root = tempfile.mkdtemp()
46        app['datacenter'].setStoragePath(self.dc_root)
47
48        # Prepopulate the ZODB...
49        self.getRootFolder()['app'] = app
50        self.app = self.getRootFolder()['app']
51        setSite(self.app)
52
53        # Create customer with subobjects
54        customer = Customer()
55        customer.firstname = u'Bob'
56        customer.lastname = u'Tester'
57        customer.matric_number = u'1234'
58        self.app['customers'].addCustomer(customer)
59        self.customer_id = customer.customer_id
60        self.customer = self.app['customers'][self.customer_id]
61
62        product = createObject('waeup.Product')
63        product.product_id = u'SAM'
64        product.title = u'Our Sample Product'
65        product.contract_category = u'sample'
66        self.app['products'].addProduct(product)
67        self.product_id = product.product_id
68        self.product = self.app['products'][self.product_id]
69
70        document = createObject('waeup.CustomerSampleDocument')
71        document.title = u'My Sample Document'
72        IWorkflowState(document).setState(VERIFIED)
73        self.customer['documents'].addDocument(document)
74        self.document_id = document.document_id
75        self.document = self.customer['documents'][self.document_id]
76
77        contract = createObject('waeup.SampleContract')
78        contract.contract_id = u'CON1'
79        contract.product_object = product
80        contract.document_object = document
81        contract.last_product_id = product.product_id
82        self.customer['contracts'].addContract(contract)
83        self.contract_id = contract.contract_id
84        self.contract = self.customer['contracts'][self.contract_id]
85
86        return
87
88    def tearDown(self):
89        shutil.rmtree(self.dc_root)
90        super(CatalogTestSetup, self).tearDown()
91        return
92
93
94class CustomersCatalogTests(CatalogTestSetup):
95
96    layer = FunctionalLayer
97
98    def test_get_catalog(self):
99        # We can get an customers catalog if we wish
100        cat = queryUtility(ICatalog, name='customers_catalog')
101        assert cat is not None
102
103    def test_search_by_id(self):
104        # We can find a certain customer id
105        cat = queryUtility(ICatalog, name='customers_catalog')
106        results = cat.searchResults(customer_id=(self.customer_id,
107                                                self.customer_id))
108        results = [x for x in results] # Turn results generator into list
109        assert len(results) == 1
110        assert results[0] is self.app['customers'][self.customer_id]
111
112    def test_search_by_name(self):
113        # We can find a certain name
114        cat = queryUtility(ICatalog, name='customers_catalog')
115        results = cat.searchResults(fullname='Bob Tester')
116        results = [x for x in results] # Turn results generator into list
117        assert len(results) == 1
118        assert results[0] is self.app['customers'][self.customer_id]
119
120
121class DocumentsCatalogTests(CatalogTestSetup):
122
123    layer = FunctionalLayer
124
125    def test_get_catalog(self):
126        # We can get an customers catalog if we wish
127        cat = queryUtility(ICatalog, name='documents_catalog')
128        assert cat is not None
129
130    def test_search_by_id(self):
131        # We can find a certain contract id
132        cat = queryUtility(ICatalog, name='documents_catalog')
133        results = cat.searchResults(document_id=(self.document_id,
134                                                self.document_id))
135        results = [x for x in results] # Turn results generator into list
136        assert len(results) == 1
137        assert results[0] is self.customer['documents'][self.document_id]
138
139
140class ContractsCatalogTests(CatalogTestSetup):
141
142    layer = FunctionalLayer
143
144    def test_get_catalog(self):
145        # We can get an customers catalog if we wish
146        cat = queryUtility(ICatalog, name='contracts_catalog')
147        assert cat is not None
148
149    def test_search_by_id(self):
150        # We can find a certain contract id
151        cat = queryUtility(ICatalog, name='contracts_catalog')
152        results = cat.searchResults(contract_id=(self.contract_id,
153                                                self.contract_id))
154        results = [x for x in results] # Turn results generator into list
155        assert len(results) == 1
156        assert results[0] is self.customer['contracts'][self.contract_id]
157
158    def test_search_by_category(self):
159        # We can find a certain name
160        cat = queryUtility(ICatalog, name='contracts_catalog')
161        results = cat.searchResults(contract_category=('sample', 'sample'))
162        results = [x for x in results] # Turn results generator into list
163        assert len(results) == 1
164        assert results[0] is self.customer['contracts'][self.contract_id]
165
166    def test_search_by_last_product_id(self):
167        # We can find a certain name
168        cat = queryUtility(ICatalog, name='contracts_catalog')
169        results = cat.searchResults(last_product_id=('SAM', 'SAM'))
170        results = [x for x in results] # Turn results generator into list
171        assert len(results) == 1
172        assert results[0] is self.customer['contracts'][self.contract_id]
173        assert results[0].product_object is self.product
174
175    def test_product_removal(self):
176        # This test does not only test catalog components, it also ensures
177        # that the product object attribute of a contract is cleared
178        # but the last_product_id is not.
179        self.assertTrue(self.contract.product_object is self.product)
180        del self.app['products'][self.product_id]
181        self.assertTrue(self.contract.product_object is None)
182        # The document referrer wasn't touched.
183        self.assertTrue(self.contract.document_object is self.document)
184        self.assertEqual(self.contract.last_product_id, 'SAM')
185        # We can still find the contract by last_product_id.
186        cat = queryUtility(ICatalog, name='contracts_catalog')
187        results = cat.searchResults(last_product_id=('SAM', 'SAM'))
188        results = [x for x in results]
189        assert len(results) == 1
190        assert results[0] is self.customer['contracts'][self.contract_id]
191        assert results[0].product_object is None
192        # Product removal is being logged in customers.log
193        logfile = os.path.join(
194            self.app['datacenter'].storage, 'logs', 'customers.log')
195        logcontent = open(logfile).read()
196        self.assertTrue(
197            'INFO - system - ObjectRemovedEvent - K1000000 - CON1 - removed: SAM\n'
198            in logcontent)
199
200    def test_document_removal(self):
201        # This test does not test catalog components at all.
202        # It only ensures that the document object attributes of a contract
203        # are cleared, when a document is removed, much like in the
204        # test_product_removal above.
205        self.assertTrue(self.contract.document_object is self.document)
206        del self.customer['documents'][self.document_id]
207        self.assertTrue(self.contract.document_object is None)
208        # The product referrer wasn't touched.
209        self.assertTrue(self.contract.product_object is self.product)
210        # Document removal is being logged in customers.log
211        logfile = os.path.join(
212            self.app['datacenter'].storage, 'logs', 'customers.log')
213        logcontent = open(logfile).read()
214        self.assertTrue(
215            'INFO - system - ObjectRemovedEvent - K1000000 - CON1 - removed: %s\n'
216            % self.document_id in logcontent)
217
Note: See TracBrowser for help on using the repository browser.