test_lxml.py 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. # -*- coding: utf-8 -*-
  2. # Part of Odoo. See LICENSE file for full copyright and licensing details.
  3. from odoo.tests import common
  4. from odoo.tools.xml_utils import _check_with_xsd
  5. import base64
  6. from lxml.etree import XMLSchemaError
  7. class TestLXML(common.TransactionCase):
  8. def test_lxml_import_from_filestore(self):
  9. resolver_schema_int = b"""
  10. <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
  11. xmlns:etype="http://codespeak.net/lxml/test/external">
  12. <xsd:import namespace="http://codespeak.net/lxml/test/external" schemaLocation="imported_schema.xsd"/>
  13. <xsd:element name="a" type="etype:AType"/>
  14. </xsd:schema>
  15. """
  16. incomplete_schema_int = b"""
  17. <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
  18. xmlns:etype="http://codespeak.net/lxml/test/external">
  19. <xsd:import namespace="http://codespeak.net/lxml/test/external" schemaLocation="non_existing_schema.xsd"/>
  20. <xsd:element name="a" type="etype:AType"/>
  21. </xsd:schema>
  22. """
  23. imported_schema = b"""
  24. <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
  25. targetNamespace="http://codespeak.net/lxml/test/external">
  26. <xsd:complexType name="AType">
  27. <xsd:sequence><xsd:element name="b" type="xsd:string" minOccurs="0" maxOccurs="unbounded"/></xsd:sequence>
  28. </xsd:complexType>
  29. </xsd:schema>
  30. """
  31. self.env['ir.attachment'].create([{
  32. 'datas': base64.b64encode(resolver_schema_int),
  33. 'name': 'resolver_schema_int.xsd'
  34. }, {
  35. 'datas': base64.b64encode(incomplete_schema_int),
  36. 'name': 'incomplete_schema_int.xsd'
  37. }, {
  38. 'datas': base64.b64encode(imported_schema),
  39. 'name': 'imported_schema.xsd'
  40. }])
  41. _check_with_xsd("<a><b></b></a>", 'resolver_schema_int.xsd', self.env)
  42. with self.assertRaises(XMLSchemaError):
  43. _check_with_xsd("<a><b></b></a>", 'incomplete_schema_int.xsd', self.env)
  44. with self.assertRaises(FileNotFoundError):
  45. _check_with_xsd("<a><b></b></a>", 'non_existing_schema.xsd', self.env)