def test_setattr_nested_struct_from_dict(self): metadata = XMPMetadata() metadata[TEST_NS].test_struct = { "a": 1, "b": 2, "c": { "x": 3, "y": 4, "z": 5 } } test_struct = metadata[TEST_NS].test_struct self.assertIsInstance(test_struct, XMPStructure) self.assertIsInstance(test_struct.a, XMPValue) self.assertIsInstance(test_struct.b, XMPValue) self.assertIsInstance(test_struct.c, XMPStructure) self.assertEqual(len(test_struct), 3) self.assertEqual(len(test_struct.c), 3) self.assertEqual(test_struct.a.value, "1") self.assertEqual(test_struct.b.value, "2") self.assertEqual(test_struct.c.x.value, "3") self.assertEqual(test_struct.c.y.value, "4") self.assertDictEqual(test_struct.value, { "test:a": "1", "test:b": "2", "test:c": { "test:x": "3", "test:y": "4", "test:z": "5" } })
def test_setattr_nested_inexistent(self): metadata = XMPMetadata() metadata[TEST_NS].A.B = 12 self.assertIsInstance(metadata[TEST_NS].A, XMPStructure) self.assertIsInstance(metadata[TEST_NS].A.B, XMPValue) self.assertEqual(metadata[TEST_NS].A.B.value, "12")
def test_array_insert(self): metadata = XMPMetadata() metadata[TEST_NS].test_array = [0,2,3] self.assertListEqual(metadata[TEST_NS].test_array.value, ["0","2","3"]) metadata[TEST_NS].test_array.insert(1, 1) self.assertListEqual(metadata[TEST_NS].test_array.value, ["0","1","2","3"])
def test_setattr_set(self): metadata = XMPMetadata() test_metadata = metadata[TEST_NS] test_metadata.root_set = {3, 1, 4} self.assertIsInstance(test_metadata.root_set, XMPSet) self.assertEqual(len(test_metadata.root_set), 3) self.assertSetEqual(test_metadata.root_set.value, {"3", "1", "4"})
def test_setattr_top_level_array(self): metadata = XMPMetadata() metadata[TEST_NS].top_level_array = [1,2,3,"a","b","c"] self.assertIsInstance(metadata[TEST_NS].top_level_array, XMPArray) self.assertIsInstance(metadata[TEST_NS].top_level_array.value, list) self.assertListEqual(metadata[TEST_NS].top_level_array.value, ["1","2","3","a","b","c"])
def test_array_shrink(self): metadata = XMPMetadata() metadata[TEST_NS].top_level_array = [3,4,5,6] self.assertEqual(len(metadata[TEST_NS].top_level_array), 4) self.assertListEqual(metadata[TEST_NS].top_level_array.value, ["3","4","5","6"]) metadata[TEST_NS].top_level_array = [1,2] self.assertEqual(len(metadata[TEST_NS].top_level_array), 2) self.assertListEqual(metadata[TEST_NS].top_level_array.value, ["1","2"])
def test_setattr_array_in_struct(self): metadata = XMPMetadata() metadata[TEST_NS].container.nested_array = [3.14, u"π"] tested_array = metadata[TEST_NS].container.nested_array self.assertIsInstance(metadata[TEST_NS].container, XMPStructure) self.assertEqual(len(metadata[TEST_NS].container), 1) self.assertIsInstance(tested_array, XMPArray) self.assertEqual(len(tested_array), 2) self.assertEqual(tested_array.value, ["3.14", u"π"])
def test_update_namespace(self): metadata = XMPMetadata() test_metadata = metadata[TEST_NS] test_metadata.update({"a": 12, "b": [1,2]}) self.assertEqual(len(test_metadata), 2) self.assertIsInstance(test_metadata.a, XMPValue) self.assertEqual(test_metadata.a.value, "12") self.assertIsInstance(test_metadata.b, XMPArray) self.assertEqual(len(test_metadata.b), 2) self.assertListEqual(test_metadata.b.value, ["1","2"])
def test_contains(self): metadata = XMPMetadata() test_metadata = metadata[TEST_NS] test_metadata.root_set = {3, 1, 4} self.assertTrue(3 in test_metadata.root_set) self.assertTrue(1 in test_metadata.root_set) self.assertTrue(4 in test_metadata.root_set) self.assertFalse(2 in test_metadata.root_set) for i in range(5,20): self.assertFalse(i in test_metadata.root_set)
def test_array_delitem(self): metadata = XMPMetadata() metadata[TEST_NS].test_array = range(12) self.assertListEqual(metadata[TEST_NS].test_array.value, [str(x) for x in range(12)]) del metadata[TEST_NS].test_array[6:] self.assertListEqual(metadata[TEST_NS].test_array.value, [str(x) for x in range(6)]) del metadata[TEST_NS].test_array[::2] self.assertListEqual(metadata[TEST_NS].test_array.value, [str(x) for x in range(6)[1::2]]) del metadata[TEST_NS].test_array[:] self.assertListEqual(metadata[TEST_NS].test_array.value, [])
def test_setattr_array_in_array(self): metadata = XMPMetadata() metadata[TEST_NS].top_level_array = [1,[2,3,4],5] created_array = metadata[TEST_NS].top_level_array self.assertIsInstance(created_array, XMPArray) self.assertEqual(len(created_array), 3) self.assertIsInstance(created_array.value, list) self.assertIsInstance(created_array[1], XMPArray) self.assertEqual(len(created_array[1]), 3) self.assertIsInstance(created_array.value, list) self.assertEqual(created_array.value, ["1", ["2","3","4"], "5"]) self.assertEqual(created_array[1].value, ["2","3","4"])
def test_namespace_unregistered(self): TEST_NS_2 = u"http://test.com/xmp/test/2" PREFIX_2 = "test2" metadata = XMPMetadata() test_metadata = metadata[TEST_NS_2] with self.assertRaises(NameError): test_metadata["test_key"]=0 registerNamespace(TEST_NS_2, "test2") assert(test_metadata.prefix == PREFIX_2) try: test_metadata["test_key"]=0 except Exception, e: assert(False) pass
def test_setattr_struct_in_array(self): metadata = XMPMetadata() metadata[TEST_NS].root_array[2].nested_structure = 12 root_array = metadata[TEST_NS].root_array self.assertIsInstance(root_array, XMPArray) self.assertEqual(len(root_array), 3) self.assertIsInstance(root_array[0], XMPValue) self.assertIsNone(root_array[0].value) self.assertIsInstance(root_array[1], XMPValue) self.assertIsNone(root_array[1].value) self.assertIsInstance(root_array[2], XMPStructure) self.assertEqual(len(root_array[2]), 1) self.assertEqual(root_array[2].value, {"test:nested_structure": "12"}) self.assertIsInstance(root_array[2].nested_structure, XMPValue) self.assertEqual(root_array[2].nested_structure.value, "12")
def test_create_namespace(self): empty_xmp = XMPMetadata() self.assertEqual(len(empty_xmp.namespaces), 0)
def test_delitem(self): metadata = XMPMetadata() metadata[TEST_NS]["element"] = {"a": 1} self.assertIsInstance(metadata[TEST_NS].element, XMPStructure) del metadata[TEST_NS]["element"] self.assertIsNone(metadata[TEST_NS].get("element"))
def test_setattr_complex(self): metadata = XMPMetadata() test_metadata = metadata[TEST_NS] test_metadata.root_value = 12
def test_empty_init(self): empty_xmp = XMPMetadata() self.assertEqual(len(empty_xmp.namespaces), 0)