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

Last change on this file since 12327 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: 5.7 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
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, IHTMLDocument)
33from waeup.ikoba.documents.container import DocumentsContainer
34from waeup.ikoba.documents.document import (
35    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                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.document_id = u'DOC'
75        container.addDocument(document)
76        self.assertEqual(container[document.document_id], document)
77        self.assertRaises(TypeError, container.addDocument, object())
78        self.assertEqual(document.document_id, 'DOC')
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
95class 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        doc.document_id = u'DOC'
154        chooser = IFileStoreNameChooser(doc)
155        result = chooser.chooseName('sample.jpg')
156        # the file would be stored in a ``_default`` directory.
157        self.assertEqual(
158            result, '__file-document__documents/sample_DOC.jpg')
159        return
Note: See TracBrowser for help on using the repository browser.