<s> from __future__ import absolute_import <EOL> import os <EOL> import shutil <EOL> from django . core . files import File <EOL> from django . core . files . images import ImageFile <EOL> from django . test import TestCase <EOL> from django . utils . _os import upath <EOL> from django . utils . unittest import skipIf <EOL> from . models import Image <EOL> if Image : <EOL> from . models import ( Person , PersonWithHeight , PersonWithHeightAndWidth , <EOL> PersonDimensionsFirst , PersonTwoImages , TestImageFieldFile ) <EOL> from . models import temp_storage_dir <EOL> else : <EOL> class Person ( ) : <EOL> pass <EOL> PersonWithHeight = PersonWithHeightAndWidth = PersonDimensionsFirst = Person <EOL> PersonTwoImages = Person <EOL> class ImageFieldTestMixin ( object ) : <EOL> """<STR_LIT>""" <EOL> PersonModel = PersonWithHeightAndWidth <EOL> File = ImageFile <EOL> def setUp ( self ) : <EOL> """<STR_LIT>""" <EOL> if os . path . exists ( temp_storage_dir ) : <EOL> shutil . rmtree ( temp_storage_dir ) <EOL> os . mkdir ( temp_storage_dir ) <EOL> file_path1 = os . path . join ( os . path . dirname ( upath ( __file__ ) ) , "<STR_LIT>" ) <EOL> self . file1 = self . File ( open ( file_path1 , '<STR_LIT:rb>' ) ) <EOL> file_path2 = os . path . join ( os . path . dirname ( upath ( __file__ ) ) , "<STR_LIT>" ) <EOL> self . file2 = self . File ( open ( file_path2 , '<STR_LIT:rb>' ) ) <EOL> def tearDown ( self ) : <EOL> """<STR_LIT>""" <EOL> shutil . rmtree ( temp_storage_dir ) <EOL> def check_dimensions ( self , instance , width , height , <EOL> field_name = '<STR_LIT>' ) : <EOL> """<STR_LIT>""" <EOL> field = getattr ( instance , field_name ) <EOL> if width is None and height is None : <EOL> self . assertRaises ( ValueError , getattr , field , '<STR_LIT:width>' ) <EOL> self . assertRaises ( ValueError , getattr , field , '<STR_LIT>' ) <EOL> else : <EOL> self . assertEqual ( field . width , width ) <EOL> self . assertEqual ( field . height , height ) <EOL> width_field_name = field_name + '<STR_LIT>' <EOL> if hasattr ( instance , width_field_name ) : <EOL> self . assertEqual ( getattr ( instance , width_field_name ) , width ) <EOL> height_field_name = field_name + '<STR_LIT>' <EOL> if hasattr ( instance , height_field_name ) : <EOL> self . assertEqual ( getattr ( instance , height_field_name ) , height ) <EOL> @ skipIf ( Image is None , "<STR_LIT>" ) <EOL> class ImageFieldTests ( ImageFieldTestMixin , TestCase ) : <EOL> """<STR_LIT>""" <EOL> def test_equal_notequal_hash ( self ) : <EOL> """<STR_LIT>""" <EOL> p1 = self . PersonModel ( name = "<STR_LIT>" ) <EOL> p1 . mugshot . save ( "<STR_LIT>" , self . file1 ) <EOL> p2 = self . PersonModel ( name = "<STR_LIT>" ) <EOL> p2 . mugshot . save ( "<STR_LIT>" , self . file2 ) <EOL> self . assertEqual ( p1 . mugshot == p2 . mugshot , False ) <EOL> self . assertEqual ( p1 . mugshot != p2 . mugshot , True ) <EOL> p1_db = self . PersonModel . objects . get ( name = "<STR_LIT>" ) <EOL> self . assertEqual ( p1_db . mugshot == p2 . mugshot , False ) <EOL> self . assertEqual ( p1_db . mugshot != p2 . mugshot , True ) <EOL> self . assertEqual ( p1_db . mugshot == p1 . mugshot , True ) <EOL> self . assertEqual ( hash ( p1_db . mugshot ) , hash ( p1 . mugshot ) ) <EOL> self . assertEqual ( p1_db . mugshot != p1 . mugshot , False ) <EOL> def test_instantiate_missing ( self ) : <EOL> """<STR_LIT>""" <EOL> p = self . PersonModel ( name = "<STR_LIT>" ) <EOL> p . mugshot . save ( "<STR_LIT>" , self . file1 ) <EOL> p = self . PersonModel . objects . get ( name = "<STR_LIT>" ) <EOL> path = p . mugshot . path <EOL> shutil . move ( path , path + '<STR_LIT>' ) <EOL> p2 = self . PersonModel . objects . get ( name = "<STR_LIT>" ) <EOL> def test_delete_when_missing ( self ) : <EOL> """<STR_LIT>""" <EOL> p = self . PersonModel ( name = "<STR_LIT>" ) <EOL> p . mugshot . save ( "<STR_LIT>" , self . file1 ) <EOL> os . remove ( p . mugshot . path ) <EOL> p . delete ( ) <EOL> def test_size_method ( self ) : <EOL> """<STR_LIT>""" <EOL> p = self . PersonModel ( name = "<STR_LIT>" ) <EOL> p . mugshot . save ( "<STR_LIT>" , self . file1 ) <EOL> p = self . PersonModel . objects . get ( name = "<STR_LIT>" ) <EOL> self . assertEqual ( p . mugshot . closed , True ) <EOL> _ = p . mugshot . size <EOL> self . assertEqual ( p . mugshot . closed , True ) <EOL> def test_pickle ( self ) : <EOL> """<STR_LIT>""" <EOL> import pickle <EOL> p = Person ( name = "<STR_LIT>" ) <EOL> p . mugshot . save ( "<STR_LIT>" , self . file1 ) <EOL> dump = pickle . dumps ( p ) <EOL> p2 = Person ( name = "<STR_LIT>" ) <EOL> p2 . mugshot = self . file1 <EOL> loaded_p = pickle . loads ( dump ) <EOL> self . assertEqual ( p . mugshot , loaded_p . mugshot ) <EOL> @ skipIf ( Image is None , "<STR_LIT>" ) <EOL> class ImageFieldTwoDimensionsTests ( ImageFieldTestMixin , TestCase ) : <EOL> """<STR_LIT>""" <EOL> def test_constructor ( self ) : <EOL> """<STR_LIT>""" <EOL> p = self . PersonModel ( name = '<STR_LIT>' , mugshot = self . file1 ) <EOL> self . check_dimensions ( p , <NUM_LIT:4> , <NUM_LIT:8> ) <EOL> p . save ( ) <EOL> self . check_dimensions ( p , <NUM_LIT:4> , <NUM_LIT:8> ) <EOL> def test_image_after_constructor ( self ) : <EOL> """<STR_LIT>""" <EOL> p = self . PersonModel ( name = '<STR_LIT>' ) <EOL> self . assertEqual ( isinstance ( p . mugshot , TestImageFieldFile ) , True ) <EOL> self . assertEqual ( bool ( p . mugshot ) , False ) <EOL> p = self . PersonModel ( name = '<STR_LIT>' ) <EOL> p . mugshot = self . file1 <EOL> self . check_dimensions ( p , <NUM_LIT:4> , <NUM_LIT:8> ) <EOL> def test_create ( self ) : <EOL> """<STR_LIT>""" <EOL> p = self . PersonModel . objects . create ( name = '<STR_LIT>' , mugshot = self . file1 ) <EOL> self . check_dimensions ( p , <NUM_LIT:4> , <NUM_LIT:8> ) <EOL> def test_default_value ( self ) : <EOL> """<STR_LIT>""" <EOL> p = self . PersonModel ( ) <EOL> self . assertEqual ( isinstance ( p . mugshot , TestImageFieldFile ) , True ) <EOL> self . assertEqual ( bool ( p . mugshot ) , False ) <EOL> def test_assignment_to_None ( self ) : <EOL> """<STR_LIT>""" <EOL> p = self . PersonModel ( name = '<STR_LIT>' , mugshot = self . file1 ) <EOL> self . check_dimensions ( p , <NUM_LIT:4> , <NUM_LIT:8> ) <EOL> p . mugshot = None <EOL> self . check_dimensions ( p , None , None ) <EOL> p . mugshot = self . file2 <EOL> self . check_dimensions ( p , <NUM_LIT:8> , <NUM_LIT:4> ) <EOL> def test_field_save_and_delete_methods ( self ) : <EOL> """<STR_LIT>""" <EOL> p = self . PersonModel ( name = '<STR_LIT>' ) <EOL> p . mugshot . save ( "<STR_LIT>" , self . file1 ) <EOL> self . check_dimensions ( p , <NUM_LIT:4> , <NUM_LIT:8> ) <EOL> p . mugshot . save ( "<STR_LIT>" , self . file2 ) <EOL> self . check_dimensions ( p , <NUM_LIT:8> , <NUM_LIT:4> ) <EOL> p . mugshot . delete ( save = False ) <EOL> self . assertEqual ( p . mugshot , None ) <EOL> self . check_dimensions ( p , None , None ) <EOL> def test_dimensions ( self ) : <EOL> """<STR_LIT>""" <EOL> p = self . PersonModel ( name = '<STR_LIT>' ) <EOL> p . mugshot . save ( "<STR_LIT>" , self . file1 ) <EOL> self . check_dimensions ( p , <NUM_LIT:4> , <NUM_LIT:8> ) <EOL> p = self . PersonModel . objects . get ( name = '<STR_LIT>' ) <EOL> self . assertEqual ( p . mugshot . was_opened , False ) <EOL> self . check_dimensions ( p , <NUM_LIT:4> , <NUM_LIT:8> ) <EOL> self . assertEqual ( p . mugshot . was_opened , True ) <EOL> p . mugshot . was_opened = False <EOL> self . check_dimensions ( p , <NUM_LIT:4> , <NUM_LIT:8> ) <EOL> self . assertEqual ( p . mugshot . was_opened , False ) <EOL> p . mugshot = self . file2 <EOL> self . check_dimensions ( p , <NUM_LIT:8> , <NUM_LIT:4> ) <EOL> self . assertEqual ( p . mugshot . was_opened , True ) <EOL> @ skipIf ( Image is None , "<STR_LIT>" ) <EOL> class ImageFieldNoDimensionsTests ( ImageFieldTwoDimensionsTests ) : <EOL> """<STR_LIT>""" <EOL> PersonModel = Person <EOL> @ skipIf ( Image is None , "<STR_LIT>" ) <EOL> class ImageFieldOneDimensionTests ( ImageFieldTwoDimensionsTests ) : <EOL> """<STR_LIT>""" <EOL> PersonModel = PersonWithHeight <EOL> @ skipIf ( Image is None , "<STR_LIT>" ) <EOL> class ImageFieldDimensionsFirstTests ( ImageFieldTwoDimensionsTests ) : <EOL> """<STR_LIT>""" <EOL> PersonModel = PersonDimensionsFirst <EOL> @ skipIf ( Image is None , "<STR_LIT>" ) <EOL> class ImageFieldUsingFileTests ( ImageFieldTwoDimensionsTests ) : <EOL> """<STR_LIT>""" <EOL> PersonModel = PersonDimensionsFirst <EOL> File = File <EOL> @ skipIf ( Image is None , "<STR_LIT>" ) <EOL> class TwoImageFieldTests ( ImageFieldTestMixin , TestCase ) : <EOL> """<STR_LIT>""" <EOL> PersonModel = PersonTwoImages <EOL> def test_constructor ( self ) : <EOL> p = self . PersonModel ( mugshot = self . file1 , headshot = self . file2 ) <EOL> self . check_dimensions ( p , <NUM_LIT:4> , <NUM_LIT:8> , '<STR_LIT>' ) <EOL> self . check_dimensions ( p , <NUM_LIT:8> , <NUM_LIT:4> , '<STR_LIT>' ) <EOL> p . save ( ) <EOL> self . check_dimensions ( p , <NUM_LIT:4> , <NUM_LIT:8> , '<STR_LIT>' ) <EOL> self . check_dimensions ( p , <NUM_LIT:8> , <NUM_LIT:4> , '<STR_LIT>' ) <EOL> def test_create ( self ) : <EOL> p = self . PersonModel . objects . create ( mugshot = self . file1 , <EOL> headshot = self . file2 ) <EOL> self . check_dimensions ( p , <NUM_LIT:4> , <NUM_LIT:8> ) <EOL> self . check_dimensions ( p , <NUM_LIT:8> , <NUM_LIT:4> , '<STR_LIT>' ) <EOL> def test_assignment ( self ) : <EOL> p = self . PersonModel ( ) <EOL> self . check_dimensions ( p , None , None , '<STR_LIT>' ) <EOL> self . check_dimensions ( p , None , None , '<STR_LIT>' ) <EOL> p . mugshot = self . file1 <EOL> self . check_dimensions ( p , <NUM_LIT:4> , <NUM_LIT:8> , '<STR_LIT>' ) <EOL> self . check_dimensions ( p , None , None , '<STR_LIT>' ) <EOL> p . headshot = self . file2 <EOL> self . check_dimensions ( p , <NUM_LIT:4> , <NUM_LIT:8> , '<STR_LIT>' ) <EOL> self . check_dimensions ( p , <NUM_LIT:8> , <NUM_LIT:4> , '<STR_LIT>' ) <EOL> p . mugshot = None <EOL> self . check_dimensions ( p , None , None , '<STR_LIT>' ) <EOL> self . check_dimensions ( p , <NUM_LIT:8> , <NUM_LIT:4> , '<STR_LIT>' ) <EOL> p . headshot = None <EOL> self . check_dimensions ( p , None , None , '<STR_LIT>' ) <EOL> self . check_dimensions ( p , None , None , '<STR_LIT>' ) <EOL> def test_field_save_and_delete_methods ( self ) : <EOL> p = self . PersonModel ( name = '<STR_LIT>' ) <EOL> p . mugshot . save ( "<STR_LIT>" , self . file1 ) <EOL> self . check_dimensions ( p , <NUM_LIT:4> , <NUM_LIT:8> , '<STR_LIT>' ) <EOL> self . check_dimensions ( p , None , None , '<STR_LIT>' ) <EOL> p . headshot . save ( "<STR_LIT>" , self . file2 ) <EOL> self . check_dimensions ( p , <NUM_LIT:4> , <NUM_LIT:8> , '<STR_LIT>' ) <EOL> self . check_dimensions ( p , <NUM_LIT:8> , <NUM_LIT:4> , '<STR_LIT>' ) <EOL> p . headshot . delete ( save = True ) <EOL> self . check_dimensions ( p , <NUM_LIT:4> , <NUM_LIT:8> , '<STR_LIT>' ) <EOL> self . check_dimensions ( p , None , None , '<STR_LIT>' ) <EOL> p . mugshot . delete ( save = False ) <EOL> self . check_dimensions ( p , None , None , '<STR_LIT>' ) <EOL> self . check_dimensions ( p , None , None , '<STR_LIT>' ) <EOL> def test_dimensions ( self ) : <EOL> """<STR_LIT>""" <EOL> p = self . PersonModel ( name = '<STR_LIT>' ) <EOL> p . mugshot . save ( "<STR_LIT>" , self . file1 ) <EOL> p . headshot . save ( "<STR_LIT>" , self . file2 ) <EOL> self . check_dimensions ( p , <NUM_LIT:4> , <NUM_LIT:8> , '<STR_LIT>' ) <EOL> self . check_dimensions ( p , <NUM_LIT:8> , <NUM_LIT:4> , '<STR_LIT>' ) <EOL> p = self . PersonModel . objects . get ( name = '<STR_LIT>' ) <EOL> self . assertEqual ( p . mugshot . was_opened , False ) <EOL> self . assertEqual ( p . headshot . was_opened , False ) <EOL> self . check_dimensions ( p , <NUM_LIT:4> , <NUM_LIT:8> , '<STR_LIT>' ) <EOL> self . check_dimensions ( p , <NUM_LIT:8> , <NUM_LIT:4> , '<STR_LIT>' ) <EOL> self . assertEqual ( p . mugshot . was_opened , True ) <EOL> self . assertEqual ( p . headshot . was_opened , True ) <EOL> p . mugshot . was_opened = False <EOL> p . headshot . was_opened = False <EOL> self . check_dimensions ( p , <NUM_LIT:4> , <NUM_LIT:8> , '<STR_LIT>' ) <EOL> self . check_dimensions ( p , <NUM_LIT:8> , <NUM_LIT:4> , '<STR_LIT>' ) <EOL> self . assertEqual ( p . mugshot . was_opened , False ) <EOL> self . assertEqual ( p . headshot . was_opened , False ) <EOL> p . mugshot = self . file2 <EOL> p . headshot = self . file1 <EOL> self . check_dimensions ( p , <NUM_LIT:8> , <NUM_LIT:4> , '<STR_LIT>' ) <EOL> self . check_dimensions ( p , <NUM_LIT:4> , <NUM_LIT:8> , '<STR_LIT>' ) <EOL> self . assertEqual ( p . mugshot . was_opened , True ) <EOL> self . assertEqual ( p . headshot . was_opened , True ) </s>
