Commits

Joseph Tate committed a586bd8

fix tests

  • Participants
  • Parent commits 1614cd3

Comments (0)

Files changed (1)

tw2/upload/tests/test_image.py

 -----------------------------721837373350705526688164684--
 """ % png
 
+t_imbroker_extents = {
+        'twox': (2, 2),
+        'four00x': (400, 399) #This is close enough, less than 0.01 off
+    }
+
+t_imbroker_broken_extents = {
+        'original': (1,1),
+        'twox': (2,3)
+    }
+
+class TestImage(image.ImageObject):
+    basename = 'salmonella.png'
+    _extents = t_imbroker_extents
+    @property
+    def broker_args(self):
+        return dict(basename=self.basename, extents=self._extents)
+
 class TestImageObject():
     @raises(NotImplementedError)
     def test_image_data(self):
-        foo = image.ImageObject().image_data
+        foo = TestImage().image_data
 
     def test_set_image_data(self):
         if not image.has_PIL:
             raise SkipTest('you must have PIL to run this test')
-        foo = image.ImageObject()
+        foo = TestImage()
 
         fs = cgi.FieldStorage(fp=StringIO(postdata), environ=env)
         foo.image_data = fs.list[0]
-        assert foo.image != None
-        assert foo.image.basename != None
+        assert foo.image_broker != None
+        assert foo.image_broker.basename != None
 
         eq_(foo.mime_type, 'image/png')
         eq_(foo.filename, 'test.png')
+        return foo
+
+    def test_del_image_data(self):
+        foo = self.test_set_image_data()
+
+        fspaths = [foo.image_broker.fs_path, foo.image_broker.twox_fs_path, foo.image_broker.four00x_fs_path]
+
+        del foo.image_data
+        for f in fspaths:
+            assert not os.path.exists(f), 'File (%s) not deleted' %f
+
+    def test_del_image_broker(self):
+        if not image.has_PIL:
+            raise SkipTest('you must have PIL to run this test')
+        foo = TestImage()
+        foo.image_broker
+
+        del foo.image_broker
+        assert not hasattr(foo, '_image_broker')
+
+        #idempotent
+        del foo.image_broker
+
+    def test_del_image_broker_1(self):
+        if not image.has_PIL:
+            raise SkipTest('you must have PIL to run this test')
+        foo = TestImage()
+
+        del foo.image_broker
+        assert not hasattr(foo, '_image_broker')
 
 class TestFileBroker():
     prefilen = None
         self.fb1.upload(fs.list[2])
         assert_not_equals(self.fb1.basename, self.prefilen)
 
-class TImageBroker(image.ImageBroker):
-    extents = {
-        'twox': (2, 2),
-        'four00x': (400, 399) #This is close enough, less than 0.01 off
-    }
-
-class TBrokeImageBroker(TImageBroker):
-    extents = {
-        'original': (1,1),
-        'twox': (2,3)
-    }
-
 class TestImageBroker():
     def setup(self):
-        fd, self.prefilen = tempfile.mkstemp('.png', 'test_image_B-', dir=os.path.join(_dir_base, TImageBroker.basedir))
+        fd, self.prefilen = tempfile.mkstemp('.png', 'test_image_B-', dir=os.path.join(_dir_base, image.ImageBroker.basedir))
         os.write(fd, png)
         os.close(fd)
-        self.ib = TImageBroker(os.path.basename(self.prefilen), dir_base = dir_base)
+        self.ib = image.ImageBroker(os.path.basename(self.prefilen), dir_base = dir_base, extents=t_imbroker_extents)
 
     @raises(image.MismatchedExtentsError)
     def test_broken(self):
-        brk = TBrokeImageBroker()
+        brk = image.ImageBroker(os.path.basename(self.prefilen), dir_base = dir_base, extents=t_imbroker_broken_extents)
 
     def test_getattr(self):
         eq_(self.ib.fs_path, self.prefilen)
         os.unlink(self.ib.four00x_fs_path)
         eq_(self.ib.four00x_web_path, self.ib.web_path)
 
-    @raises(AssertionError)
-    def test_web_path_raises(self):
+    def test_web_path_none(self):
         os.unlink(self.ib.fs_path)
-        self.ib.web_path
+        eq_(None, self.ib.web_path)
+
+    @raises(image.FileNotOnDisk)
+    def test_fs_path_raises(self):
+        os.unlink(self.ib.fs_path)
+        self.ib.fs_path
 
     def test_aftercrop(self):
         if not image.has_PIL:
         eq_(x1, 2)
         eq_(y1, 0)
 
+    @raises(image.InvalidCropRegion)
+    def test_invalid_crop_region(self):
+        if not image.has_PIL:
+            raise SkipTest('you must have PIL to run this test')
+        self.ib.crop(3,3,1,1)
+
 if image.has_sqlalchemy:
     from sqlalchemy import create_engine, MetaData
     from sqlalchemy.orm import sessionmaker
     from sqlalchemy import Table, Column, Integer, String, ForeignKey
     from sqlalchemy.orm import mapper, relation
 
-    class ref(image.ImageReference):
-        baseclass = TImageBroker
-
     metadata = MetaData()
-    dummy_image_table = Table('dummy_table_fileref_image_test', metadata.metadata,
-                        Column('image_id', Integer, primary_key=True),
-                        Column('image', ref(255, dir_base=dir_base)),
-                        Column('mime_type', String(255)),
-                        Column('filename', String(255)),
-                        )
+    dummy_image_table = image.generate_image_model(metadata, 'dummytest_')
 
     dummy_table = Table('dummy_table_fileref_test', metadata.metadata,
                         Column('dummy_id', Integer, primary_key=True),
-                        Column('image_id', Integer, ForeignKey('dummy_table_fileref_image_test.image_id'))
+                        Column('image_id', Integer, ForeignKey('dummytest_images.image_id'))
                         )
 
     class Dummy(object):pass
-    class DummyImage(image.ImageObject):pass
+    class DummyImage(image.ImageObject):
+        _extents = t_imbroker_extents
 
-    mapper(DummyImage, dummy_image_table)
+        @property
+        def broker_args(self):
+            """
+                Required for interfacing with the tw2.upload ImageBroker
+            """
+            ret = super(DummyImage, self).broker_args
+            ret['dir_base'] = dir_base
+            ret['extents'] = self._extents
+            return ret
+
+    mapper(DummyImage, dummy_image_table, polymorphic_on=dummy_image_table.c.image_class, polymorphic_identity='DummyImage')
+
 
     mapper(Dummy, dummy_table, properties={'image':relation(DummyImage, uselist=False, single_parent=True, cascade="all,delete,delete-orphan")})
 
             self.fs = None
             teardown_db()
 
-        @raises(TypeError)
-        def test_zero(self):
-            if not image.has_PIL:
-                raise SkipTest('you must have PIL to run this test')
-            d = Dummy()
-            eq_(d.image, None)
-            d.image = DummyImage()
-            d.image.image = image.ImageBroker(dir_base = dir_base)
-            d.image.image_data = self.fs.list[0]
-            self.session.add(d)
-            self.session.flush()
-
         def test_one(self):
             if not image.has_PIL:
                 raise SkipTest('you must have PIL to run this test')
             d = Dummy()
             eq_(d.image, None)
             d.image = DummyImage()
-            d.image.image = TImageBroker(dir_base = dir_base)
             d.image.image_data = self.fs.list[0]
             self.session.flush()
 
-            assert d.image.image
-            assert d.image.image.fs_path
-            eq_(open(d.image.image.fs_path).read(), png)
+            assert d.image.image_broker
+            assert d.image.image_broker.fs_path
+            eq_(open(d.image.image_broker.fs_path).read(), png)
             return d
 
         def test_two(self):
             ident = d.dummy_id
             self.session.expire_all()
             d = self.session.query(Dummy).get(ident)
-            d.image.image = TImageBroker('test.png', dir_base = dir_base)
+            d.image.basename = u'test.png'
             self.session.flush()
             self.session.expire_all()
             d = self.session.query(Dummy).get(ident)
 
-            eq_(d.image.image.basename, 'test.png')
+            eq_(d.image.image_broker.basename, 'test.png')
 
-        def test_three(self):
+        def test_default_crop(self):
             if not image.has_PIL:
                 raise SkipTest('you must have PIL to run this test')
-            d = Dummy()
-            self.session.add(d)
-            d.image = DummyImage()
-            self.session.flush()
-            ident = d.dummy_id
-
-            self.session.expire_all()
-            d = self.session.query(Dummy).get(ident)
-            #executes the else of process_result_value
-            del d.image
-            self.session.flush()
-
-        def test_default_crop(self):
             d = self.test_one()
             #replace the uploaded image with one with some size
-            img = PIL.Image.new("1", (200, 300), 1)
-            img.save(d.image.image.fs_path)
+            img = image.PIL.Image.new("1", (200, 300), 1)
+            img.save(d.image.image_broker.fs_path)
             d.image.default_crop()
             eq_(d.image.x1, 0)
             eq_(d.image.y1, 50)