showard | 364fe86 | 2008-10-17 02:01:16 +0000 | [diff] [blame] | 1 | from django.core import management |
| 2 | from django.conf import settings |
| 3 | import 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 Truty | 3536b98 | 2011-08-29 13:05:16 -0700 | [diff] [blame] | 9 | getattr(settings, 'DATABASES') |
Dan Shi | 9a535c9 | 2014-11-24 08:52:40 -0800 | [diff] [blame] | 10 | for 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:' |
showard | 364fe86 | 2008-10-17 02:01:16 +0000 | [diff] [blame] | 16 | |
Jakob Juelich | 7bef841 | 2014-10-14 19:11:54 -0700 | [diff] [blame] | 17 | |
| 18 | from django.db import connections |
showard | f8b1904 | 2009-05-12 17:22:49 +0000 | [diff] [blame] | 19 | from autotest_lib.frontend.afe import readonly_connection |
showard | 364fe86 | 2008-10-17 02:01:16 +0000 | [diff] [blame] | 20 | |
Jakob Juelich | 7bef841 | 2014-10-14 19:11:54 -0700 | [diff] [blame] | 21 | connection = connections['default'] |
| 22 | connection_readonly = connections['readonly'] |
Jakob Juelich | 8a764d1 | 2014-10-14 19:24:21 -0700 | [diff] [blame] | 23 | connection_global = connections['global'] |
Dan Shi | 9a535c9 | 2014-11-24 08:52:40 -0800 | [diff] [blame] | 24 | connection_server = connections['server'] |
Jakob Juelich | 7bef841 | 2014-10-14 19:11:54 -0700 | [diff] [blame] | 25 | |
showard | 364fe86 | 2008-10-17 02:01:16 +0000 | [diff] [blame] | 26 | def run_syncdb(verbosity=0): |
Jakob Juelich | 8a764d1 | 2014-10-14 19:24:21 -0700 | [diff] [blame] | 27 | """Call syncdb command to make sure database schema is uptodate. |
| 28 | |
| 29 | @param verbosity: Level of verbosity of the command, default to 0. |
| 30 | """ |
showard | a5288b4 | 2009-07-28 20:06:08 +0000 | [diff] [blame] | 31 | management.call_command('syncdb', verbosity=verbosity, interactive=False) |
Jakob Juelich | 7bef841 | 2014-10-14 19:11:54 -0700 | [diff] [blame] | 32 | management.call_command('syncdb', verbosity=verbosity, interactive=False, |
| 33 | database='readonly') |
Jakob Juelich | 8a764d1 | 2014-10-14 19:24:21 -0700 | [diff] [blame] | 34 | management.call_command('syncdb', verbosity=verbosity, interactive=False, |
| 35 | database='global') |
Dan Shi | 9a535c9 | 2014-11-24 08:52:40 -0800 | [diff] [blame] | 36 | management.call_command('syncdb', verbosity=verbosity, interactive=False, |
| 37 | database='server') |
Jakob Juelich | 8a764d1 | 2014-10-14 19:24:21 -0700 | [diff] [blame] | 38 | |
showard | f8b1904 | 2009-05-12 17:22:49 +0000 | [diff] [blame] | 39 | |
| 40 | def destroy_test_database(): |
Jakob Juelich | 8a764d1 | 2014-10-14 19:24:21 -0700 | [diff] [blame] | 41 | """Close all connection to the test database. |
| 42 | """ |
showard | f8b1904 | 2009-05-12 17:22:49 +0000 | [diff] [blame] | 43 | connection.close() |
Jakob Juelich | 7bef841 | 2014-10-14 19:11:54 -0700 | [diff] [blame] | 44 | connection_readonly.close() |
Jakob Juelich | 8a764d1 | 2014-10-14 19:24:21 -0700 | [diff] [blame] | 45 | connection_global.close() |
Dan Shi | 9a535c9 | 2014-11-24 08:52:40 -0800 | [diff] [blame] | 46 | connection_server.close() |
showard | f8b1904 | 2009-05-12 17:22:49 +0000 | [diff] [blame] | 47 | # 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 Juelich | 7bef841 | 2014-10-14 19:11:54 -0700 | [diff] [blame] | 51 | # when we upgrade Django. |
Dan Shi | 9a535c9 | 2014-11-24 08:52:40 -0800 | [diff] [blame] | 52 | for con in [connection, connection_global, connection_readonly, |
| 53 | connection_server]: |
Jakob Juelich | 7bef841 | 2014-10-14 19:11:54 -0700 | [diff] [blame] | 54 | real_connection = con.connection |
| 55 | if real_connection is not None: |
| 56 | real_connection.close() |
| 57 | con.connection = None |
showard | f8b1904 | 2009-05-12 17:22:49 +0000 | [diff] [blame] | 58 | |
| 59 | |
| 60 | def set_up(): |
Jakob Juelich | 8a764d1 | 2014-10-14 19:24:21 -0700 | [diff] [blame] | 61 | """Run setup before test starts. |
| 62 | """ |
showard | f8b1904 | 2009-05-12 17:22:49 +0000 | [diff] [blame] | 63 | run_syncdb() |
Jakob Juelich | 7bef841 | 2014-10-14 19:11:54 -0700 | [diff] [blame] | 64 | readonly_connection.set_globally_disabled(True) |
showard | f8b1904 | 2009-05-12 17:22:49 +0000 | [diff] [blame] | 65 | |
| 66 | |
| 67 | def tear_down(): |
Jakob Juelich | 8a764d1 | 2014-10-14 19:24:21 -0700 | [diff] [blame] | 68 | """Run cleanup after test is completed. |
| 69 | """ |
Jakob Juelich | 7bef841 | 2014-10-14 19:11:54 -0700 | [diff] [blame] | 70 | readonly_connection.set_globally_disabled(False) |
showard | f8b1904 | 2009-05-12 17:22:49 +0000 | [diff] [blame] | 71 | destroy_test_database() |
showard | 9bb33ec | 2009-08-31 18:31:58 +0000 | [diff] [blame] | 72 | |
| 73 | |
| 74 | def 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' |