Skip to content
This repository has been archived by the owner on Apr 4, 2023. It is now read-only.

Commit

Permalink
Migrate test file to py.test style.
Browse files Browse the repository at this point in the history
  • Loading branch information
CM Lubinski committed Aug 24, 2017
1 parent bac0f4f commit 3a21b40
Showing 1 changed file with 39 additions and 38 deletions.
77 changes: 39 additions & 38 deletions regulations/tests/templatetags_macros_tests.py
Original file line number Diff line number Diff line change
@@ -1,43 +1,44 @@
from unittest import TestCase
from xml.etree import ElementTree as etree # nosec - see usage below

from django.template import Context, Template


class MacrosTests(TestCase):
def _gen_link(self, content):
"""Shorthand for passing the content into a template and rendering"""
text = "{% load macros %}" + content
as_str = Template(text).render(Context({}))
# Safe because: we've constructed the XML
as_xml = etree.fromstring("<ROOT>{}</ROOT>".format(as_str)) # nosec
anchors = as_xml.findall('.//a')
self.assertTrue(len(anchors) > 0)
return anchors[0]

def test_external_link_no_optional(self):
"""The classes and title fields are optional. We should generate an
appropriate link"""
anchor = self._gen_link(
'{% external_link url="http://example.com/path" text="Click" %}')
self.assertEqual(anchor.get('target'), '_blank')
self.assertEqual(anchor.get('href'), 'http://example.com/path')
self.assertFalse('title' in anchor.attrib)
self.assertTrue('aria-label' in anchor.attrib)
self.assertTrue('Click' in anchor.text)

def test_external_link_classes_title(self):
"""The classes and title fields _can_ be added"""
anchor = self._gen_link(
'{% external_link url="url" text="text" classes="some here" '
'title="My Title" %}')
self.assertEqual(anchor.get('title'), 'My Title')
self.assertTrue('some here' in anchor.get('class'))

def test_search_for(self):
"""Macro should url-encode search terms."""
anchor = self._gen_link(
'{% search_for terms="has spaces" reg="1234" version="vvv" %}')
self.assertTrue('1234' in anchor.get('href'))
self.assertTrue('vvv' in anchor.get('href'))
self.assertTrue('has%20spaces' in anchor.get('href'))
def _gen_link(content):
"""Shorthand for passing the content into a template and rendering"""
text = "{% load macros %}" + content
as_str = Template(text).render(Context({}))
# Safe because: we've constructed the XML
as_xml = etree.fromstring("<ROOT>{}</ROOT>".format(as_str)) # nosec
anchors = as_xml.findall('.//a')
assert len(anchors) > 0
return anchors[0]


def test_external_link_no_optional():
"""The classes and title fields are optional. We should generate an
appropriate link"""
anchor = _gen_link(
'{% external_link url="http://example.com/path" text="Click" %}')
assert anchor.get('target') == '_blank'
assert anchor.get('href') == 'http://example.com/path'
assert 'title' not in anchor.attrib
assert 'aria-label' in anchor.attrib
assert 'Click' in anchor.text


def test_external_link_classes_title():
"""The classes and title fields _can_ be added"""
anchor = _gen_link(
'{% external_link url="url" text="text" classes="some here" '
'title="My Title" %}')
assert anchor.get('title') == 'My Title'
assert 'some here' in anchor.get('class')


def test_search_for():
"""Macro should url-encode search terms."""
anchor = _gen_link(
'{% search_for terms="has spaces" reg="1234" version="vvv" %}')
assert '1234' in anchor.get('href')
assert 'vvv' in anchor.get('href')
assert 'has%20spaces' in anchor.get('href')

0 comments on commit 3a21b40

Please sign in to comment.