3

The company I work at has a test server which houses all test data. I'm attempting to add some much needed unit tests that reference our Django database on the test server. The problem I'm having is the test database is being created instead of pointing to the database I've provided. I had tried setting the database if test in the system arguments like this:

if 'test' in sys.argv:
    DATABASES = {
        'default': {  # VM Testing
            'ENGINE': 'sql_server.pyodbc',
            'NAME': 'x',
            'USER': 'x',
            'PASSWORD': "x",
            'HOST': 'x.x.x.x',  #
            'PORT': '',
            'OPTIONS': {
                'driver': 'FreeTDS',
                'dsn': 'mssql_staging_1',
                'extra_params': 'TDS_VERSION=8.0',
                'use_legacy_datetime': False
            },
        },
    }

    DEBUG = False
    TEMPLATE_DEBUG = False

And while it makes it into the this if statement, the test database is still created when running python manage.py test. Any advice? And FWIW all my tests to this point are using DRF and its APITestCase class. Thanks!

EDIT:

I tried running python manage.py test -k But Django is still using the default test database. I threw in a set trace to check, no objects were found. The terminal output was:

Using existing test database for alias 'default'...

Here is my updated updated settings:

DATABASES = {
    'default': {
        'ENGINE': 'sql_server.pyodbc',
        'NAME': 'x',
        'USER': 'x',
        'PASSWORD': "x",
        'HOST': 'x.x.x.x', 
        'PORT': '',
        'OPTIONS': {
            'driver': 'FreeTDS',
            'dsn': 'mssql_staging_1',
            'extra_params': 'TDS_VERSION=8.0',
            'use_legacy_datetime': False
        }
    },
    'replica': {
        'ENGINE': 'sql_server.pyodbc',
        'NAME': 'x',
        'USER': 'x',
        'PASSWORD': "x",
        'HOST': 'x.x.x.xreplica', 
        'PORT': '',
        'OPTIONS': {
            'driver': 'FreeTDS',
            'dsn': 'mssql_staging_1',
            'extra_params': 'TDS_VERSION=8.0',
            'use_legacy_datetime': False
        },
        'TEST': {
            'MIRROR': 'default',
        }
    }
}
Obj3ctiv3_C_88
  • 1,478
  • 1
  • 17
  • 29
  • 1
    You could set up a mirror of that existing database as shown in the [documentation here](https://docs.djangoproject.com/en/1.9/topics/testing/advanced/#testing-primary-replica-configurations) – gallen Feb 23 '17 at 15:27
  • 1
    for 1.8+ you can use the `--keepdb` flag http://stackoverflow.com/a/37100979/4724196 – HassenPy Feb 23 '17 at 15:34
  • @Neelik I had tried that as well but that didn't work either. – Obj3ctiv3_C_88 Feb 23 '17 at 15:34

1 Answers1

1

I was finally able to get my test runner working by doing the following:

I moved the test condition from my staging settings file to a testing settings file (called testing.py)

The database setup:

if 'test' in sys.argv:
    # WILL NOT WORK WHEN IF IS MISSING
    DATABASES = {
        'default': {  # VM Testing
            'ENGINE': 'sql_server.pyodbc',
            'NAME': 'x',
            'USER': 'x',
            'PASSWORD': "x",
            'HOST': 'x.x.x.x',  
            'PORT': '',
            'OPTIONS': {
                'driver': 'FreeTDS',
                'dsn': 'mssql_staging_1',
                'extra_params': 'TDS_VERSION=8.0',
                'use_legacy_datetime': False
            },
            'TEST': {
                'MIRROR': 'default',
            }

        },
    }

and ran my test with the following command:

python manage.py test --settings=Project.settings.testing -k
Obj3ctiv3_C_88
  • 1,478
  • 1
  • 17
  • 29