Skip to content

Commit

Permalink
Finish tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
barseghyanartur committed Jan 3, 2023
1 parent dd6e5a4 commit b536736
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 4 deletions.
4 changes: 2 additions & 2 deletions .secrets.baseline
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@
"filename": "README.rst",
"hashed_secret": "077d5a0e0f8bb517307a6e92a73b0a9aa959233c",
"is_verified": true,
"line_number": 296
"line_number": 302
}
],
"examples/django_example/project/settings/base.py": [
Expand Down Expand Up @@ -149,5 +149,5 @@
}
]
},
"generated_at": "2023-01-03T19:30:00Z"
"generated_at": "2023-01-03T21:28:24Z"
}
6 changes: 6 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,12 @@ Latest stable version from PyPI
pip install faker-file[docx]
**With EPUB support**

.. code-block:: sh
pip install faker-file[epub]
**With images support**

.. code-block:: sh
Expand Down
5 changes: 5 additions & 0 deletions docs/quick_start.rst
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ With ``Faker``
from faker_file.providers.bin_file import BinFileProvider
from faker_file.providers.csv_file import CsvFileProvider
from faker_file.providers.docx_file import DocxFileProvider
from faker_file.providers.epub_file import EpubFileProvider
from faker_file.providers.ico_file import IcoFileProvider
from faker_file.providers.jpeg_file import JpegFileProvider
from faker_file.providers.ods_file import OdsFileProvider
Expand All @@ -37,6 +38,7 @@ With ``Faker``
FAKER.add_provider(BinFileProvider)
FAKER.add_provider(CsvFileProvider)
FAKER.add_provider(DocxFileProvider)
FAKER.add_provider(EpubFileProvider)
FAKER.add_provider(IcoFileProvider)
FAKER.add_provider(JpegFileProvider)
FAKER.add_provider(OdsFileProvider)
Expand All @@ -58,6 +60,7 @@ With ``Faker``
bin_file = FAKER.bin_file()
csv_file = FAKER.csv_file()
docx_file = FAKER.docx_file()
epub_file = FAKER.epub_file()
ico_file = FAKER.ico_file()
jpeg_file = FAKER.jpeg_file()
ods_file = FAKER.ods_file()
Expand All @@ -81,6 +84,7 @@ With ``factory_boy``
from faker_file.providers.bin_file import BinFileProvider
from faker_file.providers.csv_file import CsvFileProvider
from faker_file.providers.docx_file import DocxFileProvider
from faker_file.providers.epub_file import EpubFileProvider
from faker_file.providers.ico_file import IcoFileProvider
from faker_file.providers.jpeg_file import JpegFileProvider
from faker_file.providers.ods_file import OdsFileProvider
Expand All @@ -98,6 +102,7 @@ With ``factory_boy``
Faker.add_provider(BinFileProvider)
Faker.add_provider(CsvFileProvider)
Faker.add_provider(DocxFileProvider)
Faker.add_provider(EpubFileProvider)
Faker.add_provider(IcoFileProvider)
Faker.add_provider(JpegFileProvider)
Faker.add_provider(OdsFileProvider)
Expand Down
18 changes: 16 additions & 2 deletions src/faker_file/providers/epub_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ def epub_file(
wrap_chars_after: Optional[int] = None,
content: Optional[str] = None,
title: Optional[str] = None,
chapter_title: Optional[str] = None,
**kwargs,
) -> StringValue:
"""Generate a EPUB file with random text.
Expand All @@ -74,6 +75,8 @@ def epub_file(
are then replaced by correspondent fixtures.
:param title: E-book title. Might contain dynamic elements, which
are then replaced by correspondent fixtures.
:param chapter_title: Chapter title. Might contain dynamic elements,
which are then replaced by correspondent fixtures.
:return: Relative path (from root directory) of the generated file.
"""
# Generic
Expand All @@ -96,8 +99,15 @@ def epub_file(
content=title,
)

chapter_title = self._generate_text_content(
max_nb_chars=50,
content=chapter_title,
)

_book = xml2epub.Epub(title)
_chapter = xml2epub.create_chapter_from_string(content)
_chapter = xml2epub.create_chapter_from_string(
content, title=chapter_title
)
_book.add_chapter(_chapter)
_local_file_name = _book.create_epub(tempfile.gettempdir())

Expand All @@ -108,5 +118,9 @@ def epub_file(

# Generic
file_name = StringValue(storage.relpath(filename))
file_name.data = {"content": content}
file_name.data = {
"content": content,
"title": title,
"chapter_title": chapter_title,
}
return file_name
30 changes: 30 additions & 0 deletions src/faker_file/providers/zip_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
"create_inner_bin_file",
"create_inner_csv_file",
"create_inner_docx_file",
"create_inner_epub_file",
"create_inner_ico_file",
"create_inner_jpeg_file",
"create_inner_ods_file",
Expand Down Expand Up @@ -113,6 +114,35 @@ def create_inner_docx_file(
)


def create_inner_epub_file(
storage: BaseStorage = None,
prefix: Optional[str] = None,
generator: Union[Provider, Faker] = None,
max_nb_chars: int = DEFAULT_TEXT_MAX_NB_CHARS,
wrap_chars_after: Optional[int] = None,
content: Optional[str] = None,
title: Optional[str] = None,
chapter_title: Optional[str] = None,
**kwargs,
) -> StringValue:
"""Create inner EPUB file."""
try:
from .epub_file import EpubFileProvider
except ImportError as err:
raise err

return EpubFileProvider(generator).epub_file(
storage=storage,
prefix=prefix,
max_nb_chars=max_nb_chars,
wrap_chars_after=wrap_chars_after,
content=content,
title=title,
chapter_title=chapter_title,
**kwargs,
)


def create_inner_ico_file(
storage: BaseStorage = None,
prefix: Optional[str] = None,
Expand Down
8 changes: 8 additions & 0 deletions src/faker_file/tests/test_providers.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
create_inner_bin_file,
create_inner_csv_file,
create_inner_docx_file,
create_inner_epub_file,
create_inner_ico_file,
create_inner_jpeg_file,
create_inner_ods_file,
Expand Down Expand Up @@ -435,6 +436,7 @@ def test_standalone_providers_allow_failures(
(create_inner_bin_file, b"Lorem ipsum"),
(create_inner_csv_file, "Lorem ipsum"),
(create_inner_docx_file, "Lorem ipsum"),
(create_inner_epub_file, "Lorem ipsum"),
(create_inner_ico_file, "Lorem ipsum"),
(create_inner_jpeg_file, "Lorem ipsum"),
(create_inner_ods_file, None),
Expand Down Expand Up @@ -503,6 +505,12 @@ def test_standalone_zip_file_allow_failures(
"DocxFileProvider",
create_inner_docx_file,
),
# EPUB
(
"faker_file.providers.epub_file",
"EpubFileProvider",
create_inner_epub_file,
),
# ICO
(
"faker_file.providers.ico_file",
Expand Down

0 comments on commit b536736

Please sign in to comment.