- Timestamp:
- 23 Jul 2011, 02:25:00 (13 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
main/waeup.sirp/trunk/src/waeup/sirp/tests/test_imagestorage.py
r6520 r6529 1 1 import os 2 import sys 2 3 import tempfile 3 4 import shutil 4 5 import unittest 6 from StringIO import StringIO 5 7 from hurry.file.interfaces import IFileRetrieval 6 from zope.component.hooks import setSite , clearSite8 from zope.component.hooks import setSite 7 9 from zope.interface.verify import verifyClass, verifyObject 8 10 from waeup.sirp.app import University 9 11 from waeup.sirp.testing import FunctionalLayer, FunctionalTestCase 12 from waeup.sirp.image import createWAeUPImageFile 10 13 from waeup.sirp.imagestorage import ( 11 14 md5digest, Basket, ImageStorage, ImageStorageFileRetrieval,) … … 34 37 self.workdir = tempfile.mkdtemp() 35 38 self.samplefile = os.path.join(self.workdir, 'sample') 39 self.otherfile = os.path.join(self.workdir, 'other') 36 40 open(self.samplefile, 'wb').write('Hi there!') 41 open(self.otherfile, 'wb').write('Hi from other!') 37 42 self.basket = Basket() 38 43 self.fd = open(self.samplefile, 'r') 44 self.fd2 = open(self.otherfile, 'r') 45 self.stderr = StringIO() 46 self.old_stderr = sys.stderr 39 47 40 48 def tearDown(self): 49 sys.stderr = self.old_stderr 41 50 super(BasketTests, self).tearDown() 42 51 self.fd.close() … … 60 69 basket_id = self.basket.storeFile(self.fd, 'sample') 61 70 self.assertEqual(basket_id, '1') 71 contents = self.basket['1'].open('r').read() 72 self.assertEqual(contents, 'Hi there!') 62 73 63 74 def test_storeFile_double(self): … … 65 76 basket_id2 = self.basket.storeFile(self.fd, 'sample') 66 77 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!') 67 90 68 91 def test_retrieveFile(self): … … 75 98 result = self.basket.retrieveFile('not-a-valid-basket-id') 76 99 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 77 124 78 125 class ImageStorageTests(FunctionalTestCase): … … 110 157 full_id1 = self.storage.storeFile(self.fd, 'sample1.txt') 111 158 full_id2 = self.storage.storeFile(self.fd, 'sample2.txt') 159 full_id3 = self.storage.storeFile(self.fd, 'sample1.txt') 112 160 self.assertEqual(full_id1, '396199333edbf40ad43e62a1c1397793-1') 113 161 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!') 114 166 115 167 def test_storeFile_multiple(self): … … 227 279 228 280 def test_createFile(self): 281 # Ensure we can create WAeUPImageFiles when in site 229 282 setSite(self.app) 230 283 retrieval = ImageStorageFileRetrieval() … … 233 286 self.assertEqual(full_id, '396199333edbf40ad43e62a1c1397793-1') 234 287 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.