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

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

Store public documents in right place.

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