Skip to content

Commit

Permalink
Support optional testing of other databases
Browse files Browse the repository at this point in the history
This change adds support for using the `DATABASE_URL` environment
variable to specify Django's dev/test database, based on the
[dj-database-url](https://github.com/kennethreitz/dj-database-url)
package.

It lets you run tests against e.g. Postgres:

```sh
$ DATABASE_URL=postgres://user@localhost/db ./pytest.sh
```

It adds dj-database-url==0.4.2 to the dev/test requirements as that
matches the current version in cfgov-refresh.

It also excludes the Django settings files from coverage because
otherwise the new logic would reduce the coverage of this repo.
  • Loading branch information
richaagarwal committed Mar 16, 2018
1 parent 4a612f8 commit adb7389
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 10 deletions.
4 changes: 3 additions & 1 deletion docs/mysql.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# Using MySQL

For testing, the default Django sqlite database will be set up for you automatically. If you want to load a MySQL dataset, you can edit `settings_for_testing.py` to uncomment the MySQL database section and install MySQL as follows:
For testing, the default Django sqlite database will be set up for you automatically. If you want to load a MySQL dataset, you can install MySQL as follows and run tests by specifying a DATABASE_URL like so: `DATABASE_URL=mysql://user:password@localhost/db ./pytest.sh`

Installing MySQL:
```shell
pip install requirements/mysql.txt
brew install mysql
Expand Down
1 change: 1 addition & 0 deletions requirements/test.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
-r base.txt

coverage==4.2
dj-database-url==0.4.2
mock==2.0.0
model_mommy==1.2.6
13 changes: 4 additions & 9 deletions settings_for_testing.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import os
import sys

import dj_database_url

BASE_DIR = os.path.abspath(os.path.dirname(__file__))
sys.path.append(os.path.abspath(os.path.join(BASE_DIR, '..')))

Expand Down Expand Up @@ -41,16 +43,9 @@
}
}

# '''if using MySQL and a cfgov-refresh production data load:'''
if 'DATABASE_URL' in os.environ:
DATABASES['default'] = dj_database_url.config()

# DATABASES = {
# 'default': {
# 'ENGINE': 'django.db.backends.mysql',
# 'NAME': 'v1',
# 'USER': 'root',
# 'PASSWORD': '',
# }
# }

LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'UTC'
Expand Down

0 comments on commit adb7389

Please sign in to comment.