[12004] | 1 | ## $Id: test_document.py 12256 2014-12-18 12:58:12Z henrik $ |
---|
[11989] | 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 | ## |
---|
| 18 | """ |
---|
| 19 | Tests for documents. |
---|
| 20 | """ |
---|
[12205] | 21 | import shutil |
---|
| 22 | import tempfile |
---|
| 23 | import grok |
---|
| 24 | from StringIO import StringIO |
---|
[11989] | 25 | from zope.interface.verify import verifyClass, verifyObject |
---|
[12205] | 26 | from zope.component import createObject, queryUtility |
---|
[11989] | 27 | from hurry.workflow.interfaces import ( |
---|
| 28 | IWorkflowInfo, IWorkflowState, InvalidTransitionError) |
---|
[12205] | 29 | from waeup.ikoba.interfaces import ( |
---|
| 30 | IObjectHistory, IFileStoreNameChooser, IFileStoreHandler) |
---|
| 31 | from waeup.ikoba.imagestorage import DefaultStorage |
---|
[11989] | 32 | from waeup.ikoba.customers.interfaces import ( |
---|
| 33 | ICustomerDocumentsContainer, ICustomerDocument) |
---|
| 34 | from waeup.ikoba.interfaces import IObjectHistory |
---|
[12205] | 35 | from waeup.ikoba.customers.customer import Customer |
---|
[11989] | 36 | from waeup.ikoba.customers.documents import ( |
---|
[12205] | 37 | CustomerDocumentsContainer, CustomerSampleDocument, |
---|
| 38 | CustomerDocumentFileStoreHandler, CustomerDocumentFileNameChooser) |
---|
[11989] | 39 | from waeup.ikoba.testing import (FunctionalLayer, FunctionalTestCase) |
---|
| 40 | |
---|
| 41 | class CustomerDocumentsContainerTestCase(FunctionalTestCase): |
---|
| 42 | |
---|
| 43 | layer = FunctionalLayer |
---|
| 44 | |
---|
| 45 | def test_interfaces(self): |
---|
| 46 | # Make sure the correct interfaces are implemented. |
---|
| 47 | self.assertTrue( |
---|
| 48 | verifyClass( |
---|
| 49 | ICustomerDocumentsContainer, CustomerDocumentsContainer) |
---|
| 50 | ) |
---|
| 51 | self.assertTrue( |
---|
| 52 | verifyObject( |
---|
| 53 | ICustomerDocumentsContainer, CustomerDocumentsContainer()) |
---|
| 54 | ) |
---|
| 55 | self.assertTrue( |
---|
| 56 | verifyClass( |
---|
[12057] | 57 | ICustomerDocument, CustomerSampleDocument) |
---|
[11989] | 58 | ) |
---|
| 59 | self.assertTrue( |
---|
| 60 | verifyObject( |
---|
[12057] | 61 | ICustomerDocument, CustomerSampleDocument()) |
---|
[11989] | 62 | ) |
---|
| 63 | return |
---|
| 64 | |
---|
| 65 | def test_addDocument(self): |
---|
| 66 | container = CustomerDocumentsContainer() |
---|
[12057] | 67 | document = createObject(u'waeup.CustomerSampleDocument') |
---|
[12004] | 68 | id = document.document_id |
---|
[11989] | 69 | container.addDocument(document) |
---|
[12004] | 70 | self.assertEqual(container[id], document) |
---|
[11989] | 71 | self.assertRaises(TypeError, container.addDocument, object()) |
---|
[12057] | 72 | self.assertEqual(document.class_name, 'CustomerSampleDocument') |
---|
[11989] | 73 | return |
---|
| 74 | |
---|
| 75 | def test_document_workflow(self): |
---|
[12057] | 76 | document = createObject(u'waeup.CustomerSampleDocument') |
---|
[11989] | 77 | IWorkflowInfo(document).fireTransition('create') |
---|
| 78 | self.assertEqual(IWorkflowState(document).getState(), 'created') |
---|
| 79 | IWorkflowInfo(document).fireTransition('submit') |
---|
| 80 | self.assertEqual(IWorkflowState(document).getState(), 'submitted') |
---|
| 81 | IWorkflowInfo(document).fireTransition('verify') |
---|
| 82 | self.assertEqual(IWorkflowState(document).getState(), 'verified') |
---|
| 83 | IWorkflowInfo(document).fireTransition('reset2') |
---|
| 84 | self.assertEqual(IWorkflowState(document).getState(), 'created') |
---|
| 85 | self.assertRaises(InvalidTransitionError, |
---|
| 86 | IWorkflowInfo(document).fireTransition, 'verify') |
---|
| 87 | IWorkflowInfo(document).fireTransition('submit') |
---|
| 88 | IWorkflowInfo(document).fireTransition('reset3') |
---|
| 89 | self.assertEqual(IWorkflowState(document).getState(), 'created') |
---|
| 90 | return |
---|
| 91 | |
---|
| 92 | def test_document_history(self): |
---|
[12057] | 93 | doc = createObject(u'waeup.CustomerSampleDocument') |
---|
[11989] | 94 | IWorkflowInfo(doc).fireTransition('create') |
---|
| 95 | messages = ' '.join(doc.history.messages) |
---|
| 96 | self.assertTrue('Document created by system' in messages) |
---|
| 97 | return |
---|
[12205] | 98 | |
---|
| 99 | |
---|
| 100 | class CustomerDocumentFileTests(FunctionalTestCase): |
---|
| 101 | |
---|
| 102 | layer = FunctionalLayer |
---|
| 103 | |
---|
| 104 | def setUp(self): |
---|
| 105 | super(CustomerDocumentFileTests, self).setUp() |
---|
| 106 | self.workdir = tempfile.mkdtemp() |
---|
| 107 | return |
---|
| 108 | |
---|
| 109 | def tearDown(self): |
---|
| 110 | super(CustomerDocumentFileTests, self).tearDown() |
---|
| 111 | shutil.rmtree(self.workdir) |
---|
| 112 | return |
---|
| 113 | |
---|
| 114 | def test_file_store_handler_util_accessible(self): |
---|
| 115 | # we can get an IFileStoreHandler utility for documents |
---|
| 116 | handler = queryUtility(IFileStoreHandler, name='file-customerdocument') |
---|
| 117 | self.assertTrue( |
---|
| 118 | isinstance(handler, CustomerDocumentFileStoreHandler)) |
---|
| 119 | return |
---|
| 120 | |
---|
| 121 | def test_file_store_handler(self): |
---|
| 122 | store = DefaultStorage() |
---|
| 123 | handler = queryUtility(IFileStoreHandler, name='file-customerdocument') |
---|
| 124 | result = handler.pathFromFileID( |
---|
| 125 | store, '/fake-root', '__anything__345/c999/nice_image_d123_c999.jpeg') |
---|
| 126 | self.assertEqual( |
---|
| 127 | result, '/fake-root/customers/345/c999/nice_image_d123_c999') |
---|
| 128 | return |
---|
| 129 | |
---|
| 130 | def test_file_store_handler_create(self): |
---|
| 131 | # we can create files in image store with the document file |
---|
| 132 | # store handler |
---|
| 133 | store = DefaultStorage(self.workdir) |
---|
| 134 | handler = queryUtility(IFileStoreHandler, name='file-customerdocument') |
---|
| 135 | file_obj, path, waeup_file = handler.createFile( |
---|
| 136 | store, store.root, 'sample.jpg', '__anything__345/c999/nice_image_d123_c999.jpeg', |
---|
| 137 | StringIO('I am a JPG file')) |
---|
| 138 | self.assertEqual(path[-43:], 'customers/345/c999/nice_image_d123_c999.jpg') |
---|
| 139 | return |
---|
| 140 | |
---|
| 141 | def test_iface(self): |
---|
| 142 | # make sure we implement promised interfaces |
---|
| 143 | obj = CustomerDocumentFileNameChooser(None) # needs a context normally |
---|
| 144 | verifyClass(IFileStoreNameChooser, CustomerDocumentFileNameChooser) |
---|
| 145 | verifyObject(IFileStoreNameChooser, obj) |
---|
| 146 | return |
---|
| 147 | |
---|
| 148 | def test_name_chooser_available(self): |
---|
| 149 | # we can get a name chooser for document objects as adapter |
---|
| 150 | doc = CustomerSampleDocument() |
---|
| 151 | chooser = IFileStoreNameChooser(doc) |
---|
| 152 | self.assertTrue(chooser is not None) |
---|
| 153 | return |
---|
| 154 | |
---|
| 155 | def test_name_chooser_document(self): |
---|
| 156 | # we can't get an image filename for documents not owned by a customer |
---|
| 157 | doc = CustomerSampleDocument() |
---|
[12256] | 158 | doc.document_id = u'DOC1' |
---|
[12205] | 159 | chooser = IFileStoreNameChooser(doc) |
---|
| 160 | # no customer can be found |
---|
| 161 | self.assertRaises(AttributeError, chooser.chooseName, 'sample.jpg') |
---|
| 162 | # we add a fake customer and fake documents container and can get |
---|
| 163 | # an image name |
---|
| 164 | fake_customer = Customer() |
---|
| 165 | fake_customer.customer_id = u'K1000001' |
---|
| 166 | fake_container = grok.Container() |
---|
| 167 | fake_container.__parent__ = fake_customer |
---|
| 168 | doc.__parent__ = fake_container |
---|
| 169 | result = chooser.chooseName('sample.jpg') |
---|
| 170 | self.assertEqual( |
---|
[12256] | 171 | result, '__file-customerdocument__01000/K1000001/sample_DOC1_K1000001.jpg') |
---|
[12205] | 172 | return |
---|