def test_convert_to_string_propername(self): # Valid values tag = XmpTag('Xmp.photoshop.CaptionWriter') self.assertEqual(tag.type, 'ProperName') self.assertEqual(tag._convert_to_string('Gérard', 'ProperName'), 'Gérard') self.assertEqual(tag._convert_to_string(u'Gérard', 'ProperName'), 'Gérard') self.assertEqual(tag._convert_to_string(u'Python Software Foundation', 'ProperName'), 'Python Software Foundation') # Invalid values self.failUnlessRaises(XmpValueError, tag._convert_to_string, None, 'ProperName')
def test_convert_to_string_rational(self): # Valid values tag = XmpTag('Xmp.xmpDM.videoPixelAspectRatio') self.assertEqual(tag.type, 'Rational') self.assertEqual(tag._convert_to_string(make_fraction(5, 3), 'Rational'), '5/3') self.assertEqual(tag._convert_to_string(make_fraction(-5, 3), 'Rational'), '-5/3') # Invalid values self.failUnlessRaises(XmpValueError, tag._convert_to_string, 'invalid', 'Rational')
def test_convert_to_string_boolean(self): # Valid values tag = XmpTag('Xmp.xmpRights.Marked') self.assertEqual(tag.type, 'Boolean') self.assertEqual(tag._convert_to_string(True, 'Boolean'), 'True') self.assertEqual(tag._convert_to_string(False, 'Boolean'), 'False') # Invalid values self.failUnlessRaises(XmpValueError, tag._convert_to_string, 'invalid', 'Boolean') self.failUnlessRaises(XmpValueError, tag._convert_to_string, None, 'Boolean')
def test_convert_to_string_bag(self): # Valid values tag = XmpTag('Xmp.dc.Subject') self.assertEqual(tag._convert_to_string('', 'Text'), b'') self.assertEqual(tag._convert_to_string('One value only', 'Text'), b'One value only') # Invalid values self.assertRaises(XmpValueError, tag._convert_to_string, [1, 2, 3], 'Text')
def test_convert_to_string_mimetype(self): # Valid values tag = XmpTag('Xmp.dc.format') self.assertEqual(tag.type, 'MIMEType') self.assertEqual(tag._convert_to_string(('image', 'jpeg'), 'MIMEType'), 'image/jpeg') self.assertEqual(tag._convert_to_string(('video', 'ogg'), 'MIMEType'), 'video/ogg') # Invalid values self.failUnlessRaises(XmpValueError, tag._convert_to_string, 'invalid', 'MIMEType') self.failUnlessRaises(XmpValueError, tag._convert_to_string, ('image',), 'MIMEType')
def test_convert_to_string_bag(self): # Valid values tag = XmpTag('Xmp.dc.subject') self.assertEqual(tag.type, 'bag Text') self.assertEqual(tag._convert_to_string(u'', 'Text'), '') self.assertEqual(tag._convert_to_string('One value only', 'Text'), 'One value only') self.assertEqual(tag._convert_to_string(u'One value only', 'Text'), 'One value only') # Invalid values self.failUnlessRaises(XmpValueError, tag._convert_to_string, [1, 2, 3], 'Text')
def test_convert_to_string_boolean(self): # Valid values tag = XmpTag('Xmp.xmpRights.Marked') self.assertEqual(tag.type, 'Boolean') self.assertEqual(tag._convert_to_string(True, 'Boolean'), 'True') self.assertEqual(tag._convert_to_string(False, 'Boolean'), 'False') # Invalid values self.assertRaises(XmpValueError, tag._convert_to_string, 'invalid', 'Boolean') self.assertRaises(XmpValueError, tag._convert_to_string, None, 'Boolean')
def test_convert_to_string_text(self): # Valid values tag = XmpTag('Xmp.dc.Source') self.assertEqual(tag._convert_to_string('Some text', 'Text'), b'Some text') self.assertEqual( tag._convert_to_string('Some text with exotic chàräctérʐ.', 'Text'), b'Some text with exotic ch\xc3\xa0r\xc3\xa4ct\xc3\xa9r\xca\x90.') # Invalid values self.assertRaises(XmpValueError, tag._convert_to_string, None, 'Text')
def test_convert_to_string_url(self): # Valid values tag = XmpTag('Xmp.xmp.BaseURL') self.assertEqual(tag.type, 'URL') self.assertEqual(tag._convert_to_string('http://example.com', 'URL'), b'http://example.com') self.assertEqual( tag._convert_to_string('http://localhost:8000/resource', 'URL'), b'http://localhost:8000/resource') # Invalid values self.assertRaises(XmpValueError, tag._convert_to_string, None, 'URL')
def test_convert_to_string_text(self): # Valid values tag = XmpTag('Xmp.dc.source') self.assertEqual(tag.type, 'Text') self.assertEqual(tag._convert_to_string(u'Some text', 'Text'), 'Some text') self.assertEqual(tag._convert_to_string(u'Some text with exotic chàräctérʐ.', 'Text'), 'Some text with exotic chàräctérʐ.') self.assertEqual(tag._convert_to_string('Some text with exotic chàräctérʐ.', 'Text'), 'Some text with exotic chàräctérʐ.') # Invalid values self.failUnlessRaises(XmpValueError, tag._convert_to_string, None, 'Text')
def test_convert_to_string_url(self): # Valid values tag = XmpTag('Xmp.xmp.BaseURL') self.assertEqual(tag.type, 'URL') self.assertEqual(tag._convert_to_string('http://example.com', 'URL'), 'http://example.com') self.assertEqual(tag._convert_to_string(u'http://example.com', 'URL'), 'http://example.com') self.assertEqual(tag._convert_to_string('https://example.com', 'URL'), 'https://example.com') self.assertEqual(tag._convert_to_string('http://localhost:8000/resource', 'URL'), 'http://localhost:8000/resource') # Invalid values self.failUnlessRaises(XmpValueError, tag._convert_to_string, None, 'URL')
def test_convert_to_string_propername(self): # Valid values tag = XmpTag('Xmp.photoshop.CaptionWriter') self.assertEqual(tag.type, 'ProperName') self.assertEqual(tag._convert_to_string('Gérard', 'ProperName'), b'G\xc3\xa9rard') self.assertEqual( tag._convert_to_string('Python Software Foundation', 'ProperName'), b'Python Software Foundation') # Invalid values self.assertRaises(XmpValueError, tag._convert_to_string, None, 'ProperName')
def test_convert_to_string_rational(self): # Valid values tag = XmpTag('Xmp.xmpDM.videoPixelAspectRatio') self.assertEqual(tag.type, 'Rational') self.assertEqual( tag._convert_to_string(make_fraction(5, 3), 'Rational'), '5/3') self.assertEqual( tag._convert_to_string(make_fraction(-5, 3), 'Rational'), '-5/3') # Invalid values self.assertRaises(XmpValueError, tag._convert_to_string, 'invalid', 'Rational')
def test_convert_to_string_bag(self): # Valid values tag = XmpTag('Xmp.dc.subject') self.assertEqual(tag.type, 'bag Text') self.assertEqual(tag._convert_to_string(u'', 'Text'), '') self.assertEqual(tag._convert_to_string('One value only', 'Text'), 'One value only') self.assertEqual(tag._convert_to_string(u'One value only', 'Text'), 'One value only') # Invalid values self.failUnlessRaises(XmpValueError, tag._convert_to_string, [1, 2, 3], 'Text')
def test_convert_to_string_mimetype(self): # Valid values tag = XmpTag('Xmp.dc.format') self.assertEqual(tag.type, 'MIMEType') self.assertEqual(tag._convert_to_string(('image', 'jpeg'), 'MIMEType'), 'image/jpeg') self.assertEqual(tag._convert_to_string(('video', 'ogg'), 'MIMEType'), 'video/ogg') # Invalid values self.assertRaises(XmpValueError, tag._convert_to_string, 'invalid', 'MIMEType') self.assertRaises(XmpValueError, tag._convert_to_string, ('image', ), 'MIMEType')
def test_convert_to_string_date_with_real_timezones(self): if pytz is None: # Poor man’s test skipping. Starting with Python 2.7, decorators are # available to implement this in a cleaner fashion # (http://docs.python.org/library/unittest.html#unittest-skipping). print 'Install python-tz to run this test. Skipping.' return tag = XmpTag('Xmp.xmp.CreateDate') self.assertEqual(tag.type, 'Date') t = pytz.timezone('UTC').localize(datetime.datetime(2011, 2, 2, 10, 52, 4)) self.assertEqual(tag._convert_to_string(t, 'Date'), '2011-02-02T10:52:04Z') t = pytz.timezone('CET').localize(datetime.datetime(2011, 2, 2, 10, 52, 4)) self.assertEqual(tag._convert_to_string(t, 'Date'), '2011-02-02T10:52:04+01:00')
def test_convert_to_string_uri(self): # Valid values tag = XmpTag('Xmp.xmpMM.DocumentID') self.assertEqual(tag.type, 'URI') self.assertEqual(tag._convert_to_string('http://example.com', 'URI'), 'http://example.com') self.assertEqual(tag._convert_to_string(u'http://example.com', 'URI'), 'http://example.com') self.assertEqual(tag._convert_to_string('https://example.com', 'URI'), 'https://example.com') self.assertEqual(tag._convert_to_string('http://localhost:8000/resource', 'URI'), 'http://localhost:8000/resource') self.assertEqual(tag._convert_to_string('uuid:9A3B7F52214211DAB6308A7391270C13', 'URI'), 'uuid:9A3B7F52214211DAB6308A7391270C13') # Invalid values self.failUnlessRaises(XmpValueError, tag._convert_to_string, None, 'URI')
def test_convert_to_string_uri(self): # Valid values tag = XmpTag('Xmp.xmpMM.DocumentID') self.assertEqual(tag.type, 'URI') self.assertEqual(tag._convert_to_string('http://example.com', 'URI'), b'http://example.com') self.assertEqual( tag._convert_to_string('http://localhost:8000/resource', 'URI'), b'http://localhost:8000/resource') self.assertEqual( tag._convert_to_string('uuid:9A3B7F52214211DAB6308A7391270C13', 'URI'), b'uuid:9A3B7F52214211DAB6308A7391270C13') # Invalid values self.assertRaises(XmpValueError, tag._convert_to_string, None, 'URI')
def test_convert_to_string_text(self): # Valid values tag = XmpTag('Xmp.dc.source') self.assertEqual(tag.type, 'Text') self.assertEqual(tag._convert_to_string(u'Some text', 'Text'), 'Some text') self.assertEqual( tag._convert_to_string(u'Some text with exotic chàräctérʐ.', 'Text'), 'Some text with exotic chàräctérʐ.') self.assertEqual( tag._convert_to_string('Some text with exotic chàräctérʐ.', 'Text'), 'Some text with exotic chàräctérʐ.') # Invalid values self.failUnlessRaises(XmpValueError, tag._convert_to_string, None, 'Text')
def test_convert_to_string_date_with_real_timezones(self): if pytz is None: # Poor man’s test skipping. Starting with Python 2.7, decorators are # available to implement this in a cleaner fashion # (http://docs.python.org/library/unittest.html#unittest-skipping). print 'Install python-tz to run this test. Skipping.' return tag = XmpTag('Xmp.xmp.CreateDate') self.assertEqual(tag.type, 'Date') t = pytz.timezone('UTC').localize( datetime.datetime(2011, 2, 2, 10, 52, 4)) self.assertEqual(tag._convert_to_string(t, 'Date'), '2011-02-02T10:52:04Z') t = pytz.timezone('CET').localize( datetime.datetime(2011, 2, 2, 10, 52, 4)) self.assertEqual(tag._convert_to_string(t, 'Date'), '2011-02-02T10:52:04+01:00')
def test_convert_to_string_integer(self): # Valid values tag = XmpTag('Xmp.xmpMM.SaveID') self.assertEqual(tag.type, 'Integer') self.assertEqual(tag._convert_to_string(123, 'Integer'), '123') self.assertEqual(tag._convert_to_string(-57, 'Integer'), '-57') # Invalid values self.failUnlessRaises(XmpValueError, tag._convert_to_string, 'invalid', 'Integer') self.failUnlessRaises(XmpValueError, tag._convert_to_string, 3.14, 'Integer')
def test_convert_to_string_integer(self): # Valid values tag = XmpTag('Xmp.xmpMM.SaveID') self.assertEqual(tag.type, 'Integer') self.assertEqual(tag._convert_to_string(123, 'Integer'), '123') self.assertEqual(tag._convert_to_string(-57, 'Integer'), '-57') # Invalid values self.assertRaises(XmpValueError, tag._convert_to_string, 'invalid', 'Integer') self.assertRaises(XmpValueError, tag._convert_to_string, 3.14, 'Integer')
def test_convert_to_string_date(self): # Valid values tag = XmpTag('Xmp.xmp.CreateDate') self.assertEqual(tag.type, 'Date') self.assertEqual(tag._convert_to_string(datetime.date(2009, 2, 4), 'Date'), '2009-02-04') self.assertEqual(tag._convert_to_string(datetime.datetime(1999, 10, 13), 'Date'), '1999-10-13') self.assertEqual(tag._convert_to_string(datetime.datetime(1999, 10, 13, 5, 3, tzinfo=FixedOffset()), 'Date'), '1999-10-13T05:03Z') self.assertEqual(tag._convert_to_string(datetime.datetime(1999, 10, 13, 5, 3, tzinfo=FixedOffset('+', 5, 30)), 'Date'), '1999-10-13T05:03+05:30') self.assertEqual(tag._convert_to_string(datetime.datetime(1999, 10, 13, 5, 3, tzinfo=FixedOffset('-', 11, 30)), 'Date'), '1999-10-13T05:03-11:30') self.assertEqual(tag._convert_to_string(datetime.datetime(1999, 10, 13, 5, 3, 27, tzinfo=FixedOffset()), 'Date'), '1999-10-13T05:03:27Z') self.assertEqual(tag._convert_to_string(datetime.datetime(1999, 10, 13, 5, 3, 27, tzinfo=FixedOffset('+', 5, 30)), 'Date'), '1999-10-13T05:03:27+05:30') self.assertEqual(tag._convert_to_string(datetime.datetime(1999, 10, 13, 5, 3, 27, tzinfo=FixedOffset('-', 11, 30)), 'Date'), '1999-10-13T05:03:27-11:30') self.assertEqual(tag._convert_to_string(datetime.datetime(1999, 10, 13, 5, 3, 27, 124300, tzinfo=FixedOffset()), 'Date'), '1999-10-13T05:03:27.1243Z') self.assertEqual(tag._convert_to_string(datetime.datetime(1999, 10, 13, 5, 3, 27, 124300, tzinfo=FixedOffset('+', 5, 30)), 'Date'), '1999-10-13T05:03:27.1243+05:30') self.assertEqual(tag._convert_to_string(datetime.datetime(1999, 10, 13, 5, 3, 27, 124300, tzinfo=FixedOffset('-', 11, 30)), 'Date'), '1999-10-13T05:03:27.1243-11:30') # Invalid values self.failUnlessRaises(XmpValueError, tag._convert_to_string, 'invalid', 'Date') self.failUnlessRaises(XmpValueError, tag._convert_to_string, None, 'Date')
def test_convert_to_string_date(self): # Valid values tag = XmpTag('Xmp.xmp.CreateDate') self.assertEqual(tag.type, 'Date') self.assertEqual( tag._convert_to_string(datetime.date(2009, 2, 4), 'Date'), '2009-02-04') self.assertEqual( tag._convert_to_string(datetime.date(1899, 12, 31), 'Date'), '1899-12-31') self.assertEqual( tag._convert_to_string(datetime.datetime(1999, 10, 13), 'Date'), '1999-10-13') self.assertEqual( tag._convert_to_string(datetime.datetime(1999, 10, 13, 5, 3), 'Date'), '1999-10-13T05:03Z') self.assertEqual( tag._convert_to_string(datetime.datetime(1899, 12, 31, 23, 59), 'Date'), '1899-12-31T23:59Z') self.assertEqual( tag._convert_to_string( datetime.datetime(1999, 10, 13, 5, 3, tzinfo=FixedOffset()), 'Date'), '1999-10-13T05:03Z') self.assertEqual( tag._convert_to_string( datetime.datetime(1899, 12, 31, 23, 59, tzinfo=FixedOffset()), 'Date'), '1899-12-31T23:59Z') self.assertEqual( tag._convert_to_string( datetime.datetime(1999, 10, 13, 5, 3, tzinfo=FixedOffset('+', 5, 30)), 'Date'), '1999-10-13T05:03+05:30') self.assertEqual( tag._convert_to_string( datetime.datetime(1999, 10, 13, 5, 3, tzinfo=FixedOffset('-', 11, 30)), 'Date'), '1999-10-13T05:03-11:30') self.assertEqual( tag._convert_to_string( datetime.datetime(1899, 12, 31, 23, 59, tzinfo=FixedOffset('+', 5, 30)), 'Date'), '1899-12-31T23:59+05:30') self.assertEqual( tag._convert_to_string(datetime.datetime(1999, 10, 13, 5, 3, 27), 'Date'), '1999-10-13T05:03:27Z') self.assertEqual( tag._convert_to_string(datetime.datetime(1899, 12, 31, 23, 59, 59), 'Date'), '1899-12-31T23:59:59Z') self.assertEqual( tag._convert_to_string( datetime.datetime(1999, 10, 13, 5, 3, 27, tzinfo=FixedOffset()), 'Date'), '1999-10-13T05:03:27Z') self.assertEqual( tag._convert_to_string( datetime.datetime(1899, 12, 31, 23, 59, 59, tzinfo=FixedOffset()), 'Date'), '1899-12-31T23:59:59Z') self.assertEqual( tag._convert_to_string( datetime.datetime(1999, 10, 13, 5, 3, 27, tzinfo=FixedOffset('+', 5, 30)), 'Date'), '1999-10-13T05:03:27+05:30') self.assertEqual( tag._convert_to_string( datetime.datetime(1999, 10, 13, 5, 3, 27, tzinfo=FixedOffset('-', 11, 30)), 'Date'), '1999-10-13T05:03:27-11:30') self.assertEqual( tag._convert_to_string( datetime.datetime(1899, 12, 31, 23, 59, 59, tzinfo=FixedOffset('+', 5, 30)), 'Date'), '1899-12-31T23:59:59+05:30') self.assertEqual( tag._convert_to_string( datetime.datetime(1999, 10, 13, 5, 3, 27, 124300), 'Date'), '1999-10-13T05:03:27.1243Z') self.assertEqual( tag._convert_to_string( datetime.datetime(1899, 12, 31, 23, 59, 59, 124300), 'Date'), '1899-12-31T23:59:59.1243Z') self.assertEqual( tag._convert_to_string( datetime.datetime(1999, 10, 13, 5, 3, 27, 124300, tzinfo=FixedOffset()), 'Date'), '1999-10-13T05:03:27.1243Z') self.assertEqual( tag._convert_to_string( datetime.datetime(1899, 12, 31, 23, 59, 59, 124300, tzinfo=FixedOffset()), 'Date'), '1899-12-31T23:59:59.1243Z') self.assertEqual( tag._convert_to_string( datetime.datetime(1999, 10, 13, 5, 3, 27, 124300, tzinfo=FixedOffset('+', 5, 30)), 'Date'), '1999-10-13T05:03:27.1243+05:30') self.assertEqual( tag._convert_to_string( datetime.datetime(1999, 10, 13, 5, 3, 27, 124300, tzinfo=FixedOffset('-', 11, 30)), 'Date'), '1999-10-13T05:03:27.1243-11:30') self.assertEqual( tag._convert_to_string( datetime.datetime(1899, 12, 31, 23, 59, 59, 124300, tzinfo=FixedOffset('+', 5, 30)), 'Date'), '1899-12-31T23:59:59.1243+05:30') # Invalid values self.assertRaises(XmpValueError, tag._convert_to_string, 'invalid', 'Date') self.assertRaises(XmpValueError, tag._convert_to_string, None, 'Date')