Ignore:
Timestamp:
23 Jul 2011, 02:25:00 (13 years ago)
Author:
uli
Message:

Add more tests, most covering the brain-dead problems we had with
Blobs and to ensure this problems are gone now.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • main/waeup.sirp/trunk/src/waeup/sirp/tests/test_imagestorage.py

    r6520 r6529  
    11import os
     2import sys
    23import tempfile
    34import shutil
    45import unittest
     6from StringIO import StringIO
    57from hurry.file.interfaces import IFileRetrieval
    6 from zope.component.hooks import setSite, clearSite
     8from zope.component.hooks import setSite
    79from zope.interface.verify import verifyClass, verifyObject
    810from waeup.sirp.app import University
    911from waeup.sirp.testing import FunctionalLayer, FunctionalTestCase
     12from waeup.sirp.image import createWAeUPImageFile
    1013from waeup.sirp.imagestorage import (
    1114    md5digest, Basket, ImageStorage, ImageStorageFileRetrieval,)
     
    3437        self.workdir = tempfile.mkdtemp()
    3538        self.samplefile = os.path.join(self.workdir, 'sample')
     39        self.otherfile = os.path.join(self.workdir, 'other')
    3640        open(self.samplefile, 'wb').write('Hi there!')
     41        open(self.otherfile, 'wb').write('Hi from other!')
    3742        self.basket = Basket()
    3843        self.fd = open(self.samplefile, 'r')
     44        self.fd2 = open(self.otherfile, 'r')
     45        self.stderr = StringIO()
     46        self.old_stderr = sys.stderr
    3947
    4048    def tearDown(self):
     49        sys.stderr = self.old_stderr
    4150        super(BasketTests, self).tearDown()
    4251        self.fd.close()
     
    6069        basket_id = self.basket.storeFile(self.fd, 'sample')
    6170        self.assertEqual(basket_id, '1')
     71        contents = self.basket['1'].open('r').read()
     72        self.assertEqual(contents, 'Hi there!')
    6273
    6374    def test_storeFile_double(self):
     
    6576        basket_id2 = self.basket.storeFile(self.fd, 'sample')
    6677        self.assertTrue(basket_id1 == basket_id2 == '1')
     78        contents = self.basket['1'].open('r').read()
     79        self.assertEqual(contents, 'Hi there!')
     80
     81    def test_storeFile_multiple(self):
     82        basket_id1 = self.basket.storeFile(self.fd, 'sample')
     83        basket_id2 = self.basket.storeFile(self.fd2, 'sample')
     84        self.assertEqual(basket_id1, '1')
     85        self.assertEqual(basket_id2, '2')
     86        contents1 = self.basket['1'].open('r').read()
     87        contents2 = self.basket['2'].open('r').read()
     88        self.assertEqual(contents1, 'Hi there!')
     89        self.assertEqual(contents2, 'Hi from other!')
    6790
    6891    def test_retrieveFile(self):
     
    7598        result = self.basket.retrieveFile('not-a-valid-basket-id')
    7699        self.assertTrue(result is None)
     100
     101    def test_detect_zero_length_blobs(self):
     102        # Ensure we get a warning when an empty Blob is found
     103        self.basket.storeFile(self.fd, 'sample')
     104        self.basket['1'].open('w').write('')
     105        self.fd.seek(0)
     106        sys.stderr = self.stderr         # Redirect stderr
     107        self.basket.storeFile(self.fd, 'sample')
     108        sys.stderr = self.old_stderr     # Restore stderr
     109        self.stderr.seek(0)
     110        self.assertTrue(
     111            "EMPTY BLOB DETECTED" in self.stderr.read())
     112
     113    def test_refill_zero_length_blobs(self):
     114        # When we detect an empty Blob, it will be reused
     115        self.basket.storeFile(self.fd, 'sample')
     116        self.basket['1'].open('w').write('')
     117        self.fd.seek(0)
     118        sys.stderr = self.stderr         # Redirect stderr
     119        self.basket.storeFile(self.fd, 'sample')
     120        sys.stderr = self.old_stderr     # Restore stderr
     121        contents = self.basket['1'].open('r').read()
     122        self.assertEqual(contents, 'Hi there!')
     123
    77124
    78125class ImageStorageTests(FunctionalTestCase):
     
    110157        full_id1 = self.storage.storeFile(self.fd, 'sample1.txt')
    111158        full_id2 = self.storage.storeFile(self.fd, 'sample2.txt')
     159        full_id3 = self.storage.storeFile(self.fd, 'sample1.txt')
    112160        self.assertEqual(full_id1, '396199333edbf40ad43e62a1c1397793-1')
    113161        self.assertEqual(full_id2, '396199333edbf40ad43e62a1c1397793-1')
     162        self.assertEqual(full_id3, '396199333edbf40ad43e62a1c1397793-1')
     163        contents = self.storage.retrieveFile(
     164            '396199333edbf40ad43e62a1c1397793-1').read()
     165        self.assertEqual(contents, 'Hi there!')
    114166
    115167    def test_storeFile_multiple(self):
     
    227279
    228280    def test_createFile(self):
     281        # Ensure we can create WAeUPImageFiles when in site
    229282        setSite(self.app)
    230283        retrieval = ImageStorageFileRetrieval()
     
    233286        self.assertEqual(full_id, '396199333edbf40ad43e62a1c1397793-1')
    234287        return
     288
     289    def test_waeupimagefile(self):
     290        # Make sure WAeUPImageFile can use our file retrieval
     291        setSite(self.app)
     292        myfile = createWAeUPImageFile('sample.jpg', self.fd)
     293        contents = myfile.file.read()
     294        self.assertEqual(contents, 'Hi there!')
     295
     296    def test_waeupimagefile_raw(self):
     297        # Make sure we can retrieve a file also if it was initialized
     298        # with no image storage available
     299        myfile = createWAeUPImageFile('sample.jpg', self.fd)
     300        setSite(self.app)
     301        contents = myfile.file.read()
     302        self.assertEqual(contents, 'Hi there!')
Note: See TracChangeset for help on using the changeset viewer.