frontend.afe.model_logic: Rename validate_unique
[autotest-zwu.git] / frontend / setup_test_environment.py
blob87e5248321cb5512b00e0b7180f092378a709456
1 import tempfile, shutil, os
2 from django.core import management
3 from django.conf import settings
4 import common
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.
10 getattr(settings, 'DATABASE_ENGINE')
11 settings.DATABASE_ENGINE = 'sqlite3'
12 settings.DATABASE_NAME = ':memory:'
14 from django.db import connection
15 from autotest_lib.frontend.afe import readonly_connection
17 def run_syncdb(verbosity=0):
18 management.call_command('syncdb', verbosity=verbosity, interactive=False)
21 def destroy_test_database():
22 connection.close()
23 # Django brilliantly ignores close() requests on in-memory DBs to keep us
24 # naive users from accidentally destroying data. So reach in and close
25 # the real connection ourselves.
26 # Note this depends on Django internals and will likely need to be changed
27 # when we move to Django 1.x.
28 real_connection = connection.connection
29 if real_connection is not None:
30 real_connection.close()
31 connection.connection = None
34 def set_up():
35 run_syncdb()
36 readonly_connection.ReadOnlyConnection.set_globally_disabled(True)
39 def tear_down():
40 readonly_connection.ReadOnlyConnection.set_globally_disabled(False)
41 destroy_test_database()
44 def print_queries():
45 """
46 Print all SQL queries executed so far. Useful for debugging failing tests -
47 you can call it from tearDown(), and then execute the single test case of
48 interest from the command line.
49 """
50 for query in connection.queries:
51 print query['sql'] + ';\n'