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',
}
}
}
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
Related
How to connect SQL Server in django rather than default database? What do I have to write in "settings.py" file?
You actually can install package for mssql:
pip install mssql-django
Then change your settings.py:
DATABASES = {
'default': {
'ENGINE': 'mssql',
'NAME': 'mydb',
'USER': 'user#myserver',
'PASSWORD': 'password',
'HOST': 'myserver.database.windows.net',
'PORT': '',
'OPTIONS': {
'driver': 'ODBC Driver 17 for SQL Server',
},
},
}
And btw you can turn off pyodbc's connection pooling by adding this in your settings:
DATABASE_CONNECTION_POOLING = False
My database and project work great using this database setting:
DATABASES = {
'default': {
'ENGINE': 'django.contrib.gis.db.backends.postgis',
'NAME': 'myprojectdb',
'USER': 'myprojectuser',
'PASSWORD': 'my_secret_password',
'HOST': 'localhost',
'PORT': '5432',
'ATOMIC_REQUESTS': True
},
}
But I want to add a 'readonly' database entry for my readonly db user, like this, so that I can run django-sql-explorer:
DATABASES = {
'default': {
'ENGINE': 'django.contrib.gis.db.backends.postgis',
'NAME': 'myprojectdb',
'USER': 'myprojectuser',
'PASSWORD': 'my_secret_password',
'HOST': 'localhost',
'PORT': '5432',
'ATOMIC_REQUESTS': True
},
'readonly': {
'ENGINE': 'django.contrib.gis.db.backends.postgis',
'NAME': 'myprojectdb',
'USER': 'myprojectreadonly',
'PASSWORD': 'your_eyes_only_secret',
'HOST': 'localhost',
'PORT': '5432',
'ATOMIC_REQUESTS': True
}
}
And now django throws a couple different errors. If I try to do anything with migrations, I get:
django.db.utils.ProgrammingError: permission denied for relation django_migrations
If I try to runserver, I get:
"AUTH_USER_MODEL refers to model '%s' that has not been installed" % settings.AUTH_USER_MODEL
django.core.exceptions.ImproperlyConfigured: AUTH_USER_MODEL refers to model 'users.User' that has not been installed
It seems like when I add the second database setting, django is attempting to use that for migrations, running the server, etc, even though it's not 'default'
If I comment out the second entry, everything works great again.
Any recommendations on how to correct this?
Edit: If I change the settings to use the same username and password, everything works great, so it's not just an issue of django being confused on names. IE:
DATABASES = {
'default': {
'ENGINE': 'django.contrib.gis.db.backends.postgis',
'NAME': 'myprojectdb',
'USER': 'myprojectuser',
'PASSWORD': 'my_secret_password',
'HOST': 'localhost',
'PORT': '5432',
'ATOMIC_REQUESTS': True
},
'readonly': {
'ENGINE': 'django.contrib.gis.db.backends.postgis',
'NAME': 'myprojectdb',
'USER': 'myprojectuser',
'PASSWORD': 'my_secret_password',
'HOST': 'localhost',
'PORT': '5432',
'ATOMIC_REQUESTS': True
}
}
The two configurations shares the same database names 'NAME': 'myprojectdb'
So when you try to migrates dbname seems to conflicts so the applications is having an indecision on which to use The previous or the later
I got thid error:
(2013, 'Lost connection to MySQL server during query')
I have read that I can increase database timeout.
What do I do?
Settings.py:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'c9',
'USER': ddd,
'PASSWORD': '',
'HOST': ddd,
'PORT': '3306',
'ATOMIC_REQUESTS': True
},
'OPTIONS': {
'timeout': 99999999,
'net_read_timeout': 9999999
}
Database object declaration is not correct, OPTIONS should be declared inside default object
#settings.py
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'c9',
'USER': ddd,
'PASSWORD': '',
'HOST': ddd,
'PORT': '3306',
'ATOMIC_REQUESTS': True,
'OPTIONS':{
'timeout': 99999999,
'net_read_timeout': 9999999
},
}
}
I'm testing my code on Windows 10. I have a Django application that needs to call a stored procedure on a remote SQL Server database. Here's the DATABASES snippet from settings.py:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'db1',
'HOST': 'mycompany.com',
'PORT': '3306',
'USER': 'user',
'PASSWORD': 'pw',
},
'ss': {
'ENGINE': 'django_pyodbc',
'NAME': 'db2',
'HOST': 'myserver\SQLEXPRESS',
'USER': 'myuser',
'PASSWORD': 'mypw',
'PORT': '1433',
# 'DRIVER': 'SQL Server',
'OPTIONS': {
'driver_supports_utf8': True,
'host_is_server': True, # must be True for remote db
'autocommit': True,
'unicode_results': True,
'extra_params': 'tds_version=8.0',
},
},
}
Here's a code snippet from my view:
cursor = connections['ss'].cursor()
cursor.execute("{call dbo.mysproc(?)}", (id))
When I execute the cursor.execute statement I get this error:
django.db.utils.DatabaseError: ('The SQL contains 1 parameter markers,
but 36 parameters were supplied', 'HY000')
My parameter, id, is a GUID.
Thoughts?
Here's the fix, simply changed the parentheses surrounding the parameter to square brackets:
cursor.execute("{call dbo.mysproc(?)}", [id])
I found this by trial and error.
(Cross-posted here: https://github.com/jbalogh/django-nose/issues/129)
In settings.py I have two databases listed:
DATABASES = {
'default': {
'ENGINE': 'mysql_pymysql',
'NAME': 'OST_DEV_1',
'USER': 'root',
'PASSWORD': '',
'HOST': 'localhost',
'PORT': '3306',
},
'umc': {
'ENGINE': 'mysql_pymysql',
'NAME': 'UMC',
'USER': 'root',
'PASSWORD': '',
'HOST': 'localhost',
'PORT': '3306',
}
}
If I run the tests without REUSE_DB, they work, but slowly (nearly 2 minutes spent just on creating/destroying the databases):
bash
./manage.py test myapp
But this fails:
REUSE_DB=1 ./manage.py test myapp
DatabaseError: (1146, u"Table 'OST_DEV_1.tblMfg' doesn't exist")
This makes sense, as tblMfg is in the UMC database, not OST_DEV_1. Is there a way to tell django-nose where to find tblMfg? Note that my tests themselves don't reference tblMfg -- I'm sticking to 1+1 == 2 for now.
We currently use 'using' manually for tables that are in UMC:
active_mfgs = Mfg.objects.using('umc').filter(status="ACTIVE")