Skip to content

Commit 418a607

Browse files
added support for testing postgres using docker
1 parent 304c463 commit 418a607

File tree

7 files changed

+141
-6
lines changed

7 files changed

+141
-6
lines changed

docs/example-app.rst

+26
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,29 @@ We encourage you to give it a try with a few commandline calls:
1717
A screenshot of the example app:
1818

1919
.. image:: _static/screenshot.jpg
20+
21+
Postgres
22+
########
23+
24+
You can use Postgres as the source database. To do this, you will need to have `Docker <https://docker.com/>`_ installed.
25+
26+
Initialise the database as follows:
27+
28+
.. code:: bash
29+
30+
$ export DRFDT_TEST_TYPE=postgres
31+
$ export DRFDT_POSTGRESQL_USER=pguser
32+
$ export DRFDT_POSTGRESQL_PASSWORD=pguserpass
33+
$ export DJANGO_SETTINGS_MODULE=example.settings
34+
35+
$ # start a local postgres instance
36+
$ docker-compose -f example/pg/docker-compose.yml up -d
37+
38+
$ python example/manage.py migrate
39+
$ python example/manage.py test
40+
41+
$ # only required if you want to login to the Admin site
42+
$ ./manage.py createsuperuser --username admin --email=email@example.com
43+
44+
$ # shutdown the db (append -v to remove the data volume and delete all data)
45+
$ docker-compose -f example/pg/docker-compose.yml down
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Generated by Django 3.2.5 on 2021-07-20 21:32
2+
3+
from django.db import migrations
4+
5+
6+
class Migration(migrations.Migration):
7+
8+
dependencies = [
9+
('albums', '0001_initial'),
10+
]
11+
12+
operations = [
13+
migrations.AlterModelOptions(
14+
name='album',
15+
options={'ordering': ['name'], 'verbose_name': 'Album', 'verbose_name_plural': 'Albums'},
16+
),
17+
migrations.AlterModelOptions(
18+
name='artist',
19+
options={'ordering': ['name'], 'verbose_name': 'Artist', 'verbose_name_plural': 'Artists'},
20+
),
21+
migrations.AlterModelOptions(
22+
name='genre',
23+
options={'ordering': ['name'], 'verbose_name': 'Genre', 'verbose_name_plural': 'Genres'},
24+
),
25+
]

example/example/settings.py

+18-6
Original file line numberDiff line numberDiff line change
@@ -80,13 +80,25 @@
8080
# Database
8181
# https://docs.djangoproject.com/en/2.0/ref/settings/#databases
8282

83-
DATABASES = {
84-
'default': {
85-
'ENGINE': 'django.db.backends.sqlite3',
86-
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
87-
'TEST': {'NAME': os.path.join(BASE_DIR, 'test.sqlite3')},
83+
if os.environ.get('DRFDT_TEST_TYPE') == 'postgres':
84+
DATABASES = {
85+
'default': {
86+
'ENGINE': 'django.db.backends.postgresql',
87+
'NAME': 'pg',
88+
'USER': os.environ.get('DRFDT_POSTGRESQL_USER'),
89+
'PASSWORD': os.environ.get('DRFDT_POSTGRESQL_PASSWORD'),
90+
'HOST': 'localhost',
91+
'PORT': 5432
92+
}
93+
}
94+
else:
95+
DATABASES = {
96+
'default': {
97+
'ENGINE': 'django.db.backends.sqlite3',
98+
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
99+
'TEST': {'NAME': os.path.join(BASE_DIR, 'test.sqlite3')},
100+
}
88101
}
89-
}
90102

91103

92104
# Password validation

example/pg/README.md

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
## Create Postgres DB
2+
3+
If you want to test with a postgres database, you can do so as follows:
4+
5+
### Pre-requisites
6+
7+
- [Docker](https://docker.com)
8+
- [virtualenvwrapper](https://virtualenvwrapper.readthedocs.io/en/latest/command_ref.html)
9+
10+
### Set env vars
11+
12+
```bash
13+
export DRFDT_TEST_TYPE=postgres
14+
export DRFDT_POSTGRESQL_USER=pguser
15+
export DRFDT_POSTGRESQL_PASSWORD=pguserpass
16+
export DJANGO_SETTINGS_MODULE=example.settings
17+
18+
# start a local postgres instance
19+
docker-compose -f pg/docker-compose.yml up -d
20+
21+
python example/manage.py migrate
22+
python example/manage.py test
23+
24+
# only required if you want to login to the Admin site
25+
./manage.py createsuperuser --username admin --email=email@example.com
26+
```
27+
28+
### Load test data
29+
30+
```bash
31+
python example/manage.py loaddata test_data
32+
```
33+
34+
### Run server
35+
36+
```bash
37+
python example/manage.py runserver
38+
```

example/pg/docker-compose.yml

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
version: '3.3'
2+
services:
3+
db:
4+
container_name: drfdt_pgdb
5+
environment:
6+
DRFDT_TEST_TYPE: 'postgres'
7+
DB_HOST: 'db'
8+
DB_PORT: '5432'
9+
DB_NAME: 'pg'
10+
DRFDT_POSTGRESQL_USER: ${DRFDT_POSTGRESQL_USER}
11+
DRFDT_POSTGRESQL_PASSWORD: ${DRFDT_POSTGRESQL_PASSWORD}
12+
POSTGRES_PASSWORD: ${DRFDT_POSTGRESQL_PASSWORD}
13+
image: postgres:10
14+
restart: "no"
15+
ports:
16+
- 5432:5432
17+
volumes:
18+
- ./docker/db/:/docker-entrypoint-initdb.d/
19+
- local-db-data:/var/lib/postgresql/data
20+
21+
volumes:
22+
local-db-data:
23+
driver: local

example/pg/docker/db/init-user-db.sh

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#!/usr/bin/env bash
2+
set -e
3+
echo "init-user-db.sh"
4+
5+
psql -v ON_ERROR_STOP=1 --username "$POSTGRES_USER" --dbname "$POSTGRES_DB" <<-EOSQL
6+
CREATE USER $DRFDT_POSTGRESQL_USER WITH PASSWORD '$DRFDT_POSTGRESQL_PASSWORD';
7+
CREATE DATABASE $DB_NAME ENCODING 'utf-8';
8+
GRANT ALL PRIVILEGES ON DATABASE $DB_NAME TO $DRFDT_POSTGRESQL_USER;
9+
ALTER USER $DRFDT_POSTGRESQL_USER CREATEDB;
10+
EOSQL

requirements-dev.txt

+1
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@ Django>=2.0
33
djangorestframework>=3.8
44
pycodestyle>=2.3
55
django-filter>=2.4.0
6+
psycopg2-binary==2.8

0 commit comments

Comments
 (0)