Skip to content

Commit

Permalink
Streamline traits and pre-/post-save hooks
Browse files Browse the repository at this point in the history
  • Loading branch information
barseghyanartur committed Dec 5, 2023
1 parent 10f393e commit 0c7c176
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 58 deletions.
2 changes: 1 addition & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
45 changes: 43 additions & 2 deletions docs/factories.rst
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ Django example
FileSystemStorage,
SubFactory,
pre_save,
trait,
)
from article.models import Article
Expand All @@ -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()
Expand Down Expand Up @@ -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**
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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."""
Expand Down Expand Up @@ -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()
Expand All @@ -362,3 +401,5 @@ Dataclasses example
class Meta:
model = Article
*Used just like in previous example.*
12 changes: 4 additions & 8 deletions examples/dataclasses/article/factories.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand All @@ -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
15 changes: 5 additions & 10 deletions examples/django/article/factories.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand All @@ -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
12 changes: 4 additions & 8 deletions examples/pydantic/article/factories.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand All @@ -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
12 changes: 4 additions & 8 deletions examples/tortoise/article/factories.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand All @@ -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
32 changes: 11 additions & 21 deletions fake.py
Original file line number Diff line number Diff line change
Expand Up @@ -1643,7 +1643,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:
Expand Down Expand Up @@ -2516,14 +2516,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):
Expand Down Expand Up @@ -2595,14 +2593,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):
Expand All @@ -2619,14 +2615,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")
Expand Down Expand Up @@ -2775,14 +2769,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):
Expand All @@ -2799,14 +2791,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")
Expand Down

0 comments on commit 0c7c176

Please sign in to comment.