﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
30824	Test Cases for Multi-db and unmanged models are failing.	seenureddy	nobody	"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? 

"	Bug	closed	Testing framework	dev	Normal	invalid			Unreviewed	0	0	0	0	0	0
