0

I have a Django project that pulls data from legacy database (read only connection) into its own database, and when I run integration tests, it tries to read from test_account on legacy connection.

(1049, "Unknown database 'test_account'")

Is there a way to tell Django to leave the legacy connection alone for reading from the test database?

James Lin
  • 25,028
  • 36
  • 133
  • 233

2 Answers2

0

I actually wrote something that lets you create integration test in djenga (available on pypi) if you want to take a look at how to create a separate integration test framework.

Here is the test runner I use when using the django unit test framework:

from django.test.runner import DiscoverRunner
from django.apps import apps
import sys

class UnManagedModelTestRunner(DiscoverRunner):
    """
    Test runner that uses a legacy database connection for the duration of the test run.
    Many thanks to the Caktus Group: https://www.caktusgroup.com/blog/2013/10/02/skipping-test-db-creation/
    """
    def __init__(self, *args, **kwargs):
        super(UnManagedModelTestRunner, self).__init__(*args, **kwargs)
        self.unmanaged_models = None
        self.test_connection = None
        self.live_connection = None
        self.old_names = None

    def setup_databases(self, **kwargs):
        # override keepdb so that we don't accidentally overwrite our existing legacy database
        self.keepdb = True
        # set the Test DB name to the current DB name, which makes this more of an
        # integration test, but HEY, at least it's a start
        DATABASES['legacy']['TEST'] = { 'NAME': DATABASES['legacy']['NAME'] }
        result = super(UnManagedModelTestRunner, self).setup_databases(**kwargs)

        return result

# Set Django's test runner to the custom class defined above
TEST_RUNNER = 'config.settings.test_settings.UnManagedModelTestRunner'
TEST_NON_SERIALIZED_APPS = [ 'legacy_app' ]
2ps
  • 15,099
  • 2
  • 27
  • 47
0
from django.test import TestCase, override_settings

@override_settings(LOGIN_URL='/other/login/')
class LoginTestCase(TestCase):

    def test_login(self):
        response = self.client.get('/sekrit/')
        self.assertRedirects(response, '/other/login/?next=/sekrit/')

https://docs.djangoproject.com/en/1.10/topics/testing/tools/

You should theoretically be able to use the override settings here and switch to a dif

yrekkehs
  • 727
  • 1
  • 8
  • 15