source: main/waeup.ikoba/trunk/src/waeup/ikoba/customers/tests/test_document.py @ 17497

Last change on this file since 17497 was 12256, checked in by Henrik Bettermann, 10 years ago

Change document_id generation algorithm. Use Universally Unique IDentifiers instead of consecutive numbers.

  • Property svn:keywords set to Id
File size: 6.9 KB
Line 
1## $Id: test_document.py 12256 2014-12-18 12:58:12Z 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##
18"""
19Tests for documents.
20"""
21import shutil
22import tempfile
23import grok
24from StringIO import StringIO
25from zope.interface.verify import verifyClass, verifyObject
26from zope.component import createObject, queryUtility
27from hurry.workflow.interfaces import (
28    IWorkflowInfo, IWorkflowState, InvalidTransitionError)
29from waeup.ikoba.interfaces import (
30    IObjectHistory, IFileStoreNameChooser, IFileStoreHandler)
31from waeup.ikoba.imagestorage import DefaultStorage
32from waeup.ikoba.customers.interfaces import (
33    ICustomerDocumentsContainer, ICustomerDocument)
34from waeup.ikoba.interfaces import IObjectHistory
35from waeup.ikoba.customers.customer import Customer
36from waeup.ikoba.customers.documents import (
37    CustomerDocumentsContainer, CustomerSampleDocument,
38    CustomerDocumentFileStoreHandler, CustomerDocumentFileNameChooser)
39from waeup.ikoba.testing import (FunctionalLayer, FunctionalTestCase)
40
41class 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(
57                ICustomerDocument, CustomerSampleDocument)
58            )
59        self.assertTrue(
60            verifyObject(
61                ICustomerDocument, CustomerSampleDocument())
62            )
63        return
64
65    def test_addDocument(self):
66        container = CustomerDocumentsContainer()
67        document = createObject(u'waeup.CustomerSampleDocument')
68        id = document.document_id
69        container.addDocument(document)
70        self.assertEqual(container[id], document)
71        self.assertRaises(TypeError, container.addDocument, object())
72        self.assertEqual(document.class_name, 'CustomerSampleDocument')
73        return
74
75    def test_document_workflow(self):
76        document = createObject(u'waeup.CustomerSampleDocument')
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):
93        doc = createObject(u'waeup.CustomerSampleDocument')
94        IWorkflowInfo(doc).fireTransition('create')
95        messages = ' '.join(doc.history.messages)
96        self.assertTrue('Document created by system' in messages)
97        return
98
99
100class 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()
158        doc.document_id = u'DOC1'
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(
171            result, '__file-customerdocument__01000/K1000001/sample_DOC1_K1000001.jpg')
172        return
Note: See TracBrowser for help on using the repository browser.