# models.py
{{{
class TestModelA(models.Model):
testid = models.CharField(max_length=200)
class Meta:
managed = False
db_table = 'TestD'
}}}
# test.py
{{{
import pytest
from django.db import connection, utils
from apps.models import (
TestD
)
from django.test import RequestFactory, TestCase
from apps.views import (
test_details
)
class TestDetailsTest(TestCase):
def setUp(self):
with connection.schema_editor() as editor:
try:
connection.disable_constraint_checking()
editor.sql_delete_table = "DROP TABLE IF EXISTS TestD;"
editor.delete_model(TestD)
editor.sql_create_table = "CREATE TABLE TestD(testid);"
print("schema", editor.create_model(TestD))
except utils.NotSupportedError:
pass
self.request = RequestFactory().get('/test_details/')
self.get_app_details_mock =
self.setup_mock('apps.get_test_details')
@pytest.mark.django_db
def test_details(self):
"""
Test the app_details for successful
:return: None
"""
response = test_details(self.request)
self.assertEqual(response.status_code, 200)
# calling the test_details method
self.assertTrue(self.get_test_details_mock.called)
}}}
I was able to fix the table issue by using the schema editor. Now I was
getting the different issue.
{{{
======================================================================
ERROR: test_app_details (apps.tests.TestDetailsTest)
----------------------------------------
Traceback (most recent call last):
File "C:\apps\tests\tests.py", line 23, in setUp
with connection.schema_editor() as editor:
File "C:\lib\site-packages\django\db\backends\sqlite3\schema.py", line 24,
in __enter__
'SQLite schema editor cannot be used while foreign key '
django.db.utils.NotSupportedError: SQLite schema editor cannot be used
while foreign key constraint checks are enabled. Make sure to disable them
before entering a transaction.atomic() context because SQLite does not
support disabling them in the middle of a multi-statement transaction.
}}}
How to fix the issue?
--
Ticket URL: <https://code.djangoproject.com/ticket/30824>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
* component: Uncategorized => Testing framework
--
Ticket URL: <https://code.djangoproject.com/ticket/30824#comment:1>
* status: new => closed
* type: Uncategorized => Bug
* version: 2.2 => master
* resolution: => invalid
Comment:
It seems that `disable_constraint_checking()` couldn't effectively turn
off foreign key constraints (see
[https://github.com/django/django/blob/e1c1eaf0c6f4d3d2f60513d20aa9b84b17d096ec/django/db/backends/sqlite3/base.py#L284-L291
comment]).
Please don't use the ticket system
[https://docs.djangoproject.com/en/stable/faq/help/ for help with support
questions].
Closing per TicketClosingReasons/UseSupportChannels.
--
Ticket URL: <https://code.djangoproject.com/ticket/30824#comment:2>