Skip to content

Commit

Permalink
fix test database setup
Browse files Browse the repository at this point in the history
Django automatically renames test databases to 'test_%s' % settings.DATABASES[*]["NAME"], so modoboa_test became test_modoboa_test. There's no point creating the database in `before script` as the test suite creates it.

https://docs.djangoproject.com/en/1.11/topics/testing/advanced/#django.db.connection.creation.create_test_db

- target oldest currently supported database servers, travis currently defaults to PostgreSQL 9.2 which isn't supported anymore.

- move database dependencies out of travis.yml into test_requirements.txt and pin to minimum versions recommended by django.
  See https://docs.djangoproject.com/en/1.11/ref/databases/

- use MySQL root user for tests, we already use the root user (postgres) for PostgreSQL.

- fix typo in modoboa/test_settings for DB variable, default should be POSTGRESQL not POSTGRES

- enable MySQL strict mode (on by default on MySQL >= 5.7)
  See https://docs.djangoproject.com/en/1.11/ref/databases/#mysql-sql-mode
  • Loading branch information
Andrew Fyfe committed Jan 4, 2018
1 parent f934163 commit 9923b93
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 23 deletions.
8 changes: 2 additions & 6 deletions .travis.yml
Expand Up @@ -15,6 +15,8 @@ env:
sudo: false sudo: false


addons: addons:
postgresql: "9.3"
mysql: "5.5"
apt: apt:
packages: packages:
- ldap-utils - ldap-utils
Expand All @@ -29,8 +31,6 @@ services:


before_install: before_install:
- pip install codecov - pip install codecov
- if [[ $DB = "POSTGRESQL" ]]; then pip install -q psycopg2; fi
- if [[ $DB = "MYSQL" ]]; then pip install -q mysqlclient; fi


install: install:
- pip install -q -r requirements.txt - pip install -q -r requirements.txt
Expand All @@ -39,10 +39,6 @@ install:
- python setup.py -q develop - python setup.py -q develop


before_script: before_script:
- if [[ $DB = "POSTGRESQL" ]]; then psql -c "CREATE DATABASE modoboa_test;" -U postgres; fi
- if [[ $DB = "MYSQL" ]]; then mysql -e "CREATE DATABASE IF NOT EXISTS modoboa_test;" -uroot; fi
- if [[ $DB = "MYSQL" ]]; then mysql -e "CREATE USER 'modoboa'@'localhost' IDENTIFIED BY 'modoboa'" -uroot; fi
- if [[ $DB = "MYSQL" ]]; then mysql -e "GRANT ALL PRIVILEGES ON * . * TO 'modoboa'@'localhost';" -uroot; fi
- mkdir /tmp/slapd - mkdir /tmp/slapd
- slapd -f test_data/slapd.conf -h ldap://localhost:3389 & - slapd -f test_data/slapd.conf -h ldap://localhost:3389 &
- sleep 3 - sleep 3
Expand Down
33 changes: 16 additions & 17 deletions modoboa/test_settings.py
Expand Up @@ -5,49 +5,48 @@
import os import os


# Database # Database
# https://docs.djangoproject.com/en/1.6/ref/settings/#databases # https://docs.djangoproject.com/en/1.11/ref/settings/#databases


DB = os.environ.get('DB', 'POSTGRES') DB = os.environ.get('DB', 'POSTGRESQL')


if DB == 'MYSQL': if DB == 'MYSQL':
DATABASES = { DATABASES = {

'default': { 'default': {
'ENGINE': 'django.db.backends.mysql', 'ENGINE': 'django.db.backends.mysql',
'NAME': 'modoboa_test', 'NAME': 'modoboa',
'USER': 'modoboa', 'USER': 'root',
'PASSWORD': 'modoboa', 'PASSWORD': '',
'HOST': 'localhost', 'HOST': 'localhost',
'PORT': '', 'PORT': '',
'ATOMIC_REQUESTS': True, 'ATOMIC_REQUESTS': True,

# MySQL's Strict Mode fixes many data integrity problems in MySQL,
# such as data truncation upon insertion, by escalating warnings
# into errors. It is strongly recommended you activate it.
# MySQL >= 5.7 set STRICT_TRANS_TABLES by default
# See: https://docs.djangoproject.com/en/1.11/ref/databases/#mysql-sql-mode
'OPTIONS': {
'init_command': 'SET sql_mode=\'STRICT_TRANS_TABLES\'',
},
}, },

} }
elif DB == 'SQLITE': elif DB == 'SQLITE':
DATABASES = { DATABASES = {

'default': { 'default': {
'ENGINE': 'django.db.backends.sqlite3', 'ENGINE': 'django.db.backends.sqlite3',
'NAME': 'modoboa_test.db', 'NAME': 'modoboa.db',
'PORT': '', 'PORT': '',
'ATOMIC_REQUESTS': True, 'ATOMIC_REQUESTS': True,

}, },

} }
else: else:
DATABASES = { DATABASES = {

'default': { 'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2', 'ENGINE': 'django.db.backends.postgresql',
'NAME': 'modoboa_test', 'NAME': 'modoboa',
'USER': 'postgres', 'USER': 'postgres',
'PASSWORD': '', 'PASSWORD': '',
'HOST': 'localhost', 'HOST': 'localhost',
'PORT': '', 'PORT': '',
'ATOMIC_REQUESTS': True, 'ATOMIC_REQUESTS': True,

}, },

} }
2 changes: 2 additions & 0 deletions test-requirements.txt
Expand Up @@ -3,3 +3,5 @@ mock==2.0.0
httmock==1.2.5 httmock==1.2.5
testfixtures==4.7.0 testfixtures==4.7.0
tox tox
psycopg2>=2.5.4
mysqlclient>=1.3.3

0 comments on commit 9923b93

Please sign in to comment.