aboutsummaryrefslogtreecommitdiff
path: root/frontend/setup_test_environment.py
blob: 4895a5b575cad21b560a66a2be04063682203cde (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
from django.core import management
from django.conf import settings
import common

# we need to set DATABASE_ENGINE now, at import time, before the Django database
# system gets initialized.
# django.conf.settings.LazySettings is buggy and requires us to get something
# from it before we set stuff on it.
getattr(settings, 'DATABASES')
for name in ['default', 'global', 'readonly', 'server']:
    if name not in settings.DATABASES:
        settings.DATABASES[name] = {}
    settings.DATABASES[name]['ENGINE'] = (
            'autotest_lib.frontend.db.backends.afe_sqlite')
    settings.DATABASES[name]['NAME'] = ':memory:'


from django.db import connections
from autotest_lib.frontend.afe import readonly_connection

connection = connections['default']
connection_readonly = connections['readonly']
connection_global = connections['global']
connection_server = connections['server']

def run_syncdb(verbosity=0):
    """Call syncdb command to make sure database schema is uptodate.

    @param verbosity: Level of verbosity of the command, default to 0.
    """
    management.call_command('syncdb', verbosity=verbosity, interactive=False)
    management.call_command('syncdb', verbosity=verbosity, interactive=False,
                             database='readonly')
    management.call_command('syncdb', verbosity=verbosity, interactive=False,
                             database='global')
    management.call_command('syncdb', verbosity=verbosity, interactive=False,
                             database='server')


def destroy_test_database():
    """Close all connection to the test database.
    """
    connection.close()
    connection_readonly.close()
    connection_global.close()
    connection_server.close()
    # Django brilliantly ignores close() requests on in-memory DBs to keep us
    # naive users from accidentally destroying data.  So reach in and close
    # the real connection ourselves.
    # Note this depends on Django internals and will likely need to be changed
    # when we upgrade Django.
    for con in [connection, connection_global, connection_readonly,
                connection_server]:
        real_connection = con.connection
        if real_connection is not None:
            real_connection.close()
            con.connection = None


def set_up():
    """Run setup before test starts.
    """
    run_syncdb()
    readonly_connection.set_globally_disabled(True)


def tear_down():
    """Run cleanup after test is completed.
    """
    readonly_connection.set_globally_disabled(False)
    destroy_test_database()


def print_queries():
    """
    Print all SQL queries executed so far.  Useful for debugging failing tests -
    you can call it from tearDown(), and then execute the single test case of
    interest from the command line.
    """
    for query in connection.queries:
        print query['sql'] + ';\n'