Skip to main content

Simple library for creating fake models in the unit tests.

Project description

https://badge.fury.io/py/django-fake-model.png https://travis-ci.org/erm0l0v/django-fake-model.png?branch=master Code Health https://api.codacy.com/project/badge/235f71efbf3144178975bb3eb86964c8 Requirements Status https://codecov.io/github/erm0l0v/django-fake-model/coverage.svg?branch=master

Simple library for creating fake models in the unit tests.

This simple library allows to create fake models in your test without migrations, test apps and test tables in your base. All tables that you need will created/removed during the test.

Install

Install django-fake-model:

pip install django-fake-model

Quickstart

Just create a model in any file (Ex: in your test) and add decorator @YourModel.fake_me to test method or test class.

from django_fake_model import models as f
from django.db import models
from django.test import TestCase, TransactionTestCase


class MyFakeModel(f.FakeModel):

    name = models.CharField(max_length=100)


@MyFakeModel.fake_me
class MyFakeModelTests(TestCase):

    def test_create_model(self):
        MyFakeModel.objects.create(name='123')
        model = MyFakeModel.objects.get(name='123')
        self.assertEqual(model.name, '123')


class MyFakeModelFunctionTest(TestCase):

    @MyFakeModel.fake_me
    def test_create_model(self):
        MyFakeModel.objects.create(name='123')
        model = MyFakeModel.objects.get(name='123')
        self.assertEqual(model.name, '123')


class RelatedModel(f.FakeModel):
    text = models.CharField(max_length=400)


class NyModel(f.FakeModel):
    text = models.CharField(max_length=400)
    related_model = models.ForeignKey(RelatedModel)


@NyModel.fake_me
@RelatedModel.fake_me
class TestRelatedModelsClassDecorator(TransactionTestCase):

    def test_create_models(self):
        related_model = RelatedModel()
        related_model.text = 'qwerty'
        related_model.save()
        my_model = NyModel()
        my_model.test = 'qwerty'
        my_model.related_model = related_model
        my_model.save()
        self.assertIsNotNone(my_model)
        self.assertIsNotNone(related_model)

Development:

To develop on this locally with Docker, install the Docker Engine and Docker Compose. Then you can build the Docker image and run the tests on all tox activities(this also uses a shared pip cache to reduce download times):

docker-compose up -d pg mysql
docker-compose run --rm test

If you wanna run just one Tox activity you can specify that as well:

docker-compose run --rm test tox -e py35-dj19-mysql-unittest

If you add any dependencies or change the tox configuration, you have to rebuild the image:

docker-compose build

It will share this folder with the Docker containers, so that

TODO:

  • fix class decorator @YourModel.fake_me for nose tests

Cookiecutter Tools Used in Making This Package

  • cookiecutter

  • cookiecutter-djangopackage

History

0.1.3 (2015-12-23)

  • Fix issue #1 Multiple Fake Models. Thanks to Saul Shanabrook (@saulshanabrook)

  • Add support for developing with Docker. Thanks to Saul Shanabrook again

0.1.2 (2015-11-13)

  • Added Django 1.9 support

  • Added travis config generator

  • Added nose tests

  • Remove tests for Django master

0.1.1 (2015-09-28)

  • Added tests with different databases.

0.1.0 (2015-09-16)

  • First release on PyPI.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django-fake-model-0.1.3.tar.gz (6.0 kB view details)

Uploaded Source

Built Distribution

django_fake_model-0.1.3-py2.py3-none-any.whl (7.1 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file django-fake-model-0.1.3.tar.gz.

File metadata

File hashes

Hashes for django-fake-model-0.1.3.tar.gz
Algorithm Hash digest
SHA256 af7b82637aad3fa44757edef544ca49c598971254a0a62ce29f9dd4a1e88aeae
MD5 24fb8775c7b905406321ca8e7dc5d69a
BLAKE2b-256 7e36f29a425c4b5f0a3349ed51257d0d63a0b80e28d154f9b90e6d35f19b837e

See more details on using hashes here.

File details

Details for the file django_fake_model-0.1.3-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for django_fake_model-0.1.3-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 ab884353ab3ebd7f962b5c1686cd9056d84ed1f01ba597a1a2b2ac2211aaffd9
MD5 891f978406d6ab70f3296be386261f7f
BLAKE2b-256 f6e79505601257b4d4b5344e57775fcd031a265ca7c8f636453e2adfefdd4dbb

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page