<s> from PIL import Image <EOL> from django . core . files . base import ContentFile <EOL> from test_ella . cases import RedisTestCase as TestCase <EOL> from django . contrib . sites . models import Site <EOL> from nose import tools <EOL> from ella . photos . models import Format , FormatedPhoto , redis , REDIS_FORMATTED_PHOTO_KEY <EOL> from ella . photos . conf import photos_settings <EOL> from test_ella . test_photos . fixtures import create_photo_formats , create_photo <EOL> class TestPhoto ( TestCase ) : <EOL> def setUp ( self ) : <EOL> super ( TestPhoto , self ) . setUp ( ) <EOL> create_photo_formats ( self ) <EOL> create_photo ( self ) <EOL> def tearDown ( self ) : <EOL> photos_settings . FORMATED_PHOTO_FILENAME = None <EOL> if self . photo . pk : <EOL> self . photo . delete ( ) <EOL> super ( TestPhoto , self ) . tearDown ( ) <EOL> if redis : <EOL> redis . flushdb ( ) <EOL> def test_formated_photo_from_master_format_is_used ( self ) : <EOL> master_format = Format . objects . create ( <EOL> name = '<STR_LIT>' , <EOL> max_width = <NUM_LIT> , <EOL> max_height = <NUM_LIT:200> , <EOL> flexible_height = False , <EOL> stretch = False , <EOL> nocrop = False <EOL> ) <EOL> slave_format = Format . objects . create ( <EOL> name = '<STR_LIT>' , <EOL> max_width = <NUM_LIT> , <EOL> max_height = <NUM_LIT:100> , <EOL> flexible_height = False , <EOL> stretch = False , <EOL> nocrop = False , <EOL> master = master_format <EOL> ) <EOL> master_fp = FormatedPhoto ( photo = self . photo , format = master_format ) <EOL> p2 = create_photo ( self , color = ( <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ) , size = ( <NUM_LIT> , <NUM_LIT:200> ) ) <EOL> p2 . image . open ( ) <EOL> master_fp . image . save ( p2 . image . name , p2 . image ) <EOL> master_fp . save ( ) <EOL> fp = FormatedPhoto ( photo = self . photo , format = slave_format ) <EOL> fp . save ( ) <EOL> fp . image . open ( ) <EOL> i = Image . open ( fp . image ) <EOL> tools . assert_equals ( ( <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ) , i . getpixel ( ( <NUM_LIT:10> , <NUM_LIT:10> ) ) ) <EOL> tools . assert_equals ( ( <NUM_LIT> , <NUM_LIT:100> ) , i . size ) <EOL> p2 . image . close ( ) <EOL> def test_formatted_photo_has_zero_crop_box_if_smaller_than_format ( self ) : <EOL> format = Format . objects . create ( <EOL> name = '<STR_LIT>' , <EOL> max_width = <NUM_LIT> , <EOL> max_height = <NUM_LIT> , <EOL> flexible_height = False , <EOL> stretch = False , <EOL> nocrop = False <EOL> ) <EOL> format . sites . add ( Site . objects . get_current ( ) ) <EOL> fp = FormatedPhoto ( photo = self . photo , format = format ) <EOL> fp . generate ( False ) <EOL> tools . assert_equals ( ( <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> ) , ( fp . crop_left , fp . crop_top , fp . crop_width , fp . crop_height ) ) <EOL> def test_formated_filename_can_be_overridden ( self ) : <EOL> photos_settings . FORMATED_PHOTO_FILENAME = lambda fp : '<STR_LIT>' <EOL> formatted = FormatedPhoto . objects . get_photo_in_format ( self . photo , self . basic_format ) <EOL> tools . assert_true ( '<STR_LIT:url>' in formatted ) <EOL> tools . assert_true ( formatted [ '<STR_LIT:url>' ] . endswith ( '<STR_LIT>' ) ) <EOL> def test_retrieving_formatted_photos_on_fly ( self ) : <EOL> formatted = FormatedPhoto . objects . get_photo_in_format ( self . photo , self . basic_format ) <EOL> tools . assert_true ( '<STR_LIT:url>' in formatted ) <EOL> if redis : <EOL> expected = { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT:width>' : '<STR_LIT>' , <EOL> '<STR_LIT:url>' : '<STR_LIT>' , <EOL> } <EOL> actual = redis . hgetall ( REDIS_FORMATTED_PHOTO_KEY % ( self . photo . id , self . basic_format . id ) ) <EOL> tools . assert_equals ( expected . keys ( ) , actual . keys ( ) ) <EOL> tools . assert_equals ( expected [ '<STR_LIT:width>' ] , actual [ '<STR_LIT:width>' ] ) <EOL> tools . assert_equals ( expected [ '<STR_LIT>' ] , actual [ '<STR_LIT>' ] ) <EOL> def test_formattedphoto_cleared_when_image_changed ( self ) : <EOL> FormatedPhoto . objects . get_photo_in_format ( self . photo , self . basic_format ) <EOL> tools . assert_equals ( <NUM_LIT:1> , len ( self . photo . formatedphoto_set . all ( ) ) ) <EOL> self . photo . image . open ( ) <EOL> file = ContentFile ( self . photo . image . read ( ) ) <EOL> self . photo . image . save ( "<STR_LIT>" , file ) <EOL> self . photo . save ( ) <EOL> tools . assert_equals ( <NUM_LIT:0> , len ( self . photo . formatedphoto_set . all ( ) ) ) <EOL> def test_formattedphoto_cleared_when_format_changed ( self ) : <EOL> FormatedPhoto . objects . get_photo_in_format ( self . photo , self . basic_format ) <EOL> tools . assert_equals ( <NUM_LIT:1> , len ( self . basic_format . formatedphoto_set . all ( ) ) ) <EOL> self . basic_format . nocrop = True <EOL> self . basic_format . save ( ) <EOL> tools . assert_equals ( <NUM_LIT:0> , len ( self . basic_format . formatedphoto_set . all ( ) ) ) <EOL> def test_retrieving_ratio ( self ) : <EOL> tools . assert_equals ( <NUM_LIT:2> , self . photo . ratio ( ) ) </s>
