Running tests with unmanaged tables in django

My django app works with tables that are not managed and have the following meanings in my model:

class Meta: managed = False db_table = 'mytable' 

When I run a simple test that imports a person, I get the following:

 (person) bob@sh ~/person/dapi $ > python manage.py test Creating test database for alias 'default'... DatabaseError: (1060, "Duplicate column name 'db_Om_no'") 

Test.py is pretty simple:

 import person.management.commands.dorecall from person.models import Person from django.test import TestCase import pdb class EmailSendTests(TestCase): def test_send_email(self): person = Person.objects.all()[0] Command.send_email() 

I read in django docs where he says: "For tests involving models with managed = False, you need to make sure the tables are created correctly as part of the test setup." Therefore, I understand that my problem is that I did not create the corresponding tables. Can I create a copy of the tables in test_person db created by the test environment?

Each time I run tests, test_person db is destroyed (I think) and reinstalled, so how do I create a copy of the tables in test_person . I think about this right?

Update:

I saw this question on SO and added ManagedModelTestRunner() to utils.py . Although ManagedModelTestRunner() triggered (confirmed when pbd.set_trace() inserted), I still get a Duplicate column name error. I am not getting errors when I do python manage.py syncdb (although this may not mean much, since the tables are already created - it will try to delete the table and restart syncdb to find out if I can get any hints).

+6
source share
1 answer

I had the same problem when I had an unmanaged obsolete database in which the database name set in the meta properties was also configured.

Running tests with a test runner with a managed model that you contacted solved half of my problem, but I still had a problem with Django, not knowing the name custom_db:

 django.db.utils.ProgrammingError: relation "custom_db" does not exist 

The problem was that ./manage.py makemigrations still creates definitions of all models, driven or not, and includes your custom db names in the definition that seem to have exploded the tests. By setting:

 pip install django-test-without-migrations==0.2 

and do the following tests:

 ./manage.py test --nomigrations 

I managed to write tests against my unmanaged model without getting any errors.

+2
source

Source: https://habr.com/ru/post/951145/


All Articles