blob: 9fe58f5ed6397ec9ff0a589d02e08cf9198e6262 [file] [log] [blame]
showard364fe862008-10-17 02:01:16 +00001import tempfile, shutil, os
2from django.core import management
3from django.conf import settings
4import common
5
6# we need to set DATABASE_ENGINE now, at import time, before the Django database
7# system gets initialized.
8# django.conf.settings.LazySettings is buggy and requires us to get something
9# from it before we set stuff on it.
Mike Truty3536b982011-08-29 13:05:16 -070010getattr(settings, 'DATABASES')
11settings.DATABASES['default']['ENGINE'] = (
12 'autotest_lib.frontend.db.backends.afe_sqlite')
13settings.DATABASES['default']['NAME'] = ':memory:'
showard364fe862008-10-17 02:01:16 +000014
Jakob Juelich7bef8412014-10-14 19:11:54 -070015settings.DATABASES['readonly'] = {}
16settings.DATABASES['readonly']['ENGINE'] = (
17 'autotest_lib.frontend.db.backends.afe_sqlite')
18settings.DATABASES['readonly']['NAME'] = ':memory:'
19
20from django.db import connections
showardf8b19042009-05-12 17:22:49 +000021from autotest_lib.frontend.afe import readonly_connection
showard364fe862008-10-17 02:01:16 +000022
Jakob Juelich7bef8412014-10-14 19:11:54 -070023connection = connections['default']
24connection_readonly = connections['readonly']
25
showard364fe862008-10-17 02:01:16 +000026def run_syncdb(verbosity=0):
showarda5288b42009-07-28 20:06:08 +000027 management.call_command('syncdb', verbosity=verbosity, interactive=False)
Jakob Juelich7bef8412014-10-14 19:11:54 -070028 management.call_command('syncdb', verbosity=verbosity, interactive=False,
29 database='readonly')
showardf8b19042009-05-12 17:22:49 +000030
31def destroy_test_database():
32 connection.close()
Jakob Juelich7bef8412014-10-14 19:11:54 -070033 connection_readonly.close()
showardf8b19042009-05-12 17:22:49 +000034 # Django brilliantly ignores close() requests on in-memory DBs to keep us
35 # naive users from accidentally destroying data. So reach in and close
36 # the real connection ourselves.
37 # Note this depends on Django internals and will likely need to be changed
Jakob Juelich7bef8412014-10-14 19:11:54 -070038 # when we upgrade Django.
39 for con in [connection, connection_readonly]:
40 real_connection = con.connection
41 if real_connection is not None:
42 real_connection.close()
43 con.connection = None
showardf8b19042009-05-12 17:22:49 +000044
45
46def set_up():
47 run_syncdb()
Jakob Juelich7bef8412014-10-14 19:11:54 -070048 readonly_connection.set_globally_disabled(True)
showardf8b19042009-05-12 17:22:49 +000049
50
51def tear_down():
Jakob Juelich7bef8412014-10-14 19:11:54 -070052 readonly_connection.set_globally_disabled(False)
showardf8b19042009-05-12 17:22:49 +000053 destroy_test_database()
showard9bb33ec2009-08-31 18:31:58 +000054
55
56def print_queries():
57 """
58 Print all SQL queries executed so far. Useful for debugging failing tests -
59 you can call it from tearDown(), and then execute the single test case of
60 interest from the command line.
61 """
62 for query in connection.queries:
63 print query['sql'] + ';\n'