1from django.core import management
2from django.conf import settings
3import common
4
5# we need to set DATABASE_ENGINE now, at import time, before the Django database
6# system gets initialized.
7# django.conf.settings.LazySettings is buggy and requires us to get something
8# from it before we set stuff on it.
9getattr(settings, 'DATABASES')
10for name in ['default', 'global', 'readonly', 'server']:
11    if name not in settings.DATABASES:
12        settings.DATABASES[name] = {}
13    settings.DATABASES[name]['ENGINE'] = (
14            'autotest_lib.frontend.db.backends.afe_sqlite')
15    settings.DATABASES[name]['NAME'] = ':memory:'
16
17
18from django.db import connections
19from autotest_lib.frontend.afe import readonly_connection
20
21connection = connections['default']
22connection_readonly = connections['readonly']
23connection_global = connections['global']
24connection_server = connections['server']
25
26def run_syncdb(verbosity=0):
27    """Call syncdb command to make sure database schema is uptodate.
28
29    @param verbosity: Level of verbosity of the command, default to 0.
30    """
31    management.call_command('syncdb', verbosity=verbosity, interactive=False)
32    management.call_command('syncdb', verbosity=verbosity, interactive=False,
33                             database='readonly')
34    management.call_command('syncdb', verbosity=verbosity, interactive=False,
35                             database='global')
36    management.call_command('syncdb', verbosity=verbosity, interactive=False,
37                             database='server')
38
39
40def destroy_test_database():
41    """Close all connection to the test database.
42    """
43    connection.close()
44    connection_readonly.close()
45    connection_global.close()
46    connection_server.close()
47    # Django brilliantly ignores close() requests on in-memory DBs to keep us
48    # naive users from accidentally destroying data.  So reach in and close
49    # the real connection ourselves.
50    # Note this depends on Django internals and will likely need to be changed
51    # when we upgrade Django.
52    for con in [connection, connection_global, connection_readonly,
53                connection_server]:
54        real_connection = con.connection
55        if real_connection is not None:
56            real_connection.close()
57            con.connection = None
58
59
60def set_up():
61    """Run setup before test starts.
62    """
63    run_syncdb()
64    readonly_connection.set_globally_disabled(True)
65
66
67def tear_down():
68    """Run cleanup after test is completed.
69    """
70    readonly_connection.set_globally_disabled(False)
71    destroy_test_database()
72
73
74def print_queries():
75    """
76    Print all SQL queries executed so far.  Useful for debugging failing tests -
77    you can call it from tearDown(), and then execute the single test case of
78    interest from the command line.
79    """
80    for query in connection.queries:
81        print query['sql'] + ';\n'
82