Skip to content

Commit

Permalink
Optimize sqlalchemy factories
Browse files Browse the repository at this point in the history
  • Loading branch information
barseghyanartur committed Jan 12, 2023
1 parent 60429f3 commit 05894c9
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 73 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,12 @@ are used for versioning (schema follows below):

0.10.7
------
2023-01-12
2023-01-13

- Add ``OdtFileProvider`` and ``create_inner_odt_file`` function.
- Documentation improvements.
- Async related deprecation fixes in ``EdgeTtsMp3Generator``.
- Optimize example factories.

0.10.6
------
Expand Down
Original file line number Diff line number Diff line change
@@ -1,40 +1,84 @@
from random import choice

from factory import Faker, LazyAttribute, Sequence
from factory import Faker, LazyAttribute, Sequence, Trait
from factory.alchemy import SQLAlchemyModelFactory
from faker_file_admin import db
from faker_file_admin.models import Upload

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.eml_file import EmlFileProvider
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.mp3_file import Mp3FileProvider
from faker_file.providers.ods_file import OdsFileProvider
from faker_file.providers.odt_file import OdtFileProvider
from faker_file.providers.pdf_file import PdfFileProvider
from faker_file.providers.png_file import PngFileProvider
from faker_file.providers.pptx_file import PptxFileProvider
from faker_file.providers.rtf_file import RtfFileProvider
from faker_file.providers.svg_file import SvgFileProvider
from faker_file.providers.txt_file import TxtFileProvider
from faker_file.providers.webp_file import WebpFileProvider
from faker_file.providers.xlsx_file import XlsxFileProvider
from faker_file.providers.zip_file import ZipFileProvider
from faker_file.storages.filesystem import FileSystemStorage

__all__ = (
"DocxUploadFactory",
"OdsUploadFactory",
"PdfUploadFactory",
"PptxUploadFactory",
"TxtUploadFactory",
"UploadFactory",
"ZipUploadFactory",
)
__all__ = ("UploadFactory",)

# Faker._DEFAULT_LOCALE = "hy_AM"
Faker.add_provider(BinFileProvider)
Faker.add_provider(CsvFileProvider)
Faker.add_provider(DocxFileProvider)
Faker.add_provider(EmlFileProvider)
Faker.add_provider(EpubFileProvider)
Faker.add_provider(IcoFileProvider)
Faker.add_provider(JpegFileProvider)
Faker.add_provider(Mp3FileProvider)
Faker.add_provider(OdsFileProvider)
Faker.add_provider(OdtFileProvider)
Faker.add_provider(PdfFileProvider)
Faker.add_provider(PngFileProvider)
Faker.add_provider(PptxFileProvider)
Faker.add_provider(RtfFileProvider)
Faker.add_provider(SvgFileProvider)
Faker.add_provider(TxtFileProvider)
Faker.add_provider(WebpFileProvider)
Faker.add_provider(XlsxFileProvider)
Faker.add_provider(ZipFileProvider)


STORAGE = FileSystemStorage(root_path="", rel_path="tmp")


PROVIDER_CHOICES = [
lambda: BinFileProvider(None).bin_file(storage=STORAGE),
lambda: CsvFileProvider(None).csv_file(storage=STORAGE),
lambda: DocxFileProvider(None).docx_file(storage=STORAGE),
lambda: EmlFileProvider(None).eml_file(storage=STORAGE),
lambda: EpubFileProvider(None).epub_file(storage=STORAGE),
lambda: IcoFileProvider(None).ico_file(storage=STORAGE),
lambda: JpegFileProvider(None).jpeg_file(storage=STORAGE),
lambda: Mp3FileProvider(None).mp3_file(storage=STORAGE),
lambda: OdsFileProvider(None).ods_file(storage=STORAGE),
lambda: OdtFileProvider(None).odt_file(storage=STORAGE),
lambda: PdfFileProvider(None).pdf_file(storage=STORAGE),
lambda: PngFileProvider(None).png_file(storage=STORAGE),
lambda: PptxFileProvider(None).pptx_file(storage=STORAGE),
lambda: RtfFileProvider(None).rtf_file(storage=STORAGE),
lambda: SvgFileProvider(None).svg_file(storage=STORAGE),
lambda: TxtFileProvider(None).txt_file(storage=STORAGE),
lambda: XlsxFileProvider(None).xlsx_file(storage=STORAGE),
lambda: ZipFileProvider(None).zip_file(storage=STORAGE),
]


def pick_random_provider(*args, **kwargs):
return choice(PROVIDER_CHOICES)()


class AbstractUploadFactory(SQLAlchemyModelFactory):
"""Base Upload factory.
Expand All @@ -60,56 +104,27 @@ class Meta:
abstract = True


class DocxUploadFactory(AbstractUploadFactory):
"""DOCX Upload factory."""

file = Faker("docx_file", storage=STORAGE)


class OdsUploadFactory(AbstractUploadFactory):
"""ODS Upload factory."""

file = Faker("ods_file", storage=STORAGE)


class PptxUploadFactory(AbstractUploadFactory):
"""PPTX Upload factory."""

file = Faker("pptx_file", storage=STORAGE)


class PdfUploadFactory(AbstractUploadFactory):
"""PDF Upload factory."""

file = Faker("pdf_file", storage=STORAGE)


class TxtUploadFactory(AbstractUploadFactory):
"""TXT Upload factory."""

file = Faker("txt_file", storage=STORAGE)


class ZipUploadFactory(AbstractUploadFactory):
"""ZIP Upload factory."""

file = Faker("zip_file", storage=STORAGE)


PROVIDER_CHOICES = [
lambda: DocxFileProvider(None).docx_file(storage=STORAGE),
lambda: PdfFileProvider(None).pdf_file(storage=STORAGE),
lambda: PptxFileProvider(None).pptx_file(storage=STORAGE),
lambda: TxtFileProvider(None).txt_file(storage=STORAGE),
lambda: ZipFileProvider(None).zip_file(storage=STORAGE),
]


def pick_random_provider(*args, **kwargs):
return choice(PROVIDER_CHOICES)()


class UploadFactory(AbstractUploadFactory):
"""Upload factory that randomly picks a file provider."""

file = LazyAttribute(pick_random_provider)
"""Upload factory."""

class Params:
bin_file = Trait(file=Faker("bin_file", storage=STORAGE))
csv_file = Trait(file=Faker("csv_file", storage=STORAGE))
docx_file = Trait(file=Faker("docx_file", storage=STORAGE))
eml_file = Trait(file=Faker("eml_file", storage=STORAGE))
epub_file = Trait(file=Faker("epub_file", storage=STORAGE))
ico_file = Trait(file=Faker("ico_file", storage=STORAGE))
jpeg_file = Trait(file=Faker("jpeg_file", storage=STORAGE))
mp3_file = Trait(file=Faker("mp3_file", storage=STORAGE))
ods_file = Trait(file=Faker("ods_file", storage=STORAGE))
odt_file = Trait(file=Faker("odt_file", storage=STORAGE))
pdf_file = Trait(file=Faker("pdf_file", storage=STORAGE))
png_file = Trait(file=Faker("png_file", storage=STORAGE))
pptx_file = Trait(file=Faker("pptx_file", storage=STORAGE))
rtf_file = Trait(file=Faker("rtf_file", storage=STORAGE))
svg_file = Trait(file=Faker("svg_file", storage=STORAGE))
txt_file = Trait(file=Faker("txt_file", storage=STORAGE))
webp_file = Trait(file=Faker("webp_file", storage=STORAGE))
xlsx_file = Trait(file=Faker("xlsx_file", storage=STORAGE))
zip_file = Trait(file=Faker("zip_file", storage=STORAGE))
random_file = Trait(file=LazyAttribute(pick_random_provider))
24 changes: 14 additions & 10 deletions src/faker_file/tests/test_sqlalchemy_integration.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import Callable
from typing import Any, Callable, Dict
from unittest import TestCase

import pytest
Expand Down Expand Up @@ -38,21 +38,25 @@ def tearDown(self: "SQLAlchemyIntegrationTestCase"):
FAKER: Faker

@parametrize(
"factory",
"factory, kwargs",
[
(factories.DocxUploadFactory,),
(factories.PdfUploadFactory,),
(factories.PptxUploadFactory,),
(factories.TxtUploadFactory,),
(factories.ZipUploadFactory,),
(factories.UploadFactory, {}),
(factories.UploadFactory, {"random_file": True}),
(factories.UploadFactory, {"pdf_file": True}),
(factories.UploadFactory, {"pptx_file": True}),
(factories.UploadFactory, {"txt_file": True}),
(factories.UploadFactory, {"zip_file": True}),
],
)
@pytest.mark.optional
def test_file(
self: "SQLAlchemyIntegrationTestCase", factory: Callable
self: "SQLAlchemyIntegrationTestCase",
factory: Callable,
kwargs: Dict[str, Any],
) -> None:
"""Test file."""
with app.app_context():
_upload = factory()
_upload = factory(**kwargs)
self.session.commit()
self.assertTrue(STORAGE.exists(_upload.file))
if kwargs:
self.assertTrue(STORAGE.exists(_upload.file))

0 comments on commit 05894c9

Please sign in to comment.