Skip to content

Commit

Permalink
More on docs
Browse files Browse the repository at this point in the history
  • Loading branch information
barseghyanartur committed Nov 30, 2023
1 parent 2c75edb commit 40f77bb
Show file tree
Hide file tree
Showing 2 changed files with 114 additions and 3 deletions.
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.1.3
VERSION := 0.1.4
SHELL := /bin/bash
# Makefile for project
VENV := ~/.virtualenvs/fake.py/bin/activate
Expand Down
115 changes: 113 additions & 2 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ fake.py
.. External references
.. _Faker: https://faker.readthedocs.io/
.. _Django: https://www.djangoproject.com/
.. _TortoiseORM: https://tortoise.github.io/
.. _Pydantic: https://docs.pydantic.dev/l

Minimalistic, standalone alternative fake data generator with no dependencies.

Expand Down Expand Up @@ -42,8 +45,9 @@ various types of random data for testing and other purposes. The package
provides a simplified, dependency-free alternative for generating random
words, sentences, paragraphs, file names, URLs, person names, as well as
bytes content for ``PDF``-, ``DOCX``- and various image formats (such as
``PNG``, ``SVG``, ``BMP`` and ``GIF``). It also can create files directly
on your filesystem.
``PNG``, ``SVG``, ``BMP`` and ``GIF``). It can create files directly
on your filesystem too. It comes with factories (works with `Django`_,
`TortoiseORM`_, `Pydantic`_).

Requirements
============
Expand All @@ -70,6 +74,8 @@ coverage (you might need to apply tweaks to your coverage configuration).

Usage
=====
Generate data
-------------
**Imports and initialization**

.. code-block:: python
Expand Down Expand Up @@ -567,6 +573,111 @@ Arguments:
argument.
- ``prefix`` (type: ``str``, default value: ``None``) is an optional argument.

Factories
---------
Django example
~~~~~~~~~~~~~~
**factories.py**

.. code-block:: python
from django.conf import settings
from fake import (
DjangoModelFactory,
Factory,
FileSystemStorage,
SubFactory,
pre_save,
)
from article.models import Article
STORAGE = FileSystemStorage(root_path=settings.MEDIA_ROOT, rel_path="tmp")
class ArticleFactory(DjangoModelFactory):
title = Factory.sentence()
slug = Factory.slug()
content = Factory.text()
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
**Usage example**

.. code-block:: python
article = ArticleFactory() # Create one article
articles = ArticleFactory.create_batch(5) # Create 5 articles
Pydantic example
~~~~~~~~~~~~~~~~

.. code-block:: python
from pathlib import Path
from fake import Factory, FileSystemStorage, ModelFactory, SubFactory
from article.models import Article
BASE_DIR = Path(__file__).resolve().parent.parent
MEDIA_ROOT = BASE_DIR / "media"
STORAGE = FileSystemStorage(root_path=MEDIA_ROOT, rel_path="tmp")
class ArticleFactory(ModelFactory):
id = Factory.pyint()
title = Factory.sentence()
slug = Factory.slug()
content = Factory.text()
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
*Used just like in previous example.*

TortoiseORM example
~~~~~~~~~~~~~~~~~~~

.. code-block:: python
from pathlib import Path
from fake import Factory, FileSystemStorage, SubFactory, TortoiseModelFactory
from article.models import Article, User
# 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")
class ArticleFactory(TortoiseModelFactory):
id = Factory.pyint()
title = Factory.sentence()
slug = Factory.slug()
content = Factory.text()
image = Factory.png_file(storage=STORAGE)
pub_date = Factory.date_time()
safe_for_work = Factory.pybool()
minutes_to_read = Factory.pyint(min_value=1, max_value=10)
author = SubFactory(UserFactory)
class Meta:
model = Article
*Used just like in previous example.*

Tests
=====

Expand Down

0 comments on commit 40f77bb

Please sign in to comment.