Commit 6e1575ec authored by Sylvain Thénault's avatar Sylvain Thénault
Browse files

[test] Move some XSD testing related method to XmlTestMixIn

and introduce a new 'xpath' convenience method, to use instead of lambdas
parent 56dff6caffb9
......@@ -23,9 +23,6 @@ from cubes.seda.xsd2yams import XSDMMapping
from cubes.seda.entities.profile_generation import _path_target_values
from testutils import XmlTestMixin
NAMESPACES = {'xs': 'http://www.w3.org/2001/XMLSchema',
'seda': 'fr:gouv:culture:archivesdefrance:seda:v2.0'}
def create_archive_unit(parent, **kwargs):
cnx = parent._cw
......@@ -125,16 +122,17 @@ class SEDAXSDExportTC(XmlTestMixin, CubicWebTC):
with self.admin_access.client_cnx() as cnx:
profile = self.profile_etree(cnx.create_entity('SEDAArchiveTransfer',
title=u'test profile'))
xpath = lambda x: profile.xpath(x, namespaces=NAMESPACES)
dates = xpath('/xs:schema/xs:element/xs:complexType/'
'xs:sequence/xs:element[@name="Date"]')
dates = self.xpath(profile,
'/xs:schema/xs:element/xs:complexType/'
'xs:sequence/xs:element[@name="Date"]')
self.assertEqual(len(dates), 1)
self.assertEqual(dates[0].attrib, {'name': 'Date'})
self.assertXSDExtensionType(dates[0], 'xsd:dateTime')
self.assertXSDAttributes(dates[0], [])
self.assertXSDElements(dates[0], [])
identifiers = xpath('/xs:schema/xs:element/xs:complexType/'
'xs:sequence/xs:element[@name="MessageIdentifier"]')
identifiers = self.xpath(profile,
'/xs:schema/xs:element/xs:complexType/'
'xs:sequence/xs:element[@name="MessageIdentifier"]')
self.assertEqual(len(identifiers), 1)
self.assertEqual(identifiers[0].attrib, {'name': 'MessageIdentifier'})
self.assertXSDExtensionType(identifiers[0], 'xsd:token')
......@@ -159,9 +157,9 @@ class SEDAXSDExportTC(XmlTestMixin, CubicWebTC):
attachement = cnx.create_entity('SEDAAttachment', seda_attachment=bdo_alt)
cnx.create_entity('SEDAfilename', seda_filename=attachement)
profile = self.profile_etree(transfer)
xpath = lambda x: profile.xpath(x, namespaces=NAMESPACES)
fnames = xpath('//xs:element[@name="FileInfo"]/xs:complexType/'
'xs:sequence/xs:element[@name="Filename"]')
fnames = self.xpath(profile,
'//xs:element[@name="FileInfo"]/xs:complexType/'
'xs:sequence/xs:element[@name="Filename"]')
self.assertEqual(len(fnames), 1)
self.assertXSDExtensionType(fnames[0], 'xsd:string')
self.assertXSDAttributes(fnames[0], [])
......@@ -175,14 +173,12 @@ class SEDAXSDExportTC(XmlTestMixin, CubicWebTC):
appname = cnx.create_entity('SEDACreatingApplicationName',
seda_creating_application_name=bdo)
profile = self.profile_etree(transfer)
xpath = lambda x: profile.xpath(x, namespaces=NAMESPACES)
fnames = xpath('//xs:element[@name="FileInfo"]')
fnames = self.xpath(profile, '//xs:element[@name="FileInfo"]')
self.assertEqual(len(fnames), 1)
self.assertEqual(fnames[0].attrib, {'name': 'FileInfo', 'minOccurs': '0'})
appname.cw_set(user_cardinality=u'1')
profile = self.profile_etree(transfer)
xpath = lambda x: profile.xpath(x, namespaces=NAMESPACES)
fnames = xpath('//xs:element[@name="FileInfo"]')
fnames = self.xpath(profile, '//xs:element[@name="FileInfo"]')
self.assertEqual(fnames[0].attrib, {'name': 'FileInfo'})
def test_transfer_annotation(self):
......@@ -190,8 +186,7 @@ class SEDAXSDExportTC(XmlTestMixin, CubicWebTC):
profile = self.profile_etree(cnx.create_entity('SEDAArchiveTransfer',
title=u'test profile',
user_annotation=u'some description'))
xpath = lambda x: profile.xpath(x, namespaces=NAMESPACES)
docs = xpath('/xs:schema/xs:element/xs:annotation/xs:documentation')
docs = self.xpath(profile, '/xs:schema/xs:element/xs:annotation/xs:documentation')
self.assertEqual(len(docs), 1)
self.assertEqual(docs[0].text, 'some description')
......@@ -202,9 +197,10 @@ class SEDAXSDExportTC(XmlTestMixin, CubicWebTC):
archival_org = cnx.create_entity('Agent', name=u'Archival inc.',
reverse_seda_archival_agency=transfer)
profile = self.profile_etree(transfer)
enum_elts = profile.xpath('//xs:element[@name="ArchivalAgency"]/xs:complexType'
'/xs:sequence/xs:element/xs:simpleType/xs:restriction'
'/xs:enumeration', namespaces=NAMESPACES)
enum_elts = self.xpath(profile,
'//xs:element[@name="ArchivalAgency"]/xs:complexType'
'/xs:sequence/xs:element/xs:simpleType/xs:restriction'
'/xs:enumeration')
self.assertEqual(len(enum_elts), 1)
self.assertEqual(enum_elts[0].attrib['value'], archival_org.absolute_url())
......@@ -213,12 +209,11 @@ class SEDAXSDExportTC(XmlTestMixin, CubicWebTC):
transfer = cnx.create_entity('SEDAArchiveTransfer', title=u'test profile')
cnx.create_entity('SEDASignature', seda_signature=transfer)
profile = self.profile_etree(transfer)
xpath = lambda x: profile.xpath(x, namespaces=NAMESPACES)
signatures = xpath('//xs:element[@name="Signature"]')
signatures = self.xpath(profile, '//xs:element[@name="Signature"]')
self.assertEqual(len(signatures), 1)
self.assertEqual(signatures[0].attrib, {'name': 'Signature', 'type': 'OpenType',
'minOccurs': '0'})
open_types = xpath('//xs:complexType[@name="OpenType"]')
open_types = self.xpath(profile, '//xs:complexType[@name="OpenType"]')
self.assertEqual(len(open_types), 1)
def test_keyword(self):
......@@ -237,25 +232,23 @@ class SEDAXSDExportTC(XmlTestMixin, CubicWebTC):
kw = create('SEDAKeyword', seda_keyword=content)
create('SEDAKeywordContent', reverse_seda_keyword_content=kw,
keyword_content=u'kwick')
kwr_e = create('SEDAKeywordReference', seda_keyword_reference_from=kw,
seda_keyword_reference_to_scheme=scheme)
kwr_e = create('SEDAKeywordReference', seda_keyword_reference_from=kw)
create('SEDAKeywordType', seda_keyword_type_from=kw,
seda_keyword_type_to=kw_type)
profile = self.profile_etree(transfer)
xpath = lambda x: profile.xpath(x, namespaces=NAMESPACES)
kwc = xpath('//xs:element[@name="KeywordContent"]')
kwc = self.xpath(profile, '//xs:element[@name="KeywordContent"]')
self.assertEqual(len(kwc), 1)
self.assertEqual(kwc[0].attrib, {'name': 'KeywordContent', 'fixed': 'kwick'})
kwt = xpath('//xs:element[@name="KeywordType"]')
kwt = self.xpath(profile, '//xs:element[@name="KeywordType"]')
self.assertEqual(len(kwt), 1)
self.assertEqual(kwt[0].attrib, {'name': 'KeywordType', 'fixed': 'theme',
'minOccurs': '0'})
self.assertXSDAttributes(kwt[0],
[{'name': 'listVersionID',
'fixed': scheme.absolute_url()}])
kwr = xpath('//xs:element[@name="KeywordReference"]')
kwr = self.xpath(profile, '//xs:element[@name="KeywordReference"]')
self.assertEqual(len(kwr), 1)
self.assertEqual(kwr[0].attrib, {'name': 'KeywordReference',
'minOccurs': '0'})
......@@ -266,8 +259,7 @@ class SEDAXSDExportTC(XmlTestMixin, CubicWebTC):
kwr_e.cw_set(seda_keyword_reference_to=kw_type)
profile = self.profile_etree(transfer)
xpath = lambda x: profile.xpath(x, namespaces=NAMESPACES)
kwr = xpath('//xs:element[@name="KeywordReference"]')
kwr = self.xpath(profile, '//xs:element[@name="KeywordReference"]')
self.assertEqual(len(kwr), 1)
self.assertEqual(kwr[0].attrib, {'name': 'KeywordReference', 'fixed': 'theme',
'minOccurs': '0'})
......@@ -282,30 +274,6 @@ class SEDAXSDExportTC(XmlTestMixin, CubicWebTC):
self.assertXmlValid(xsd, self.datapath('XMLSchema.xsd'))
return etree.fromstring(xsd)
def assertXSDExtensionType(self, element, expected_type):
xpath = lambda x: element.xpath(x, namespaces=NAMESPACES)
types = xpath('xs:complexType/xs:simpleContent/xs:extension')
assert len(types) <= 1
if types:
type = types[0].attrib['base']
else:
type = None
self.assertEqual(type, expected_type)
def assertXSDAttributes(self, element, expected_attributes):
xpath = lambda x: element.xpath(x, namespaces=NAMESPACES)
# attributes for regular elements
attrs = [x.attrib for x in xpath('xs:complexType/xs:attribute')]
# attributes for simple elements
attrs += [x.attrib for x in xpath('xs:complexType/xs:simpleContent/'
'xs:extension/xs:attribute')]
self.assertEqual(attrs, expected_attributes)
def assertXSDElements(self, element, expected_children_name):
xpath = lambda x: element.xpath(x, namespaces=NAMESPACES)
children = [x.attrib['name'] for x in xpath('xs:complexType/xs:sequence/xs:element')]
self.assertEqual(children, expected_children_name)
class SEDAXSDExportFuncTC(XmlTestMixin, CubicWebTC):
"""Test that SEDA profile export works correctly."""
......@@ -407,16 +375,16 @@ class SEDAXSDExportFuncTC(XmlTestMixin, CubicWebTC):
xsd = transfer.cw_adapt_to('SEDA-2.0.xsd').dump()
open('/tmp/sedaprof.xsd', 'w').write(xsd)
profile = self.check_xsd_profile(xsd, self.datapath('BV2.0_min.xml'))
xpath = lambda x: profile.xpath(x, namespaces=NAMESPACES)
# ensure profile's temporary id are exported in custom seda:profid attribute
self.assertEqual(len(xpath('//xs:attribute[@seda:profid]')), 2)
self.assertEqual(len(self.xpath(profile, '//xs:attribute[@seda:profid]')), 2)
# ensure they are properly referenced using 'default' attribute
references = xpath('//xs:element[@default="bdo1"]')
references = self.xpath(profile, '//xs:element[@default="bdo1"]')
self.assertEqual(len(references), 1)
self.assertEqual(references[0].attrib['name'], 'DataObjectReferenceId')
# ensure optional id are properly reinjected
references = xpath('//xs:element[@name="Keyword"]'
'//xs:attribute[@name="id" and @use="optional"]')
references = self.xpath(profile,
'//xs:element[@name="Keyword"]'
'//xs:attribute[@name="id" and @use="optional"]')
self.assertEqual(len(references), 1)
......
......@@ -23,9 +23,36 @@ from lxml import etree
class XmlTestMixin(unittest.TestCase):
"""Mixin class provinding additional assertion methods for checking XML data."""
NAMESPACES = {'xs': 'http://www.w3.org/2001/XMLSchema',
'seda': 'fr:gouv:culture:archivesdefrance:seda:v2.0'}
def xpath(self, element, expression):
return element.xpath(expression, namespaces=self.NAMESPACES)
def assertXmlValid(self, xml_str, xsd_filename):
"""Validate an XML string according to an XSD file (.xsd)."""
with open(xsd_filename) as dtd_f:
xsd = etree.XMLSchema(etree.parse(dtd_f))
xsd.assert_(etree.fromstring(xml_str))
def assertXSDExtensionType(self, element, expected_type):
types = self.xpath(element, 'xs:complexType/xs:simpleContent/xs:extension')
assert len(types) <= 1
if types:
type = types[0].attrib['base']
else:
type = None
self.assertEqual(type, expected_type)
def assertXSDAttributes(self, element, expected_attributes):
# attributes for regular elements
attrs = [x.attrib for x in self.xpath(element, 'xs:complexType/xs:attribute')]
# attributes for simple elements
attrs += [x.attrib for x in self.xpath(
element, 'xs:complexType/xs:simpleContent/xs:extension/xs:attribute')]
self.assertEqual(sorted(attrs), expected_attributes)
def assertXSDElements(self, element, expected_children_name):
children = [x.attrib['name'] for x in self.xpath(
element, 'xs:complexType/xs:sequence/xs:element')]
self.assertEqual(children, expected_children_name)
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment