Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix/traits (#18) #19

Merged
merged 1 commit into from
Dec 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -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
Expand Down
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
1 change: 1 addition & 0 deletions examples/dataclasses/manage.py
Original file line number Diff line number Diff line change
Expand Up @@ -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."
Expand Down
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
1 change: 1 addition & 0 deletions examples/django/manage.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
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
1 change: 1 addition & 0 deletions examples/pydantic/manage.py
Original file line number Diff line number Diff line change
Expand Up @@ -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."
Expand Down
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
1 change: 1 addition & 0 deletions examples/tortoise/manage.py
Original file line number Diff line number Diff line change
Expand Up @@ -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())

Expand Down
35 changes: 12 additions & 23 deletions fake.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
)

__title__ = "fake.py"
__version__ = "0.3.1"
__version__ = "0.4"
__author__ = "Artur Barseghyan <artur.barseghyan@gmail.com>"
__copyright__ = "2023 Artur Barseghyan"
__license__ = "MIT"
Expand Down Expand Up @@ -1554,7 +1554,6 @@ class FactoryMeta(type):
"svg_file",
"text",
"txt_file",
"txt_file",
"url",
"username",
"uuid",
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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):
Expand All @@ -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")
Expand Down Expand Up @@ -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):
Expand All @@ -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")
Expand Down
Loading