diff --git a/requirements-testing.txt b/requirements-testing.txt index 21b44d9..0946eb3 100644 --- a/requirements-testing.txt +++ b/requirements-testing.txt @@ -1,3 +1,5 @@ +django +pytest-django mock pytest-pep8 pytest-cov diff --git a/test_django_project/manage.py b/test_django_project/manage.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/django_app/__init__.py b/tests/django_app/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/django_app/models.py b/tests/django_app/models.py new file mode 100644 index 0000000..0c56748 --- /dev/null +++ b/tests/django_app/models.py @@ -0,0 +1,6 @@ +from django.db import models + + +class Bar(models.Model): + right = models.CharField(max_length=10, null=True) + wrong = models.CharField(max_length=10, null=True) diff --git a/tests/django_settings.py b/tests/django_settings.py new file mode 100644 index 0000000..29b2044 --- /dev/null +++ b/tests/django_settings.py @@ -0,0 +1,2 @@ +INSTALLED_APPS = ('django_app',) +DATABASES = {'default': {'ENGINE': 'django.db.backends.sqlite3', 'NAME': 'django_db', }} diff --git a/tests/test_create_override.py b/tests/test_create_override.py new file mode 100644 index 0000000..b719183 --- /dev/null +++ b/tests/test_create_override.py @@ -0,0 +1,50 @@ +import factory + +from pytest_factoryboy import register + +from .django_app.model import Bar + + +class BaseBarFactory(factory.DjangoModelFactory): + """Base Bar factory.""" + + @classmethod + def _create(cls, model_class, *args, **kwargs): + bar = super(BaseBarFactory, cls)._create(model_class, *args, **kwargs) + bar.wrong = 'spam' + return bar + + class Meta: + model = Bar + + +class BarFactory(BaseBarFactory): + """Bar factory.""" + + @classmethod + def _create(cls, model_class, *args, **kwargs): + bar = super(BaseBarFactory, cls)._create(model_class, *args, **kwargs) + bar.right = 'foo' + return bar + + class Meta: + model = Bar + + +register(BarFactory, 'bar') + + +def test_create_is_called(db, bar, bar_factory): + # FactoryBoy fixture works fine + bar_no_register = BarFactory() + assert getattr(bar_no_register, 'wrong') is None + assert getattr(bar_no_register, 'right') == 'foo' + + # registered factory works fine + registered_bar = bar_factory() + assert getattr(registered_bar, 'wrong') is None + assert getattr(registered_bar, 'right') == 'foo' + + # registered instance works fine + assert getattr(bar, 'wrong') is None + assert getattr(bar, 'right') == 'foo' diff --git a/tox.ini b/tox.ini index 5906a41..17a7c0f 100644 --- a/tox.ini +++ b/tox.ini @@ -12,3 +12,4 @@ deps = -r{toxinidir}/requirements-testing.txt [pytest] addopts = -vv -l --pep8 pep8maxlinelength = 120 +DJANGO_SETTINGS_MODULE = django_settings