Opened 5 years ago

Closed 5 years ago

#30824 closed Bug (invalid)

Test Cases for Multi-db and unmanged models are failing.

Reported by: seenureddy Owned by: nobody
Component: Testing framework Version: dev
Severity: Normal Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

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?

Change History (2)

comment:1 by seenureddy, 5 years ago

Component: UncategorizedTesting framework

comment:2 by Mariusz Felisiak, 5 years ago

Resolution: invalid
Status: newclosed
Summary: Test Cases for Multi-db and unmanged models - Test case are failingTest Cases for Multi-db and unmanged models are failing.
Type: UncategorizedBug
Version: 2.2master

It seems that disable_constraint_checking() couldn't effectively turn off foreign key constraints (see comment).

Please don't use the ticket system for help with support questions.

Closing per TicketClosingReasons/UseSupportChannels.

Note: See TracTickets for help on using tickets.
Back to Top