Skip to content

Instantly share code, notes, and snippets.

@jarshwah
Last active October 13, 2018 20:24
Show Gist options
  • Save jarshwah/8252181 to your computer and use it in GitHub Desktop.
Save jarshwah/8252181 to your computer and use it in GitHub Desktop.
Creating testing databases for django on Postgres 9.3 for OSX (installed with homebrew)
# Note that the "smeatonj" below is the user that installed postgres with homebrew
# first, the "default" database:
$ createuser -U smeatonj djangotest -P
Enter password for new role:
Enter it again:
$ createdb djangotest
$ psql -U smeatonj -d djangotest
psql (9.3.1)
Type "help" for help.
djangotest=# GRANT ALL PRIVILEGES ON DATABASE djangotest TO djangotest;
GRANT
djangotest=# ALTER USER djangotest WITH CREATEDB;
ALTER ROLE
djangotest=# \q
$
# next, create the "other" database:
$ createuser -U smeatonj djangotestother -P
Enter password for new role:
Enter it again:
$ createdb djangotestother
$ psql -U smeatonj -d djangotestother
psql (9.3.1)
Type "help" for help.
djangotestother=# GRANT ALL PRIVILEGES ON DATABASE djangotestother TO djangotestother;
GRANT
djangotestother=# ALTER USER djangotestother WITH CREATEDB;
ALTER ROLE
djangotestother=# \q
$
# now, to run the tests, do the following:
# create the settings module:
cd /path/to/django/tests/
cat > test_pg.py <<EOF
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'djangotest',
'USER': 'djangotest',
'PASSWORD': 'qwerty',
'HOST': 'localhost',
'PORT': '5432',
'OPTIONS': {
'autocommit': True
}
},
'other': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'djangotestother',
'USER': 'djangotestother',
'PASSWORD': 'qwerty',
'HOST': 'localhost',
'PORT': '5432',
'OPTIONS': {
'autocommit': True
}
}
}
SECRET_KEY = "django_tests_secret_key"
# Use a fast hasher to speed up tests.
PASSWORD_HASHERS = (
'django.contrib.auth.hashers.MD5PasswordHasher',
)
EOF
# now run the tests:
$ PYTHONPATH=..:$PYTHONPATH ./runtests.py --settings=test_pg
# this assumes that you've installed all the dependencies as mentioned here: https://docs.djangoproject.com/en/1.6/internals/contributing/writing-code/unit-tests/#running-all-the-tests
# Make sure to use a virtualenv otherwise some dependencies will not install cleanly.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment