Ignore:
Timestamp:
12 Dec 2014, 22:14:37 (10 years ago)
Author:
Henrik Bettermann
Message:

Add missing functional tests for CustomerDocumentFileNameChooser? and CustomerDocumentFileStoreHandler?.

Location:
main/waeup.ikoba/trunk/src/waeup/ikoba/customers
Files:
2 edited

Legend:

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

    r12200 r12205  
    268268        the customers container this chooser would create:
    269269
    270           ``'__file-customerdocument__customers/A/A123456/nice_image_d123_A123456.jpeg'``
     270          ``'__file-customerdocument__customers/345/c999/nice_image_d123_c999.jpeg'``
    271271
    272272        meaning that the nice image of this customer document would be
    273273        stored in the site-wide file storage in path:
    274274
    275           ``customers/A/A123456/nice_image_d123_A123456.jpeg``
     275          ``customers/345/c999/nice_image_d123_c999.jpeg``
    276276
    277277        """
  • main/waeup.ikoba/trunk/src/waeup/ikoba/customers/tests/test_document.py

    r12057 r12205  
    1919Tests for documents.
    2020"""
     21import shutil
     22import tempfile
     23import grok
     24from StringIO import StringIO
    2125from zope.interface.verify import verifyClass, verifyObject
    22 from zope.component import createObject
     26from zope.component import createObject, queryUtility
    2327from hurry.workflow.interfaces import (
    2428    IWorkflowInfo, IWorkflowState, InvalidTransitionError)
     29from waeup.ikoba.interfaces import (
     30    IObjectHistory, IFileStoreNameChooser, IFileStoreHandler)
     31from waeup.ikoba.imagestorage import DefaultStorage
    2532from waeup.ikoba.customers.interfaces import (
    2633    ICustomerDocumentsContainer, ICustomerDocument)
    2734from waeup.ikoba.interfaces import IObjectHistory
     35from waeup.ikoba.customers.customer import Customer
    2836from waeup.ikoba.customers.documents import (
    29     CustomerDocumentsContainer, CustomerSampleDocument)
     37    CustomerDocumentsContainer, CustomerSampleDocument,
     38    CustomerDocumentFileStoreHandler, CustomerDocumentFileNameChooser)
    3039from waeup.ikoba.testing import (FunctionalLayer, FunctionalTestCase)
    3140
     
    8796        self.assertTrue('Document created by system' in messages)
    8897        return
     98
     99
     100class CustomerDocumentFileTests(FunctionalTestCase):
     101
     102    layer = FunctionalLayer
     103
     104    def setUp(self):
     105        super(CustomerDocumentFileTests, self).setUp()
     106        self.workdir = tempfile.mkdtemp()
     107        return
     108
     109    def tearDown(self):
     110        super(CustomerDocumentFileTests, self).tearDown()
     111        shutil.rmtree(self.workdir)
     112        return
     113
     114    def test_file_store_handler_util_accessible(self):
     115        # we can get an IFileStoreHandler utility for documents
     116        handler = queryUtility(IFileStoreHandler, name='file-customerdocument')
     117        self.assertTrue(
     118            isinstance(handler, CustomerDocumentFileStoreHandler))
     119        return
     120
     121    def test_file_store_handler(self):
     122        store = DefaultStorage()
     123        handler = queryUtility(IFileStoreHandler, name='file-customerdocument')
     124        result = handler.pathFromFileID(
     125            store, '/fake-root', '__anything__345/c999/nice_image_d123_c999.jpeg')
     126        self.assertEqual(
     127            result, '/fake-root/customers/345/c999/nice_image_d123_c999')
     128        return
     129
     130    def test_file_store_handler_create(self):
     131        # we can create files in image store with the document file
     132        # store handler
     133        store = DefaultStorage(self.workdir)
     134        handler = queryUtility(IFileStoreHandler, name='file-customerdocument')
     135        file_obj, path, waeup_file = handler.createFile(
     136            store, store.root, 'sample.jpg', '__anything__345/c999/nice_image_d123_c999.jpeg',
     137            StringIO('I am a JPG file'))
     138        self.assertEqual(path[-43:], 'customers/345/c999/nice_image_d123_c999.jpg')
     139        return
     140
     141    def test_iface(self):
     142        # make sure we implement promised interfaces
     143        obj = CustomerDocumentFileNameChooser(None) # needs a context normally
     144        verifyClass(IFileStoreNameChooser, CustomerDocumentFileNameChooser)
     145        verifyObject(IFileStoreNameChooser, obj)
     146        return
     147
     148    def test_name_chooser_available(self):
     149        # we can get a name chooser for document objects as adapter
     150        doc = CustomerSampleDocument()
     151        chooser = IFileStoreNameChooser(doc)
     152        self.assertTrue(chooser is not None)
     153        return
     154
     155    def test_name_chooser_document(self):
     156        # we can't get an image filename for documents not owned by a customer
     157        doc = CustomerSampleDocument()
     158        chooser = IFileStoreNameChooser(doc)
     159        # no customer can be found
     160        self.assertRaises(AttributeError, chooser.chooseName, 'sample.jpg')
     161        # we add a fake customer and fake documents container and can get
     162        # an image name
     163        fake_customer = Customer()
     164        fake_customer.customer_id = u'K1000001'
     165        fake_container = grok.Container()
     166        fake_container.__parent__ = fake_customer
     167        doc.__parent__ = fake_container
     168        result = chooser.chooseName('sample.jpg')
     169        self.assertEqual(
     170            result, '__file-customerdocument__01000/K1000001/sample_d123_K1000001.jpg')
     171        return
Note: See TracChangeset for help on using the changeset viewer.