By using the new database management module. WARNING: it's currently necessary to edit the code to run the unittests against other database backends. This is done on purpose because running the database unittests involve creating and dropping the database and may result in data loss if pointed to your production database.
Signed-off-by: Cleber Rosa <[email protected]> --- frontend/setup_test_environment.py | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/frontend/setup_test_environment.py b/frontend/setup_test_environment.py index d3a9d7c..d3d4e6b 100644 --- a/frontend/setup_test_environment.py +++ b/frontend/setup_test_environment.py @@ -16,6 +16,24 @@ settings.DATABASES['default']['NAME'] = ':memory:' from django.db import connection from autotest.frontend.afe import readonly_connection +from autotest.installation_support import database_manager + +# PLEASE READ: +# +# Previously the unittests would run only on the memory based SQLite +# database, but now that it can also run on other backends such as MySQL +# there's no easy way to make the test environment decide which one to +# use. +# +# Now *developers* can change the settings.DATABASES configurations (or +# comment them out to use the configuration on global_config.ini) to run +# the unittests on other backends. +# +# WARNING: if you change the following variable, and have a database +# with data, you WILL LOSE IT. Only set the following variable to true and +# run the unittests on a development system and database. +COMPLETELY_DESTROY_THE_DATABASE = False + def run_syncdb(verbosity=0): management.call_command('syncdb', verbosity=verbosity, interactive=False) @@ -28,13 +46,33 @@ def destroy_test_database(): # the real connection ourselves. # Note this depends on Django internals and will likely need to be changed # when we move to Django 1.x. + if ((not settings.DATABASES['default']['ENGINE'].endswith('sqlite')) and + COMPLETELY_DESTROY_THE_DATABASE): + database_name = settings.DATABASES['default']['NAME'] + cur = connection.cursor() + cur.execute('DROP DATABASE IF EXISTS %s' % database_name) + real_connection = connection.connection if real_connection is not None: real_connection.close() connection.connection = None +def setup_database_via_manager(): + ''' + Creates a database manager instance + ''' + engine = settings.DATABASES['default']['ENGINE'] + rdbms_type = database_manager.engine_to_rdbms_type(engine) + manager = database_manager.get_manager_from_config(rdbms_type=rdbms_type) + manager.create_instance() + manager.grant_privileges() + + def set_up(): + if COMPLETELY_DESTROY_THE_DATABASE: + setup_database_via_manager() + run_syncdb() readonly_connection.ReadOnlyConnection.set_globally_disabled(True) -- 1.7.11.7 _______________________________________________ Autotest-kernel mailing list [email protected] https://www.redhat.com/mailman/listinfo/autotest-kernel
