From 1dec092857c79a3d302b101e36ab085d2cd7a32a Mon Sep 17 00:00:00 2001 From: Gabriel Hurley Date: Fri, 18 Nov 2011 12:38:21 -0800 Subject: [PATCH] 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 --- .gitignore | 1 + .../dashboards/nova/containers/tests.py | 13 +++--- .../dashboards/nova/floating_ips/tests.py | 4 +- .../horizon/dashboards/nova/images/tests.py | 8 ++-- .../dashboards/nova/instances/tests.py | 2 +- .../horizon/dashboards/nova/keypairs/tests.py | 2 +- .../dashboards/nova/security_groups/tests.py | 3 +- .../dashboards/nova/snapshots/tests.py | 4 +- .../nova/templates/nova/images/_list.html | 4 +- .../dashboards/syspanel/users/tests.py | 2 +- horizon/horizon/templatetags/launch_form.py | 1 + horizon/horizon/templatetags/parse_date.py | 6 ++- horizon/horizon/test.py | 43 +------------------ horizon/horizon/tests/templates/base.html | 32 ++++++++++++++ 14 files changed, 62 insertions(+), 63 deletions(-) diff --git a/.gitignore b/.gitignore index ac626da47e..376ccea28f 100644 --- a/.gitignore +++ b/.gitignore @@ -3,6 +3,7 @@ .environment_version .selenium_log .coverage +.noseids coverage.xml pep8.txt pylint.txt diff --git a/horizon/horizon/dashboards/nova/containers/tests.py b/horizon/horizon/dashboards/nova/containers/tests.py index 86b41d2124..465075433e 100644 --- a/horizon/horizon/dashboards/nova/containers/tests.py +++ b/horizon/horizon/dashboards/nova/containers/tests.py @@ -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]), diff --git a/horizon/horizon/dashboards/nova/floating_ips/tests.py b/horizon/horizon/dashboards/nova/floating_ips/tests.py index 45383c3e3b..441e861145 100644 --- a/horizon/horizon/dashboards/nova/floating_ips/tests.py +++ b/horizon/horizon/dashboards/nova/floating_ips/tests.py @@ -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 diff --git a/horizon/horizon/dashboards/nova/images/tests.py b/horizon/horizon/dashboards/nova/images/tests.py index 77dc414f51..53322713d0 100644 --- a/horizon/horizon/dashboards/nova/images/tests.py +++ b/horizon/horizon/dashboards/nova/images/tests.py @@ -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,) diff --git a/horizon/horizon/dashboards/nova/instances/tests.py b/horizon/horizon/dashboards/nova/instances/tests.py index 35d411de05..e90367972c 100644 --- a/horizon/horizon/dashboards/nova/instances/tests.py +++ b/horizon/horizon/dashboards/nova/instances/tests.py @@ -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,) diff --git a/horizon/horizon/dashboards/nova/keypairs/tests.py b/horizon/horizon/dashboards/nova/keypairs/tests.py index 7a95aaf91f..e63d1a2f06 100644 --- a/horizon/horizon/dashboards/nova/keypairs/tests.py +++ b/horizon/horizon/dashboards/nova/keypairs/tests.py @@ -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,) diff --git a/horizon/horizon/dashboards/nova/security_groups/tests.py b/horizon/horizon/dashboards/nova/security_groups/tests.py index 708f8aae94..88b57ca237 100644 --- a/horizon/horizon/dashboards/nova/security_groups/tests.py +++ b/horizon/horizon/dashboards/nova/security_groups/tests.py @@ -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,) diff --git a/horizon/horizon/dashboards/nova/snapshots/tests.py b/horizon/horizon/dashboards/nova/snapshots/tests.py index b5408e395f..55703afafc 100644 --- a/horizon/horizon/dashboards/nova/snapshots/tests.py +++ b/horizon/horizon/dashboards/nova/snapshots/tests.py @@ -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' diff --git a/horizon/horizon/dashboards/nova/templates/nova/images/_list.html b/horizon/horizon/dashboards/nova/templates/nova/images/_list.html index 63eb50d64f..f5705429b0 100644 --- a/horizon/horizon/dashboards/nova/templates/nova/images/_list.html +++ b/horizon/horizon/dashboards/nova/templates/nova/images/_list.html @@ -1,7 +1,5 @@ -{% load i18n %} +{% load i18n parse_date launch_form %} -{% load parse_date %} -{% load launch_form %} diff --git a/horizon/horizon/dashboards/syspanel/users/tests.py b/horizon/horizon/dashboards/syspanel/users/tests.py index 03c3287a9f..25dc206fda 100644 --- a/horizon/horizon/dashboards/syspanel/users/tests.py +++ b/horizon/horizon/dashboards/syspanel/users/tests.py @@ -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 diff --git a/horizon/horizon/templatetags/launch_form.py b/horizon/horizon/templatetags/launch_form.py index e7a12df944..9b50fe1da0 100644 --- a/horizon/horizon/templatetags/launch_form.py +++ b/horizon/horizon/templatetags/launch_form.py @@ -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 diff --git a/horizon/horizon/templatetags/parse_date.py b/horizon/horizon/templatetags/parse_date.py index 6adff07bb7..b9c4996d39 100644 --- a/horizon/horizon/templatetags/parse_date.py +++ b/horizon/horizon/templatetags/parse_date.py @@ -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") diff --git a/horizon/horizon/test.py b/horizon/horizon/test.py index c2749d7b08..bb95866592 100644 --- a/horizon/horizon/test.py +++ b/horizon/horizon/test.py @@ -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('

' - 'This is a fake httpresponse for testing purposes only' - '

') - - # 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)) diff --git a/horizon/horizon/tests/templates/base.html b/horizon/horizon/tests/templates/base.html index e69de29bb2..2ddaf011a9 100644 --- a/horizon/horizon/tests/templates/base.html +++ b/horizon/horizon/tests/templates/base.html @@ -0,0 +1,32 @@ +{% load branding %} +{% load i18n %} + + + + {% block title %}{% endblock %} – {% site_branding %} Dashboard + {% block headerjs %}{% endblock %} + {% block headercss %}{% endblock %} + + + {% block topbar %} +
+

+ Logged in as: {{ request.user.username }}. + {% trans "Settings"%} + {% trans "Sign Out"%} +

+
+ {% endblock %} + {% block content %} +
+ {% block sidebar %}{% endblock %} + {% block main %}{% endblock %} +
+ {% endblock %} + + {% block footer_js %}{% endblock %} + + +
{%trans "ID"%}