Skip to content

Commit

Permalink
Fix/clean ups (#33)
Browse files Browse the repository at this point in the history
* Clean up
* Fix mypy issues
* Add LazyAttribute and LazyFunction
* Update docs
  • Loading branch information
barseghyanartur authored Dec 13, 2023
1 parent c528916 commit a7e37d3
Show file tree
Hide file tree
Showing 12 changed files with 695 additions and 284 deletions.
6 changes: 3 additions & 3 deletions .secrets.baseline
Original file line number Diff line number Diff line change
Expand Up @@ -118,16 +118,16 @@
"filename": "Makefile",
"hashed_secret": "ee783f2421477b5483c23f47eca1f69a1f2bf4fb",
"is_verified": true,
"line_number": 86
"line_number": 92
},
{
"type": "Secret Keyword",
"filename": "Makefile",
"hashed_secret": "1457a35245051927fac6fa556074300f4162ed66",
"is_verified": true,
"line_number": 89
"line_number": 95
}
]
},
"generated_at": "2023-12-10T22:39:54Z"
"generated_at": "2023-12-12T23:38:33Z"
}
8 changes: 8 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,14 @@ are used for versioning (schema follows below):
0.3.4 to 0.4).
- All backwards incompatible changes are mentioned in this document.

0.6.3
-----
2023-12-13

- Add ``LazyAttribute`` and ``LazyFunction``.
- Improve package portability (tests).
- Improve tests.

0.6.2
-----
2023-12-11
Expand Down
10 changes: 8 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.2
VERSION := 0.6.3
SHELL := /bin/bash
# Makefile for project
VENV := ~/.virtualenvs/fake.py/bin/activate
Expand Down Expand Up @@ -37,7 +37,7 @@ serve_docs:
install:
source $(VENV) && pip install -e .[all]

test:
test: clean
source $(VENV) && pytest -vrx -s

customization-test:
Expand All @@ -52,6 +52,9 @@ django-test:
hypothesis-test:
source $(VENV) && cd examples/hypothesis/ && ./manage.py test

lazyfuzzy-test:
source $(VENV) && cd examples/lazyfuzzy/ && ./manage.py test

pydantic-test:
source $(VENV) && cd examples/pydantic/ && python manage.py test

Expand All @@ -73,6 +76,9 @@ dataclasses-shell:
django-shell:
source $(VENV) && python examples/django/manage.py shell

lazyfuzzy-shell:
source $(VENV) && cd examples/lazyfuzzy/ && python manage.py shell

pydantic-shell:
source $(VENV) && cd examples/pydantic/ && python manage.py shell

Expand Down
1 change: 1 addition & 0 deletions __copy_fake.py
63 changes: 60 additions & 3 deletions docs/factories.rst
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ Django example
title = models.CharField(max_length=255)
slug = models.SlugField(unique=True)
content = models.TextField()
headline = models.TextField()
category = models.CharField()
image = models.ImageField(null=True, blank=True)
pub_date = models.DateTimeField(default=timezone.now)
safe_for_work = models.BooleanField(default=False)
Expand All @@ -30,12 +32,17 @@ Django example

.. code-block:: python
import random
from functools import partial
from django.conf import settings
from django.contrib.auth.models import User
from fake import (
FACTORY,
DjangoModelFactory,
FileSystemStorage,
LazyAttribute,
LazyFunction,
SubFactory,
pre_save,
trait,
Expand All @@ -48,6 +55,11 @@ Django example
# custom `FileSystemStorage` class and pass it to the file factory as
# `storage` argument.
STORAGE = FileSystemStorage(root_path=settings.MEDIA_ROOT, rel_path="tmp")
CATEGORIES = (
"art",
"technology",
"literature",
)
class UserFactory(DjangoModelFactory):
Expand Down Expand Up @@ -80,6 +92,8 @@ Django example
title = FACTORY.sentence()
slug = FACTORY.slug()
content = FACTORY.text()
headline = LazyAttribute(lambda o: o.content[:25])
category = LazyFunction(partial(random.choice, CATEGORIES))
image = FACTORY.png_file(storage=STORAGE)
pub_date = FACTORY.date()
safe_for_work = FACTORY.pybool()
Expand Down Expand Up @@ -140,6 +154,8 @@ Pydantic example
title: str = Field(..., max_length=255)
slug: str = Field(..., max_length=255, unique=True)
content: str
headline: str
category: str
image: Optional[str] = None # Use str to represent the image path or URL
pub_date: datetime = Field(default_factory=datetime.now)
safe_for_work: bool = False
Expand All @@ -153,6 +169,8 @@ Pydantic example

.. code-block:: python
import random
from functools import partial
from pathlib import Path
from fake import FACTORY, FileSystemStorage, ModelFactory, SubFactory
Expand All @@ -161,8 +179,12 @@ Pydantic example
BASE_DIR = Path(__file__).resolve().parent.parent
MEDIA_ROOT = BASE_DIR / "media"
STORAGE = FileSystemStorage(root_path=MEDIA_ROOT, rel_path="tmp")
CATEGORIES = (
"art",
"technology",
"literature",
)
class UserFactory(ModelFactory):
id = FACTORY.pyint()
Expand Down Expand Up @@ -194,6 +216,8 @@ Pydantic example
title = FACTORY.sentence()
slug = FACTORY.slug()
content = FACTORY.text()
headline = LazyAttribute(lambda o: o.content[:25])
category = LazyFunction(partial(random.choice, CATEGORIES))
image = FACTORY.png_file(storage=STORAGE)
pub_date = FACTORY.date()
safe_for_work = FACTORY.pybool()
Expand Down Expand Up @@ -240,6 +264,8 @@ TortoiseORM example
title = fields.CharField(max_length=255)
slug = fields.CharField(max_length=255, unique=True)
content = fields.TextField()
headline = fields.TextField()
category = fields.CharField(max_length=255)
image = fields.TextField(null=True, blank=True)
pub_date = fields.DatetimeField(default=datetime.now)
safe_for_work = fields.BooleanField(default=False)
Expand All @@ -253,6 +279,8 @@ TortoiseORM example

.. code-block:: python
import random
from functools import partial
from pathlib import Path
from fake import FACTORY, FileSystemStorage, SubFactory, TortoiseModelFactory
Expand All @@ -261,8 +289,12 @@ TortoiseORM example
BASE_DIR = Path(__file__).resolve().parent.parent
MEDIA_ROOT = BASE_DIR / "media"
STORAGE = FileSystemStorage(root_path=MEDIA_ROOT, rel_path="tmp")
CATEGORIES = (
"art",
"technology",
"literature",
)
class UserFactory(TortoiseModelFactory):
"""User factory."""
Expand Down Expand Up @@ -297,6 +329,8 @@ TortoiseORM example
title = FACTORY.sentence()
slug = FACTORY.slug()
content = FACTORY.text()
headline = LazyAttribute(lambda o: o.content[:25])
category = LazyFunction(partial(random.choice, CATEGORIES))
image = FACTORY.png_file(storage=STORAGE)
pub_date = FACTORY.date_time()
safe_for_work = FACTORY.pybool()
Expand All @@ -315,8 +349,10 @@ Dataclasses example

.. code-block:: python
import random
from dataclasses import dataclass
from datetime import datetime
from functools import partial
from typing import Optional
@dataclass
Expand All @@ -342,6 +378,8 @@ Dataclasses example
title: str
slug: str
content: str
headline: str
category: str
author: User
image: Optional[str] = None # Use str to represent the image path or URL
pub_date: datetime = datetime.now()
Expand All @@ -355,6 +393,8 @@ Dataclasses example

.. code-block:: python
import random
from functools import partial
from pathlib import Path
from fake import FACTORY, FileSystemStorage, ModelFactory, SubFactory
Expand All @@ -363,8 +403,12 @@ Dataclasses example
BASE_DIR = Path(__file__).resolve().parent.parent
MEDIA_ROOT = BASE_DIR / "media"
STORAGE = FileSystemStorage(root_path=MEDIA_ROOT, rel_path="tmp")
CATEGORIES = (
"art",
"technology",
"literature",
)
class UserFactory(ModelFactory):
id = FACTORY.pyint()
Expand Down Expand Up @@ -396,6 +440,8 @@ Dataclasses example
title = FACTORY.sentence()
slug = FACTORY.slug()
content = FACTORY.text()
headline = LazyAttribute(lambda o: o.content[:25])
category = LazyFunction(partial(random.choice, CATEGORIES))
image = FACTORY.png_file(storage=STORAGE)
pub_date = FACTORY.date()
safe_for_work = FACTORY.pybool()
Expand Down Expand Up @@ -469,6 +515,8 @@ SQLAlchemy example
title = Column(String(255))
slug = Column(String(255), unique=True)
content = Column(Text)
headline = Column(Text)
category = Column(String(255))
image = Column(Text, nullable=True)
pub_date = Column(DateTime, default=datetime.utcnow)
safe_for_work = Column(Boolean, default=False)
Expand All @@ -484,6 +532,8 @@ method.

.. code-block:: python
import random
from functools import partial
from pathlib import Path
from fake import (
Expand All @@ -502,6 +552,11 @@ method.
BASE_DIR = Path(__file__).resolve().parent.parent
MEDIA_ROOT = BASE_DIR / "media"
STORAGE = FileSystemStorage(root_path=MEDIA_ROOT, rel_path="tmp")
CATEGORIES = (
"art",
"technology",
"literature",
)
def get_session():
return SESSION()
Expand Down Expand Up @@ -542,6 +597,8 @@ method.
title = FACTORY.sentence()
slug = FACTORY.slug()
content = FACTORY.text()
headline = LazyAttribute(lambda o: o.content[:25])
category = LazyFunction(partial(random.choice, CATEGORIES))
image = FACTORY.png_file(storage=STORAGE)
pub_date = FACTORY.date()
safe_for_work = FACTORY.pybool()
Expand Down
Empty file.
92 changes: 92 additions & 0 deletions examples/lazyfuzzy/article/factories.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
import random
from datetime import datetime
from functools import partial
from pathlib import Path

from fake import (
FACTORY,
FileSystemStorage,
LazyAttribute,
LazyFunction,
ModelFactory,
SubFactory,
post_save,
pre_save,
trait,
)

from article.models import Article, User

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

# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent
MEDIA_ROOT = BASE_DIR / "media"

STORAGE = FileSystemStorage(root_path=MEDIA_ROOT, rel_path="tmp")
CATEGORIES = (
"art",
"technology",
"literature",
)


class UserFactory(ModelFactory):
id = FACTORY.pyint()
username = FACTORY.username()
first_name = FACTORY.first_name()
last_name = FACTORY.last_name()
email = LazyAttribute(lambda o: f"{o.username}@example.com")
last_login = FACTORY.date_time()
is_superuser = False
is_staff = False
is_active = FACTORY.pybool()
date_joined = LazyFunction(datetime.now)

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 _pre_save_method(self, instance):
instance.pre_save_called = True

@post_save
def _post_save_method(self, instance):
instance.post_save_called = True


class ArticleFactory(ModelFactory):
id = FACTORY.pyint()
title = FACTORY.sentence()
slug = FACTORY.slug()
content = FACTORY.text()
headline = LazyAttribute(lambda o: o.content[:25])
category = LazyFunction(partial(random.choice, CATEGORIES))
image = FACTORY.png_file(storage=STORAGE)
pub_date = FACTORY.date()
safe_for_work = FACTORY.pybool()
minutes_to_read = FACTORY.pyint(min_value=1, max_value=10)
author = SubFactory(UserFactory)

class Meta:
model = Article

@pre_save
def _pre_save_method(self, instance):
instance.pre_save_called = True

@post_save
def _post_save_method(self, instance):
instance.post_save_called = True
Loading

0 comments on commit a7e37d3

Please sign in to comment.