Skip to content

Commit 254ff24

Browse files
committed
Improve local testing for contributors.
Changes make test to default to SQLite via tests/settings.py's defaults. Changes the GitHub actions configuration to use database connection credentials specific to the toolbar. This makes the local setup and the CI tests more similar. Pulls the database environment configuration out of GitHub actions and into tox. This allows a developer to run tox and test against all locally installed and setup databases. The contributing docs have been updated to contain database shell commands to setup the local environment.
1 parent 0b58338 commit 254ff24

File tree

5 files changed

+55
-26
lines changed

5 files changed

+55
-26
lines changed

.github/workflows/test.yml

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,7 @@ jobs:
1515
mariadb:
1616
image: mariadb:10.3
1717
env:
18-
MYSQL_ROOT_PASSWORD: mysql
19-
MYSQL_DATABASE: mysql
18+
MYSQL_ROOT_PASSWORD: debug_toolbar
2019
options: >-
2120
--health-cmd "mysqladmin ping"
2221
--health-interval 10s
@@ -61,11 +60,10 @@ jobs:
6160
run: tox
6261
env:
6362
DB_BACKEND: mysql
64-
DB_NAME: mysql
6563
DB_USER: root
66-
DB_PASSWORD: mysql
67-
DB_HOST: "127.0.0.1"
68-
DB_PORT: "3306"
64+
DB_PASSWORD: debug_toolbar
65+
DB_HOST: 127.0.0.1
66+
DB_PORT: 3306
6967

7068
- name: Upload coverage
7169
uses: codecov/codecov-action@v1
@@ -84,7 +82,9 @@ jobs:
8482
postgres:
8583
image: 'postgres:9.5'
8684
env:
87-
POSTGRES_PASSWORD: postgres
85+
POSTGRES_DB: debug_toolbar
86+
POSTGRES_USER: debug_toolbar
87+
POSTGRES_PASSWORD: debug_toolbar
8888
ports:
8989
- 5432:5432
9090
options: >-
@@ -129,9 +129,6 @@ jobs:
129129
run: tox
130130
env:
131131
DB_BACKEND: postgresql
132-
DB_NAME: postgres
133-
DB_USER: postgres
134-
DB_PASSWORD: postgres
135132
DB_HOST: localhost
136133
DB_PORT: 5432
137134

docs/contributing.rst

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ Tests
5353

5454
Once you've set up a development environment as explained above, you can run
5555
the test suite for the versions of Django and Python installed in that
56-
environment::
56+
environment using the SQLite database::
5757

5858
$ make test
5959

@@ -79,8 +79,23 @@ or by setting the ``DJANGO_SELENIUM_TESTS`` environment variable::
7979
$ DJANGO_SELENIUM_TESTS=true make coverage
8080
$ DJANGO_SELENIUM_TESTS=true tox
8181

82-
At this time, there isn't an easy way to test against databases other than
83-
SQLite.
82+
To test via `tox` against other databases, you'll need to create the user,
83+
database and assign the proper permissions. For PostgreSQL in a `psql`
84+
shell (note this allows the debug_toolbar user the permission to create
85+
databases)::
86+
87+
psql> CREATE USER debug_toolbar WITH PASSWORD 'debug_toolbar';
88+
psql> ALTER USER debug_toolbar CREATEDB;
89+
psql> CREATE DATABASE debug_toolbar;
90+
psql> GRANT ALL PRIVILEGES ON DATABASE debug_toolbar to debug_toolbar;
91+
92+
For MySQL/MariaDB in a `mysql` shell::
93+
94+
mysql> CREATE DATABASE debug_toolbar;
95+
mysql> CREATE USER 'debug_toolbar'@'localhost' IDENTIFIED BY 'debug_toolbar';
96+
mysql> GRANT ALL PRIVILEGES ON debug_toolbar.* TO 'debug_toolbar'@'localhost';
97+
mysql> GRANT ALL PRIVILEGES ON test_debug_toolbar.* TO 'debug_toolbar'@'localhost';
98+
8499

85100
Style
86101
-----

example/settings.py

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -73,26 +73,23 @@
7373

7474
# To use another database, set the DB_BACKEND environment variable.
7575
if os.environ.get("DB_BACKEND", "").lower() == "postgresql":
76-
# % su postgres
77-
# % createuser debug_toolbar
78-
# % createdb debug_toolbar -O debug_toolbar
76+
# See docs/contributing for instructions on configuring PostgreSQL.
7977
DATABASES = {
8078
"default": {
8179
"ENGINE": "django.db.backends.postgresql",
8280
"NAME": "debug_toolbar",
8381
"USER": "debug_toolbar",
82+
"PASSWORD": "debug_toolbar",
8483
}
8584
}
8685
if os.environ.get("DB_BACKEND", "").lower() == "mysql":
87-
# % mysql
88-
# mysql> CREATE DATABASE debug_toolbar;
89-
# mysql> CREATE USER 'debug_toolbar'@'localhost';
90-
# mysql> GRANT ALL PRIVILEGES ON debug_toolbar.* TO 'debug_toolbar'@'localhost';
86+
# See docs/contributing for instructions on configuring MySQL/MariaDB.
9187
DATABASES = {
9288
"default": {
9389
"ENGINE": "django.db.backends.mysql",
9490
"NAME": "debug_toolbar",
9591
"USER": "debug_toolbar",
92+
"PASSWORD": "debug_toolbar",
9693
}
9794
}
9895

tests/settings.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,8 @@
8181

8282
DATABASES = {
8383
"default": {
84-
"ENGINE": "django.db.backends.%s" % os.getenv("DB_BACKEND"),
85-
"NAME": os.getenv("DB_NAME"),
84+
"ENGINE": "django.db.backends.%s" % os.getenv("DB_BACKEND", "sqlite3"),
85+
"NAME": os.getenv("DB_NAME", ":memory:"),
8686
"USER": os.getenv("DB_USER"),
8787
"PASSWORD": os.getenv("DB_PASSWORD"),
8888
"HOST": os.getenv("DB_HOST", ""),

tox.ini

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,8 @@ envlist =
33
docs
44
style
55
readme
6-
py{36,37,38,39}-dj22-sqlite
7-
py{36,37,38,39}-dj{30,31}-sqlite
8-
py{36,37,38,39}-djmaster-sqlite
9-
py{37,38,39}-dj{22,30,31}-{postgresql,mysql}
6+
py{36,37,38,39}-dj{22,30,31,master}-sqlite
7+
py{36,37,38,39}-dj{22,30,31}-{postgresql,mysql}
108

119
[testenv]
1210
deps =
@@ -35,10 +33,32 @@ setenv =
3533
PYTHONPATH = {toxinidir}
3634
PYTHONWARNINGS = d
3735
py38-dj31-postgresql: DJANGO_SELENIUM_TESTS = true
36+
DB_NAME = {env:DB_NAME:debug_toolbar}
37+
DB_USER = {env:DB_USER:debug_toolbar}
38+
DB_HOST = {env:DB_HOST:localhost}
39+
DB_PASSWORD = {env:DB_PASSWORD:debug_toolbar}
3840
whitelist_externals = make
3941
pip_pre = True
4042
commands = make coverage TEST_ARGS='{posargs:tests}'
4143

44+
[testenv:py{36,37,38,39}-dj{22,30,31}-postgresql]
45+
setenv =
46+
{[testenv]setenv}
47+
DB_BACKEND = postgresql
48+
DB_PORT = {env:DB_PORT:5432}
49+
50+
[testenv:py{36,37,38,39}-dj{22,30,31}-mysql]
51+
setenv =
52+
{[testenv]setenv}
53+
DB_BACKEND = mysql
54+
DB_PORT = {env:DB_PORT:3306}
55+
56+
[testenv:py{36,37,38,39}-dj{22,30,31,master}-sqlite]
57+
setenv =
58+
{[testenv]setenv}
59+
DB_BACKEND = sqlite3
60+
DB_NAME = ":memory:"
61+
4262
[testenv:docs]
4363
commands = make -C {toxinidir}/docs spelling
4464
deps =

0 commit comments

Comments
 (0)