Skip to content

Commit

Permalink
Merge pull request #29 from barseghyanartur/dev
Browse files Browse the repository at this point in the history
Improve tests. Allow to load ``Faker`` instances by ``uid`` or ``alias``.
  • Loading branch information
barseghyanartur committed Dec 9, 2023
2 parents 009abd0 + 8fffc06 commit e94f38c
Show file tree
Hide file tree
Showing 7 changed files with 77 additions and 32 deletions.
8 changes: 7 additions & 1 deletion CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,13 @@ are used for versioning (schema follows below):
0.3.4 to 0.4).
- All backwards incompatible changes are mentioned in this document.

0.6.1
-----
2023-12-10

- Allow to load registered ``Faker`` instance by ``uid`` or ``alias``.
- Improve test coverage.

0.6
---
2023-12-09
Expand All @@ -24,7 +31,6 @@ are used for versioning (schema follows below):
- Improve multiple ``Faker`` instances.
- Add ``generic_file`` provider.


0.5
---
2023-12-08
Expand Down
7 changes: 5 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Update version ONLY here
VERSION := 0.6
VERSION := 0.6.1
SHELL := /bin/bash
# Makefile for project
VENV := ~/.virtualenvs/fake.py/bin/activate
Expand Down Expand Up @@ -124,4 +124,7 @@ test-release:
source $(VENV) && twine upload --repository testpypi dist/*

mypy:
mypy .
source $(VENV) && mypy .

%:
@:
23 changes: 12 additions & 11 deletions examples/customization/address/factories.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
from fake import ModelFactory, SubFactory, post_save, pre_save
from override_default_data import FACTORY as OVERRIDE_DEFAULT_DATA_FACTORY

from address.models import Address, Person
from fake_address import FACTORY
from fake_address import FACTORY as ADDRESS_FACTORY

__author__ = "Artur Barseghyan <artur.barseghyan@gmail.com>"
__copyright__ = "2023 Artur Barseghyan"
Expand All @@ -13,11 +14,11 @@


class AddressFactory(ModelFactory):
id = FACTORY.pyint()
address_line = FACTORY.address_line()
postal_code = FACTORY.postal_code()
city = FACTORY.city()
region = FACTORY.region()
id = ADDRESS_FACTORY.pyint()
address_line = ADDRESS_FACTORY.address_line()
postal_code = ADDRESS_FACTORY.postal_code()
city = ADDRESS_FACTORY.city()
region = ADDRESS_FACTORY.region()

class Meta:
model = Address
Expand All @@ -32,11 +33,11 @@ def _post_save_method(self, instance):


class PersonFactory(ModelFactory):
id = FACTORY.pyint()
first_name = FACTORY.first_name()
last_name = FACTORY.last_name()
email = FACTORY.email()
dob = FACTORY.date()
id = OVERRIDE_DEFAULT_DATA_FACTORY.pyint()
first_name = OVERRIDE_DEFAULT_DATA_FACTORY.first_name()
last_name = OVERRIDE_DEFAULT_DATA_FACTORY.last_name()
email = OVERRIDE_DEFAULT_DATA_FACTORY.email()
dob = OVERRIDE_DEFAULT_DATA_FACTORY.date()
address = SubFactory(AddressFactory)

class Meta:
Expand Down
13 changes: 2 additions & 11 deletions examples/customization/fake_address.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

from fake import Factory, Faker, provider

from data import CITIES, FIRST_NAMES, LAST_NAMES, REGIONS, STREET_NAMES, WORDS
from data import CITIES, REGIONS, STREET_NAMES

__author__ = "Artur Barseghyan <artur.barseghyan@gmail.com>"
__copyright__ = "2023 Artur Barseghyan"
Expand All @@ -17,15 +17,6 @@
class FakerAddress(Faker):
"""Custom Faker class for addresses."""

def load_names(self) -> None:
"""Override default first- and last-names dictionaries."""
self._first_names = FIRST_NAMES
self._last_names = LAST_NAMES

def load_words(self) -> None:
"""Override default words dictionary."""
self._words = WORDS

@provider
def address_line(self) -> str:
"""Generate a random Dutch address line like 'Oranjestraat 1'.
Expand Down Expand Up @@ -60,5 +51,5 @@ def postal_code(self) -> str:
return f"{number_part} {letter_part}"


FAKER = FakerAddress(alias="nl_NL:address")
FAKER = FakerAddress(alias="address")
FACTORY = Factory(FAKER)
28 changes: 28 additions & 0 deletions examples/customization/override_default_data.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
from fake import Factory, Faker

from data import FIRST_NAMES, LAST_NAMES, WORDS

__author__ = "Artur Barseghyan <artur.barseghyan@gmail.com>"
__copyright__ = "2023 Artur Barseghyan"
__license__ = "MIT"
__all__ = (
"FACTORY",
"FAKER",
)


class FakerOverrideDefaultData(Faker):
"""Faker class for custom names and words."""

def load_names(self) -> None:
"""Override default first- and last-names dictionaries."""
self._first_names = FIRST_NAMES
self._last_names = LAST_NAMES

def load_words(self) -> None:
"""Override default words dictionary."""
self._words = WORDS


FAKER = FakerOverrideDefaultData(alias="override_default_data")
FACTORY = Factory(FAKER)
26 changes: 21 additions & 5 deletions fake.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
)

__title__ = "fake.py"
__version__ = "0.6"
__version__ = "0.6.1"
__author__ = "Artur Barseghyan <artur.barseghyan@gmail.com>"
__copyright__ = "2023 Artur Barseghyan"
__license__ = "MIT"
Expand Down Expand Up @@ -940,6 +940,14 @@ def __init__(self, alias: Optional[str] = None) -> None:
self.load_words()
self.load_names()

@staticmethod
def get_by_uid(uid: str) -> Union["Faker", None]:
return UID_REGISTRY.get(uid, None)

@staticmethod
def get_by_alias(alias: str) -> Union["Faker", None]:
return ALIAS_REGISTRY.get(alias, None)

def load_words(self) -> None:
with contextlib.redirect_stdout(io.StringIO()):
# Dynamically import 'this' module
Expand Down Expand Up @@ -2609,6 +2617,18 @@ def test_metadata(self) -> None:
metadata.add_content(content)
self.assertEqual(metadata.content, "\n".join(content))

def test_faker_init(self) -> None:
faker = Faker(alias="default")
self.assertNotEqual(faker.alias, "default")

def test_get_by_uid(self) -> None:
faker = Faker.get_by_uid("fake.Faker")
self.assertIs(faker, self.faker)

def test_get_by_alias(self) -> None:
faker = Faker.get_by_alias("default")
self.assertIs(faker, self.faker)

def test_factory_method(self) -> None:
"""Test FactoryMethod."""
with self.subTest("sentence"):
Expand Down Expand Up @@ -3166,7 +3186,3 @@ def test_clean_up_exceptions(self):

# Clean up by removing the handler
LOGGER.removeHandler(handler)


if __name__ == "__main__":
unittest.main()
4 changes: 2 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
name = "fake.py"
description = "Minimalistic, standalone alternative fake data generator with no dependencies."
readme = "README.rst"
version = "0.6"
version = "0.6.1"
dependencies = []
authors = [
{name = "Artur Barseghyan", email = "artur.barseghyan@gmail.com"},
Expand Down Expand Up @@ -134,7 +134,7 @@ ignore-path = [
]

[tool.pytest.ini_options]
minversion = "0.6"
minversion = "0.6.1"
addopts = [
"-ra",
"-vvv",
Expand Down

0 comments on commit e94f38c

Please sign in to comment.