blob: 4895a5b575cad21b560a66a2be04063682203cde [file] [log] [blame]
showard364fe862008-10-17 02:01:16 +00001from 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.
Mike Truty3536b982011-08-29 13:05:16 -07009getattr(settings, 'DATABASES')
Dan Shi9a535c92014-11-24 08:52:40 -080010for 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:'
showard364fe862008-10-17 02:01:16 +000016
Jakob Juelich7bef8412014-10-14 19:11:54 -070017
18from django.db import connections
showardf8b19042009-05-12 17:22:49 +000019from autotest_lib.frontend.afe import readonly_connection
showard364fe862008-10-17 02:01:16 +000020
Jakob Juelich7bef8412014-10-14 19:11:54 -070021connection = connections['default']
22connection_readonly = connections['readonly']
Jakob Juelich8a764d12014-10-14 19:24:21 -070023connection_global = connections['global']
Dan Shi9a535c92014-11-24 08:52:40 -080024connection_server = connections['server']
Jakob Juelich7bef8412014-10-14 19:11:54 -070025
showard364fe862008-10-17 02:01:16 +000026def run_syncdb(verbosity=0):
Jakob Juelich8a764d12014-10-14 19:24:21 -070027 """Call syncdb command to make sure database schema is uptodate.
28
29 @param verbosity: Level of verbosity of the command, default to 0.
30 """
showarda5288b42009-07-28 20:06:08 +000031 management.call_command('syncdb', verbosity=verbosity, interactive=False)
Jakob Juelich7bef8412014-10-14 19:11:54 -070032 management.call_command('syncdb', verbosity=verbosity, interactive=False,
33 database='readonly')
Jakob Juelich8a764d12014-10-14 19:24:21 -070034 management.call_command('syncdb', verbosity=verbosity, interactive=False,
35 database='global')
Dan Shi9a535c92014-11-24 08:52:40 -080036 management.call_command('syncdb', verbosity=verbosity, interactive=False,
37 database='server')
Jakob Juelich8a764d12014-10-14 19:24:21 -070038
showardf8b19042009-05-12 17:22:49 +000039
40def destroy_test_database():
Jakob Juelich8a764d12014-10-14 19:24:21 -070041 """Close all connection to the test database.
42 """
showardf8b19042009-05-12 17:22:49 +000043 connection.close()
Jakob Juelich7bef8412014-10-14 19:11:54 -070044 connection_readonly.close()
Jakob Juelich8a764d12014-10-14 19:24:21 -070045 connection_global.close()
Dan Shi9a535c92014-11-24 08:52:40 -080046 connection_server.close()
showardf8b19042009-05-12 17:22:49 +000047 # 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
Jakob Juelich7bef8412014-10-14 19:11:54 -070051 # when we upgrade Django.
Dan Shi9a535c92014-11-24 08:52:40 -080052 for con in [connection, connection_global, connection_readonly,
53 connection_server]:
Jakob Juelich7bef8412014-10-14 19:11:54 -070054 real_connection = con.connection
55 if real_connection is not None:
56 real_connection.close()
57 con.connection = None
showardf8b19042009-05-12 17:22:49 +000058
59
60def set_up():
Jakob Juelich8a764d12014-10-14 19:24:21 -070061 """Run setup before test starts.
62 """
showardf8b19042009-05-12 17:22:49 +000063 run_syncdb()
Jakob Juelich7bef8412014-10-14 19:11:54 -070064 readonly_connection.set_globally_disabled(True)
showardf8b19042009-05-12 17:22:49 +000065
66
67def tear_down():
Jakob Juelich8a764d12014-10-14 19:24:21 -070068 """Run cleanup after test is completed.
69 """
Jakob Juelich7bef8412014-10-14 19:11:54 -070070 readonly_connection.set_globally_disabled(False)
showardf8b19042009-05-12 17:22:49 +000071 destroy_test_database()
showard9bb33ec2009-08-31 18:31:58 +000072
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'