#!/usr/bin/env python # -*- coding: utf-8 -*- # # Copyright (c), 2016-2019, SISSA (International School for Advanced Studies). # All rights reserved. # This file is distributed under the terms of the MIT License. # See the file 'LICENSE' in the root directory of the present # distribution, or http://opensource.org/licenses/MIT. # # @author Davide Brunato # from __future__ import print_function, unicode_literals import unittest from xmlschema import XMLSchemaParseError from xmlschema.tests import XsdValidatorTestCase from xmlschema.validators import XMLSchema11, XsdDefaultOpenContent class TestXsdWildcards(XsdValidatorTestCase): def test_overlap(self): schema = self.schema_class(""" """) any1, any2, any3 = schema.groups['group1'][:] self.assertFalse(any1.is_overlap(any2)) self.assertFalse(any2.is_overlap(any1)) self.assertTrue(any3.is_matching('{foo}x')) self.assertTrue(any3.is_matching('{bar}x')) self.assertTrue(any3.is_matching('{tns1}x')) def test_any_wildcard(self): schema = self.check_schema(""" """) self.assertEqual(schema.types['taggedType'].content_type[-1].namespace, ['##other']) schema = self.check_schema(""" """) self.assertEqual(schema.types['taggedType'].content_type[-1].namespace, ['']) schema = self.check_schema(""" """) self.assertEqual(schema.types['taggedType'].content_type[-1].namespace, ['ns', '']) schema = self.check_schema(""" """) self.assertEqual(schema.types['taggedType'].content_type[-1].namespace, ['tns2', 'tns1', 'tns3']) self.assertEqual(schema.types['taggedType'].content_type[-1].min_occurs, 1) self.assertEqual(schema.types['taggedType'].content_type[-1].max_occurs, 1) schema = self.check_schema(""" """) self.assertEqual(schema.types['taggedType'].content_type[-1].namespace, ('##any',)) self.assertEqual(schema.types['taggedType'].content_type[-1].min_occurs, 10) self.assertIsNone(schema.types['taggedType'].content_type[-1].max_occurs) def test_any_attribute_wildcard(self): schema = self.check_schema(""" """) self.assertEqual(schema.types['taggedType'].attributes[None].namespace, ['tns1:foo']) schema = self.check_schema(""" """) self.assertEqual(schema.types['taggedType'].attributes[None].namespace, ['']) def test_namespace_variants(self): schema = self.schema_class(""" """) any1 = schema.groups['group1'][0] self.assertEqual(any1.namespace, ['urn:a']) any2 = schema.groups['group1'][1] self.assertEqual(any2.namespace, []) class TestXsd11Wildcards(TestXsdWildcards): schema_class = XMLSchema11 def test_is_restriction(self): schema = self.schema_class(""" """) any1, any2, any3 = schema.groups['group1'][:3] self.assertTrue(any1.is_restriction(any1)) self.assertFalse(any1.is_restriction(any2)) self.assertFalse(any1.is_restriction(any3)) self.assertTrue(any2.is_restriction(any1)) self.assertTrue(any2.is_restriction(any2)) self.assertFalse(any2.is_restriction(any3)) self.assertTrue(any3.is_restriction(any1)) self.assertTrue(any3.is_restriction(any2)) self.assertTrue(any3.is_restriction(any3)) any1, any2, any3 = schema.groups['group1'][3:6] self.assertTrue(any1.is_restriction(any1)) self.assertTrue(any2.is_restriction(any1)) self.assertTrue(any3.is_restriction(any1)) any1, any2, any3 = schema.groups['group1'][6:9] self.assertFalse(any2.is_restriction(any1)) self.assertTrue(any3.is_restriction(any1)) def test_wildcard_union(self): schema = self.schema_class(""" """) # any1, any2 = schema.groups['group1'][:2] self.assertListEqual(any1.namespace, ['tns1']) any1.union(any2) self.assertListEqual(any1.namespace, ['tns1', 'tns2']) # any1, any2 = schema.groups['group1'][2:4] self.assertListEqual(any1.namespace, []) self.assertListEqual(any1.not_namespace, ['tns1']) any1.union(any2) self.assertListEqual(any1.not_namespace, ['tns1']) any2.union(any1) self.assertListEqual(any2.not_namespace, ['tns1']) # any1, any2 = schema.groups['group1'][4:6] any1.union(any2) self.assertEqual(any1.namespace, ('##any',)) self.assertEqual(any1.not_namespace, ()) # any1, any2 = schema.groups['group1'][6:8] any1.union(any2) self.assertListEqual(any1.namespace, []) self.assertListEqual(any1.not_namespace, ['tns1']) # any1, any2 = schema.groups['group1'][8:10] any1.union(any2) self.assertListEqual(any1.namespace, []) self.assertListEqual(any1.not_namespace, ['tns1']) # any1, any2 = schema.groups['group1'][10:12] any1.union(any2) self.assertListEqual(any1.namespace, []) self.assertListEqual(any1.not_namespace, ['', 'tns1']) # any1, any2 = schema.groups['group1'][12:14] any1.union(any2) self.assertListEqual(any1.namespace, ['##any']) self.assertListEqual(any1.not_namespace, []) def test_wildcard_intersection(self): schema = self.schema_class(""" """) # any1, any2 = schema.groups['group1'][:2] self.assertListEqual(any1.namespace, ['tns1']) any1.intersection(any2) self.assertListEqual(any1.namespace, ['tns1']) # any1, any2 = schema.groups['group1'][2:4] self.assertListEqual(any1.namespace, []) self.assertListEqual(any1.not_namespace, ['tns1']) any1.intersection(any2) self.assertListEqual(any1.not_namespace, ['tns1', 'tns2']) any2.intersection(any1) self.assertListEqual(any2.not_namespace, ['tns1', 'tns2']) # any1, any2 = schema.groups['group1'][4:6] any1.intersection(any2) self.assertEqual(any1.namespace, []) self.assertEqual(any1.not_namespace, ['tns1']) # any1, any2 = schema.groups['group1'][6:8] any1.intersection(any2) self.assertListEqual(any1.namespace, []) self.assertListEqual(any1.not_namespace, ['tns1', '']) # any1, any2 = schema.groups['group1'][8:10] any1.intersection(any2) self.assertListEqual(any1.namespace, []) self.assertListEqual(any1.not_namespace, ['tns1', '']) # any1, any2 = schema.groups['group1'][10:12] any1.intersection(any2) self.assertListEqual(any1.namespace, []) self.assertListEqual(any1.not_namespace, ['', 'tns1']) # any1, any2 = schema.groups['group1'][12:14] any1.intersection(any2) self.assertListEqual(any1.namespace, []) self.assertListEqual(any1.not_namespace, ['tns2', 'tns1', '']) # # any1, any2 = schema.groups['group1'][14:16] any1.intersection(any2) self.assertListEqual(any1.namespace, ['']) self.assertListEqual(any1.not_qname, ['##defined', 'qn1']) def test_open_content_mode_interleave(self): schema = self.check_schema(""" """) self.assertEqual(schema.elements['Book'].type.open_content.mode, 'interleave') self.assertEqual(schema.elements['Book'].type.open_content.any_element.min_occurs, 0) self.assertIsNone(schema.elements['Book'].type.open_content.any_element.max_occurs) schema = self.check_schema(""" """) self.assertEqual(schema.types['name'].open_content.mode, 'interleave') self.check_schema(""" """, XMLSchemaParseError) def test_open_content_mode_suffix(self): schema = self.check_schema(""" """) self.assertEqual(schema.types['name'].open_content.mode, 'suffix') self.assertEqual(schema.types['name'].open_content.any_element.min_occurs, 0) self.assertIsNone(schema.types['name'].open_content.any_element.max_occurs) self.check_schema(""" """, XMLSchemaParseError) def test_open_content_mode_none(self): schema = self.check_schema(""" """) self.assertEqual(schema.types['name'].open_content.mode, 'none') self.check_schema(""" """, XMLSchemaParseError) def test_open_content_allowed(self): self.check_schema(""" """) def test_open_content_not_allowed(self): self.check_schema(""" """, XMLSchemaParseError) self.check_schema(""" """, XMLSchemaParseError) with self.assertRaises(XMLSchemaParseError): self.schema_class(""" """) def test_open_content_wrong_attributes(self): self.check_schema(""" """, XMLSchemaParseError) self.check_schema(""" """, XMLSchemaParseError) self.check_schema(""" """, XMLSchemaParseError) def test_default_open_content(self): schema = self.schema_class(""" """) self.assertIsInstance(schema.default_open_content, XsdDefaultOpenContent) self.assertFalse(schema.default_open_content.applies_to_empty) schema = self.schema_class(""" """) self.assertTrue(schema.default_open_content.applies_to_empty) with self.assertRaises(XMLSchemaParseError): self.schema_class(""" """) with self.assertRaises(XMLSchemaParseError): self.schema_class(""" """) with self.assertRaises(XMLSchemaParseError): self.schema_class(""" """) with self.assertRaises(XMLSchemaParseError): self.schema_class(""" """) with self.assertRaises(XMLSchemaParseError): self.schema_class(""" """) def test_open_content_restriction(self): schema = self.check_schema(""" """) self.assertEqual(schema.types['derivedType'].content_type[0].name, 'foo') self.check_schema(""" """, XMLSchemaParseError) def test_open_content_extension(self): schema = self.check_schema(""" """) self.assertEqual(schema.types['derivedType'].content_type[0][0].name, 'foo') self.assertEqual(schema.types['derivedType'].content_type[1][0].name, 'bar') self.check_schema(""" """, XMLSchemaParseError) def test_not_qname_attribute(self): self.assertIsInstance(self.schema_class(""" """), XMLSchema11) self.assertIsInstance(self.schema_class(""" """), XMLSchema11) self.check_schema(""" """, XMLSchemaParseError) def test_any_wildcard(self): super(TestXsd11Wildcards, self).test_any_wildcard() self.check_schema(""" """, XMLSchemaParseError) schema = self.check_schema(""" """) self.assertEqual(schema.types['taggedType'].content_type[-1].not_namespace, ['']) schema = self.schema_class(""" """) self.assertEqual(schema.types['taggedType'].content_type[-1].not_qname, ['{tns1}foo', '{tns1}bar']) schema = self.schema_class(""" """) self.assertEqual(schema.types['taggedType'].content_type[-1].not_qname, ['##defined', '{tns1}foo', '##definedSibling']) def test_any_attribute_wildcard(self): super(TestXsd11Wildcards, self).test_any_attribute_wildcard() schema = self.schema_class(""" """) self.assertEqual(schema.types['taggedType'].attributes[None].namespace, ('##any',)) self.assertEqual(schema.types['taggedType'].attributes[None].not_qname, ['{tns1}foo']) if __name__ == '__main__': from xmlschema.tests import print_test_header print_test_header() unittest.main()