Changeset - 922808a61274
[Not reviewed]
default
0 3 0
Mads Kiilerich (mads) - 6 years ago 2020-06-25 01:29:02
mads@kiilerich.com
tests: introduce REUSE_TEST_DB as an alternative to TEST_DB

With TEST_DB, the specified database would be dropped (if existing) and created
from scratch. That would fail if the user only had been granted access to that
database without general database creation permissions.

With REUSE_TEST_DB, the database must exist, and the tables in the existing
database will be deleted before creating the new ones.

This is for example useful for testing on PostgreSQL without full database
privileges. (With MariaDB/MySQL, it is possible to grant create permissions to
a database before it exists, so it is easier to use TEST_DB.)
3 files changed with 20 insertions and 5 deletions:
0 comments (0 inline, 0 general)
docs/contributing.rst
Show inline comments
 
@@ -92,6 +92,17 @@ Note that on unix systems, the temporary
 
and the test suite creates repositories in the temporary directory. Linux
 
systems with /tmp mounted noexec will thus fail.
 

	
 
Tests can be run on PostgreSQL like::
 

	
 
    sudo -u postgres createuser 'kallithea-test' --pwprompt  # password password
 
    sudo -u postgres createdb 'kallithea-test' --owner 'kallithea-test'
 
    REUSE_TEST_DB='postgresql://kallithea-test:password@localhost/kallithea-test' py.test
 

	
 
Tests can be run on MariaDB/MySQL like::
 

	
 
    echo "GRANT ALL PRIVILEGES ON \`kallithea-test\`.* TO 'kallithea-test'@'localhost' IDENTIFIED BY 'password'" | sudo -u mysql mysql
 
    TEST_DB='mysql://kallithea-test:password@localhost/kallithea-test?charset=utf8' py.test
 

	
 
You can also use ``tox`` to run the tests with all supported Python versions.
 

	
 
When running tests, Kallithea generates a `test.ini` based on template values
kallithea/tests/conftest.py
Show inline comments
 
@@ -59,8 +59,12 @@ def pytest_configure():
 
            'formatter': 'color_formatter_sql',
 
        },
 
    }
 
    if os.environ.get('TEST_DB'):
 
        ini_settings['[app:main]']['sqlalchemy.url'] = os.environ.get('TEST_DB')
 
    create_database = os.environ.get('TEST_DB')  # TODO: rename to 'CREATE_TEST_DB'
 
    if create_database:
 
        ini_settings['[app:main]']['sqlalchemy.url'] = create_database
 
    reuse_database = os.environ.get('REUSE_TEST_DB')
 
    if reuse_database:
 
        ini_settings['[app:main]']['sqlalchemy.url'] = reuse_database
 

	
 
    test_ini_file = os.path.join(TESTS_TMP_PATH, 'test.ini')
 
    inifile.create(test_ini_file, None, ini_settings)
 
@@ -70,7 +74,7 @@ def pytest_configure():
 

	
 
    # set KALLITHEA_NO_TMP_PATH=1 to disable re-creating the database and test repos
 
    if not int(os.environ.get('KALLITHEA_NO_TMP_PATH', 0)):
 
        create_test_env(TESTS_TMP_PATH, context.config())
 
        create_test_env(TESTS_TMP_PATH, context.config(), reuse_database=bool(reuse_database))
 

	
 
    # set KALLITHEA_WHOOSH_TEST_DISABLE=1 to disable whoosh index during tests
 
    if not int(os.environ.get('KALLITHEA_WHOOSH_TEST_DISABLE', 0)):
kallithea/tests/fixture.py
Show inline comments
 
@@ -349,7 +349,7 @@ class Fixture(object):
 
# Global test environment setup
 
#==============================================================================
 

	
 
def create_test_env(repos_test_path, config):
 
def create_test_env(repos_test_path, config, reuse_database):
 
    """
 
    Makes a fresh database and
 
    install test repository into tmp dir
 
@@ -366,7 +366,7 @@ def create_test_env(repos_test_path, con
 

	
 
    dbmanage = DbManage(dbconf=dbconf, root=config['here'],
 
                        tests=True)
 
    dbmanage.create_tables()
 
    dbmanage.create_tables(reuse_database=reuse_database)
 
    # for tests dynamically set new root paths based on generated content
 
    dbmanage.create_settings(dbmanage.prompt_repo_root_path(repos_test_path))
 
    dbmanage.create_default_user()
0 comments (0 inline, 0 general)