From b3a2f7f68831f1b4e6fa45d65177d04ac5a6b12e Mon Sep 17 00:00:00 2001 From: Kevin Kubasik Date: Wed, 1 Jul 2009 10:39:21 +0000 Subject: [PATCH] [gsoc2009-testing] Add test_models loading code. Not very clean yet, but it works git-svn-id: http://code.djangoproject.com/svn/django/branches/soc2009/test-improvements@11137 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- django/test/testcases.py | 73 +++++++++++++++++++ .../admin_views/test_models.py | 16 ++++ tests/regressiontests/admin_views/tests.py | 35 ++++++++- tests/runtests.py | 1 - 4 files changed, 122 insertions(+), 3 deletions(-) create mode 100644 tests/regressiontests/admin_views/test_models.py diff --git a/django/test/testcases.py b/django/test/testcases.py index 8c73c63796..bda0a35655 100644 --- a/django/test/testcases.py +++ b/django/test/testcases.py @@ -212,8 +212,11 @@ class TransactionTestCase(unittest.TestCase): named fixtures. * If the Test Case class has a 'urls' member, replace the ROOT_URLCONF with it. + * If the Test Case class has a 'test_models' member, load the relivent + named models. * Clearing the mail test outbox. """ + self._test_model_setup() self._fixture_setup() self._urlconf_setup() mail.outbox = [] @@ -225,6 +228,35 @@ class TransactionTestCase(unittest.TestCase): # that we're using *args and **kwargs together. call_command('loaddata', *self.fixtures, **{'verbosity': 0}) + def _test_model_setup(self): + if hasattr(self, 'test_models'): + print self.test_models + if self.__module__.endswith('tests'): + app_label = self.__module__.split('.')[:-1][-1] + else: + app_label = self.__module__.split('.')[:-2][-1] + from django.db.models.loading import cache + from django.utils import importlib + from django.db import models + #importlib.import_module() + cache.write_lock.acquire() + try: + app_mods = cache.app_models[app_label] + for tm in self.test_models: + print "importing %s " % tm + im = importlib.import_module(tm) + #cache.app_store[im] = len(cache.app_store) + print "finding model classes" + mod_classes = [f for f in im.__dict__.values() if hasattr(f,'__bases__') and issubclass(f,models.Model)] + print "Found models %s " % mod_classes + for mc in mod_classes: + print "Adding %s to AppCache" % mc + app_mods[mc.__name__.lower()] = mc + finally: + cache.write_lock.release() + #call_command('syncdb', **{'verbosity': 0}) + + def _urlconf_setup(self): if hasattr(self, 'urls'): self._old_root_urlconf = settings.ROOT_URLCONF @@ -261,12 +293,53 @@ class TransactionTestCase(unittest.TestCase): * Putting back the original ROOT_URLCONF if it was changed. """ + self._test_model_teardown() self._fixture_teardown() self._urlconf_teardown() def _fixture_teardown(self): pass + def _test_model_teardown(self): + if hasattr(self, 'test_models'): + print self.test_models + if self.__module__.endswith('tests'): + app_label = self.__module__.split('.')[:-1][-1] + else: + app_label = self.__module__.split('.')[:-2][-1] + from django.db.models.loading import cache + from django.utils import importlib + from django.db import models + #importlib.import_module() + # cc = cache.get_app(app_label) + # del cache.app_store[cc] + # #del cache.app_models[app_label] + # cache.loaded = False + # print cache.handled + # print '.'.join(cc.__name__.split('.')[:-1]) + # print cc.__package__ + # del cache.handled[cc.__package__] + # cache._populate() + # print cache.get_app(app_label) + #cc = cache.get_app(app_label) + + #reload(cache.get_app(app_label)) + cache.write_lock.acquire() + try: + app_mods = cache.app_models[app_label] + print app_mods + for tm in self.test_models: + print "importing %s " % tm + im = importlib.import_module(tm) + #cache.app_store[im] = len(cache.app_store) + print "finding model classes" + mod_classes = [f for f in im.__dict__.values() if hasattr(f,'__bases__') and issubclass(f,models.Model)] + print "Found models %s " % mod_classes + for mc in mod_classes: + print "Deleting %s from AppCache" % mc + del app_mods[mc.__name__.lower()] + finally: + cache.write_lock.release() def _urlconf_teardown(self): if hasattr(self, '_old_root_urlconf'): settings.ROOT_URLCONF = self._old_root_urlconf diff --git a/tests/regressiontests/admin_views/test_models.py b/tests/regressiontests/admin_views/test_models.py new file mode 100644 index 0000000000..d62e4b2d53 --- /dev/null +++ b/tests/regressiontests/admin_views/test_models.py @@ -0,0 +1,16 @@ +# -*- coding: utf-8 -*- +import tempfile +import os +from django.core.files.storage import FileSystemStorage +from django.db import models +from django.contrib import admin +from django.core.mail import EmailMessage + +class SectionTest(models.Model): + """ + A simple section that links to articles, to test linking to related items + in admin views. + """ + name = models.CharField(max_length=100) + +admin.site.register(SectionTest, save_as=True) \ No newline at end of file diff --git a/tests/regressiontests/admin_views/tests.py b/tests/regressiontests/admin_views/tests.py index ffc1d91fd1..b59c959184 100644 --- a/tests/regressiontests/admin_views/tests.py +++ b/tests/regressiontests/admin_views/tests.py @@ -18,6 +18,8 @@ from models import Article, BarAccount, CustomArticle, EmptyModel, \ Person, Persona, Picture, Podcast, Section, Subscriber, Vodcast, \ Language, Collector, Widget, Grommet, DooHickey, FancyDoodad, Whatsit +from test_models import SectionTest + try: set except NameError: @@ -25,16 +27,20 @@ except NameError: class AdminViewBasicTest(TestCase): fixtures = ['admin-views-users.xml', 'admin-views-colors.xml', 'admin-views-fabrics.xml'] - + test_models = ['regressiontests.admin_views.test_models'] # Store the bit of the URL where the admin is registered as a class # variable. That way we can test a second AdminSite just by subclassing # this test case and changing urlbit. urlbit = 'admin' def setUp(self): + from django.contrib import admin + admin.autodiscover() self.client.login(username='super', password='secret') def tearDown(self): + from django.contrib import admin + admin.autodiscover() self.client.logout() def testTrailingSlashRequired(self): @@ -53,6 +59,13 @@ class AdminViewBasicTest(TestCase): response = self.client.get('/test_admin/%s/admin_views/section/add/' % self.urlbit) self.failUnlessEqual(response.status_code, 200) + def testBasicAddGetTest(self): + """ + A smoke test to ensure GET on the add_view works. + """ + response = self.client.get('/test_admin/%s/admin_views/sectiontest/add/' % self.urlbit) + self.failUnlessEqual(response.status_code, 200) + def testAddWithGETArgs(self): response = self.client.get('/test_admin/%s/admin_views/section/add/' % self.urlbit, {'name': 'My Section'}) self.failUnlessEqual(response.status_code, 200) @@ -280,6 +293,13 @@ class CustomModelAdminTest(AdminViewBasicTest): self.client.login(username='super', password='secret') response = self.client.get('/test_admin/%s/my_view/' % self.urlbit) self.assert_(response.content == "Django is a magical pony!", response.content) + + def testBasicAddGetTest(self): + """ + A smoke test to ensure GET on the add_view works. + """ + response = self.client.get('/test_admin/%s/admin_views/section/add/' % self.urlbit) + self.failUnlessEqual(response.status_code, 200) def get_perm(Model, perm): """Return the permission object, for the Model""" @@ -591,7 +611,18 @@ class AdminViewStringPrimaryKeyTest(TestCase): def tearDown(self): self.client.logout() - + + def testBasicAddGetTest(self): + """ + A smoke test to ensure GET on the add_view works. + """ + from django.contrib import admin + admin.site.unregister(SectionTest) + import regressiontests.admin_views.urls + reload(regressiontests.admin_views.urls) + response = self.client.get('/test_admin/admin/admin_views/sectiontest/add/') + self.failUnlessEqual(response.status_code, 404) + def test_get_change_view(self): "Retrieving the object using urlencoded form of primary key should work" response = self.client.get('/test_admin/admin/admin_views/modelwithstringprimarykey/%s/' % quote(self.pk)) diff --git a/tests/runtests.py b/tests/runtests.py index e73d076277..3b4e7fbeb9 100755 --- a/tests/runtests.py +++ b/tests/runtests.py @@ -1,5 +1,4 @@ #!/usr/bin/env python - import os, sys, traceback import unittest import django