source: main/waeup.ikoba/trunk/src/waeup/ikoba/documents/tests/test_document.py @ 12210

Last change on this file since 12210 was 12204, checked in by Henrik Bettermann, 11 years ago

Add DocumentFileNameChooser? and DocumentFileStoreHandler? with functional tests.

  • Property svn:keywords set to Id
File size: 6.7 KB
Line 
1## $Id: test_document.py 12204 2014-12-12 18:28:54Z 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
23from StringIO import StringIO
24from zope.interface.verify import verifyClass, verifyObject
25from zope.component import createObject, queryUtility
26from hurry.workflow.interfaces import (
27    IWorkflowInfo, IWorkflowState, InvalidTransitionError)
28from waeup.ikoba.interfaces import (
29    IObjectHistory, IFileStoreNameChooser, IFileStoreHandler)
30from waeup.ikoba.imagestorage import DefaultStorage
31from waeup.ikoba.documents.interfaces import (
32    IDocumentsContainer, IDocument, IPDFDocument, IHTMLDocument)
33from waeup.ikoba.documents.container import DocumentsContainer
34from waeup.ikoba.documents.document import (
35    Document, PDFDocument, HTMLDocument,
36    DocumentFileNameChooser, DocumentFileStoreHandler)
37from waeup.ikoba.testing import (FunctionalLayer, FunctionalTestCase)
38
39class 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                IDocument, Document)
56            )
57        self.assertTrue(
58            verifyObject(
59                IDocument, Document())
60            )
61
62        self.assertTrue(
63            verifyClass(
64                IPDFDocument, PDFDocument)
65            )
66        self.assertTrue(
67            verifyObject(
68                IPDFDocument, PDFDocument())
69            )
70
71        self.assertTrue(
72            verifyClass(
73                IHTMLDocument, HTMLDocument)
74            )
75        self.assertTrue(
76            verifyObject(
77                IHTMLDocument, HTMLDocument())
78            )
79        return
80
81    def test_addDocument(self):
82        container = DocumentsContainer()
83        document = createObject(u'waeup.HTMLDocument')
84        document_id = document.document_id
85        container.addDocument(document)
86        self.assertEqual(container[document_id], document)
87        self.assertRaises(TypeError, container.addDocument, object())
88        self.assertEqual(document_id, 'd123')
89        return
90
91    def test_document_workflow(self):
92        document = createObject(u'waeup.HTMLDocument')
93        IWorkflowInfo(document).fireTransition('create')
94        self.assertEqual(IWorkflowState(document).getState(), 'created')
95        IWorkflowInfo(document).fireTransition('submit')
96        self.assertEqual(IWorkflowState(document).getState(), 'submitted')
97        IWorkflowInfo(document).fireTransition('verify')
98        self.assertEqual(IWorkflowState(document).getState(), 'verified')
99        IWorkflowInfo(document).fireTransition('reset2')
100        self.assertEqual(IWorkflowState(document).getState(), 'created')
101        self.assertRaises(InvalidTransitionError,
102            IWorkflowInfo(document).fireTransition, 'verify')
103        IWorkflowInfo(document).fireTransition('submit')
104        IWorkflowInfo(document).fireTransition('reset3')
105        self.assertEqual(IWorkflowState(document).getState(), 'created')
106        IWorkflowInfo(document).fireTransition('publish')
107        self.assertEqual(IWorkflowState(document).getState(), 'published')
108        return
109
110    def test_document_history(self):
111        doc = createObject(u'waeup.HTMLDocument')
112        IWorkflowInfo(doc).fireTransition('create')
113        messages = ' '.join(doc.history.messages)
114        self.assertTrue('Document created by system' in messages)
115        return
116
117
118class DocumentFileTests(FunctionalTestCase):
119
120    layer = FunctionalLayer
121
122    def setUp(self):
123        super(DocumentFileTests, self).setUp()
124        self.workdir = tempfile.mkdtemp()
125        return
126
127    def tearDown(self):
128        super(DocumentFileTests, self).tearDown()
129        shutil.rmtree(self.workdir)
130        return
131
132    def test_file_store_handler_util_accessible(self):
133        # we can get an IFileStoreHandler utility for documents
134        handler = queryUtility(IFileStoreHandler, name='file-document')
135        self.assertTrue(
136            isinstance(handler, DocumentFileStoreHandler))
137        return
138
139    def test_file_store_handler(self):
140        store = DefaultStorage()
141        handler = queryUtility(IFileStoreHandler, name='file-document')
142        result = handler.pathFromFileID(
143            store, '/fake-root', '__any_marker__sample.jpg')
144        self.assertEqual(
145            result, '/fake-root/documents/sample')
146        return
147
148    def test_file_store_handler_create(self):
149        # we can create files in image store with the document file
150        # store handler
151        store = DefaultStorage(self.workdir)
152        handler = queryUtility(IFileStoreHandler, name='file-document')
153        file_obj, path, waeup_file = handler.createFile(
154            store, store.root, 'sample.jpg', '__file_document__sample',
155            StringIO('I am a JPG file'))
156        self.assertEqual(path[-20:], 'documents/sample.jpg')
157        return
158
159    def test_iface(self):
160        # make sure we implement promised interfaces
161        obj = DocumentFileNameChooser(None) # needs a context normally
162        verifyClass(IFileStoreNameChooser, DocumentFileNameChooser)
163        verifyObject(IFileStoreNameChooser, obj)
164        return
165
166    def test_name_chooser_available(self):
167        # we can get a name chooser for document objects as adapter
168        doc = Document()
169        chooser = IFileStoreNameChooser(doc)
170        self.assertTrue(chooser is not None)
171        return
172
173    def test_name_chooser_document(self):
174        # we can get an image filename for documents not in a container
175        doc = Document()
176        chooser = IFileStoreNameChooser(doc)
177        result = chooser.chooseName('sample.jpg')
178        # the file would be stored in a ``_default`` directory.
179        self.assertEqual(
180            result, '__file-document__documents/sample_d123.jpg')
181        return
Note: See TracBrowser for help on using the repository browser.