-
-
Notifications
You must be signed in to change notification settings - Fork 62
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
287 additions
and
53 deletions.
There are no files selected for viewing
46 changes: 46 additions & 0 deletions
46
tests/formats/dataclass/filters/test_attribute_metadata.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
from unittest import TestCase | ||
|
||
from tests.factories import AttrFactory | ||
from tests.factories import FactoryTestCase | ||
from xsdata.formats.dataclass.filters import attribute_metadata | ||
from xsdata.models.enums import Tag | ||
|
||
|
||
class AttributeMetadataTests(FactoryTestCase): | ||
def test_attribute_metadata(self): | ||
attr = AttrFactory.element() | ||
expected = {"name": "attr_B", "type": "Element"} | ||
self.assertEqual(expected, attribute_metadata(attr, None)) | ||
self.assertEqual(expected, attribute_metadata(attr, "foo")) | ||
|
||
def test_attribute_metadata_namespace(self): | ||
attr = AttrFactory.element(namespace="foo") | ||
expected = {"name": "attr_B", "namespace": "foo", "type": "Element"} | ||
|
||
self.assertEqual(expected, attribute_metadata(attr, None)) | ||
self.assertNotIn("namespace", attribute_metadata(attr, "foo")) | ||
|
||
attr = AttrFactory.attribute(namespace="foo") | ||
expected = {"name": "attr_C", "namespace": "foo", "type": "Attribute"} | ||
|
||
self.assertEqual(expected, attribute_metadata(attr, None)) | ||
self.assertIn("namespace", attribute_metadata(attr, "foo")) | ||
|
||
def test_attribute_metadata_name(self): | ||
attr = AttrFactory.element(local_name="foo", name="bar") | ||
actual = attribute_metadata(attr, None) | ||
self.assertEqual("foo", actual["name"]) | ||
|
||
attr = AttrFactory.element(local_name="foo", name="Foo") | ||
self.assertNotIn("name", attribute_metadata(attr, None)) | ||
|
||
attr = AttrFactory.create(tag=Tag.ANY, local_name="foo", name="bar") | ||
self.assertNotIn("name", attribute_metadata(attr, None)) | ||
|
||
def test_attribute_metadata_restrictions(self): | ||
attr = AttrFactory.create(tag=Tag.RESTRICTION) | ||
attr.restrictions.max_occurs = 2 | ||
attr.restrictions.required = False | ||
|
||
expected = {"max_occurs": 2} | ||
self.assertEqual(expected, attribute_metadata(attr, None)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
from unittest import TestCase | ||
|
||
from xsdata.formats.dataclass.filters import default_imports | ||
|
||
|
||
class DefaultImportsTests(TestCase): | ||
def test_default_imports_with_decimal(self): | ||
output = " Decimal " | ||
|
||
expected = "from decimal import Decimal" | ||
self.assertIn(expected, default_imports(output)) | ||
|
||
def test_default_imports_with_enum(self): | ||
output = " (Enum) " | ||
|
||
expected = "from enum import Enum" | ||
self.assertIn(expected, default_imports(output)) | ||
|
||
def test_default_imports_with_dataclasses(self): | ||
output = " @dataclass " | ||
|
||
expected = "from dataclasses import dataclass" | ||
self.assertIn(expected, default_imports(output)) | ||
|
||
output = " field( " | ||
expected = "from dataclasses import field" | ||
self.assertIn(expected, default_imports(output)) | ||
|
||
output = " field( @dataclass " | ||
expected = "from dataclasses import dataclass, field" | ||
self.assertIn(expected, default_imports(output)) | ||
|
||
def test_default_imports_with_qname(self): | ||
output = " QName " | ||
|
||
expected = "from lxml.etree import QName" | ||
self.assertIn(expected, default_imports(output)) | ||
|
||
def test_default_imports_with_typing(self): | ||
output = " Dict[ " | ||
|
||
expected = "from typing import Dict" | ||
self.assertIn(expected, default_imports(output)) | ||
|
||
output = " List[ " | ||
|
||
expected = "from typing import List" | ||
self.assertIn(expected, default_imports(output)) | ||
|
||
output = " Optional[ " | ||
|
||
expected = "from typing import Optional" | ||
self.assertIn(expected, default_imports(output)) | ||
|
||
output = " Union[ " | ||
|
||
expected = "from typing import Union" | ||
self.assertIn(expected, default_imports(output)) | ||
|
||
output = " Optional[Union[Dict[ " | ||
|
||
expected = "from typing import Dict, Optional, Union" | ||
self.assertIn(expected, default_imports(output)) | ||
|
||
def test_default_imports_combo(self): | ||
output = """@dataclass | ||
class Foo: | ||
field: Optional[str] = field(default=None)""" | ||
|
||
expected = """from dataclasses import dataclass, field | ||
from typing import Optional""" | ||
self.assertEqual(expected, default_imports(output)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
from unittest import TestCase | ||
|
||
from tests.factories import AttrFactory | ||
from tests.factories import FactoryTestCase | ||
from xsdata.formats.dataclass.filters import attribute_metadata | ||
from xsdata.formats.dataclass.filters import format_arguments | ||
from xsdata.models.enums import Tag | ||
|
||
|
||
class AttributeMetadataTests(TestCase): | ||
def test_format_arguments(self): | ||
data = dict( | ||
num=1, text="foo", text_two="fo'o", text_three='fo"o', pattern="foo", | ||
) | ||
|
||
expected = '''num=1, | ||
text="foo", | ||
text_two="fo'o", | ||
text_three="fo'o", | ||
pattern=r"foo"''' | ||
self.assertEqual(expected, format_arguments(data)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
from tests.factories import AttrFactory | ||
from tests.factories import ClassFactory | ||
from tests.factories import FactoryTestCase | ||
from xsdata.formats.dataclass.filters import class_docstring | ||
|
||
|
||
class ClassDocstringTests(FactoryTestCase): | ||
def test_class_docstring(self): | ||
target = ClassFactory.create( | ||
attrs=[ | ||
AttrFactory.element(help="help"), | ||
AttrFactory.element(help="Foo\nBar"), | ||
AttrFactory.element(), | ||
] | ||
) | ||
|
||
expected = '''""" | ||
:ivar attr_b: help | ||
:ivar attr_c: Foo | ||
Bar | ||
:ivar attr_d: | ||
"""''' | ||
self.assertEqual(expected, class_docstring(target)) | ||
|
||
def test_class_docstring_with_class_help(self): | ||
target = ClassFactory.elements(2, help="Help Me!") | ||
|
||
expected = '''"""Help Me! | ||
:ivar attr_b: | ||
:ivar attr_c: | ||
"""''' | ||
self.assertEqual(expected, class_docstring(target)) | ||
|
||
def test_class_docstring_with_enumeration(self): | ||
target = ClassFactory.enumeration(2, help="Help Me!") | ||
|
||
expected = '''"""Help Me! | ||
:cvar ATTR_B: | ||
:cvar ATTR_C: | ||
"""''' | ||
self.assertEqual(expected, class_docstring(target, enum=True)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -776,7 +776,6 @@ def test_sanitize_class( | |
mock_sanitize_attribute_sequence, | ||
mock_sanitize_duplicate_attribute_names, | ||
): | ||
|
||
target = ClassFactory.elements(2) | ||
inner = ClassFactory.elements(1) | ||
target.inner.append(inner) | ||
|
@@ -877,6 +876,15 @@ def test_sanitize_attribute_default_value_with_enumeration(self): | |
|
||
self.assertEqual([None, 2, None, "@[email protected]"], actual) | ||
|
||
attr = AttrFactory.create( | ||
default="missing", types=AttrTypeFactory.list(1, name="hit") | ||
) | ||
|
||
with self.assertRaises(AnalyzerError) as cm: | ||
self.analyzer.sanitize_attribute_default_value(target, attr) | ||
|
||
self.assertEqual("Unknown enumeration hit: missing", str(cm.exception)) | ||
|
||
@mock.patch.object(ClassAnalyzer, "flatten_class") | ||
def test_class_depends_on_has_a_depth_limit(self, *args): | ||
one = ClassFactory.create(extensions=ExtensionFactory.list(1)) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.