Unit tests shouldn't override template rendering.

Removes the fake_render_to_response function that was preventing
templates from being rendered. This required a number of adjustments
to the mocked obejcts in the view tests (which didn't need to be
mocked to begin with). A couple of legitimate problems were
exposed upon rendering the templates which are also now fixed.

Change-Id: I1647dcec01084318be1bb8f6d6ac1f4308992afa
This commit is contained in:
Gabriel Hurley 2011-11-18 12:38:21 -08:00
parent e1d78e0321
commit 1dec092857
14 changed files with 62 additions and 63 deletions

1
.gitignore vendored
View File

@ -3,6 +3,7 @@
.environment_version
.selenium_log
.coverage
.noseids
coverage.xml
pep8.txt
pylint.txt

View File

@ -37,7 +37,7 @@ CONTAINER_INDEX_URL = reverse('horizon:nova:containers:index')
class ContainerViewTests(test.BaseViewTests):
def setUp(self):
super(ContainerViewTests, self).setUp()
self.container = self.mox.CreateMock(api.Container)
self.container = api.Container(None)
self.container.name = 'containerName'
def test_index(self):
@ -117,7 +117,8 @@ class ObjectViewTests(test.BaseViewTests):
def setUp(self):
super(ObjectViewTests, self).setUp()
swift_object = self.mox.CreateMock(api.SwiftObject)
swift_object = api.SwiftObject(None)
swift_object.name = "test_object"
self.swift_objects = [swift_object]
def test_index(self):
@ -161,10 +162,10 @@ class ObjectViewTests(test.BaseViewTests):
self.mox.ReplayAll()
t = template.loader.get_template('nova/objects/_upload.html')
c = template.Context({"container_name": self.CONTAINER_NAME})
rendered = t.render(c)
self.assertNotEqual(-1, rendered.find('enctype="multipart/form-data"'))
res = self.client.get(reverse('horizon:nova:containers:object_upload',
args=[self.CONTAINER_NAME]))
self.assertContains(res, 'enctype="multipart/form-data"')
res = self.client.post(reverse('horizon:nova:containers:object_upload',
args=[self.CONTAINER_NAME]),

View File

@ -39,13 +39,13 @@ class FloatingIpViewTests(test.BaseViewTests):
def setUp(self):
super(FloatingIpViewTests, self).setUp()
server = self.mox.CreateMock(api.Server)
server = api.Server(None, self.request)
server.id = 1
server.name = 'serverName'
self.server = server
self.servers = (server, )
floating_ip = self.mox.CreateMock(api.FloatingIp)
floating_ip = api.FloatingIp(None)
floating_ip.id = 1
floating_ip.fixed_ip = '10.0.0.4'
floating_ip.instance_id = 1

View File

@ -42,14 +42,16 @@ class ImageViewTests(test.BaseViewTests):
image_dict = {'name': 'visibleImage',
'container_format': 'novaImage'}
self.visibleImage = api.Image(image_dict)
self.visibleImage.id = '1'
image_dict = {'name': 'invisibleImage',
'container_format': 'aki'}
self.invisibleImage = api.Image(image_dict)
self.invisibleImage.id = '2'
self.images = (self.visibleImage, self.invisibleImage)
flavor = self.mox.CreateMock(api.Flavor)
flavor = api.Flavor(None)
flavor.id = 1
flavor.name = 'm1.massive'
flavor.vcpus = 1000
@ -57,11 +59,11 @@ class ImageViewTests(test.BaseViewTests):
flavor.ram = 10000
self.flavors = (flavor,)
keypair = self.mox.CreateMock(api.KeyPair)
keypair = api.KeyPair(None)
keypair.name = 'keyName'
self.keypairs = (keypair,)
security_group = self.mox.CreateMock(api.SecurityGroup)
security_group = api.SecurityGroup(None)
security_group.name = 'default'
self.security_groups = (security_group,)

View File

@ -33,7 +33,7 @@ from horizon import test
class InstanceViewTests(test.BaseViewTests):
def setUp(self):
super(InstanceViewTests, self).setUp()
server = self.mox.CreateMock(api.Server)
server = api.Server(None, self.request)
server.id = 1
server.name = 'serverName'
self.servers = (server,)

View File

@ -31,7 +31,7 @@ from horizon import test
class KeyPairViewTests(test.BaseViewTests):
def setUp(self):
super(KeyPairViewTests, self).setUp()
keypair = self.mox.CreateMock(api.KeyPair)
keypair = api.KeyPair(None)
keypair.name = 'keyName'
self.keypairs = (keypair,)

View File

@ -39,7 +39,8 @@ class SecurityGroupsViewTests(test.BaseViewTests):
def setUp(self):
super(SecurityGroupsViewTests, self).setUp()
security_group = self.mox.CreateMock(api.SecurityGroup)
security_group = api.SecurityGroup(None)
security_group.id = '1'
security_group.name = 'default'
self.security_groups = (security_group,)

View File

@ -36,13 +36,13 @@ class SnapshotsViewTests(test.BaseViewTests):
'container_format': 'novaImage'}
self.images = [image_dict]
server = self.mox.CreateMock(api.Server)
server = api.Server(None, self.request)
server.id = 1
server.status = 'ACTIVE'
server.name = 'sgoody'
self.good_server = server
server = self.mox.CreateMock(api.Server)
server = api.Server(None, self.request)
server.id = 2
server.status = 'BUILD'
server.name = 'baddy'

View File

@ -1,7 +1,5 @@
{% load i18n %}
{% load i18n parse_date launch_form %}
{% load parse_date %}
{% load launch_form %}
<table id="images" class="zebra-striped">
<tr>
<th>{%trans "ID"%}</th>

View File

@ -33,7 +33,7 @@ class UsersViewTests(test.BaseAdminViewTests):
def setUp(self):
super(UsersViewTests, self).setUp()
self.user = self.mox.CreateMock(api.User)
self.user = api.User(None)
self.user.enabled = True
self.user.id = self.TEST_USER
self.user.roles = self.TEST_ROLES

View File

@ -25,6 +25,7 @@ Template tags for dynamic creation of image launch form.
from __future__ import absolute_import
from django import template
from openstackx import exceptions as api_exceptions
from horizon import api
from horizon import forms

View File

@ -31,6 +31,8 @@ register = template.Library()
def _parse_datetime(dtstr):
if not dtstr:
return "None"
fmts = ["%Y-%m-%dT%H:%M:%S.%f", "%Y-%m-%d %H:%M:%S.%f",
"%Y-%m-%dT%H:%M:%S", "%Y-%m-%d %H:%M:%S"]
for fmt in fmts:
@ -43,7 +45,7 @@ def _parse_datetime(dtstr):
class ParseDateNode(template.Node):
def render(self, context):
"""Turn an iso formatted time back into a datetime."""
if context == None:
if not context:
return "None"
date_obj = _parse_datetime(context)
return date_obj.strftime("%m/%d/%y at %H:%M:%S")
@ -70,4 +72,6 @@ def parse_local_datetime(value):
@register.filter(name='pretty_date')
def pretty_date(value):
if not value:
return "None"
return value.strftime("%d/%m/%y at %H:%M:%S")

View File

@ -127,9 +127,9 @@ class TestCase(django_test.TestCase):
def tearDown(self):
self.mox.UnsetStubs()
self.mox.VerifyAll()
context_processors.horizon = self._real_horizon_context_processor
users.get_user_from_request = self._real_get_user_from_request
self.mox.VerifyAll()
def setActiveUser(self, id=None, token=None, username=None, tenant_id=None,
service_catalog=None, tenant_name=None, roles=None):
@ -155,51 +155,10 @@ class TestCase(django_test.TestCase):
utcnow.override_time = None
def fake_render_to_response(template_name, context, context_instance=None,
mimetype='text/html'):
"""Replacement for render_to_response so that views can be tested
without having to stub out templates that belong in the frontend
implementation.
Should be able to be tested using the django unit test assertions like a
normal render_to_response return value can be.
"""
class Template(object):
def __init__(self, name):
self.name = name
if context_instance is None:
context_instance = django_template.Context(context)
else:
context_instance.update(context)
resp = http.HttpResponse()
template = Template(template_name)
resp.write('<html><body><p>'
'This is a fake httpresponse for testing purposes only'
'</p></body></html>')
# Allows django.test.client to populate fields on the response object
django_test.signals.template_rendered.send(template, template=template,
context=context_instance)
return resp
class BaseViewTests(TestCase):
"""
Base class for view based unit tests.
"""
def setUp(self):
super(BaseViewTests, self).setUp()
self._real_render_to_response = shortcuts.render_to_response
shortcuts.render_to_response = fake_render_to_response
def tearDown(self):
super(BaseViewTests, self).tearDown()
shortcuts.render_to_response = self._real_render_to_response
def assertRedirectsNoFollow(self, response, expected_url):
self.assertEqual(response._headers['location'],
('Location', settings.TESTSERVER + expected_url))

View File

@ -0,0 +1,32 @@
{% load branding %}
{% load i18n %}
<!DOCTYPE html>
<html>
<head>
<title>{% block title %}{% endblock %} {% site_branding %} Dashboard</title>
{% block headerjs %}{% endblock %}
{% block headercss %}{% endblock %}
</head>
<body>
{% block topbar %}
<div id="user_info" class="pull-right">
<p>
Logged in as: {{ request.user.username }}.
<a href="{% url horizon:settings:user:index %}">{% trans "Settings"%}</a>
<a href="{% url horizon:auth_logout %}">{% trans "Sign Out"%}</a>
</p>
</div>
{% endblock %}
{% block content %}
<div id="container" class="container-fluid">
{% block sidebar %}{% endblock %}
{% block main %}{% endblock %}
</div>
{% endblock %}
<div id="footer">
{% block footer %}{% endblock %}
</div>
{% block footer_js %}{% endblock %}
</body>
</html>