Multiple Fake Models
Closed this issue · 6 comments
I have two fake models that looks like this:
from django.db import models
from django_fake_model import models as f
from libs.slugify.fields import SlugifyField
class RelatedModel(f.FakeModel):
text = models.CharField(max_length=400)
class SlugifyModel(f.FakeModel):
text = models.CharField(max_length=400)
related_model = models.ForeignKey(RelatedModel)
slug = SlugifyField(populate_from=('text', 'related_model'))
And I need both of those models for my tests, but when I put the related one above, like this:
from django.test import TestCase
from . import models as slug_models
@slug_models.RelatedModel.fake_me
@slug_models.SlugifyModel.fake_me
class TestSlugify(TestCase):
It fails with
======================================================================
ERROR: test_doesnt_change_on_save (django_fake_model.models.FakeModel._class_extension.<locals>.__wrap_class__)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/app/.heroku/python/lib/python3.5/site-packages/django/db/backends/utils.py", line 64, in execute
return self.cursor.execute(sql, params)
psycopg2.ProgrammingError: relation "django_fake_models_relatedmodel" does not exist
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "/app/.heroku/python/lib/python3.5/site-packages/django/test/testcases.py", line 210, in __call__
self._pre_setup()
File "/app/.heroku/python/lib/python3.5/site-packages/django_fake_model/case_extension.py", line 15, in _pre_setup
self._map_models('create_table')
File "/app/.heroku/python/lib/python3.5/site-packages/django_fake_model/case_extension.py", line 24, in _map_models
getattr(model, method_name)()
File "/app/.heroku/python/lib/python3.5/site-packages/django_fake_model/models.py", line 32, in create_table
schema_editor.create_model(cls)
File "/app/.heroku/python/lib/python3.5/site-packages/django/db/backends/base/schema.py", line 90, in __exit__
self.execute(sql)
File "/app/.heroku/python/lib/python3.5/site-packages/django/db/backends/base/schema.py", line 110, in execute
cursor.execute(sql, params)
File "/app/.heroku/python/lib/python3.5/site-packages/django/db/backends/utils.py", line 64, in execute
return self.cursor.execute(sql, params)
File "/app/.heroku/python/lib/python3.5/site-packages/django/db/utils.py", line 95, in __exit__
six.reraise(dj_exc_type, dj_exc_value, traceback)
File "/app/.heroku/python/lib/python3.5/site-packages/django/utils/six.py", line 685, in reraise
raise value.with_traceback(tb)
File "/app/.heroku/python/lib/python3.5/site-packages/django/db/backends/utils.py", line 64, in execute
return self.cursor.execute(sql, params)
django.db.utils.ProgrammingError: relation "django_fake_models_relatedmodel" does not exist
And when I put the related one below, like this:
It fails with:
======================================================================
ERROR: test_doesnt_change_on_save (django_fake_model.models.FakeModel._class_extension.<locals>.__wrap_class__)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/app/.heroku/python/lib/python3.5/site-packages/django/db/backends/utils.py", line 64, in execute
return self.cursor.execute(sql, params)
psycopg2.OperationalError: cannot DROP TABLE "django_fake_models_slugifymodel" because it has pending trigger events
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "/app/.heroku/python/lib/python3.5/site-packages/django/test/testcases.py", line 217, in __call__
self._post_teardown()
File "/app/.heroku/python/lib/python3.5/site-packages/django_fake_model/case_extension.py", line 18, in _post_teardown
self._map_models('delete_table')
File "/app/.heroku/python/lib/python3.5/site-packages/django_fake_model/case_extension.py", line 24, in _map_models
getattr(model, method_name)()
File "/app/.heroku/python/lib/python3.5/site-packages/django_fake_model/models.py", line 56, in delete_table
schema_editor.delete_model(cls)
File "/app/.heroku/python/lib/python3.5/site-packages/django/db/backends/base/schema.py", line 305, in delete_model
"table": self.quote_name(model._meta.db_table),
File "/app/.heroku/python/lib/python3.5/site-packages/django/db/backends/base/schema.py", line 110, in execute
cursor.execute(sql, params)
File "/app/.heroku/python/lib/python3.5/site-packages/django/db/backends/utils.py", line 64, in execute
return self.cursor.execute(sql, params)
File "/app/.heroku/python/lib/python3.5/site-packages/django/db/utils.py", line 95, in __exit__
six.reraise(dj_exc_type, dj_exc_value, traceback)
File "/app/.heroku/python/lib/python3.5/site-packages/django/utils/six.py", line 685, in reraise
raise value.with_traceback(tb)
File "/app/.heroku/python/lib/python3.5/site-packages/django/db/backends/utils.py", line 64, in execute
return self.cursor.execute(sql, params)
django.db.utils.OperationalError: cannot DROP TABLE "django_fake_models_slugifymodel" because it has pending trigger events
How can I have my tests create with both these models?
@saulshanabrook thank you for issue. I can't repeat your case, however I found that fake_model has problems with related models and test on some real databases.
Could you share your custom field and write versions of python, django and test database that you use?
I am using Python 3.5 with Django 1.9, Postgres, and unittest.
This looks similar to my error:
======================================================================
ERROR: test_create_models (django_fake_model.models.__wrap_class__)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/home/travis/build/erm0l0v/django-fake-model/.tox/py34-dj19-postgres-unittest/lib/python3.4/site-packages/django/db/backends/utils.py", line 64, in execute
return self.cursor.execute(sql, params)
psycopg2.OperationalError: cannot DROP TABLE "django_fake_models_nymodel" because it has pending trigger events
I am working on a solution now
It looks like dropping the table need to be inside of it's own transaction. Django's TestCase
surrounds the test case in an atomic()
block, so these are all in the same transaction.
So instead, I am going to try extending from the TransactionTestCase
. @erm0l0v Would this present any problems to you?