def test_templatetag(template_string, filename): storage = StaticFilesStorage(location=settings.STATICFILES_TEST_DIR) try: storage.save(name=filename, content=ContentFile("body { background: red; }")) resp = T(template_string).render(CTX).strip().split("\n") expected_output = '<link href="{}{}" rel="stylesheet" type="text/css" />'.format( settings.STATIC_URL, filename) assert expected_output in resp assert "<!--" not in resp assert "-->" not in resp finally: storage.delete(filename)
def test_templatetag_assignment(request_path, expected_output): CTX = Context({ 'path': request_path, }) resp = T(''' {% load path2css %} {% path2css path as GOOSE %} {% for part in GOOSE %} {{part}} {% endfor %} ''').render(CTX).strip() parts = [x.strip() for x in resp.split() if x.strip()] assert parts == expected_output
def test_templatetag_multiple_parts_of_path(): filenames = ('css/level1.css', 'css/level1-level2.css', 'css/level1-level2-level3.css') storage = StaticFilesStorage(location=settings.STATICFILES_TEST_DIR) try: for filename in filenames: storage.save(name=filename, content=ContentFile("body { background: red; }")) resp = T('{% load path2css %}{% css4path "/level1/level2/level3/" %}' ).render(CTX).strip() assert resp.split("\n") == [ '<link href="{}css/level1.css" rel="stylesheet" type="text/css" />' .format(settings.STATIC_URL), '<link href="{}css/level1-level2.css" rel="stylesheet" type="text/css" />' .format(settings.STATIC_URL), '<link href="{}css/level1-level2-level3.css" rel="stylesheet" type="text/css" />' .format(settings.STATIC_URL) ] finally: for filename in filenames: storage.delete(filename)
def test_templatetag_assignment(request_path, filenames): storage = StaticFilesStorage(location=settings.STATICFILES_TEST_DIR) try: for filename in filenames: storage.save(name=filename, content=ContentFile("body { background: red; }")) resp = T(''' {% load path2css %} {% css4path path as GOOSE %} {% for part, exists in GOOSE %} {% if exists %}{{ part }}{% endif %} {% endfor %} ''').render(Context({'path': request_path})).strip() parts = [x.strip() for x in resp.split("\n") if x.strip()] expected_output = [ "{}{}".format(settings.STATIC_URL, filename) for filename in filenames ] assert parts == expected_output finally: for filename in filenames: storage.delete(filename)
def test_templatetag_root_does_nothing(): resp = T('{% load path2css %}{% css4path "//" %}').render(CTX).strip() assert resp == ''
def test_templatetag(template_string, expected_output): template = r'{% load path2css %}<body class="' + template_string + '">' resp = T(template).render(CTX).strip() assert resp == '<body class="%s">' % (expected_output,)
def test_context_processor(rf): request = rf.get('/test/path/yeah/') resp = T('{{ PATH2CSS }}').render(CTX(request=request, dict_={})).strip() assert resp == 'test test-path test-path-yeah'