Ignore:
Timestamp:
12 Dec 2014, 18:28:54 (10 years ago)
Author:
Henrik Bettermann
Message:

Add DocumentFileNameChooser? and DocumentFileStoreHandler? with functional tests.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • main/waeup.ikoba/trunk/src/waeup/ikoba/documents/tests/test_document.py

    r12203 r12204  
    1919Tests for documents.
    2020"""
     21import shutil
     22import tempfile
     23from StringIO import StringIO
    2124from zope.interface.verify import verifyClass, verifyObject
    22 from zope.component import createObject
     25from zope.component import createObject, queryUtility
    2326from hurry.workflow.interfaces import (
    2427    IWorkflowInfo, IWorkflowState, InvalidTransitionError)
     28from waeup.ikoba.interfaces import (
     29    IObjectHistory, IFileStoreNameChooser, IFileStoreHandler)
     30from waeup.ikoba.imagestorage import DefaultStorage
    2531from waeup.ikoba.documents.interfaces import (
    2632    IDocumentsContainer, IDocument, IPDFDocument, IHTMLDocument)
    27 from waeup.ikoba.interfaces import IObjectHistory
    2833from waeup.ikoba.documents.container import DocumentsContainer
    2934from waeup.ikoba.documents.document import (
    30     Document, PDFDocument, HTMLDocument)
     35    Document, PDFDocument, HTMLDocument,
     36    DocumentFileNameChooser, DocumentFileStoreHandler)
    3137from waeup.ikoba.testing import (FunctionalLayer, FunctionalTestCase)
    3238
     
    108114        self.assertTrue('Document created by system' in messages)
    109115        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 TracChangeset for help on using the changeset viewer.