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 | |
Jakob Juelich | 7bef841 | 2014-10-14 19:11:54 -0700 | [diff] [blame] | 15 | settings.DATABASES['readonly'] = {} |
| 16 | settings.DATABASES['readonly']['ENGINE'] = ( |
| 17 | 'autotest_lib.frontend.db.backends.afe_sqlite') |
| 18 | settings.DATABASES['readonly']['NAME'] = ':memory:' |
| 19 | |
| 20 | from django.db import connections |
showard | f8b1904 | 2009-05-12 17:22:49 +0000 | [diff] [blame] | 21 | from autotest_lib.frontend.afe import readonly_connection |
showard | 364fe86 | 2008-10-17 02:01:16 +0000 | [diff] [blame] | 22 | |
Jakob Juelich | 7bef841 | 2014-10-14 19:11:54 -0700 | [diff] [blame] | 23 | connection = connections['default'] |
| 24 | connection_readonly = connections['readonly'] |
| 25 | |
showard | 364fe86 | 2008-10-17 02:01:16 +0000 | [diff] [blame] | 26 | def run_syncdb(verbosity=0): |
showard | a5288b4 | 2009-07-28 20:06:08 +0000 | [diff] [blame] | 27 | management.call_command('syncdb', verbosity=verbosity, interactive=False) |
Jakob Juelich | 7bef841 | 2014-10-14 19:11:54 -0700 | [diff] [blame] | 28 | management.call_command('syncdb', verbosity=verbosity, interactive=False, |
| 29 | database='readonly') |
showard | f8b1904 | 2009-05-12 17:22:49 +0000 | [diff] [blame] | 30 | |
| 31 | def destroy_test_database(): |
| 32 | connection.close() |
Jakob Juelich | 7bef841 | 2014-10-14 19:11:54 -0700 | [diff] [blame] | 33 | connection_readonly.close() |
showard | f8b1904 | 2009-05-12 17:22:49 +0000 | [diff] [blame] | 34 | # 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 Juelich | 7bef841 | 2014-10-14 19:11:54 -0700 | [diff] [blame] | 38 | # 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 |
showard | f8b1904 | 2009-05-12 17:22:49 +0000 | [diff] [blame] | 44 | |
| 45 | |
| 46 | def set_up(): |
| 47 | run_syncdb() |
Jakob Juelich | 7bef841 | 2014-10-14 19:11:54 -0700 | [diff] [blame] | 48 | readonly_connection.set_globally_disabled(True) |
showard | f8b1904 | 2009-05-12 17:22:49 +0000 | [diff] [blame] | 49 | |
| 50 | |
| 51 | def tear_down(): |
Jakob Juelich | 7bef841 | 2014-10-14 19:11:54 -0700 | [diff] [blame] | 52 | readonly_connection.set_globally_disabled(False) |
showard | f8b1904 | 2009-05-12 17:22:49 +0000 | [diff] [blame] | 53 | destroy_test_database() |
showard | 9bb33ec | 2009-08-31 18:31:58 +0000 | [diff] [blame] | 54 | |
| 55 | |
| 56 | def 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' |