From c28b1ad64e691453a404a399926be1d2d3e8d105 Mon Sep 17 00:00:00 2001
From: Nicolas Trangez <ikke@nicolast.be>
Date: Thu, 27 Sep 2007 13:57:50 +0200
Subject: [PATCH] Add pre_test signal
Added a new signal, pre_test, which is sent when running a project testsuite.
The signal is sent after test database creation, before actually running the
tests.
It can eg. be used to load some test data fixture to prepopulate the test
database, when connecting to it in some application's management.py file.
---
django/db/models/signals.py | 2 ++
django/test/simple.py | 4 +++-
django/test/utils.py | 6 ++++++
3 files changed, 11 insertions(+), 1 deletions(-)
diff --git a/django/db/models/signals.py b/django/db/models/signals.py
index 2171cb1..a8d2090 100644
a
|
b
|
pre_delete = object()
|
10 | 10 | post_delete = object() |
11 | 11 | |
12 | 12 | post_syncdb = object() |
| 13 | |
| 14 | pre_test = object() |
diff --git a/django/test/simple.py b/django/test/simple.py
index 6fa381a..c8668e5 100644
a
|
b
|
from django.db.models import get_app, get_apps
|
4 | 4 | from django.test import _doctest as doctest |
5 | 5 | from django.test.utils import setup_test_environment, teardown_test_environment |
6 | 6 | from django.test.utils import create_test_db, destroy_test_db |
| 7 | from django.test.utils import emit_pre_test_signal |
7 | 8 | from django.test.testcases import OutputChecker, DocTestRunner |
8 | 9 | |
9 | 10 | # The module name for tests outside models.py |
… |
… |
def run_tests(test_labels, verbosity=1, interactive=True, extra_tests=[]):
|
140 | 141 | |
141 | 142 | old_name = settings.DATABASE_NAME |
142 | 143 | create_test_db(verbosity, autoclobber=not interactive) |
| 144 | emit_pre_test_signal(verbosity, interactive) |
143 | 145 | result = unittest.TextTestRunner(verbosity=verbosity).run(suite) |
144 | 146 | destroy_test_db(old_name, verbosity) |
145 | 147 | |
146 | 148 | teardown_test_environment() |
147 | 149 | |
148 | 150 | return len(result.failures) + len(result.errors) |
149 | | |
150 | | No newline at end of file |
| 151 | |
diff --git a/django/test/utils.py b/django/test/utils.py
index 6edd186..5d20983 100644
a
|
b
|
def destroy_test_db(old_database_name, verbosity=1):
|
184 | 184 | time.sleep(1) # To avoid "database is being accessed by other users" errors. |
185 | 185 | cursor.execute("DROP DATABASE %s" % connection.ops.quote_name(TEST_DATABASE_NAME)) |
186 | 186 | connection.close() |
| 187 | |
| 188 | def emit_pre_test_signal(verbosity, interactive): |
| 189 | from django.db import models |
| 190 | from django.dispatch import dispatcher |
| 191 | dispatcher.send(signal=models.signals.pre_test, verbosity=verbosity, |
| 192 | interactive=interactive) |