fake.py 0.3__tar.gz → 0.4__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {fake.py-0.3 → fake.py-0.4}/.secrets.baseline +3 -3
- {fake.py-0.3 → fake.py-0.4}/CHANGELOG.rst +14 -0
- {fake.py-0.3 → fake.py-0.4}/CONTRIBUTING.rst +8 -3
- {fake.py-0.3 → fake.py-0.4}/Makefile +16 -4
- {fake.py-0.3/fake.py.egg-info → fake.py-0.4}/PKG-INFO +44 -14
- {fake.py-0.3 → fake.py-0.4}/README.rst +43 -13
- {fake.py-0.3 → fake.py-0.4}/docs/creating_images.rst +3 -3
- {fake.py-0.3 → fake.py-0.4}/docs/factories.rst +172 -6
- {fake.py-0.3/examples/pydantic → fake.py-0.4/examples/dataclasses}/article/factories.py +31 -1
- fake.py-0.4/examples/dataclasses/article/tests.py +48 -0
- fake.py-0.4/examples/dataclasses/manage.py +38 -0
- {fake.py-0.3 → fake.py-0.4}/examples/django/article/factories.py +25 -2
- fake.py-0.4/examples/django/article/tests.py +85 -0
- {fake.py-0.3 → fake.py-0.4}/examples/django/manage.py +1 -0
- {fake.py-0.3/examples/dataclasses → fake.py-0.4/examples/pydantic}/article/factories.py +31 -1
- {fake.py-0.3 → fake.py-0.4}/examples/pydantic/article/models.py +6 -0
- fake.py-0.4/examples/pydantic/article/tests.py +48 -0
- fake.py-0.4/examples/pydantic/manage.py +38 -0
- {fake.py-0.3 → fake.py-0.4}/examples/tortoise/article/factories.py +31 -3
- fake.py-0.4/examples/tortoise/article/tests.py +97 -0
- fake.py-0.4/examples/tortoise/manage.py +53 -0
- {fake.py-0.3 → fake.py-0.4}/fake.py +355 -57
- {fake.py-0.3 → fake.py-0.4/fake.py.egg-info}/PKG-INFO +44 -14
- {fake.py-0.3 → fake.py-0.4}/fake.py.egg-info/SOURCES.txt +7 -3
- {fake.py-0.3 → fake.py-0.4}/pyproject.toml +2 -2
- fake.py-0.3/examples/dataclasses/shell.py +0 -15
- fake.py-0.3/examples/pydantic/shell.py +0 -15
- fake.py-0.3/examples/tortoise/shell.py +0 -29
- {fake.py-0.3 → fake.py-0.4}/.coveralls.yml +0 -0
- {fake.py-0.3 → fake.py-0.4}/.env +0 -0
- {fake.py-0.3 → fake.py-0.4}/.github/workflows/test.yml +0 -0
- {fake.py-0.3 → fake.py-0.4}/.gitignore +0 -0
- {fake.py-0.3 → fake.py-0.4}/.pre-commit-config.yaml +0 -0
- {fake.py-0.3 → fake.py-0.4}/.pre-commit-hooks.yaml +0 -0
- {fake.py-0.3 → fake.py-0.4}/.readthedocs.yaml +0 -0
- {fake.py-0.3 → fake.py-0.4}/CODE_OF_CONDUCT.md +0 -0
- {fake.py-0.3 → fake.py-0.4}/CODE_OF_CONDUCT.rst +0 -0
- {fake.py-0.3 → fake.py-0.4}/LICENSE +0 -0
- {fake.py-0.3 → fake.py-0.4}/SECURITY.md +0 -0
- {fake.py-0.3 → fake.py-0.4}/SECURITY.rst +0 -0
- {fake.py-0.3 → fake.py-0.4}/docs/Makefile +0 -0
- {fake.py-0.3 → fake.py-0.4}/docs/_static/examples/creating_docx/docx_bytes_1.py +0 -0
- {fake.py-0.3 → fake.py-0.4}/docs/_static/examples/creating_docx/docx_bytes_2.py +0 -0
- {fake.py-0.3 → fake.py-0.4}/docs/_static/examples/creating_docx/docx_bytes_3.py +0 -0
- {fake.py-0.3 → fake.py-0.4}/docs/_static/examples/creating_docx/docx_file_1.py +0 -0
- {fake.py-0.3 → fake.py-0.4}/docs/_static/examples/creating_docx/docx_file_2.py +0 -0
- {fake.py-0.3 → fake.py-0.4}/docs/_static/examples/creating_docx/docx_file_3.py +0 -0
- {fake.py-0.3 → fake.py-0.4}/docs/_static/examples/creating_images/png_bytes_1.py +0 -0
- {fake.py-0.3 → fake.py-0.4}/docs/_static/examples/creating_images/png_bytes_2.py +0 -0
- {fake.py-0.3 → fake.py-0.4}/docs/_static/examples/creating_images/png_file_1.py +0 -0
- {fake.py-0.3 → fake.py-0.4}/docs/_static/examples/creating_images/png_file_2.py +0 -0
- {fake.py-0.3 → fake.py-0.4}/docs/_static/examples/creating_pdf/graphic_pdf_bytes_1.py +0 -0
- {fake.py-0.3 → fake.py-0.4}/docs/_static/examples/creating_pdf/graphic_pdf_bytes_2.py +0 -0
- {fake.py-0.3 → fake.py-0.4}/docs/_static/examples/creating_pdf/graphic_pdf_file_1.py +0 -0
- {fake.py-0.3 → fake.py-0.4}/docs/_static/examples/creating_pdf/graphic_pdf_file_2.py +0 -0
- {fake.py-0.3 → fake.py-0.4}/docs/_static/examples/creating_pdf/text_pdf_bytes_1.py +0 -0
- {fake.py-0.3 → fake.py-0.4}/docs/_static/examples/creating_pdf/text_pdf_bytes_2.py +0 -0
- {fake.py-0.3 → fake.py-0.4}/docs/_static/examples/creating_pdf/text_pdf_bytes_3.py +0 -0
- {fake.py-0.3 → fake.py-0.4}/docs/_static/examples/creating_pdf/text_pdf_file_1.py +0 -0
- {fake.py-0.3 → fake.py-0.4}/docs/_static/examples/creating_pdf/text_pdf_file_2.py +0 -0
- {fake.py-0.3 → fake.py-0.4}/docs/_static/examples/creating_pdf/text_pdf_file_3.py +0 -0
- {fake.py-0.3 → fake.py-0.4}/docs/_static/examples/creating_pdf/text_pdf_file_django_1.py +0 -0
- {fake.py-0.3 → fake.py-0.4}/docs/_static/examples/creating_pdf/text_pdf_file_pydantic_1.py +0 -0
- {fake.py-0.3 → fake.py-0.4}/docs/_static/examples/factories/django_factories_1.py +0 -0
- {fake.py-0.3 → fake.py-0.4}/docs/_static/examples/factories/django_models_1.py +0 -0
- {fake.py-0.3 → fake.py-0.4}/docs/_static/examples/factories/pydantic_factories_1.py +0 -0
- {fake.py-0.3 → fake.py-0.4}/docs/_static/examples/factories/pydantic_models_1.py +0 -0
- {fake.py-0.3 → fake.py-0.4}/docs/_static/examples/factories/tortoise_factories_1.py +0 -0
- {fake.py-0.3 → fake.py-0.4}/docs/_static/examples/factories/tortoise_models_1.py +0 -0
- {fake.py-0.3 → fake.py-0.4}/docs/changelog.rst +0 -0
- {fake.py-0.3 → fake.py-0.4}/docs/code_of_conduct.rst +0 -0
- {fake.py-0.3 → fake.py-0.4}/docs/conf.py +0 -0
- {fake.py-0.3 → fake.py-0.4}/docs/conf.py.distrib +0 -0
- {fake.py-0.3 → fake.py-0.4}/docs/contributor_guidelines.rst +0 -0
- {fake.py-0.3 → fake.py-0.4}/docs/creating_docx.rst +0 -0
- {fake.py-0.3 → fake.py-0.4}/docs/creating_pdf.rst +0 -0
- {fake.py-0.3 → fake.py-0.4}/docs/documentation.rst +0 -0
- {fake.py-0.3 → fake.py-0.4}/docs/fake.rst +0 -0
- {fake.py-0.3 → fake.py-0.4}/docs/index.rst +0 -0
- {fake.py-0.3 → fake.py-0.4}/docs/index.rst.distrib +0 -0
- {fake.py-0.3 → fake.py-0.4}/docs/make.bat +0 -0
- {fake.py-0.3 → fake.py-0.4}/docs/package.rst +0 -0
- {fake.py-0.3 → fake.py-0.4}/docs/readme.rst +0 -0
- {fake.py-0.3 → fake.py-0.4}/docs/recipes.rst +0 -0
- {fake.py-0.3 → fake.py-0.4}/docs/requirements.txt +0 -0
- {fake.py-0.3 → fake.py-0.4}/docs/security.rst +0 -0
- {fake.py-0.3 → fake.py-0.4}/examples/dataclasses/article/__init__.py +0 -0
- {fake.py-0.3 → fake.py-0.4}/examples/dataclasses/article/models.py +0 -0
- {fake.py-0.3 → fake.py-0.4}/examples/django/article/__init__.py +0 -0
- {fake.py-0.3 → fake.py-0.4}/examples/django/article/admin.py +0 -0
- {fake.py-0.3 → fake.py-0.4}/examples/django/article/apps.py +0 -0
- {fake.py-0.3 → fake.py-0.4}/examples/django/article/migrations/0001_initial.py +0 -0
- {fake.py-0.3 → fake.py-0.4}/examples/django/article/migrations/0002_article_image.py +0 -0
- {fake.py-0.3 → fake.py-0.4}/examples/django/article/migrations/__init__.py +0 -0
- {fake.py-0.3 → fake.py-0.4}/examples/django/article/models.py +0 -0
- {fake.py-0.3 → fake.py-0.4}/examples/django/blog/__init__.py +0 -0
- {fake.py-0.3 → fake.py-0.4}/examples/django/blog/asgi.py +0 -0
- {fake.py-0.3 → fake.py-0.4}/examples/django/blog/settings.py +0 -0
- {fake.py-0.3 → fake.py-0.4}/examples/django/blog/urls.py +0 -0
- {fake.py-0.3 → fake.py-0.4}/examples/django/blog/wsgi.py +0 -0
- {fake.py-0.3 → fake.py-0.4}/examples/pydantic/article/__init__.py +0 -0
- {fake.py-0.3 → fake.py-0.4}/examples/tortoise/article/__init__.py +0 -0
- {fake.py-0.3 → fake.py-0.4}/examples/tortoise/article/models.py +0 -0
- {fake.py-0.3 → fake.py-0.4}/fake.py.egg-info/dependency_links.txt +0 -0
- {fake.py-0.3 → fake.py-0.4}/fake.py.egg-info/requires.txt +0 -0
- {fake.py-0.3 → fake.py-0.4}/fake.py.egg-info/top_level.txt +0 -0
- {fake.py-0.3 → fake.py-0.4}/setup.cfg +0 -0
|
@@ -118,16 +118,16 @@
|
|
|
118
118
|
"filename": "Makefile",
|
|
119
119
|
"hashed_secret": "ee783f2421477b5483c23f47eca1f69a1f2bf4fb",
|
|
120
120
|
"is_verified": true,
|
|
121
|
-
"line_number":
|
|
121
|
+
"line_number": 71
|
|
122
122
|
},
|
|
123
123
|
{
|
|
124
124
|
"type": "Secret Keyword",
|
|
125
125
|
"filename": "Makefile",
|
|
126
126
|
"hashed_secret": "1457a35245051927fac6fa556074300f4162ed66",
|
|
127
127
|
"is_verified": true,
|
|
128
|
-
"line_number":
|
|
128
|
+
"line_number": 74
|
|
129
129
|
}
|
|
130
130
|
]
|
|
131
131
|
},
|
|
132
|
-
"generated_at": "2023-12-
|
|
132
|
+
"generated_at": "2023-12-03T22:23:51Z"
|
|
133
133
|
}
|
|
@@ -16,6 +16,20 @@ are used for versioning (schema follows below):
|
|
|
16
16
|
0.3.4 to 0.4).
|
|
17
17
|
- All backwards incompatible changes are mentioned in this document.
|
|
18
18
|
|
|
19
|
+
0.4
|
|
20
|
+
---
|
|
21
|
+
2023-12-06
|
|
22
|
+
|
|
23
|
+
- Streamline on how to use traits, pre- and post-save hooks.
|
|
24
|
+
|
|
25
|
+
0.3.1
|
|
26
|
+
-----
|
|
27
|
+
2023-12-04
|
|
28
|
+
|
|
29
|
+
- Improve Tortoise ORM factory.
|
|
30
|
+
- Add traits.
|
|
31
|
+
- Improve docmentation.
|
|
32
|
+
|
|
19
33
|
0.3
|
|
20
34
|
---
|
|
21
35
|
2023-12-03
|
|
@@ -99,8 +99,13 @@ For example:
|
|
|
99
99
|
|
|
100
100
|
**Good to know:**
|
|
101
101
|
|
|
102
|
-
-
|
|
103
|
-
|
|
102
|
+
- This library consists of a single ``fake.py`` module. That module is
|
|
103
|
+
dependency free, self-contained (includes all tests) and portable.
|
|
104
|
+
Do not submit pull requests splitting the ``fake.py`` module into small
|
|
105
|
+
parts.
|
|
106
|
+
- Some tests contain simplified implementation of existing libraries (Django
|
|
107
|
+
ORM). If you need to add integration tests for existing functionality,
|
|
108
|
+
you can add the relevant code and requirements to the examples.
|
|
104
109
|
|
|
105
110
|
**General list to go through:**
|
|
106
111
|
|
|
@@ -108,7 +113,7 @@ For example:
|
|
|
108
113
|
- Does your change require update to tests?
|
|
109
114
|
- Does your change rely on third-party package or a cloud based service?
|
|
110
115
|
If so, please consider turning it into a dedicated standalone package,
|
|
111
|
-
since this library is dependency free.
|
|
116
|
+
since this library is dependency free (and will always stay so).
|
|
112
117
|
|
|
113
118
|
**When fixing bugs (in addition to the general list):**
|
|
114
119
|
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
# Update version ONLY here
|
|
2
|
-
VERSION := 0.
|
|
2
|
+
VERSION := 0.4
|
|
3
3
|
SHELL := /bin/bash
|
|
4
4
|
# Makefile for project
|
|
5
5
|
VENV := ~/.virtualenvs/fake.py/bin/activate
|
|
@@ -40,6 +40,18 @@ install:
|
|
|
40
40
|
test:
|
|
41
41
|
source $(VENV) && pytest -vrx -s
|
|
42
42
|
|
|
43
|
+
django-test:
|
|
44
|
+
source $(VENV) && cd examples/django/ && ./manage.py test
|
|
45
|
+
|
|
46
|
+
pydantic-test:
|
|
47
|
+
source $(VENV) && cd examples/pydantic/ && python manage.py test
|
|
48
|
+
|
|
49
|
+
dataclasses-test:
|
|
50
|
+
source $(VENV) && cd examples/dataclasses/ && python manage.py test
|
|
51
|
+
|
|
52
|
+
tortoise-test:
|
|
53
|
+
source $(VENV) && cd examples/tortoise/ && python manage.py test
|
|
54
|
+
|
|
43
55
|
shell:
|
|
44
56
|
source $(VENV) && ipython
|
|
45
57
|
|
|
@@ -47,13 +59,13 @@ django-shell:
|
|
|
47
59
|
source $(VENV) && python examples/django/manage.py shell
|
|
48
60
|
|
|
49
61
|
pydantic-shell:
|
|
50
|
-
source $(VENV) &&
|
|
62
|
+
source $(VENV) && cd examples/pydantic/ && python manage.py shell
|
|
51
63
|
|
|
52
64
|
dataclasses-shell:
|
|
53
|
-
source $(VENV) &&
|
|
65
|
+
source $(VENV) && cd examples/dataclasses/ && python manage.py shell
|
|
54
66
|
|
|
55
67
|
tortoise-shell:
|
|
56
|
-
source $(VENV) &&
|
|
68
|
+
source $(VENV) && cd examples/tortoise/ && python manage.py shell
|
|
57
69
|
|
|
58
70
|
create-secrets:
|
|
59
71
|
source $(VENV) && detect-secrets scan > .secrets.baseline
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.1
|
|
2
2
|
Name: fake.py
|
|
3
|
-
Version: 0.
|
|
3
|
+
Version: 0.4
|
|
4
4
|
Summary: Minimalistic, standalone alternative fake data generator with no dependencies.
|
|
5
5
|
Author-email: Artur Barseghyan <artur.barseghyan@gmail.com>
|
|
6
6
|
License: MIT
|
|
@@ -51,12 +51,17 @@ fake.py
|
|
|
51
51
|
.. External references
|
|
52
52
|
|
|
53
53
|
.. _Faker: https://faker.readthedocs.io/
|
|
54
|
+
.. _factory_boy: https://factoryboy.readthedocs.io/
|
|
55
|
+
.. _faker-file: https://faker-file.readthedocs.io/
|
|
56
|
+
.. _Pillow: https://python-pillow.org/
|
|
57
|
+
.. _dateutil: https://dateutil.readthedocs.io/
|
|
54
58
|
.. _Django: https://www.djangoproject.com/
|
|
55
59
|
.. _TortoiseORM: https://tortoise.github.io/
|
|
56
60
|
.. _Pydantic: https://docs.pydantic.dev/
|
|
57
61
|
|
|
58
62
|
.. Internal references
|
|
59
63
|
|
|
64
|
+
.. _fake.py: https://github.com/barseghyanartur/fake.py/
|
|
60
65
|
.. _Read the Docs: http://fakepy.readthedocs.io/
|
|
61
66
|
.. _Quick start: https://fakepy.readthedocs.io/en/latest/quick_start.html
|
|
62
67
|
.. _Recipes: https://fakepy.readthedocs.io/en/latest/recipes.html
|
|
@@ -91,12 +96,7 @@ Minimalistic, standalone alternative fake data generator with no dependencies.
|
|
|
91
96
|
:target: https://coveralls.io/github/barseghyanartur/fake.py?branch=main
|
|
92
97
|
:alt: Coverage
|
|
93
98
|
|
|
94
|
-
|
|
95
|
-
:depth: 2
|
|
96
|
-
|
|
97
|
-
Overview
|
|
98
|
-
========
|
|
99
|
-
``fake.py`` is a standalone, portable library designed for generating various
|
|
99
|
+
`fake.py`_ is a standalone, portable library designed for generating various
|
|
100
100
|
random data types for testing.
|
|
101
101
|
|
|
102
102
|
It offers a simplified, dependency-free alternative for creating random
|
|
@@ -257,6 +257,7 @@ This is how you could define a factory for `Django`_'s built-in ``User`` model.
|
|
|
257
257
|
FileSystemStorage,
|
|
258
258
|
SubFactory,
|
|
259
259
|
pre_save,
|
|
260
|
+
trait,
|
|
260
261
|
)
|
|
261
262
|
|
|
262
263
|
STORAGE = FileSystemStorage(root_path=settings.MEDIA_ROOT, rel_path="tmp")
|
|
@@ -277,40 +278,69 @@ This is how you could define a factory for `Django`_'s built-in ``User`` model.
|
|
|
277
278
|
model = User
|
|
278
279
|
get_or_create = ("username",)
|
|
279
280
|
|
|
281
|
+
@trait
|
|
282
|
+
def is_admin_user(self, instance: User) -> None:
|
|
283
|
+
instance.is_superuser = True
|
|
284
|
+
instance.is_staff = True
|
|
285
|
+
instance.is_active = True
|
|
286
|
+
|
|
280
287
|
@pre_save
|
|
281
|
-
def
|
|
288
|
+
def _set_password(self, instance: User) -> None:
|
|
282
289
|
instance.set_password("test")
|
|
283
290
|
|
|
284
291
|
And this is how you could use it:
|
|
285
292
|
|
|
286
293
|
.. code-block:: python
|
|
287
294
|
|
|
295
|
+
# Create just one user
|
|
288
296
|
user = UserFactory()
|
|
297
|
+
|
|
298
|
+
# Create 5 users
|
|
289
299
|
users = UserFactory.create_batch(5)
|
|
290
300
|
|
|
301
|
+
# Create a user using `is_admin_user` trait
|
|
302
|
+
user = UserFactory(is_admin_user=True)
|
|
303
|
+
|
|
291
304
|
Tests
|
|
292
305
|
=====
|
|
293
306
|
|
|
294
307
|
Run the tests with unittest:
|
|
295
308
|
|
|
296
|
-
.. code-block::
|
|
309
|
+
.. code-block:: sh
|
|
297
310
|
|
|
298
|
-
python -m unittest
|
|
311
|
+
python -m unittest fake.py
|
|
299
312
|
|
|
300
313
|
Or pytest:
|
|
301
314
|
|
|
302
|
-
.. code-block::
|
|
315
|
+
.. code-block:: sh
|
|
303
316
|
|
|
304
317
|
pytest
|
|
305
318
|
|
|
306
|
-
Differences with
|
|
307
|
-
|
|
308
|
-
|
|
319
|
+
Differences with alternatives
|
|
320
|
+
=============================
|
|
321
|
+
`fake.py`_ is `Faker`_ + `factory_boy`_ + `faker-file`_ in one package,
|
|
322
|
+
radically simplified and reduced in features, but without any external
|
|
323
|
+
dependencies (not even `Pillow`_ or `dateutil`_).
|
|
324
|
+
|
|
325
|
+
`fake.py`_ is modeled after the famous `Faker`_ package. Its' API is highly
|
|
309
326
|
compatible, although drastically reduced. It's not multilingual and does not
|
|
310
327
|
support postal codes or that many RAW file formats. However, you could easily
|
|
311
328
|
include it in your production setup without worrying about yet another
|
|
312
329
|
dependency.
|
|
313
330
|
|
|
331
|
+
On the other hand, `fake.py`_ factories look quite similar to `factory_boy`_
|
|
332
|
+
factories, although again - drastically simplified and reduced in
|
|
333
|
+
features.
|
|
334
|
+
|
|
335
|
+
The file generation part of `fake.py`_ are modelled after the `faker-file`_.
|
|
336
|
+
You don't get a large variety of file types supported and you don't have that
|
|
337
|
+
much control over the content of the files generated, but you get
|
|
338
|
+
dependency-free valid files and if that's all you need, you don't need to look
|
|
339
|
+
further.
|
|
340
|
+
|
|
341
|
+
However, at any point, if you discover that you "need more", go for `Faker`_,
|
|
342
|
+
`factory_boy`_ and `faker-file`_ combination.
|
|
343
|
+
|
|
314
344
|
License
|
|
315
345
|
=======
|
|
316
346
|
|
|
@@ -4,12 +4,17 @@ fake.py
|
|
|
4
4
|
.. External references
|
|
5
5
|
|
|
6
6
|
.. _Faker: https://faker.readthedocs.io/
|
|
7
|
+
.. _factory_boy: https://factoryboy.readthedocs.io/
|
|
8
|
+
.. _faker-file: https://faker-file.readthedocs.io/
|
|
9
|
+
.. _Pillow: https://python-pillow.org/
|
|
10
|
+
.. _dateutil: https://dateutil.readthedocs.io/
|
|
7
11
|
.. _Django: https://www.djangoproject.com/
|
|
8
12
|
.. _TortoiseORM: https://tortoise.github.io/
|
|
9
13
|
.. _Pydantic: https://docs.pydantic.dev/
|
|
10
14
|
|
|
11
15
|
.. Internal references
|
|
12
16
|
|
|
17
|
+
.. _fake.py: https://github.com/barseghyanartur/fake.py/
|
|
13
18
|
.. _Read the Docs: http://fakepy.readthedocs.io/
|
|
14
19
|
.. _Quick start: https://fakepy.readthedocs.io/en/latest/quick_start.html
|
|
15
20
|
.. _Recipes: https://fakepy.readthedocs.io/en/latest/recipes.html
|
|
@@ -44,12 +49,7 @@ Minimalistic, standalone alternative fake data generator with no dependencies.
|
|
|
44
49
|
:target: https://coveralls.io/github/barseghyanartur/fake.py?branch=main
|
|
45
50
|
:alt: Coverage
|
|
46
51
|
|
|
47
|
-
|
|
48
|
-
:depth: 2
|
|
49
|
-
|
|
50
|
-
Overview
|
|
51
|
-
========
|
|
52
|
-
``fake.py`` is a standalone, portable library designed for generating various
|
|
52
|
+
`fake.py`_ is a standalone, portable library designed for generating various
|
|
53
53
|
random data types for testing.
|
|
54
54
|
|
|
55
55
|
It offers a simplified, dependency-free alternative for creating random
|
|
@@ -210,6 +210,7 @@ This is how you could define a factory for `Django`_'s built-in ``User`` model.
|
|
|
210
210
|
FileSystemStorage,
|
|
211
211
|
SubFactory,
|
|
212
212
|
pre_save,
|
|
213
|
+
trait,
|
|
213
214
|
)
|
|
214
215
|
|
|
215
216
|
STORAGE = FileSystemStorage(root_path=settings.MEDIA_ROOT, rel_path="tmp")
|
|
@@ -230,40 +231,69 @@ This is how you could define a factory for `Django`_'s built-in ``User`` model.
|
|
|
230
231
|
model = User
|
|
231
232
|
get_or_create = ("username",)
|
|
232
233
|
|
|
234
|
+
@trait
|
|
235
|
+
def is_admin_user(self, instance: User) -> None:
|
|
236
|
+
instance.is_superuser = True
|
|
237
|
+
instance.is_staff = True
|
|
238
|
+
instance.is_active = True
|
|
239
|
+
|
|
233
240
|
@pre_save
|
|
234
|
-
def
|
|
241
|
+
def _set_password(self, instance: User) -> None:
|
|
235
242
|
instance.set_password("test")
|
|
236
243
|
|
|
237
244
|
And this is how you could use it:
|
|
238
245
|
|
|
239
246
|
.. code-block:: python
|
|
240
247
|
|
|
248
|
+
# Create just one user
|
|
241
249
|
user = UserFactory()
|
|
250
|
+
|
|
251
|
+
# Create 5 users
|
|
242
252
|
users = UserFactory.create_batch(5)
|
|
243
253
|
|
|
254
|
+
# Create a user using `is_admin_user` trait
|
|
255
|
+
user = UserFactory(is_admin_user=True)
|
|
256
|
+
|
|
244
257
|
Tests
|
|
245
258
|
=====
|
|
246
259
|
|
|
247
260
|
Run the tests with unittest:
|
|
248
261
|
|
|
249
|
-
.. code-block::
|
|
262
|
+
.. code-block:: sh
|
|
250
263
|
|
|
251
|
-
python -m unittest
|
|
264
|
+
python -m unittest fake.py
|
|
252
265
|
|
|
253
266
|
Or pytest:
|
|
254
267
|
|
|
255
|
-
.. code-block::
|
|
268
|
+
.. code-block:: sh
|
|
256
269
|
|
|
257
270
|
pytest
|
|
258
271
|
|
|
259
|
-
Differences with
|
|
260
|
-
|
|
261
|
-
|
|
272
|
+
Differences with alternatives
|
|
273
|
+
=============================
|
|
274
|
+
`fake.py`_ is `Faker`_ + `factory_boy`_ + `faker-file`_ in one package,
|
|
275
|
+
radically simplified and reduced in features, but without any external
|
|
276
|
+
dependencies (not even `Pillow`_ or `dateutil`_).
|
|
277
|
+
|
|
278
|
+
`fake.py`_ is modeled after the famous `Faker`_ package. Its' API is highly
|
|
262
279
|
compatible, although drastically reduced. It's not multilingual and does not
|
|
263
280
|
support postal codes or that many RAW file formats. However, you could easily
|
|
264
281
|
include it in your production setup without worrying about yet another
|
|
265
282
|
dependency.
|
|
266
283
|
|
|
284
|
+
On the other hand, `fake.py`_ factories look quite similar to `factory_boy`_
|
|
285
|
+
factories, although again - drastically simplified and reduced in
|
|
286
|
+
features.
|
|
287
|
+
|
|
288
|
+
The file generation part of `fake.py`_ are modelled after the `faker-file`_.
|
|
289
|
+
You don't get a large variety of file types supported and you don't have that
|
|
290
|
+
much control over the content of the files generated, but you get
|
|
291
|
+
dependency-free valid files and if that's all you need, you don't need to look
|
|
292
|
+
further.
|
|
293
|
+
|
|
294
|
+
However, at any point, if you discover that you "need more", go for `Faker`_,
|
|
295
|
+
`factory_boy`_ and `faker-file`_ combination.
|
|
296
|
+
|
|
267
297
|
License
|
|
268
298
|
=======
|
|
269
299
|
|
|
@@ -4,9 +4,9 @@ Creating images
|
|
|
4
4
|
|
|
5
5
|
.. _faker-file: https://pypi.org/project/faker-file/
|
|
6
6
|
|
|
7
|
-
Creating images could be a challenging
|
|
8
|
-
is to help you out with basic tasks. You can easily generate very
|
|
9
|
-
graphic images, but no custom shapes. Paper size is A4.
|
|
7
|
+
Creating images for testing could be a challenging job. The goal of this
|
|
8
|
+
library is to help you out with basic tasks. You can easily generate very
|
|
9
|
+
basic graphic images, but no custom shapes. Paper size is A4.
|
|
10
10
|
|
|
11
11
|
If you don't like how image files are generated by this library, you can
|
|
12
12
|
check the `faker-file`_ package, which can produce complex images.
|
|
@@ -38,13 +38,17 @@ Django example
|
|
|
38
38
|
FileSystemStorage,
|
|
39
39
|
SubFactory,
|
|
40
40
|
pre_save,
|
|
41
|
+
trait,
|
|
41
42
|
)
|
|
42
43
|
|
|
43
44
|
from article.models import Article
|
|
44
45
|
|
|
46
|
+
# For Django, all files shall be placed inside `MEDIA_ROOT` directory.
|
|
47
|
+
# That's why you need to apply this trick - define a
|
|
48
|
+
# custom `FileSystemStorage` class and pass it to the file factory as
|
|
49
|
+
# `storage` argument.
|
|
45
50
|
STORAGE = FileSystemStorage(root_path=settings.MEDIA_ROOT, rel_path="tmp")
|
|
46
51
|
|
|
47
|
-
|
|
48
52
|
class UserFactory(DjangoModelFactory):
|
|
49
53
|
|
|
50
54
|
username = FACTORY.username()
|
|
@@ -61,11 +65,15 @@ Django example
|
|
|
61
65
|
model = User
|
|
62
66
|
get_or_create = ("username",)
|
|
63
67
|
|
|
64
|
-
@staticmethod
|
|
65
68
|
@pre_save
|
|
66
|
-
def
|
|
69
|
+
def _set_password(self, instance):
|
|
67
70
|
instance.set_password("test")
|
|
68
71
|
|
|
72
|
+
@trait
|
|
73
|
+
def is_admin_user(self, instance: User) -> None:
|
|
74
|
+
instance.is_superuser = True
|
|
75
|
+
instance.is_staff = True
|
|
76
|
+
instance.is_active = True
|
|
69
77
|
|
|
70
78
|
class ArticleFactory(DjangoModelFactory):
|
|
71
79
|
|
|
@@ -81,7 +89,6 @@ Django example
|
|
|
81
89
|
class Meta:
|
|
82
90
|
model = Article
|
|
83
91
|
|
|
84
|
-
|
|
85
92
|
**Usage example**
|
|
86
93
|
|
|
87
94
|
.. code-block:: python
|
|
@@ -95,8 +102,51 @@ Django example
|
|
|
95
102
|
# Create one article with authors username set to admin.
|
|
96
103
|
article = ArticleFactory(author__username="admin")
|
|
97
104
|
|
|
105
|
+
# Using trait
|
|
106
|
+
user = UserFactory(is_admin_user=True)
|
|
107
|
+
|
|
98
108
|
Pydantic example
|
|
99
109
|
----------------
|
|
110
|
+
**article/models.py**
|
|
111
|
+
|
|
112
|
+
.. code-block:: python
|
|
113
|
+
|
|
114
|
+
from datetime import datetime
|
|
115
|
+
from typing import Optional
|
|
116
|
+
|
|
117
|
+
from pydantic import BaseModel, Field
|
|
118
|
+
|
|
119
|
+
class User(BaseModel):
|
|
120
|
+
id: int
|
|
121
|
+
username: str = Field(..., max_length=255)
|
|
122
|
+
first_name: str = Field(..., max_length=255)
|
|
123
|
+
last_name: str = Field(..., max_length=255)
|
|
124
|
+
email: str = Field(..., max_length=255)
|
|
125
|
+
password: Optional[str] = Field("", max_length=255)
|
|
126
|
+
last_login: Optional[datetime]
|
|
127
|
+
is_superuser: bool = Field(default=False)
|
|
128
|
+
is_staff: bool = Field(default=False)
|
|
129
|
+
is_active: bool = Field(default=True)
|
|
130
|
+
date_joined: Optional[datetime]
|
|
131
|
+
|
|
132
|
+
def __str__(self):
|
|
133
|
+
return self.username
|
|
134
|
+
|
|
135
|
+
class Article(BaseModel):
|
|
136
|
+
id: int
|
|
137
|
+
title: str = Field(..., max_length=255)
|
|
138
|
+
slug: str = Field(..., max_length=255, unique=True)
|
|
139
|
+
content: str
|
|
140
|
+
image: Optional[str] = None # Use str to represent the image path or URL
|
|
141
|
+
pub_date: datetime = Field(default_factory=datetime.now)
|
|
142
|
+
safe_for_work: bool = False
|
|
143
|
+
minutes_to_read: int = 5
|
|
144
|
+
author: User
|
|
145
|
+
|
|
146
|
+
def __str__(self):
|
|
147
|
+
return self.title
|
|
148
|
+
|
|
149
|
+
**article/factories.py**
|
|
100
150
|
|
|
101
151
|
.. code-block:: python
|
|
102
152
|
|
|
@@ -126,6 +176,16 @@ Pydantic example
|
|
|
126
176
|
class Meta:
|
|
127
177
|
model = User
|
|
128
178
|
|
|
179
|
+
@trait
|
|
180
|
+
def is_admin_user(self, instance: User) -> None:
|
|
181
|
+
instance.is_superuser = True
|
|
182
|
+
instance.is_staff = True
|
|
183
|
+
instance.is_active = True
|
|
184
|
+
|
|
185
|
+
@pre_save
|
|
186
|
+
def _set_password(self, instance):
|
|
187
|
+
instance.set_password("test")
|
|
188
|
+
|
|
129
189
|
class ArticleFactory(ModelFactory):
|
|
130
190
|
id = FACTORY.pyint()
|
|
131
191
|
title = FACTORY.sentence()
|
|
@@ -145,6 +205,49 @@ Pydantic example
|
|
|
145
205
|
TortoiseORM example
|
|
146
206
|
-------------------
|
|
147
207
|
|
|
208
|
+
**article/models.py**
|
|
209
|
+
|
|
210
|
+
.. code-block:: python
|
|
211
|
+
|
|
212
|
+
from datetime import datetime
|
|
213
|
+
|
|
214
|
+
from tortoise import fields
|
|
215
|
+
from tortoise.models import Model
|
|
216
|
+
|
|
217
|
+
class User(Model):
|
|
218
|
+
|
|
219
|
+
id = fields.IntField(pk=True)
|
|
220
|
+
username = fields.CharField(max_length=255, unique=True)
|
|
221
|
+
first_name = fields.CharField(max_length=255)
|
|
222
|
+
last_name = fields.CharField(max_length=255)
|
|
223
|
+
email = fields.CharField(max_length=255)
|
|
224
|
+
password = fields.CharField(max_length=255, null=True, blank=True)
|
|
225
|
+
last_login = fields.DatetimeField(null=True, blank=True)
|
|
226
|
+
is_superuser = fields.BooleanField(default=False)
|
|
227
|
+
is_staff = fields.BooleanField(default=False)
|
|
228
|
+
is_active = fields.BooleanField(default=True)
|
|
229
|
+
date_joined = fields.DatetimeField(null=True, blank=True)
|
|
230
|
+
|
|
231
|
+
def __str__(self):
|
|
232
|
+
return self.title
|
|
233
|
+
|
|
234
|
+
class Article(Model):
|
|
235
|
+
|
|
236
|
+
id = fields.IntField(pk=True)
|
|
237
|
+
title = fields.CharField(max_length=255)
|
|
238
|
+
slug = fields.CharField(max_length=255, unique=True)
|
|
239
|
+
content = fields.TextField()
|
|
240
|
+
image = fields.TextField(null=True, blank=True)
|
|
241
|
+
pub_date = fields.DatetimeField(default=datetime.now)
|
|
242
|
+
safe_for_work = fields.BooleanField(default=False)
|
|
243
|
+
minutes_to_read = fields.IntField(default=5)
|
|
244
|
+
author = fields.ForeignKeyField("models.User", on_delete=fields.CASCADE)
|
|
245
|
+
|
|
246
|
+
def __str__(self):
|
|
247
|
+
return self.title
|
|
248
|
+
|
|
249
|
+
**article/factories.py**
|
|
250
|
+
|
|
148
251
|
.. code-block:: python
|
|
149
252
|
|
|
150
253
|
from pathlib import Path
|
|
@@ -161,7 +264,6 @@ TortoiseORM example
|
|
|
161
264
|
class UserFactory(TortoiseModelFactory):
|
|
162
265
|
"""User factory."""
|
|
163
266
|
|
|
164
|
-
# id = FACTORY.pyint()
|
|
165
267
|
username = FACTORY.username()
|
|
166
268
|
first_name = FACTORY.first_name()
|
|
167
269
|
last_name = FACTORY.last_name()
|
|
@@ -176,10 +278,19 @@ TortoiseORM example
|
|
|
176
278
|
model = User
|
|
177
279
|
get_or_create = ("username",)
|
|
178
280
|
|
|
281
|
+
@trait
|
|
282
|
+
def is_admin_user(self, instance: User) -> None:
|
|
283
|
+
instance.is_superuser = True
|
|
284
|
+
instance.is_staff = True
|
|
285
|
+
instance.is_active = True
|
|
286
|
+
|
|
287
|
+
@pre_save
|
|
288
|
+
def _set_password(self, instance):
|
|
289
|
+
instance.set_password("test")
|
|
290
|
+
|
|
179
291
|
class ArticleFactory(TortoiseModelFactory):
|
|
180
292
|
"""Article factory."""
|
|
181
293
|
|
|
182
|
-
# id = FACTORY.pyint()
|
|
183
294
|
title = FACTORY.sentence()
|
|
184
295
|
slug = FACTORY.slug()
|
|
185
296
|
content = FACTORY.text()
|
|
@@ -196,6 +307,49 @@ TortoiseORM example
|
|
|
196
307
|
|
|
197
308
|
Dataclasses example
|
|
198
309
|
-------------------
|
|
310
|
+
|
|
311
|
+
**article/models.py**
|
|
312
|
+
|
|
313
|
+
.. code-block:: python
|
|
314
|
+
|
|
315
|
+
from dataclasses import dataclass
|
|
316
|
+
from datetime import datetime
|
|
317
|
+
from typing import Optional
|
|
318
|
+
|
|
319
|
+
@dataclass
|
|
320
|
+
class User:
|
|
321
|
+
id: int
|
|
322
|
+
username: str
|
|
323
|
+
first_name: str
|
|
324
|
+
last_name: str
|
|
325
|
+
email: str
|
|
326
|
+
last_login: Optional[datetime]
|
|
327
|
+
date_joined: Optional[datetime]
|
|
328
|
+
password: Optional[str] = None
|
|
329
|
+
is_superuser: bool = False
|
|
330
|
+
is_staff: bool = False
|
|
331
|
+
is_active: bool = True
|
|
332
|
+
|
|
333
|
+
def __str__(self):
|
|
334
|
+
return self.username
|
|
335
|
+
|
|
336
|
+
@dataclass
|
|
337
|
+
class Article:
|
|
338
|
+
id: int
|
|
339
|
+
title: str
|
|
340
|
+
slug: str
|
|
341
|
+
content: str
|
|
342
|
+
author: User
|
|
343
|
+
image: Optional[str] = None # Use str to represent the image path or URL
|
|
344
|
+
pub_date: datetime = datetime.now()
|
|
345
|
+
safe_for_work: bool = False
|
|
346
|
+
minutes_to_read: int = 5
|
|
347
|
+
|
|
348
|
+
def __str__(self):
|
|
349
|
+
return self.title
|
|
350
|
+
|
|
351
|
+
**article/factories.py**
|
|
352
|
+
|
|
199
353
|
.. code-block:: python
|
|
200
354
|
|
|
201
355
|
from pathlib import Path
|
|
@@ -224,6 +378,16 @@ Dataclasses example
|
|
|
224
378
|
class Meta:
|
|
225
379
|
model = User
|
|
226
380
|
|
|
381
|
+
@trait
|
|
382
|
+
def is_admin_user(self, instance: User) -> None:
|
|
383
|
+
instance.is_superuser = True
|
|
384
|
+
instance.is_staff = True
|
|
385
|
+
instance.is_active = True
|
|
386
|
+
|
|
387
|
+
@pre_save
|
|
388
|
+
def _set_password(self, instance):
|
|
389
|
+
instance.set_password("test")
|
|
390
|
+
|
|
227
391
|
class ArticleFactory(ModelFactory):
|
|
228
392
|
id = FACTORY.pyint()
|
|
229
393
|
title = FACTORY.sentence()
|
|
@@ -237,3 +401,5 @@ Dataclasses example
|
|
|
237
401
|
|
|
238
402
|
class Meta:
|
|
239
403
|
model = Article
|
|
404
|
+
|
|
405
|
+
*Used just like in previous example.*
|
|
@@ -1,6 +1,14 @@
|
|
|
1
1
|
from pathlib import Path
|
|
2
2
|
|
|
3
|
-
from fake import
|
|
3
|
+
from fake import (
|
|
4
|
+
FACTORY,
|
|
5
|
+
FileSystemStorage,
|
|
6
|
+
ModelFactory,
|
|
7
|
+
SubFactory,
|
|
8
|
+
post_save,
|
|
9
|
+
pre_save,
|
|
10
|
+
trait,
|
|
11
|
+
)
|
|
4
12
|
|
|
5
13
|
from article.models import Article, User
|
|
6
14
|
|
|
@@ -31,6 +39,20 @@ class UserFactory(ModelFactory):
|
|
|
31
39
|
class Meta:
|
|
32
40
|
model = User
|
|
33
41
|
|
|
42
|
+
@trait
|
|
43
|
+
def is_admin_user(self, instance: User) -> None:
|
|
44
|
+
instance.is_superuser = True
|
|
45
|
+
instance.is_staff = True
|
|
46
|
+
instance.is_active = True
|
|
47
|
+
|
|
48
|
+
@pre_save
|
|
49
|
+
def _pre_save_method(self, instance):
|
|
50
|
+
instance.pre_save_called = True
|
|
51
|
+
|
|
52
|
+
@post_save
|
|
53
|
+
def _post_save_method(self, instance):
|
|
54
|
+
instance.post_save_called = True
|
|
55
|
+
|
|
34
56
|
|
|
35
57
|
class ArticleFactory(ModelFactory):
|
|
36
58
|
id = FACTORY.pyint()
|
|
@@ -45,3 +67,11 @@ class ArticleFactory(ModelFactory):
|
|
|
45
67
|
|
|
46
68
|
class Meta:
|
|
47
69
|
model = Article
|
|
70
|
+
|
|
71
|
+
@pre_save
|
|
72
|
+
def _pre_save_method(self, instance):
|
|
73
|
+
instance.pre_save_called = True
|
|
74
|
+
|
|
75
|
+
@post_save
|
|
76
|
+
def _post_save_method(self, instance):
|
|
77
|
+
instance.post_save_called = True
|