diff options
-rwxr-xr-x | test/TestsRunner.py | 3 | ||||
-rw-r--r-- | test/xmp.py | 103 |
2 files changed, 104 insertions, 2 deletions
diff --git a/test/TestsRunner.py b/test/TestsRunner.py index d0a1c92..22b94b9 100755 --- a/test/TestsRunner.py +++ b/test/TestsRunner.py @@ -34,7 +34,7 @@ from gps_coordinate import TestGPSCoordinate from notifying_list import TestNotifyingList from exif import TestExifTag from iptc import TestIptcTag -from xmp import TestXmpTag +from xmp import TestXmpTag, TestXmpNamespaces from metadata import TestImageMetadata from buffer import TestBuffer from encoding import TestEncodings @@ -51,6 +51,7 @@ def run_unit_tests(): suite.addTest(unittest.defaultTestLoader.loadTestsFromTestCase(TestExifTag)) suite.addTest(unittest.defaultTestLoader.loadTestsFromTestCase(TestIptcTag)) suite.addTest(unittest.defaultTestLoader.loadTestsFromTestCase(TestXmpTag)) + suite.addTest(unittest.defaultTestLoader.loadTestsFromTestCase(TestXmpNamespaces)) suite.addTest(unittest.defaultTestLoader.loadTestsFromTestCase(TestImageMetadata)) suite.addTest(unittest.defaultTestLoader.loadTestsFromTestCase(TestBuffer)) suite.addTest(unittest.defaultTestLoader.loadTestsFromTestCase(TestEncodings)) diff --git a/test/xmp.py b/test/xmp.py index 10e03a8..cfcf2fd 100644 --- a/test/xmp.py +++ b/test/xmp.py @@ -26,8 +26,10 @@ import unittest -from pyexiv2.xmp import XmpTag, XmpValueError +from pyexiv2.xmp import XmpTag, XmpValueError, register_namespace, \ + unregister_namespace, unregister_namespaces from pyexiv2.utils import FixedOffset +from pyexiv2.metadata import ImageMetadata import datetime @@ -298,3 +300,102 @@ class TestXmpTag(unittest.TestCase): self.failUnlessEqual(tag.type, 'Lang Alt') self.failUnlessRaises(ValueError, tag._set_value, {}) + +class TestXmpNamespaces(unittest.TestCase): + + def setUp(self): + # FIXME: EMPTY_JPG_DATA should go in testutils + from metadata import EMPTY_JPG_DATA + self.metadata = ImageMetadata.from_buffer(EMPTY_JPG_DATA) + self.metadata.read() + + def test_not_registered(self): + self.assertEqual(len(self.metadata.xmp_keys), 0) + key = 'Xmp.foo.bar' + value = 'foobar' + self.assertRaises(KeyError, self.metadata.__setitem__, key, value) + + def test_name_must_end_with_slash(self): + self.assertRaises(ValueError, register_namespace, 'foobar', 'foo') + self.assertRaises(ValueError, unregister_namespace, 'foobar') + + def test_cannot_register_builtin(self): + self.assertRaises(KeyError, register_namespace, 'foobar/', 'dc') + + def test_cannot_register_twice(self): + name = 'foobar/' + prefix = 'boo' + register_namespace(name, prefix) + self.assertRaises(KeyError, register_namespace, name, prefix) + + def test_register_and_set(self): + register_namespace('foobar/', 'bar') + key = 'Xmp.bar.foo' + value = 'foobar' + self.metadata[key] = value + self.assert_(key in self.metadata.xmp_keys) + + def test_can_only_set_text_values(self): + # At the moment custom namespaces only support setting simple text + # values. + register_namespace('foobar/', 'far') + key = 'Xmp.far.foo' + value = datetime.date.today() + self.assertRaises(XmpValueError, self.metadata.__setitem__, key, value) + value = datetime.datetime.now() + self.assertRaises(XmpValueError, self.metadata.__setitem__, key, value) + value = ['foo', 'bar'] + self.assertRaises(XmpValueError, self.metadata.__setitem__, key, value) + value = {'x-default': 'foo', 'fr-FR': 'bar'} + self.assertRaises(XmpValueError, self.metadata.__setitem__, key, value) + value = 'simple text value' + self.metadata[key] = value + + def test_cannot_unregister_builtin(self): + name = 'http://purl.org/dc/elements/1.1/' # DC builtin namespace + self.assertRaises(KeyError, unregister_namespace, name) + + def test_cannot_unregister_inexistent(self): + name = 'boofar/' + self.assertRaises(KeyError, unregister_namespace, name) + + def test_cannot_unregister_twice(self): + name = 'bleh/' + prefix = 'ble' + register_namespace(name, prefix) + unregister_namespace(name) + self.assertRaises(KeyError, unregister_namespace, name) + + def test_unregister(self): + name = 'blah/' + prefix = 'bla' + register_namespace(name, prefix) + unregister_namespace(name) + + def test_unregister_invalidates_keys_in_ns(self): + name = 'blih/' + prefix = 'bli' + register_namespace(name, prefix) + key = 'Xmp.%s.blu' % prefix + self.metadata[key] = 'foobar' + self.assert_(key in self.metadata.xmp_keys) + unregister_namespace(name) + self.assertRaises(KeyError, self.metadata.write) + + def test_unregister_all_ns(self): + # Unregistering all custom namespaces will always succeed, even if there + # are no custom namespaces registered. + unregister_namespaces() + + name = 'blop/' + prefix = 'blo' + register_namespace(name, prefix) + self.metadata['Xmp.%s.bar' % prefix] = 'foobar' + name2 = 'blup/' + prefix2 = 'blu' + register_namespace(name2, prefix2) + self.metadata['Xmp.%s.bar' % prefix2] = 'foobar' + unregister_namespaces() + self.assertRaises(KeyError, self.metadata.__setitem__, 'Xmp.%s.baz' % prefix, 'foobaz') + self.assertRaises(KeyError, self.metadata.__setitem__, 'Xmp.%s.baz' % prefix2, 'foobaz') + |