[Django] #30824: Test Cases for Multi-db and unmanged models - Test case are failing

5 views
Skip to first unread message

Django

unread,
Oct 1, 2019, 4:24:51 AM10/1/19
to django-...@googlegroups.com
#30824: Test Cases for Multi-db and unmanged models - Test case are failing
-----------------------------------------+------------------------
Reporter: seenureddy | Owner: nobody
Type: Uncategorized | Status: new
Component: Uncategorized | Version: 2.2
Severity: Normal | Keywords:
Triage Stage: Unreviewed | Has patch: 0
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-----------------------------------------+------------------------
I was trying to write the test case for un-managed models. There's no
migrations for these un-managed models. I have explained the my setup in
details in this stack overflow question:
https://stackoverflow.com/questions/58171340/multi-db-and-unmanged-models-
test-case-are-failing

# 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.

Django

unread,
Oct 1, 2019, 4:30:03 AM10/1/19
to django-...@googlegroups.com
#30824: Test Cases for Multi-db and unmanged models - Test case are failing
-----------------------------------+--------------------------------------

Reporter: seenureddy | Owner: nobody
Type: Uncategorized | Status: new
Component: Testing framework | Version: 2.2
Severity: Normal | Resolution:

Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-----------------------------------+--------------------------------------
Changes (by seenureddy):

* component: Uncategorized => Testing framework


--
Ticket URL: <https://code.djangoproject.com/ticket/30824#comment:1>

Django

unread,
Oct 1, 2019, 4:50:00 AM10/1/19
to django-...@googlegroups.com
#30824: Test Cases for Multi-db and unmanged models are failing.
-----------------------------------+--------------------------------------
Reporter: seenureddy | Owner: nobody
Type: Bug | Status: closed
Component: Testing framework | Version: master
Severity: Normal | Resolution: invalid

Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-----------------------------------+--------------------------------------
Changes (by felixxm):

* 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>

Reply all
Reply to author
Forward
0 new messages