def test_set(self): # Create a Photo object. test_kw = PhotoTag.objects.create(name="test") photo = Photo() owner = User.objects.create(username='******') album = Album.objects.create(name='test', owner=owner) photo.owner = owner photo.album = album image = open(self.file_path) photo.image = ImageFile(image) photo.save() image.close() self.assertEqual(test_kw.photo_set.count(), 0) # Reminder: Database is assumed to be empty at the start of this test. self.assertEqual(PhotoTag.objects.count(), 1) # Set the photo's keywords. photo.keyword_list = ['Test', 'photo'] photo.save() self.assertEqual(photo.keyword_list, [u'test', u'photo']) self.assertEqual(test_kw.photo_set.count(), 1) self.assertEqual(PhotoTag.objects.count(), 2) # Set the photo's keywords to something completely different. photo.keyword_list = ['foo', 'bar'] photo.save() self.assertEqual(photo.keyword_list, [u'foo', u'bar']) self.assertEqual(test_kw.photo_set.count(), 0) self.assertEqual(PhotoTag.objects.count(), 4) # Set the photo's keywords to an empty list. photo.keyword_list = [] photo.save() self.assertEqual(photo.keyword_list, [])
def runTest(self): # Create an image. file_descriptor, file_path = tempfile.mkstemp(suffix='.jpg') os.close(file_descriptor) Image.new('RGB', (1, 1)).save(file_path, 'JPEG') # Create a Photo object. user = User.objects.create(username="******") album = Album.objects.create(owner=user, name="Test") photo = Photo() photo.owner = user image = open(file_path) photo.image = ImageFile(image) photo.description = "Test file" photo.artist = "Adam" photo.country = "USA" photo.province_state = "Virginia" photo.city = "Blacksburg" photo.location = "Dreamland" photo.time_created = datetime.datetime(2007, 9, 28, 3, 0) photo.album = album photo.is_jpeg = True photo.save() image.close() os.remove(file_path) photo.keyword_list = ['test', 'photo'] photo.save() # Synchronize. performed_sync = photo.sync_metadata_to_file() self.assertTrue(performed_sync) # Metadata should exist in the file itself. metadata = pyexiv2.Image(photo.image.path) metadata.readMetadata() self.assertEqual(metadata['Exif.Image.ImageDescription'], 'Test file') self.assertEqual(metadata['Exif.Image.Artist'], 'Adam') self.assertEqual(metadata['Iptc.Application2.CountryName'], 'USA') self.assertEqual(metadata['Iptc.Application2.ProvinceState'], 'Virginia') self.assertEqual(metadata['Iptc.Application2.City'], 'Blacksburg') self.assertEqual(metadata['Iptc.Application2.SubLocation'], 'Dreamland') self.assertEqual(str(metadata['Exif.Photo.DateTimeOriginal']), '2007-09-28 03:00:00') self.assertEqual(metadata['Iptc.Application2.Keywords'], ('test', 'photo')) self.assertEqual(metadata['Exif.Photo.PixelXDimension'], 1) self.assertEqual(metadata['Exif.Photo.PixelYDimension'], 1) # Attempt to synchronize again... self.assertFalse(photo.sync_metadata_to_file()) # Nothing should have changed. metadata = pyexiv2.Image(photo.image.path) metadata.readMetadata() self.assertEqual(metadata['Exif.Image.ImageDescription'], 'Test file') self.assertEqual(metadata['Exif.Image.Artist'], 'Adam') self.assertEqual(metadata['Iptc.Application2.CountryName'], 'USA') self.assertEqual(metadata['Iptc.Application2.ProvinceState'], 'Virginia') self.assertEqual(metadata['Iptc.Application2.City'], 'Blacksburg') self.assertEqual(metadata['Iptc.Application2.SubLocation'], 'Dreamland') self.assertEqual(str(metadata['Exif.Photo.DateTimeOriginal']), '2007-09-28 03:00:00') self.assertEqual(metadata['Iptc.Application2.Keywords'], ('test', 'photo')) self.assertEqual(metadata['Exif.Photo.PixelXDimension'], 1) self.assertEqual(metadata['Exif.Photo.PixelYDimension'], 1) # Synchronize only one field. photo.description = "Image for testing" photo.save() performed_sync = photo.sync_metadata_to_file() self.assertTrue(performed_sync) metadata = pyexiv2.Image(photo.image.path) metadata.readMetadata() self.assertEqual(metadata['Exif.Image.ImageDescription'], 'Image for testing') # Disable synchronization. photo.metadata_sync_enabled = False photo.description = "Sample image for testing purposes" photo.save() self.assertFalse(photo.sync_metadata_to_file()) # Sometimes the image isn't a JPEG. file_descriptor, file_path = tempfile.mkstemp(suffix='.tif') os.close(file_descriptor) Image.new('RGB', (1, 1)).save(file_path, 'TIFF') photo = Photo() photo.owner = user photo.description = 'Test file' image = open(file_path) photo.image = ImageFile(image) photo.album = album photo.is_jpeg = False photo.save() image.close() os.remove(file_path) performed_sync = photo.sync_metadata_to_file() self.assertTrue(performed_sync) metadata = pyexiv2.Image(photo.image.path) metadata.readMetadata() self.assertEqual(metadata['Exif.Image.ImageWidth'], 1) self.assertEqual(metadata['Exif.Image.ImageLength'], 1) # Sometimes metadata can't be read from the image. file_descriptor, file_path = tempfile.mkstemp(suffix='.pcx') os.close(file_descriptor) Image.new('RGB', (1, 1)).save(file_path, 'PCX') photo = Photo() photo.owner = user photo.description = 'Test file' image = open(file_path) photo.image = ImageFile(image) photo.album = album photo.save() image.close() os.remove(file_path) self.assertFalse(photo.sync_metadata_to_file()) self.assertFalse(photo.metadata_sync_enabled) # Sometimes metadata can't be written to the image. file_descriptor, file_path = tempfile.mkstemp(suffix='.bmp') os.close(file_descriptor) Image.new('RGB', (1, 1)).save(file_path, 'BMP') photo = Photo() photo.owner = user photo.description = 'Test file' image = open(file_path) photo.image = ImageFile(image) photo.album = album photo.save() image.close() os.remove(file_path) self.assertFalse(photo.sync_metadata_to_file()) self.assertFalse(photo.metadata_sync_enabled) User.objects.all().delete() Album.objects.all().delete() PhotoTag.objects.all().delete() Photo.objects.all().delete()