1 | ## $Id: test_document.py 12213 2014-12-13 10:51:17Z 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 | """ |
---|
19 | Tests for documents. |
---|
20 | """ |
---|
21 | import shutil |
---|
22 | import tempfile |
---|
23 | from StringIO import StringIO |
---|
24 | from zope.interface.verify import verifyClass, verifyObject |
---|
25 | from zope.component import createObject, queryUtility |
---|
26 | from hurry.workflow.interfaces import ( |
---|
27 | IWorkflowInfo, IWorkflowState, InvalidTransitionError) |
---|
28 | from waeup.ikoba.interfaces import ( |
---|
29 | IObjectHistory, IFileStoreNameChooser, IFileStoreHandler) |
---|
30 | from waeup.ikoba.imagestorage import DefaultStorage |
---|
31 | from waeup.ikoba.documents.interfaces import ( |
---|
32 | IDocumentsContainer, IPublicDocument, IPDFDocument, IHTMLDocument) |
---|
33 | from waeup.ikoba.documents.container import DocumentsContainer |
---|
34 | from waeup.ikoba.documents.document import ( |
---|
35 | PDFDocument, HTMLDocument, |
---|
36 | DocumentFileNameChooser, DocumentFileStoreHandler) |
---|
37 | from waeup.ikoba.testing import (FunctionalLayer, FunctionalTestCase) |
---|
38 | |
---|
39 | class DocumentsContainerTestCase(FunctionalTestCase): |
---|
40 | |
---|
41 | layer = FunctionalLayer |
---|
42 | |
---|
43 | def test_interfaces(self): |
---|
44 | # Make sure the correct interfaces are implemented. |
---|
45 | self.assertTrue( |
---|
46 | verifyClass( |
---|
47 | IDocumentsContainer, DocumentsContainer) |
---|
48 | ) |
---|
49 | self.assertTrue( |
---|
50 | verifyObject( |
---|
51 | IDocumentsContainer, DocumentsContainer()) |
---|
52 | ) |
---|
53 | self.assertTrue( |
---|
54 | verifyClass( |
---|
55 | IPDFDocument, PDFDocument) |
---|
56 | ) |
---|
57 | self.assertTrue( |
---|
58 | verifyObject( |
---|
59 | IPDFDocument, PDFDocument()) |
---|
60 | ) |
---|
61 | self.assertTrue( |
---|
62 | verifyClass( |
---|
63 | IHTMLDocument, HTMLDocument) |
---|
64 | ) |
---|
65 | self.assertTrue( |
---|
66 | verifyObject( |
---|
67 | IHTMLDocument, HTMLDocument()) |
---|
68 | ) |
---|
69 | return |
---|
70 | |
---|
71 | def test_addDocument(self): |
---|
72 | container = DocumentsContainer() |
---|
73 | document = createObject(u'waeup.HTMLDocument') |
---|
74 | document_id = document.document_id |
---|
75 | container.addDocument(document) |
---|
76 | self.assertEqual(container[document_id], document) |
---|
77 | self.assertRaises(TypeError, container.addDocument, object()) |
---|
78 | self.assertEqual(document_id, 'd123') |
---|
79 | return |
---|
80 | |
---|
81 | def test_document_workflow(self): |
---|
82 | document = createObject(u'waeup.HTMLDocument') |
---|
83 | IWorkflowInfo(document).fireTransition('create') |
---|
84 | self.assertEqual(IWorkflowState(document).getState(), 'created') |
---|
85 | return |
---|
86 | |
---|
87 | def test_document_history(self): |
---|
88 | doc = createObject(u'waeup.HTMLDocument') |
---|
89 | IWorkflowInfo(doc).fireTransition('create') |
---|
90 | messages = ' '.join(doc.history.messages) |
---|
91 | self.assertTrue('Document created by system' in messages) |
---|
92 | return |
---|
93 | |
---|
94 | |
---|
95 | class DocumentFileTests(FunctionalTestCase): |
---|
96 | |
---|
97 | layer = FunctionalLayer |
---|
98 | |
---|
99 | def setUp(self): |
---|
100 | super(DocumentFileTests, self).setUp() |
---|
101 | self.workdir = tempfile.mkdtemp() |
---|
102 | return |
---|
103 | |
---|
104 | def tearDown(self): |
---|
105 | super(DocumentFileTests, self).tearDown() |
---|
106 | shutil.rmtree(self.workdir) |
---|
107 | return |
---|
108 | |
---|
109 | def test_file_store_handler_util_accessible(self): |
---|
110 | # we can get an IFileStoreHandler utility for documents |
---|
111 | handler = queryUtility(IFileStoreHandler, name='file-document') |
---|
112 | self.assertTrue( |
---|
113 | isinstance(handler, DocumentFileStoreHandler)) |
---|
114 | return |
---|
115 | |
---|
116 | def test_file_store_handler(self): |
---|
117 | store = DefaultStorage() |
---|
118 | handler = queryUtility(IFileStoreHandler, name='file-document') |
---|
119 | result = handler.pathFromFileID( |
---|
120 | store, '/fake-root', '__any_marker__sample.jpg') |
---|
121 | self.assertEqual( |
---|
122 | result, '/fake-root/documents/sample') |
---|
123 | return |
---|
124 | |
---|
125 | def test_file_store_handler_create(self): |
---|
126 | # we can create files in image store with the document file |
---|
127 | # store handler |
---|
128 | store = DefaultStorage(self.workdir) |
---|
129 | handler = queryUtility(IFileStoreHandler, name='file-document') |
---|
130 | file_obj, path, waeup_file = handler.createFile( |
---|
131 | store, store.root, 'sample.jpg', '__file_document__sample', |
---|
132 | StringIO('I am a JPG file')) |
---|
133 | self.assertEqual(path[-20:], 'documents/sample.jpg') |
---|
134 | return |
---|
135 | |
---|
136 | def test_iface(self): |
---|
137 | # make sure we implement promised interfaces |
---|
138 | obj = DocumentFileNameChooser(None) # needs a context normally |
---|
139 | verifyClass(IFileStoreNameChooser, DocumentFileNameChooser) |
---|
140 | verifyObject(IFileStoreNameChooser, obj) |
---|
141 | return |
---|
142 | |
---|
143 | def test_name_chooser_available(self): |
---|
144 | # we can get a name chooser for document objects as adapter |
---|
145 | doc = PDFDocument() |
---|
146 | chooser = IFileStoreNameChooser(doc) |
---|
147 | self.assertTrue(chooser is not None) |
---|
148 | return |
---|
149 | |
---|
150 | def test_name_chooser_document(self): |
---|
151 | # we can get an image filename for documents not in a container |
---|
152 | doc = PDFDocument() |
---|
153 | chooser = IFileStoreNameChooser(doc) |
---|
154 | result = chooser.chooseName('sample.jpg') |
---|
155 | # the file would be stored in a ``_default`` directory. |
---|
156 | self.assertEqual( |
---|
157 | result, '__file-document__documents/sample_d123.jpg') |
---|
158 | return |
---|