diff options
author | Jason A. Donenfeld <Jason@zx2c4.com> | 2009-10-19 20:20:09 -0400 |
---|---|---|
committer | Jason A. Donenfeld <Jason@zx2c4.com> | 2009-10-19 20:20:09 -0400 |
commit | 2d6dd2c5ade3f5fad3e2257dce52a6e188fe7535 (patch) | |
tree | da9c93d2f87df6d2b688a455a31e69859117ba1e /google_appengine/lib/django/django/test/testcases.py | |
download | FramedPrototype-2d6dd2c5ade3f5fad3e2257dce52a6e188fe7535.tar.xz FramedPrototype-2d6dd2c5ade3f5fad3e2257dce52a6e188fe7535.zip |
Initial import.
Diffstat (limited to 'google_appengine/lib/django/django/test/testcases.py')
-rwxr-xr-x | google_appengine/lib/django/django/test/testcases.py | 50 |
1 files changed, 50 insertions, 0 deletions
diff --git a/google_appengine/lib/django/django/test/testcases.py b/google_appengine/lib/django/django/test/testcases.py new file mode 100755 index 0000000..2bfb9a7 --- /dev/null +++ b/google_appengine/lib/django/django/test/testcases.py @@ -0,0 +1,50 @@ +import re, doctest, unittest +from django.db import transaction +from django.core import management +from django.db.models import get_apps + +normalize_long_ints = lambda s: re.sub(r'(?<![\w])(\d+)L(?![\w])', '\\1', s) + +class OutputChecker(doctest.OutputChecker): + def check_output(self, want, got, optionflags): + ok = doctest.OutputChecker.check_output(self, want, got, optionflags) + + # Doctest does an exact string comparison of output, which means long + # integers aren't equal to normal integers ("22L" vs. "22"). The + # following code normalizes long integers so that they equal normal + # integers. + if not ok: + return normalize_long_ints(want) == normalize_long_ints(got) + return ok + +class DocTestRunner(doctest.DocTestRunner): + def __init__(self, *args, **kwargs): + doctest.DocTestRunner.__init__(self, *args, **kwargs) + self.optionflags = doctest.ELLIPSIS + + def report_unexpected_exception(self, out, test, example, exc_info): + doctest.DocTestRunner.report_unexpected_exception(self,out,test,example,exc_info) + + # Rollback, in case of database errors. Otherwise they'd have + # side effects on other tests. + from django.db import transaction + transaction.rollback_unless_managed() + +class TestCase(unittest.TestCase): + def install_fixtures(self): + """If the Test Case class has a 'fixtures' member, clear the database and + install the named fixtures at the start of each test. + + """ + management.flush(verbosity=0, interactive=False) + if hasattr(self, 'fixtures'): + management.load_data(self.fixtures, verbosity=0) + + def run(self, result=None): + """Wrapper around default run method so that user-defined Test Cases + automatically call install_fixtures without having to include a call to + super(). + + """ + self.install_fixtures() + super(TestCase, self).run(result) |