diff --git a/setup.py b/setup.py index 985e103..7c1495e 100644 --- a/setup.py +++ b/setup.py @@ -31,6 +31,7 @@ setup( zip_safe=False, include_package_data=True, tests_require=[ + 'beautifulsoup4==4.1.3', 'nose==1.2.1', 'nose-progressive==1.3', 'django-nose==1.1', diff --git a/tests/imagespecs.py b/tests/imagespecs.py new file mode 100644 index 0000000..536db7a --- /dev/null +++ b/tests/imagespecs.py @@ -0,0 +1,8 @@ +from imagekit import ImageSpec, register + + +class TestSpec(ImageSpec): + pass + + +register.spec(TestSpec, 'testspec') diff --git a/tests/test_generateimage_tag.py b/tests/test_generateimage_tag.py new file mode 100644 index 0000000..a7214ae --- /dev/null +++ b/tests/test_generateimage_tag.py @@ -0,0 +1,65 @@ +from bs4 import BeautifulSoup +from django.template import Context, Template, TemplateSyntaxError +from nose.tools import eq_, assert_not_in, raises, assert_not_equal +from . import imagespecs +from .utils import get_named_image_file + + +def render_tag(ttag): + img = get_named_image_file() + img.name = 'tmp' # FIXME: If we don't do this, we get a SuspiciousOperation + template = Template('{%% load imagekit %%}%s' % ttag) + context = Context({'img': img}) + return template.render(context) + + +def get_html_attrs(ttag): + return BeautifulSoup(render_tag(ttag)).img.attrs + + +def test_img_tag(): + ttag = r"""{% generateimage 'testspec' from=img %}""" + attrs = get_html_attrs(ttag) + expected_attrs = set(['src', 'width', 'height']) + eq_(set(attrs.keys()), expected_attrs) + for k in expected_attrs: + assert_not_equal(attrs[k].strip(), '') + + +def test_img_tag_attrs(): + ttag = r"""{% generateimage 'testspec' from=img with alt="Hello" %}""" + attrs = get_html_attrs(ttag) + eq_(attrs.get('alt'), 'Hello') + + +@raises(TemplateSyntaxError) +def test_dangling_with(): + ttag = r"""{% generateimage 'testspec' from=img with %}""" + render_tag(ttag) + + +@raises(TemplateSyntaxError) +def test_with_assignment(): + """ + You can either use generateimage as an assigment tag or specify html attrs, + but not both. + + """ + ttag = r"""{% generateimage 'testspec' from=img with alt="Hello" as th %}""" + render_tag(ttag) + + +def test_single_dimension_attr(): + """ + If you only provide one of width or height, the other should not be added. + + """ + ttag = r"""{% generateimage 'testspec' from=img with width="50" %}""" + attrs = get_html_attrs(ttag) + assert_not_in('height', attrs) + + +def test_assignment_tag(): + ttag = r"""{% generateimage 'testspec' from=img as th %} {{ th.url }}""" + html = render_tag(ttag) + assert_not_equal(html.strip(), '')