-
Notifications
You must be signed in to change notification settings - Fork 30
Support extra="allow" for real
#305
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
RobertoRoos
wants to merge
4
commits into
dapper91:master
Choose a base branch
from
RobertoRoos:304-extra-allow
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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,148 @@ | ||
| from pydantic import ValidationError | ||
| from pydantic_xml import BaseXmlModel, attr, element | ||
| from pydantic_xml.element.native import ElementT | ||
|
|
||
| import pytest | ||
|
|
||
| from tests.helpers import assert_xml_equal | ||
|
|
||
|
|
||
| def test_extra_attributes_ignored(): | ||
| class TestModel(BaseXmlModel, tag='model', extra='ignore'): | ||
| prop1: str = attr() | ||
| data: str | ||
|
|
||
| xml = ''' | ||
| <model prop1="p1" prop2="p2">text</model> | ||
| ''' | ||
|
|
||
| actual_obj = TestModel.from_xml(xml) | ||
| assert actual_obj.model_extra is None | ||
|
|
||
|
|
||
| def test_extra_attributes_forbidden(): | ||
| class TestModel(BaseXmlModel, tag='model', extra='forbid'): | ||
| prop1: str = attr() | ||
| data: str | ||
|
|
||
| xml = ''' | ||
| <model prop1="p1" prop2="p2">text</model> | ||
| ''' | ||
|
|
||
| with pytest.raises(ValidationError): | ||
| _ = TestModel.from_xml(xml) | ||
|
|
||
|
|
||
| def test_extra_attributes(): | ||
| class TestModel(BaseXmlModel, tag='model', extra='allow'): | ||
| prop1: str = attr() | ||
| data: str | ||
|
|
||
| xml = ''' | ||
| <model prop1="p1" prop2="p2">text</model> | ||
| ''' | ||
|
|
||
| actual_obj = TestModel.from_xml(xml) | ||
| assert 'p2' == actual_obj.model_extra['prop2'] | ||
|
|
||
|
|
||
| def test_extra_elements(): | ||
|
|
||
| class TestModelChild(BaseXmlModel, tag='child'): | ||
| data: str | ||
|
|
||
| class TestModel(BaseXmlModel, tag='model', extra='allow'): | ||
| child: TestModelChild | ||
|
|
||
| xml = ''' | ||
| <model> | ||
| <child>hello world!</child> | ||
| <extra_child>hi again...</extra_child> | ||
| <extra_nested> | ||
| <extra_sub>1</extra_sub> | ||
| <extra_sub>2</extra_sub> | ||
| <extra_sub>3</extra_sub> | ||
| <extra_subsub> | ||
| <extra_subsubsub>3.14</extra_subsubsub> | ||
| </extra_subsub> | ||
| </extra_nested> | ||
| </model> | ||
| ''' | ||
|
|
||
| actual_obj = TestModel.from_xml(xml) | ||
| assert 'hello world!' == actual_obj.child.data | ||
| assert 'extra_child' in actual_obj.model_extra | ||
| assert 'extra_nested' in actual_obj.model_extra | ||
|
|
||
|
|
||
| def test_raw_save(): | ||
| # Just for debugging!!! | ||
|
|
||
| class TestModel(BaseXmlModel, tag='model', arbitrary_types_allowed=True): | ||
| extra_nested: ElementT = element() | ||
|
|
||
| xml = ''' | ||
| <model> | ||
| <extra_nested> | ||
| <extra_sub>1</extra_sub> | ||
| <extra_sub>2</extra_sub> | ||
| <extra_sub>3</extra_sub> | ||
| <extra_subsub subprop="x"> | ||
| <extra_subsubsub>3.14</extra_subsubsub> | ||
| </extra_subsub> | ||
| </extra_nested> | ||
| </model> | ||
| ''' | ||
|
|
||
| actual_obj = TestModel.from_xml(xml) | ||
| actual_xml = actual_obj.to_xml() | ||
| assert_xml_equal(actual_xml, xml) | ||
|
|
||
|
|
||
| def test_extra_save(): | ||
|
|
||
| class TestModelChild(BaseXmlModel, tag='child'): | ||
| data: str | ||
|
|
||
| class TestModel(BaseXmlModel, tag='model', extra='allow'): | ||
| prop1: str = attr() | ||
| child: TestModelChild | ||
|
|
||
| xml = ''' | ||
| <model prop1="p1" prop2="p2" prop3="p3"> | ||
| <child>hello world!</child> | ||
| <extra_child>hi again...</extra_child> | ||
| <extra_nested> | ||
| <extra_sub>1</extra_sub> | ||
| <extra_sub>2</extra_sub> | ||
| <extra_sub>3</extra_sub> | ||
| <extra_subsub subprop="x"> | ||
| <extra_subsubsub>3.14</extra_subsubsub> | ||
| </extra_subsub> | ||
| </extra_nested> | ||
| </model> | ||
| ''' | ||
|
|
||
| actual_obj = TestModel.from_xml(xml) | ||
| actual_xml = actual_obj.to_xml() | ||
| assert_xml_equal(actual_xml, xml) | ||
|
|
||
|
|
||
| def test_extra_save_order(): | ||
|
|
||
| class TestModelChild(BaseXmlModel, tag='child'): | ||
| data: str | ||
|
|
||
| class TestModel(BaseXmlModel, tag='model', extra='allow', search_mode='ordered'): | ||
| child: TestModelChild | ||
|
|
||
| xml = ''' | ||
| <model> | ||
| <other_child>Hi there</other_child> | ||
| <child>Hello world!</child> | ||
| </model> | ||
| ''' | ||
|
|
||
| actual_obj = TestModel.from_xml(xml) | ||
| actual_xml = actual_obj.to_xml() | ||
| assert_xml_equal(actual_xml, xml) | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The version I have now only works on attributes, not on child elements.