diff --git a/CHANGELOG.rst b/CHANGELOG.rst index a2a64d9..3dce366 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -16,6 +16,12 @@ are used for versioning (schema follows below): 0.3.4 to 0.4). - All backwards incompatible changes are mentioned in this document. +0.4 +--- +2023-12-06 + +- Streamline on how to use traits, pre- and post-save hooks. + 0.3.1 ----- 2023-12-04 diff --git a/Makefile b/Makefile index 972675a..3eebce6 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,5 @@ # Update version ONLY here -VERSION := 0.3.1 +VERSION := 0.4 SHELL := /bin/bash # Makefile for project VENV := ~/.virtualenvs/fake.py/bin/activate diff --git a/README.rst b/README.rst index 322961b..fbcc0dd 100644 --- a/README.rst +++ b/README.rst @@ -238,7 +238,7 @@ This is how you could define a factory for `Django`_'s built-in ``User`` model. instance.is_active = True @pre_save - def __set_password(instance): + def _set_password(self, instance: User) -> None: instance.set_password("test") And this is how you could use it: diff --git a/docs/factories.rst b/docs/factories.rst index 131a37b..08e33a5 100644 --- a/docs/factories.rst +++ b/docs/factories.rst @@ -38,6 +38,7 @@ Django example FileSystemStorage, SubFactory, pre_save, + trait, ) from article.models import Article @@ -64,11 +65,16 @@ Django example model = User get_or_create = ("username",) - @staticmethod @pre_save - def __set_password(instance): + def _set_password(self, instance): instance.set_password("test") + @trait + def is_admin_user(self, instance: User) -> None: + instance.is_superuser = True + instance.is_staff = True + instance.is_active = True + class ArticleFactory(DjangoModelFactory): title = FACTORY.sentence() @@ -96,6 +102,9 @@ Django example # Create one article with authors username set to admin. article = ArticleFactory(author__username="admin") + # Using trait + user = UserFactory(is_admin_user=True) + Pydantic example ---------------- **article/models.py** @@ -167,6 +176,16 @@ Pydantic example class Meta: model = User + @trait + def is_admin_user(self, instance: User) -> None: + instance.is_superuser = True + instance.is_staff = True + instance.is_active = True + + @pre_save + def _set_password(self, instance): + instance.set_password("test") + class ArticleFactory(ModelFactory): id = FACTORY.pyint() title = FACTORY.sentence() @@ -259,6 +278,16 @@ TortoiseORM example model = User get_or_create = ("username",) + @trait + def is_admin_user(self, instance: User) -> None: + instance.is_superuser = True + instance.is_staff = True + instance.is_active = True + + @pre_save + def _set_password(self, instance): + instance.set_password("test") + class ArticleFactory(TortoiseModelFactory): """Article factory.""" @@ -349,6 +378,16 @@ Dataclasses example class Meta: model = User + @trait + def is_admin_user(self, instance: User) -> None: + instance.is_superuser = True + instance.is_staff = True + instance.is_active = True + + @pre_save + def _set_password(self, instance): + instance.set_password("test") + class ArticleFactory(ModelFactory): id = FACTORY.pyint() title = FACTORY.sentence() @@ -362,3 +401,5 @@ Dataclasses example class Meta: model = Article + +*Used just like in previous example.* diff --git a/examples/dataclasses/article/factories.py b/examples/dataclasses/article/factories.py index 4e31a28..881b329 100644 --- a/examples/dataclasses/article/factories.py +++ b/examples/dataclasses/article/factories.py @@ -45,14 +45,12 @@ def is_admin_user(self, instance: User) -> None: instance.is_staff = True instance.is_active = True - @staticmethod @pre_save - def __pre_save_method(instance): + def _pre_save_method(self, instance): instance.pre_save_called = True - @staticmethod @post_save - def __post_save_method(instance): + def _post_save_method(self, instance): instance.post_save_called = True @@ -70,12 +68,10 @@ class ArticleFactory(ModelFactory): class Meta: model = Article - @staticmethod @pre_save - def __pre_save_method(instance): + def _pre_save_method(self, instance): instance.pre_save_called = True - @staticmethod @post_save - def __post_save_method(instance): + def _post_save_method(self, instance): instance.post_save_called = True diff --git a/examples/dataclasses/manage.py b/examples/dataclasses/manage.py index 1b715b0..e71fa1d 100755 --- a/examples/dataclasses/manage.py +++ b/examples/dataclasses/manage.py @@ -17,6 +17,7 @@ def run_tests(): def main(): """Run administrative tasks based on command line arguments.""" + sys.path.insert(0, os.path.abspath(os.path.join("..", ".."))) sys.path.insert(0, os.path.abspath(".")) parser = argparse.ArgumentParser( description="Management script for the project." diff --git a/examples/django/article/factories.py b/examples/django/article/factories.py index 060a066..d4c9c75 100644 --- a/examples/django/article/factories.py +++ b/examples/django/article/factories.py @@ -51,19 +51,16 @@ def is_admin_user(self, instance: User) -> None: instance.is_staff = True instance.is_active = True - @staticmethod @pre_save - def __set_password(instance): + def _set_password(self, instance): instance.set_password("test") - @staticmethod @pre_save - def __pre_save_method(instance): + def _pre_save_method(self, instance): instance.pre_save_called = True - @staticmethod @post_save - def __post_save_method(instance): + def _post_save_method(self, instance): instance.post_save_called = True @@ -88,12 +85,10 @@ class ArticleFactory(DjangoModelFactory): class Meta: model = Article - @staticmethod @pre_save - def __pre_save_method(instance): + def _pre_save_method(self, instance): instance.pre_save_called = True - @staticmethod @post_save - def __post_save_method(instance): + def _post_save_method(self, instance): instance.post_save_called = True diff --git a/examples/django/manage.py b/examples/django/manage.py index 75a510d..43f6f85 100755 --- a/examples/django/manage.py +++ b/examples/django/manage.py @@ -6,6 +6,7 @@ def main(): """Run administrative tasks.""" + sys.path.insert(0, os.path.abspath(os.path.join("..", ".."))) os.environ.setdefault("DJANGO_SETTINGS_MODULE", "blog.settings") try: from django.core.management import execute_from_command_line diff --git a/examples/pydantic/article/factories.py b/examples/pydantic/article/factories.py index 4e31a28..881b329 100644 --- a/examples/pydantic/article/factories.py +++ b/examples/pydantic/article/factories.py @@ -45,14 +45,12 @@ def is_admin_user(self, instance: User) -> None: instance.is_staff = True instance.is_active = True - @staticmethod @pre_save - def __pre_save_method(instance): + def _pre_save_method(self, instance): instance.pre_save_called = True - @staticmethod @post_save - def __post_save_method(instance): + def _post_save_method(self, instance): instance.post_save_called = True @@ -70,12 +68,10 @@ class ArticleFactory(ModelFactory): class Meta: model = Article - @staticmethod @pre_save - def __pre_save_method(instance): + def _pre_save_method(self, instance): instance.pre_save_called = True - @staticmethod @post_save - def __post_save_method(instance): + def _post_save_method(self, instance): instance.post_save_called = True diff --git a/examples/pydantic/manage.py b/examples/pydantic/manage.py index 1b715b0..e71fa1d 100755 --- a/examples/pydantic/manage.py +++ b/examples/pydantic/manage.py @@ -17,6 +17,7 @@ def run_tests(): def main(): """Run administrative tasks based on command line arguments.""" + sys.path.insert(0, os.path.abspath(os.path.join("..", ".."))) sys.path.insert(0, os.path.abspath(".")) parser = argparse.ArgumentParser( description="Management script for the project." diff --git a/examples/tortoise/article/factories.py b/examples/tortoise/article/factories.py index 3b4c7c8..02d4acc 100644 --- a/examples/tortoise/article/factories.py +++ b/examples/tortoise/article/factories.py @@ -47,14 +47,12 @@ def is_admin_user(self, instance: User) -> None: instance.is_staff = True instance.is_active = True - @staticmethod @pre_save - def __pre_save_method(instance): + def _pre_save_method(self, instance): instance.pre_save_called = True - @staticmethod @post_save - def __post_save_method(instance): + def _post_save_method(self, instance): instance.post_save_called = True @@ -73,12 +71,10 @@ class ArticleFactory(TortoiseModelFactory): class Meta: model = Article - @staticmethod @pre_save - def __pre_save_method(instance): + def _pre_save_method(self, instance): instance.pre_save_called = True - @staticmethod @post_save - def __post_save_method(instance): + def _post_save_method(self, instance): instance.post_save_called = True diff --git a/examples/tortoise/manage.py b/examples/tortoise/manage.py index 5820dff..d6c6daf 100755 --- a/examples/tortoise/manage.py +++ b/examples/tortoise/manage.py @@ -30,6 +30,7 @@ async def init(): def main(): """Run administrative tasks.""" + sys.path.insert(0, os.path.abspath(os.path.join("..", ".."))) sys.path.insert(0, os.path.abspath(".")) run_async(init()) diff --git a/fake.py b/fake.py index 891ebc0..3489c2f 100644 --- a/fake.py +++ b/fake.py @@ -35,7 +35,7 @@ ) __title__ = "fake.py" -__version__ = "0.3.1" +__version__ = "0.4" __author__ = "Artur Barseghyan " __copyright__ = "2023 Artur Barseghyan" __license__ = "MIT" @@ -1554,7 +1554,6 @@ class FactoryMeta(type): "svg_file", "text", "txt_file", - "txt_file", "url", "username", "uuid", @@ -1643,7 +1642,7 @@ def __init_subclass__(cls, **kwargs): @classmethod def _run_hooks(cls, hooks, instance): for method in hooks: - getattr(cls, method)(instance) + getattr(cls, method)(cls, instance) @classmethod def _apply_traits(cls, instance, **kwargs) -> None: @@ -2516,14 +2515,12 @@ def is_admin_user(self, instance: User) -> None: instance.is_staff = True instance.is_active = True - @staticmethod @pre_save - def __pre_save_method(instance): + def _pre_save_method(self, instance): instance.pre_save_called = True - @staticmethod @post_save - def __post_save_method(instance): + def _post_save_method(self, instance): instance.post_save_called = True class ArticleFactory(ModelFactory): @@ -2595,14 +2592,12 @@ def is_admin_user(self, instance: User) -> None: instance.is_staff = True instance.is_active = True - @staticmethod @pre_save - def __pre_save_method(instance): + def _pre_save_method(self, instance): instance.pre_save_called = True - @staticmethod @post_save - def __post_save_method(instance): + def _post_save_method(self, instance): instance.post_save_called = True class DjangoArticleFactory(DjangoModelFactory): @@ -2619,14 +2614,12 @@ class DjangoArticleFactory(DjangoModelFactory): class Meta: model = Article - @staticmethod @pre_save - def __pre_save_method(instance): + def _pre_save_method(self, instance): instance.pre_save_called = True - @staticmethod @post_save - def __post_save_method(instance): + def _post_save_method(self, instance): instance.post_save_called = True django_article = DjangoArticleFactory(author__username="admin") @@ -2775,14 +2768,12 @@ def is_admin_user(self, instance: TortoiseUser) -> None: instance.is_staff = True instance.is_active = True - @staticmethod @pre_save - def __pre_save_method(instance): + def _pre_save_method(self, instance): instance.pre_save_called = True - @staticmethod @post_save - def __post_save_method(instance): + def _post_save_method(self, instance): instance.post_save_called = True class TortoiseArticleFactory(TortoiseModelFactory): @@ -2799,14 +2790,12 @@ class TortoiseArticleFactory(TortoiseModelFactory): class Meta: model = TortoiseArticle - @staticmethod @pre_save - def __pre_save_method(instance): + def _pre_save_method(self, instance): instance.pre_save_called = True - @staticmethod @post_save - def __post_save_method(instance): + def _post_save_method(self, instance): instance.post_save_called = True tortoise_article = TortoiseArticleFactory(author__username="admin") diff --git a/pyproject.toml b/pyproject.toml index 5886b98..63416d9 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -2,7 +2,7 @@ name = "fake.py" description = "Minimalistic, standalone alternative fake data generator with no dependencies." readme = "README.rst" -version = "0.3.1" +version = "0.4" dependencies = [] authors = [ {name = "Artur Barseghyan", email = "artur.barseghyan@gmail.com"}, @@ -134,7 +134,7 @@ ignore-path = [ ] [tool.pytest.ini_options] -minversion = "0.3.1" +minversion = "0.4" addopts = [ "-ra", "-vvv",