Skip to content

Commit

Permalink
Add xml provider
Browse files Browse the repository at this point in the history
Co-authored-by: Flavio Curella <89607+fcurella@users.noreply.github.com>
Co-authored-by: elihaybe <elihay@minutemedia.com>
  • Loading branch information
3 people authored May 16, 2023
1 parent 7e4c42d commit 5a61a05
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 0 deletions.
30 changes: 30 additions & 0 deletions faker/providers/misc/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
from faker.exceptions import UnsupportedFeature

from .. import BaseProvider
from ..python import TypesSpec

localized = True

Expand Down Expand Up @@ -613,6 +614,35 @@ def create_json_structure(data_columns: Union[Dict, List]) -> dict:
data = [create_json_structure(data_columns) for _ in range(num_rows)]
return json.dumps(data, indent=indent, cls=cls)

def xml(
self,
nb_elements: int = 10,
variable_nb_elements: bool = True,
value_types: Optional[TypesSpec] = None,
allowed_types: Optional[TypesSpec] = None,
) -> str:
"""
Returns some XML.
:nb_elements: number of elements for dictionary
:variable_nb_elements: is use variable number of elements for dictionary
:value_types: type of dictionary values
Note: this provider required xmltodict library installed
"""
try:
import xmltodict
except ImportError:
raise UnsupportedFeature("`xml` requires the `xmltodict` Python library.", "xml")
_dict = self.generator.pydict(
nb_elements=nb_elements,
variable_nb_elements=variable_nb_elements,
value_types=value_types,
allowed_types=allowed_types,
)
_dict = {self.generator.word(): _dict}
return xmltodict.unparse(_dict)

def fixed_width(self, data_columns: Optional[list] = None, num_rows: int = 10, align: str = "left") -> str:
"""
Generate random fixed width values.
Expand Down
20 changes: 20 additions & 0 deletions tests/providers/test_misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,19 @@
import tarfile
import unittest
import uuid
import xml
import zipfile

try:
import PIL.Image
except ImportError:
PIL = None

try:
import xmltodict
except ImportError:
xmltodict = None

from typing import Pattern
from unittest.mock import patch

Expand Down Expand Up @@ -455,6 +461,20 @@ def test_dsv_csvwriter_kwargs(self, faker):
for args, kwargs in mock_writer.call_args_list:
assert kwargs == test_kwargs

@unittest.skipUnless(xmltodict, "requires the Python xmltodict Library")
def test_xml(self, faker):
try:
xml.etree.ElementTree.fromstring(faker.xml())
except xml.etree.ElementTree.ParseError:
raise AssertionError("The XML format is invalid.")

def test_xml_no_xmltodict(self, faker):
with patch.dict("sys.modules", {"xmltodict": None}):
with pytest.raises(exceptions.UnsupportedFeature) as excinfo:
faker.xml()

assert excinfo.value.name == "xml"

def test_csv_helper_method(self, faker):
kwargs = {
"header": ["Column 1", "Column 2"],
Expand Down
1 change: 1 addition & 0 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ deps =
validators>=0.13.0
sphinx>=2.4,<3.0
Pillow
xmltodict
commands =
coverage run --source=faker -m pytest {posargs}
coverage run --source=faker -a -m pytest --exclusive-faker-session tests/pytest/session_overrides {posargs}
Expand Down

0 comments on commit 5a61a05

Please sign in to comment.