# HG changeset patch # User Mads Kiilerich # Date 1593041342 -7200 # Node ID 922808a6127471449c3262c98ed6d5f7624e11d3 # Parent 307c876a6e89671f65ca5203c125b563ccc601c4 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.) diff -r 307c876a6e89 -r 922808a61274 docs/contributing.rst --- a/docs/contributing.rst Thu Jun 25 01:51:18 2020 +0200 +++ b/docs/contributing.rst Thu Jun 25 01:29:02 2020 +0200 @@ -92,6 +92,17 @@ 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 diff -r 307c876a6e89 -r 922808a61274 kallithea/tests/conftest.py --- a/kallithea/tests/conftest.py Thu Jun 25 01:51:18 2020 +0200 +++ b/kallithea/tests/conftest.py Thu Jun 25 01:29:02 2020 +0200 @@ -59,8 +59,12 @@ '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 @@ # 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)): diff -r 307c876a6e89 -r 922808a61274 kallithea/tests/fixture.py --- a/kallithea/tests/fixture.py Thu Jun 25 01:51:18 2020 +0200 +++ b/kallithea/tests/fixture.py Thu Jun 25 01:29:02 2020 +0200 @@ -349,7 +349,7 @@ # 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 @@ 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()