showard | 364fe86 | 2008-10-17 02:01:16 +0000 | [diff] [blame] | 1 | import tempfile, shutil, os |
| 2 | from django.core import management |
| 3 | from django.conf import settings |
| 4 | import 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 Truty | 3536b98 | 2011-08-29 13:05:16 -0700 | [diff] [blame] | 10 | getattr(settings, 'DATABASES') |
| 11 | settings.DATABASES['default']['ENGINE'] = ( |
| 12 | 'autotest_lib.frontend.db.backends.afe_sqlite') |
| 13 | settings.DATABASES['default']['NAME'] = ':memory:' |
showard | 364fe86 | 2008-10-17 02:01:16 +0000 | [diff] [blame] | 14 | |
| 15 | from django.db import connection |
showard | f8b1904 | 2009-05-12 17:22:49 +0000 | [diff] [blame] | 16 | from autotest_lib.frontend.afe import readonly_connection |
showard | 364fe86 | 2008-10-17 02:01:16 +0000 | [diff] [blame] | 17 | |
showard | 364fe86 | 2008-10-17 02:01:16 +0000 | [diff] [blame] | 18 | def run_syncdb(verbosity=0): |
showard | a5288b4 | 2009-07-28 20:06:08 +0000 | [diff] [blame] | 19 | management.call_command('syncdb', verbosity=verbosity, interactive=False) |
showard | f8b1904 | 2009-05-12 17:22:49 +0000 | [diff] [blame] | 20 | |
| 21 | |
| 22 | def 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 | |
| 35 | def set_up(): |
| 36 | run_syncdb() |
showard | 844960a | 2009-05-29 18:41:18 +0000 | [diff] [blame] | 37 | readonly_connection.ReadOnlyConnection.set_globally_disabled(True) |
showard | f8b1904 | 2009-05-12 17:22:49 +0000 | [diff] [blame] | 38 | |
| 39 | |
| 40 | def tear_down(): |
showard | 844960a | 2009-05-29 18:41:18 +0000 | [diff] [blame] | 41 | readonly_connection.ReadOnlyConnection.set_globally_disabled(False) |
showard | f8b1904 | 2009-05-12 17:22:49 +0000 | [diff] [blame] | 42 | destroy_test_database() |
showard | 9bb33ec | 2009-08-31 18:31:58 +0000 | [diff] [blame] | 43 | |
| 44 | |
| 45 | def 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' |