Skip to content

Commit

Permalink
AttributeDefaultValueHandler: process attr choices
Browse files Browse the repository at this point in the history
  • Loading branch information
tefra committed Jun 19, 2021
1 parent fb30305 commit b71f0a4
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
29 changes: 29 additions & 0 deletions tests/codegen/handlers/test_attribute_default_value.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from xsdata.codegen.container import ClassContainer
from xsdata.codegen.handlers import AttributeDefaultValueHandler
from xsdata.models.config import GeneratorConfig
from xsdata.models.enums import DataType
from xsdata.models.enums import Namespace
from xsdata.utils.testing import AttrFactory
from xsdata.utils.testing import AttrTypeFactory
Expand All @@ -26,6 +27,34 @@ def test_process_attribute_with_enumeration(self):
self.processor.process_attribute(target, attr)
self.assertTrue(attr.fixed)

@mock.patch.object(AttributeDefaultValueHandler, "process_attribute")
def test_process_with_attr_choices(self, mock_process_attribute):
choice = AttrFactory.create(
name="attr_B_Or_attr_C",
tag="Choice",
index=0,
types=[AttrTypeFactory.native(DataType.ANY_TYPE)],
choices=[
AttrFactory.reference("one"),
AttrFactory.reference("two"),
AttrFactory.reference("three"),
],
)
target = ClassFactory.create()
target.attrs.append(choice)

self.processor.process(target)

self.assertEqual(4, mock_process_attribute.call_count)
mock_process_attribute.assert_has_calls(
[
mock.call(target, target.attrs[0]),
mock.call(target, target.attrs[0].choices[0]),
mock.call(target, target.attrs[0].choices[1]),
mock.call(target, target.attrs[0].choices[2]),
]
)

def test_process_attribute_with_optional_field(self):
target = ClassFactory.create()
attr = AttrFactory.create(fixed=True, default=2)
Expand Down
3 changes: 3 additions & 0 deletions xsdata/codegen/handlers/attribute_default_value.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ def process(self, target: Class):
for attr in target.attrs:
self.process_attribute(target, attr)

for choice in attr.choices:
self.process_attribute(target, choice)

def process_attribute(self, target: Class, attr: Attr):

if attr.is_enumeration:
Expand Down

0 comments on commit b71f0a4

Please sign in to comment.