{ "info": { "author": "Mattias Jakobsson", "author_email": "mjakob422@gmail.com", "bugtrack_url": null, "classifiers": [ "Development Status :: 3 - Alpha", "Environment :: Plugins", "Framework :: Django", "Framework :: Pytest", "Intended Audience :: Developers", "License :: OSI Approved :: BSD License", "Programming Language :: Python :: 2", "Programming Language :: Python :: 2.7", "Programming Language :: Python :: 3", "Programming Language :: Python :: 3.4", "Programming Language :: Python :: 3.5", "Programming Language :: Python :: 3.6", "Programming Language :: Python :: 3.7", "Topic :: Software Development :: Testing" ], "description": "[![pipeline status](https://gitlab.com/mjakob422/pytest-django-ifactory/badges/develop/pipeline.svg)](https://gitlab.com/mjakob422/pytest-django-ifactory/commits/develop)\n[![coverage report](https://gitlab.com/mjakob422/pytest-django-ifactory/badges/develop/coverage.svg)](https://gitlab.com/mjakob422/pytest-django-ifactory/commits/develop)\n\n# pytest-django-ifactory\n\nA model instance factory for [pytest-django][].\n\n[pytest-django]: https://pytest-django.readthedocs.io/\n\n## Motivation\n\npytest-django-ifactory makes it easy to create model instances for\nyour test cases even if they contain a lot of non-nullable fields and\ncomplex foreign key relationships. If you every felt like you spent\ntoo much time coming up with dummy values for you models' fields just\nto get an instance into the database to use in a test then\npytest-django-ifactory might be for you.\n\npytest's fixtures are great and perfect if you want to have a static\nmodel instance available in the database for your tests. Problems\narise however when you want to vary one of its fields. To reuse your\nfixture in that case you need to modify the field and save the\ninstance back to the database (assuming your test needs it in the\ndatabase, of course). This results in at least two lines of extra\nsetup code in you test case and an extra call to the database.\n\npytest-django-ifactory is simply an instance factory (hence\n*ifactory*) function that automatically detects your Django models and\ntries to come up with acceptable defaults for the fields you don't\ncare about. This includes generating unique values for fields marked\nas unique and to create related instances for non-nullable foreign\nkeys.\n\n## Usage\n\n**Note that this library is very much in alpha and its API may change\nin future versions.**\n\nThis plugin comes with two fixtures: `ifactory` and\n`transactional_ifactory`. Use them when you need to put model\ninstances in the database. `ifactory` and `transactional_ifactory`\nare identical except that the latter uses [pytest-django][]'s\n`transactional_db` fixture instead of the `db` fixture. See\npytest-django's and Django's documentation for when you would want to\nuse it. Below is a contrived example to test a function that finds\nduplicate names of your users:\n\n```python\nfrom itertools import groupby\nfrom operator import methodcaller\n\nfrom django.contrib.auth.models import User\n\n\ndef get_duplicate_names():\n all_users = User.objects.order_by(\"last_name\", \"first_name\")\n users_by_name = groupby(all_users, methodcaller(\"get_full_name\"))\n return [full_name for full_name, users in users_by_name if len(list(users)) > 1]\n\n\ndef test_get_duplicate_names(ifactory):\n ifactory.auth.user(first_name=\"Albert\", last_name=\"Einstein\")\n ifactory.auth.user(first_name=\"Albert\", last_name=\"Einstein\")\n ifactory.auth.user(first_name=\"Erwin\", last_name=\"Schrodinger\")\n ifactory.auth.user(first_name=\"Richard\", last_name=\"Feynman\")\n assert get_duplicate_names() == [\"Albert Einstein\"]\n assert User.objects.count() == 4\n```\n\nYou find you models namespaced by the application name and the model\nname on the `ifactory` instance. For instance, if you have created a\n`Book` model in a `library` application (and put your library\napplication in INSTALLED_APPS), its factory function will be\n`ifactory.library.book()`. This function accepts the same arguments as\nyour model constructor does and returns the created instance.\n\nNotice in the example above that we create four new users without\nspecifying their unique usernames. The goal of this project is that\nyou should never have to specify the value of a field if you're not\ninterested in that value in your test. Conversely, you should never\nrely on a value you haven't explicity set. This library gives no\nguarantees that the value it chooses for a field will be the same the\nnext time the test is run. It just tries to make sure that the\ninstance can be saved to the database without any integrity errors.\n\nWhile I would recommend against it, if you do want to know which\ndefault value will be used, you can use the\n`pytest_django_ifactory_configure` hook. A good place to put it is in\nyour *conftest.py*:\n\n```python\ndef pytest_django_ifactory_configure(ifactory):\n ifactory.configure_defaults(\"auth.user\", {\n \"first_name\": \"Albert\",\n \"last_name\": \"Einstein\",\n })\n```\n\nFrom now on, all users in your tests will be Albert Einstein unless\nyou say otherwise:\n\n```python\ndef test_albert_by_default(ifactory):\n albert = ifactory.auth.user()\n assert albert.get_full_name() == \"Albert Einstein\"\n not_albert = ifactory.auth.user(first_name=\"Erwin\", last_name=\"Schrodinger\")\n assert not_albert.full_name() == \"Erwin Schrodinger\"\n```\n\nWhile the above example might not be the best idea as your test suit\ngrows the hook can be used to enforce constraints in your models that\nthis library is unaware of, e.g., validation errors raised by your\nmodels that depend on the model's field values.\n\n## Development\n\nThis project uses [black][] to auto-format the code, [flake8][] to\ncheck it, [pytest][] to test it, and finally [check-manifest][] to\ncheck the project's [MANIFEST.in](MANIFEST.in). To facilitate (and\nremember) to actually run all these tools, [pre-commit][] is\nused. Hence, the only hard development requirement is\n*pre-commit*. Install it and run\n\n```console\n$ pre-commit install\n```\n\nonce the first time you check out the repo and from now on all checks\nexcept the unit tests will be run everytime a commit is made. The unit\ntests are run manually with pytest\n\n```console\n$ pytest\n```\n\nGitlab CI is configured to run the tests with Python 2.7 and 3.4-3.7\nand all supported Django versions.\n\nIf you want to install all development requirements to run them\nmanually (and without using `pre-commit run --all-files`), use the\n[requirements.txt](requirements.txt) file:\n\n```console\n$ pip install -r requirements.txt\n```\n\n[black]: https://github.com/ambv/black\n[check-manifest]: https://github.com/mgedmin/check-manifest\n[flake8]: https://gitlab.com/pycqa/flake8\n[pre-commit]: https://github.com/pre-commit/pre-commit\n[pytest]: http://pytest.org/\n\n## License\n\nLike [pytest-django][], pytest-django-ifactory is released under the\n[BSD 3-clause](LICENSE) license.\n\n\n", "description_content_type": "text/markdown", "docs_url": null, "download_url": "", "downloads": { "last_day": -1, "last_month": -1, "last_week": -1 }, "home_page": "https://gitlab.com/mjakob422/pytest-django-ifactory", "keywords": "pytest django database testing", "license": "BSD-3-Clause", "maintainer": "", "maintainer_email": "", "name": "pytest-django-ifactory", "package_url": "https://pypi.org/project/pytest-django-ifactory/", "platform": "", "project_url": "https://pypi.org/project/pytest-django-ifactory/", "project_urls": { "Homepage": "https://gitlab.com/mjakob422/pytest-django-ifactory" }, "release_url": "https://pypi.org/project/pytest-django-ifactory/0.2.1/", "requires_dist": [ "Django", "pytest-django", "six" ], "requires_python": "", "summary": "A model instance factory for pytest-django", "version": "0.2.1" }, "last_serial": 4619676, "releases": { "0.1.0": [ { "comment_text": "", "digests": { "md5": "3207600b06e85b104cd30d904695663d", "sha256": "41e4017f20b6bafdeb35d0a062102e3caa50558c6eda86125d899483a1f16942" }, "downloads": -1, "filename": "pytest_django_ifactory-0.1.0-py2.py3-none-any.whl", "has_sig": false, "md5_digest": "3207600b06e85b104cd30d904695663d", "packagetype": "bdist_wheel", "python_version": "py2.py3", "requires_python": null, "size": 8022, "upload_time": "2018-08-10T09:50:49", "url": "https://files.pythonhosted.org/packages/30/83/4bb56ee836aafb6917ae54582b877f524da7eebea5af5024811670a79b1d/pytest_django_ifactory-0.1.0-py2.py3-none-any.whl" }, { "comment_text": "", "digests": { "md5": "3841a91deee2a26f127f54edcfbcc438", "sha256": "5fa67932f401bac6e1d9b0a20f613d4108eb12f0bdb051c69bebc0d461a09588" }, "downloads": -1, "filename": "pytest-django-ifactory-0.1.0.tar.gz", "has_sig": false, "md5_digest": "3841a91deee2a26f127f54edcfbcc438", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 11857, "upload_time": "2018-08-10T09:50:56", "url": "https://files.pythonhosted.org/packages/02/63/861661176cb09440b6793a7d6967b49757027b450027a4407ba7dbbf8912/pytest-django-ifactory-0.1.0.tar.gz" } ], "0.2.0": [ { "comment_text": "", "digests": { "md5": "adddef666ebe0a4feb8da359e2c7e471", "sha256": "a2f2893d8774b9294927ea81ee6b2e1705c17fbd85e6ec16ab94e9cfd77e4562" }, "downloads": -1, "filename": "pytest_django_ifactory-0.2.0-py2.py3-none-any.whl", "has_sig": false, "md5_digest": "adddef666ebe0a4feb8da359e2c7e471", "packagetype": "bdist_wheel", "python_version": "py2.py3", "requires_python": null, "size": 9378, "upload_time": "2018-10-28T20:00:51", "url": "https://files.pythonhosted.org/packages/16/87/378b052d3e95fe9da8dfcaaeb3d2d0fe08ed75f343cc60dd3c642ec1a298/pytest_django_ifactory-0.2.0-py2.py3-none-any.whl" }, { "comment_text": "", "digests": { "md5": "c1dfddaceb6d1b704ded37867a02e10d", "sha256": "8a921dfb48d4ff3a43ba2e889678f49ef09aaefadb30c4a362a125a8763c3b33" }, "downloads": -1, "filename": "pytest-django-ifactory-0.2.0.tar.gz", "has_sig": false, "md5_digest": "c1dfddaceb6d1b704ded37867a02e10d", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 16126, "upload_time": "2018-10-28T20:00:53", "url": "https://files.pythonhosted.org/packages/2f/99/68df92f48c8215c9cbd34b93dc1c06970f9adea441df8321271419882df1/pytest-django-ifactory-0.2.0.tar.gz" } ], "0.2.1": [ { "comment_text": "", "digests": { "md5": "3f981e994f21697d7590590c4b0edaf2", "sha256": "9a3709659ca146f94af03c6e07a26e8e8c80d5b63caf96f6c9b1d1d4444c8b76" }, "downloads": -1, "filename": "pytest_django_ifactory-0.2.1-py2.py3-none-any.whl", "has_sig": false, "md5_digest": "3f981e994f21697d7590590c4b0edaf2", "packagetype": "bdist_wheel", "python_version": "py2.py3", "requires_python": null, "size": 9433, "upload_time": "2018-12-20T06:55:13", "url": "https://files.pythonhosted.org/packages/83/a2/717210244ed6a00af8b85b21759343c09a823a8428298e33da51a819c08d/pytest_django_ifactory-0.2.1-py2.py3-none-any.whl" }, { "comment_text": "", "digests": { "md5": "c9520e119f0ad5fef7ca111924d9e783", "sha256": "eaa8e39142c3a56775e799318d12cb4a567e17d35a5c7d7edbee8b0ab1b3e326" }, "downloads": -1, "filename": "pytest-django-ifactory-0.2.1.tar.gz", "has_sig": false, "md5_digest": "c9520e119f0ad5fef7ca111924d9e783", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 16223, "upload_time": "2018-12-20T06:55:14", "url": "https://files.pythonhosted.org/packages/3f/5a/820bd4ce24fd2aa3974ddac37790a769d3d18b053c455586de849306f058/pytest-django-ifactory-0.2.1.tar.gz" } ] }, "urls": [ { "comment_text": "", "digests": { "md5": "3f981e994f21697d7590590c4b0edaf2", "sha256": "9a3709659ca146f94af03c6e07a26e8e8c80d5b63caf96f6c9b1d1d4444c8b76" }, "downloads": -1, "filename": "pytest_django_ifactory-0.2.1-py2.py3-none-any.whl", "has_sig": false, "md5_digest": "3f981e994f21697d7590590c4b0edaf2", "packagetype": "bdist_wheel", "python_version": "py2.py3", "requires_python": null, "size": 9433, "upload_time": "2018-12-20T06:55:13", "url": "https://files.pythonhosted.org/packages/83/a2/717210244ed6a00af8b85b21759343c09a823a8428298e33da51a819c08d/pytest_django_ifactory-0.2.1-py2.py3-none-any.whl" }, { "comment_text": "", "digests": { "md5": "c9520e119f0ad5fef7ca111924d9e783", "sha256": "eaa8e39142c3a56775e799318d12cb4a567e17d35a5c7d7edbee8b0ab1b3e326" }, "downloads": -1, "filename": "pytest-django-ifactory-0.2.1.tar.gz", "has_sig": false, "md5_digest": "c9520e119f0ad5fef7ca111924d9e783", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 16223, "upload_time": "2018-12-20T06:55:14", "url": "https://files.pythonhosted.org/packages/3f/5a/820bd4ce24fd2aa3974ddac37790a769d3d18b053c455586de849306f058/pytest-django-ifactory-0.2.1.tar.gz" } ] }