mirror of
https://github.com/django/django.git
synced 2025-07-05 18:29:11 +00:00
[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
This commit is contained in:
parent
833b2334bc
commit
b3a2f7f688
@ -212,8 +212,11 @@ class TransactionTestCase(unittest.TestCase):
|
|||||||
named fixtures.
|
named fixtures.
|
||||||
* If the Test Case class has a 'urls' member, replace the
|
* If the Test Case class has a 'urls' member, replace the
|
||||||
ROOT_URLCONF with it.
|
ROOT_URLCONF with it.
|
||||||
|
* If the Test Case class has a 'test_models' member, load the relivent
|
||||||
|
named models.
|
||||||
* Clearing the mail test outbox.
|
* Clearing the mail test outbox.
|
||||||
"""
|
"""
|
||||||
|
self._test_model_setup()
|
||||||
self._fixture_setup()
|
self._fixture_setup()
|
||||||
self._urlconf_setup()
|
self._urlconf_setup()
|
||||||
mail.outbox = []
|
mail.outbox = []
|
||||||
@ -225,6 +228,35 @@ class TransactionTestCase(unittest.TestCase):
|
|||||||
# that we're using *args and **kwargs together.
|
# that we're using *args and **kwargs together.
|
||||||
call_command('loaddata', *self.fixtures, **{'verbosity': 0})
|
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):
|
def _urlconf_setup(self):
|
||||||
if hasattr(self, 'urls'):
|
if hasattr(self, 'urls'):
|
||||||
self._old_root_urlconf = settings.ROOT_URLCONF
|
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.
|
* Putting back the original ROOT_URLCONF if it was changed.
|
||||||
"""
|
"""
|
||||||
|
self._test_model_teardown()
|
||||||
self._fixture_teardown()
|
self._fixture_teardown()
|
||||||
self._urlconf_teardown()
|
self._urlconf_teardown()
|
||||||
|
|
||||||
def _fixture_teardown(self):
|
def _fixture_teardown(self):
|
||||||
pass
|
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):
|
def _urlconf_teardown(self):
|
||||||
if hasattr(self, '_old_root_urlconf'):
|
if hasattr(self, '_old_root_urlconf'):
|
||||||
settings.ROOT_URLCONF = self._old_root_urlconf
|
settings.ROOT_URLCONF = self._old_root_urlconf
|
||||||
|
16
tests/regressiontests/admin_views/test_models.py
Normal file
16
tests/regressiontests/admin_views/test_models.py
Normal file
@ -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)
|
@ -18,6 +18,8 @@ from models import Article, BarAccount, CustomArticle, EmptyModel, \
|
|||||||
Person, Persona, Picture, Podcast, Section, Subscriber, Vodcast, \
|
Person, Persona, Picture, Podcast, Section, Subscriber, Vodcast, \
|
||||||
Language, Collector, Widget, Grommet, DooHickey, FancyDoodad, Whatsit
|
Language, Collector, Widget, Grommet, DooHickey, FancyDoodad, Whatsit
|
||||||
|
|
||||||
|
from test_models import SectionTest
|
||||||
|
|
||||||
try:
|
try:
|
||||||
set
|
set
|
||||||
except NameError:
|
except NameError:
|
||||||
@ -25,16 +27,20 @@ except NameError:
|
|||||||
|
|
||||||
class AdminViewBasicTest(TestCase):
|
class AdminViewBasicTest(TestCase):
|
||||||
fixtures = ['admin-views-users.xml', 'admin-views-colors.xml', 'admin-views-fabrics.xml']
|
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
|
# 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
|
# variable. That way we can test a second AdminSite just by subclassing
|
||||||
# this test case and changing urlbit.
|
# this test case and changing urlbit.
|
||||||
urlbit = 'admin'
|
urlbit = 'admin'
|
||||||
|
|
||||||
def setUp(self):
|
def setUp(self):
|
||||||
|
from django.contrib import admin
|
||||||
|
admin.autodiscover()
|
||||||
self.client.login(username='super', password='secret')
|
self.client.login(username='super', password='secret')
|
||||||
|
|
||||||
def tearDown(self):
|
def tearDown(self):
|
||||||
|
from django.contrib import admin
|
||||||
|
admin.autodiscover()
|
||||||
self.client.logout()
|
self.client.logout()
|
||||||
|
|
||||||
def testTrailingSlashRequired(self):
|
def testTrailingSlashRequired(self):
|
||||||
@ -53,6 +59,13 @@ class AdminViewBasicTest(TestCase):
|
|||||||
response = self.client.get('/test_admin/%s/admin_views/section/add/' % self.urlbit)
|
response = self.client.get('/test_admin/%s/admin_views/section/add/' % self.urlbit)
|
||||||
self.failUnlessEqual(response.status_code, 200)
|
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):
|
def testAddWithGETArgs(self):
|
||||||
response = self.client.get('/test_admin/%s/admin_views/section/add/' % self.urlbit, {'name': 'My Section'})
|
response = self.client.get('/test_admin/%s/admin_views/section/add/' % self.urlbit, {'name': 'My Section'})
|
||||||
self.failUnlessEqual(response.status_code, 200)
|
self.failUnlessEqual(response.status_code, 200)
|
||||||
@ -281,6 +294,13 @@ class CustomModelAdminTest(AdminViewBasicTest):
|
|||||||
response = self.client.get('/test_admin/%s/my_view/' % self.urlbit)
|
response = self.client.get('/test_admin/%s/my_view/' % self.urlbit)
|
||||||
self.assert_(response.content == "Django is a magical pony!", response.content)
|
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):
|
def get_perm(Model, perm):
|
||||||
"""Return the permission object, for the Model"""
|
"""Return the permission object, for the Model"""
|
||||||
ct = ContentType.objects.get_for_model(Model)
|
ct = ContentType.objects.get_for_model(Model)
|
||||||
@ -592,6 +612,17 @@ class AdminViewStringPrimaryKeyTest(TestCase):
|
|||||||
def tearDown(self):
|
def tearDown(self):
|
||||||
self.client.logout()
|
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):
|
def test_get_change_view(self):
|
||||||
"Retrieving the object using urlencoded form of primary key should work"
|
"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))
|
response = self.client.get('/test_admin/admin/admin_views/modelwithstringprimarykey/%s/' % quote(self.pk))
|
||||||
|
@ -1,5 +1,4 @@
|
|||||||
#!/usr/bin/env python
|
#!/usr/bin/env python
|
||||||
|
|
||||||
import os, sys, traceback
|
import os, sys, traceback
|
||||||
import unittest
|
import unittest
|
||||||
import django
|
import django
|
||||||
|
Loading…
x
Reference in New Issue
Block a user