Skip to content

Commit

Permalink
Merge branch 'fix-falsey-raw-templatetag-values' into 'master'
Browse files Browse the repository at this point in the history
Fix falsey raw values for template tags, add tests and update runtests.py to…

Closes #13

See merge request mikalai.radchuk/django-pattern-library!8
  • Loading branch information
Ben Dickinson committed Feb 5, 2018
2 parents b2c334c + acfd1ac commit 5accf91
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 12 deletions.
23 changes: 13 additions & 10 deletions pattern_library/monkey_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ def tag_func(parser, token):

def node_render(context):
if is_pattern_library_context(context):
tag_overridden = False
result = ''

# Load pattern's config
Expand All @@ -42,21 +43,23 @@ def node_render(context):
# Get config for specific arguments
tag_config = tag_config.get(arguments, {})

# Return raw data (it can be string or a structure), if defined
if 'raw' in tag_config:
# Return raw data (it can be string or a structure), if defined
result = tag_config['raw']
tag_overridden = True
elif 'template_name' in tag_config:
# Render pattern, if raw string is not defined
template_name = tag_config['template_name']
request = context.get('request')
result = render_pattern(request, template_name)
tag_overridden = True

# TODO: Allow objects with the __str__ method
# In some cases we must return an object that can
# be rendered as a string `{{ result }}`
# and allow users to access it's attributes `{{ result.url }}`
result = tag_config.get('raw')

# Render pattern, if raw string is not defined
if not result:
template_name = tag_config.get('template_name')
if template_name:
request = context.get('request')
result = render_pattern(request, template_name)

if result:
if tag_overridden:
if isinstance(original_node, SimpleNode):
# If it's a SimpleNode try to use it's target_var
target_var = original_node.target_var
Expand Down
20 changes: 19 additions & 1 deletion runtests.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#!/usr/bin/env python
import argparse
import os
import sys

Expand All @@ -8,15 +9,32 @@

import coverage

parser = argparse.ArgumentParser()
parser.add_argument(
'-v', '--verbosity', action='store', dest='verbosity', default=1,
type=int, choices=range(4),
help="Verbosity level; 0=minimal output, 1=normal output, 2=verbose output, 3=very verbose output",
)

if __name__ == "__main__":
# Coverage setup
cov = coverage.Coverage()
cov.start()

# Django setup
os.environ['DJANGO_SETTINGS_MODULE'] = 'tests.test_settings'
django.setup()

# Test runner setup
TestRunner = get_runner(settings)
test_runner = TestRunner()
TestRunner.add_arguments(parser)
args = parser.parse_args()
test_runner = TestRunner(**vars(args))
failures = test_runner.run_tests(["tests"])

# Generate coverage report
cov.stop()
cov.save()
cov.html_report()

sys.exit(bool(failures))
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{% load test_tags %}

SAND{% error_tag empty_string %}WICH
SAND{% error_tag none %}WICH
SAND{% error_tag zero %}WICH
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
tags:
error_tag:
empty_string:
raw: ''
none:
raw: null
zero:
raw: 0
Empty file added tests/templatetags/__init__.py
Empty file.
14 changes: 14 additions & 0 deletions tests/templatetags/test_tags.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
from django import template

from pattern_library.monkey_utils import override_tag

register = template.Library()


@register.simple_tag
def error_tag(arg=None):
"Just raise an exception, never do anything"
raise Exception("error_tag raised an exception")


override_tag(register, 'error_tag')
10 changes: 9 additions & 1 deletion tests/tests/test_tags.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
from django.core.urlresolvers import reverse
from django.test import SimpleTestCase


class TagsTesCase(SimpleTestCase):
pass
def test_falsey_raw_values_for_tag_output(self):
response = self.client.get(reverse(
'pattern_library:display_pattern',
kwargs={'template_name': 'patterns/atoms/tags_test_atom/tags_test_atom.html'},
))
self.assertContains(response, "SANDWICH")
self.assertContains(response, "SANDNoneWICH")
self.assertContains(response, "SAND0WICH")

0 comments on commit 5accf91

Please sign in to comment.