blob: 0ca2788ee221d9515f5ca338256492d9ca9ed025 [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
15from django.db import connection
showardf8b19042009-05-12 17:22:49 +000016from autotest_lib.frontend.afe import readonly_connection
showard364fe862008-10-17 02:01:16 +000017
showard364fe862008-10-17 02:01:16 +000018def run_syncdb(verbosity=0):
showarda5288b42009-07-28 20:06:08 +000019 management.call_command('syncdb', verbosity=verbosity, interactive=False)
showardf8b19042009-05-12 17:22:49 +000020
21
22def destroy_test_database():
23 connection.close()
24 # Django brilliantly ignores close() requests on in-memory DBs to keep us
25 # naive users from accidentally destroying data. So reach in and close
26 # the real connection ourselves.
27 # Note this depends on Django internals and will likely need to be changed
28 # when we move to Django 1.x.
29 real_connection = connection.connection
30 if real_connection is not None:
31 real_connection.close()
32 connection.connection = None
33
34
35def set_up():
36 run_syncdb()
showard844960a2009-05-29 18:41:18 +000037 readonly_connection.ReadOnlyConnection.set_globally_disabled(True)
showardf8b19042009-05-12 17:22:49 +000038
39
40def tear_down():
showard844960a2009-05-29 18:41:18 +000041 readonly_connection.ReadOnlyConnection.set_globally_disabled(False)
showardf8b19042009-05-12 17:22:49 +000042 destroy_test_database()
showard9bb33ec2009-08-31 18:31:58 +000043
44
45def print_queries():
46 """
47 Print all SQL queries executed so far. Useful for debugging failing tests -
48 you can call it from tearDown(), and then execute the single test case of
49 interest from the command line.
50 """
51 for query in connection.queries:
52 print query['sql'] + ';\n'