Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] add PR for failing test of _create #64

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions requirements-testing.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
django
pytest-django
mock
pytest-pep8
pytest-cov
Expand Down
Empty file added test_django_project/manage.py
Empty file.
Empty file added tests/django_app/__init__.py
Empty file.
6 changes: 6 additions & 0 deletions tests/django_app/models.py
Original file line number Diff line number Diff line change
@@ -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)
2 changes: 2 additions & 0 deletions tests/django_settings.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
INSTALLED_APPS = ('django_app',)
DATABASES = {'default': {'ENGINE': 'django.db.backends.sqlite3', 'NAME': 'django_db', }}
50 changes: 50 additions & 0 deletions tests/test_create_override.py
Original file line number Diff line number Diff line change
@@ -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'
1 change: 1 addition & 0 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ deps = -r{toxinidir}/requirements-testing.txt
[pytest]
addopts = -vv -l --pep8
pep8maxlinelength = 120
DJANGO_SETTINGS_MODULE = django_settings