Merge pull request #176 from diox/b72dddd4a6e485a7520e5e1d8f3fa603d270f434
Re-organize offline compression tests to make them really indendant from each other
This commit is contained in:
@@ -66,9 +66,14 @@ class Command(NoArgsCommand):
|
||||
from django.template.loader import (
|
||||
find_template_source as finder_func)
|
||||
try:
|
||||
# Force django to calculate template_source_loaders from
|
||||
# TEMPLATE_LOADERS settings, by asking to find a dummy template
|
||||
source, name = finder_func('test')
|
||||
except TemplateDoesNotExist:
|
||||
pass
|
||||
# Reload template_source_loaders now that it has been calculated ;
|
||||
# it should contain the list of valid, instanciated template loaders
|
||||
# to use.
|
||||
from django.template.loader import template_source_loaders
|
||||
loaders = []
|
||||
# If template loader is CachedTemplateLoader, return the loaders
|
||||
|
||||
@@ -21,7 +21,10 @@ MEDIA_URL = '/media/'
|
||||
MEDIA_ROOT = os.path.join(TEST_DIR, 'media')
|
||||
|
||||
TEMPLATE_DIRS = (
|
||||
os.path.join(TEST_DIR, 'templates'),
|
||||
# Specifically choose a name that will not be considered
|
||||
# by app_directories loader, to make sure each test uses
|
||||
# a specific template without considering the others.
|
||||
os.path.join(TEST_DIR, 'test_templates'),
|
||||
)
|
||||
|
||||
JENKINS_TASKS = (
|
||||
|
||||
@@ -1,49 +0,0 @@
|
||||
{% extends "base.html" %}
|
||||
{% load compress %}
|
||||
|
||||
{% block content %}{% spaceless %}
|
||||
{% compress css%}
|
||||
<style type="text/css">
|
||||
body {
|
||||
background: {{ color|default:"red" }};
|
||||
}
|
||||
</style>
|
||||
{% endcompress %}
|
||||
|
||||
{% compress js%}
|
||||
<script type="text/javascript">
|
||||
alert("{% firstof "test" %}");
|
||||
</script>
|
||||
{% endcompress %}
|
||||
|
||||
{% compress js%}
|
||||
<script type="text/javascript">
|
||||
alert("test 2");
|
||||
</script>
|
||||
{% endcompress %}
|
||||
|
||||
{% if condition %}
|
||||
{% compress js%}
|
||||
<script type="text/javascript">alert("{{ condition|default:"yellow" }}");</script>
|
||||
{% endcompress %}
|
||||
{% endif %}
|
||||
|
||||
{% endspaceless %}{% endblock %}
|
||||
|
||||
{% block js %}{% spaceless %}
|
||||
{% compress js %}
|
||||
{{ block.super }}
|
||||
<script type="text/javascript">
|
||||
alert("test 4");
|
||||
</script>
|
||||
{% endcompress %}
|
||||
{% endspaceless %}{% endblock %}
|
||||
|
||||
{% block css %}{% spaceless %}
|
||||
{% compress css %}
|
||||
{{ block.super }}
|
||||
<style type="text/css">
|
||||
body { color: orange ; }
|
||||
</style>
|
||||
{% endcompress %}
|
||||
{% endspaceless %}{% endblock %}
|
||||
8
tests/test_templates/basic/test_compressor_offline.html
Normal file
8
tests/test_templates/basic/test_compressor_offline.html
Normal file
@@ -0,0 +1,8 @@
|
||||
{% load compress %}{% spaceless %}
|
||||
|
||||
{% compress js %}
|
||||
<script type="text/javascript">
|
||||
alert("Basic test");
|
||||
</script>
|
||||
{% endcompress %}
|
||||
{% endspaceless %}
|
||||
@@ -1,16 +1,15 @@
|
||||
{% block content %}{% endblock %}
|
||||
|
||||
{% block js%}
|
||||
{% spaceless %}
|
||||
{% block js %}
|
||||
<script type="text/javascript">
|
||||
alert("test 3");
|
||||
alert("test using block.super");
|
||||
</script>
|
||||
{% endblock %}
|
||||
|
||||
{% block css %}
|
||||
<style type="text/css">
|
||||
body {
|
||||
background: {{ color|default:"purple" }};
|
||||
background: red;
|
||||
}
|
||||
</style>
|
||||
{% endblock %}
|
||||
|
||||
{% endspaceless %}
|
||||
@@ -0,0 +1,13 @@
|
||||
{% extends "base.html" %}
|
||||
{% load compress %}
|
||||
|
||||
{% block js %}{% spaceless %}
|
||||
{% compress js %}
|
||||
{{ block.super }}
|
||||
<script type="text/javascript">
|
||||
alert("this alert shouldn't be alone!");
|
||||
</script>
|
||||
{% endcompress %}
|
||||
{% endspaceless %}{% endblock %}
|
||||
|
||||
{% block css %}{% endblock %}
|
||||
@@ -0,0 +1,7 @@
|
||||
{% load compress %}{% spaceless %}
|
||||
|
||||
{% if condition %}
|
||||
{% compress js%}
|
||||
<script type="text/javascript">alert("{{ condition|default:"yellow" }}");</script>
|
||||
{% endcompress %}
|
||||
{% endif %}{% endspaceless %}
|
||||
@@ -1,4 +1,4 @@
|
||||
{% extends "buggy_extends.html" %}
|
||||
{% extends "buggy_template.html" %}
|
||||
{% load compress %}
|
||||
|
||||
{% compress css %}
|
||||
@@ -0,0 +1,8 @@
|
||||
{% load compress %}{% spaceless %}
|
||||
|
||||
{% compress js %}
|
||||
<script type="text/javascript">
|
||||
alert("Basic test, should pass in spite of errors in other templates");
|
||||
</script>
|
||||
{% endcompress %}
|
||||
{% endspaceless %}
|
||||
@@ -0,0 +1,7 @@
|
||||
{% load compress %}{% spaceless %}
|
||||
|
||||
{% compress js %}
|
||||
<script type="text/javascript">
|
||||
alert("{% firstof "testtemplatetag" %}");
|
||||
</script>
|
||||
{% endcompress %}{% endspaceless %}
|
||||
@@ -0,0 +1,7 @@
|
||||
{% load compress %}{% spaceless %}
|
||||
|
||||
{% compress js %}
|
||||
<script type="text/javascript">
|
||||
alert("{{ content|default:"Ooops!" }}");
|
||||
</script>
|
||||
{% endcompress %}{% endspaceless %}
|
||||
@@ -4,7 +4,13 @@ from .filters import (CssTidyTestCase, PrecompilerTestCase, CssMinTestCase,
|
||||
CssAbsolutizingTestCase, CssAbsolutizingTestCaseWithHash,
|
||||
CssDataUriTestCase)
|
||||
from .jinja2ext import TestJinja2CompressorExtension
|
||||
from .offline import OfflineGenerationTestCase
|
||||
from .offline import (
|
||||
OfflineGenerationBlockSuperTestCase,
|
||||
OfflineGenerationConditionTestCase,
|
||||
OfflineGenerationTemplateTagTestCase,
|
||||
OfflineGenerationTestCaseWithContext,
|
||||
OfflineGenerationTestCaseErrors,
|
||||
OfflineGenerationTestCase)
|
||||
from .parsers import (LxmlParserTests, Html5LibParserTests,
|
||||
BeautifulSoupParserTests, HtmlParserTests)
|
||||
from .signals import PostCompressSignalTestCase
|
||||
|
||||
@@ -1,6 +1,11 @@
|
||||
from __future__ import with_statement
|
||||
import os
|
||||
|
||||
try:
|
||||
from cStringIO import StringIO
|
||||
except ImportError:
|
||||
from StringIO import StringIO
|
||||
|
||||
from django.template import Template, Context
|
||||
from django.test import TestCase
|
||||
|
||||
@@ -9,28 +14,100 @@ from compressor.exceptions import OfflineGenerationError
|
||||
from compressor.management.commands.compress import Command as CompressCommand
|
||||
from compressor.storage import default_storage
|
||||
|
||||
from .base import test_dir, css_tag
|
||||
from .base import css_tag
|
||||
|
||||
class OfflineGenerationTestCase(TestCase):
|
||||
"""Uses templates/test_compressor_offline.html"""
|
||||
maxDiff = None
|
||||
class OfflineTestCaseMixin():
|
||||
template_name = "test_compressor_offline.html"
|
||||
templates_dir = "" # Change this for each test class, to separate templates
|
||||
expected_hash = "" # Change this for each test class to the expected result
|
||||
verbosity = 0
|
||||
|
||||
def setUp(self):
|
||||
self._old_compress = settings.COMPRESS_ENABLED
|
||||
self._old_compress_offline = settings.COMPRESS_OFFLINE
|
||||
self._old_template_dirs = settings.TEMPLATE_DIRS
|
||||
self._old_offline_context = settings.COMPRESS_OFFLINE_CONTEXT
|
||||
self._old_template_loaders = settings.TEMPLATE_LOADERS
|
||||
self.log = StringIO()
|
||||
|
||||
# Force template dirs, because it enables us to force compress to
|
||||
# consider only a specific directory (helps us make true,
|
||||
# independant unit tests).
|
||||
settings.TEMPLATE_DIRS = (
|
||||
os.path.join(settings.TEMPLATE_DIRS[0], self.templates_dir),
|
||||
)
|
||||
# Enable offline compress
|
||||
settings.COMPRESS_ENABLED = True
|
||||
settings.COMPRESS_OFFLINE = True
|
||||
self.template_file = open(os.path.join(test_dir, "templates/test_compressor_offline.html"))
|
||||
self.template_path = os.path.join(settings.TEMPLATE_DIRS[0], self.template_name)
|
||||
self.template_file = open(self.template_path)
|
||||
self.template = Template(self.template_file.read().decode(settings.FILE_CHARSET))
|
||||
|
||||
def tearDown(self):
|
||||
settings.COMPRESS_ENABLED = self._old_compress
|
||||
settings.COMPRESS_OFFLINE = self._old_compress_offline
|
||||
settings.TEMPLATE_DIRS = self._old_template_dirs
|
||||
settings.TEMPLATE_LOADERS = self._old_template_loaders
|
||||
self.template_file.close()
|
||||
manifest_path = os.path.join('CACHE', 'manifest.json')
|
||||
if default_storage.exists(manifest_path):
|
||||
default_storage.delete(manifest_path)
|
||||
|
||||
def test_offline(self):
|
||||
count, result = CompressCommand().compress(log=self.log, verbosity=self.verbosity)
|
||||
self.assertEqual(1, count)
|
||||
self.assertEqual([
|
||||
u'<script type="text/javascript" src="/media/CACHE/js/%s.js"></script>' % (self.expected_hash, ),
|
||||
], result)
|
||||
rendered_template = self.template.render(Context(settings.COMPRESS_OFFLINE_CONTEXT))
|
||||
self.assertEqual(rendered_template, "".join(result) + "\n")
|
||||
|
||||
class OfflineGenerationBlockSuperTestCase(OfflineTestCaseMixin, TestCase):
|
||||
templates_dir = "test_block_super"
|
||||
expected_hash = "7c02d201f69d"
|
||||
|
||||
class OfflineGenerationConditionTestCase(OfflineTestCaseMixin, TestCase):
|
||||
templates_dir = "test_condition"
|
||||
expected_hash = "4e3758d50224"
|
||||
|
||||
def setUp(self):
|
||||
self.old_offline_context = settings.COMPRESS_OFFLINE_CONTEXT
|
||||
settings.COMPRESS_OFFLINE_CONTEXT = {
|
||||
'condition': 'red',
|
||||
}
|
||||
super(OfflineGenerationConditionTestCase, self).setUp()
|
||||
|
||||
def tearDown(self):
|
||||
self.COMPRESS_OFFLINE_CONTEXT = self.old_offline_context
|
||||
super(OfflineGenerationConditionTestCase, self).tearDown()
|
||||
|
||||
class OfflineGenerationTemplateTagTestCase(OfflineTestCaseMixin, TestCase):
|
||||
templates_dir = "test_templatetag"
|
||||
expected_hash = "a27e1d3a619a"
|
||||
|
||||
class OfflineGenerationTestCaseWithContext(OfflineTestCaseMixin, TestCase):
|
||||
templates_dir = "test_with_context"
|
||||
expected_hash = "5838e2fd66af"
|
||||
|
||||
def setUp(self):
|
||||
self.old_offline_context = settings.COMPRESS_OFFLINE_CONTEXT
|
||||
settings.COMPRESS_OFFLINE_CONTEXT = {
|
||||
'content': 'OK!',
|
||||
}
|
||||
super(OfflineGenerationTestCaseWithContext, self).setUp()
|
||||
|
||||
def tearDown(self):
|
||||
self.COMPRESS_OFFLINE_CONTEXT = self.old_offline_context
|
||||
super(OfflineGenerationTestCaseWithContext, self).tearDown()
|
||||
|
||||
class OfflineGenerationTestCaseErrors(OfflineTestCaseMixin, TestCase):
|
||||
templates_dir = "test_error_handling"
|
||||
expected_hash = "cd8870829421"
|
||||
|
||||
class OfflineGenerationTestCase(OfflineTestCaseMixin, TestCase):
|
||||
templates_dir = "basic"
|
||||
expected_hash = "f5e179b8eca4"
|
||||
|
||||
def test_rendering_without_compressing_raises_exception(self):
|
||||
self.assertRaises(OfflineGenerationError,
|
||||
self.template.render, Context({}))
|
||||
@@ -38,40 +115,6 @@ class OfflineGenerationTestCase(TestCase):
|
||||
def test_requires_model_validation(self):
|
||||
self.assertFalse(CompressCommand.requires_model_validation)
|
||||
|
||||
def test_offline(self):
|
||||
count, result = CompressCommand().compress()
|
||||
self.assertEqual(6, count)
|
||||
self.assertEqual([
|
||||
css_tag('/media/CACHE/css/cd579b7deb7d.css'),
|
||||
u'<script type="text/javascript" src="/media/CACHE/js/0a2bb9a287c0.js"></script>',
|
||||
u'<script type="text/javascript" src="/media/CACHE/js/fb1736ad48b7.js"></script>',
|
||||
u'<script type="text/javascript" src="/media/CACHE/js/1a63aacfe9de.js"></script>',
|
||||
u'<script type="text/javascript" src="/media/CACHE/js/770a7311729e.js"></script>',
|
||||
u'<link rel="stylesheet" href="/media/CACHE/css/67ed6aff7f7b.css" type="text/css" />',
|
||||
], result)
|
||||
|
||||
def test_offline_with_context(self):
|
||||
self._old_offline_context = settings.COMPRESS_OFFLINE_CONTEXT
|
||||
settings.COMPRESS_OFFLINE_CONTEXT = {
|
||||
'color': 'blue',
|
||||
'condition': 'red',
|
||||
}
|
||||
count, result = CompressCommand().compress()
|
||||
self.assertEqual(6, count)
|
||||
self.assertEqual([
|
||||
css_tag('/media/CACHE/css/ee62fbfd116a.css'),
|
||||
u'<script type="text/javascript" src="/media/CACHE/js/0a2bb9a287c0.js"></script>',
|
||||
u'<script type="text/javascript" src="/media/CACHE/js/fb1736ad48b7.js"></script>',
|
||||
u'<script type="text/javascript" src="/media/CACHE/js/4e3758d50224.js"></script>',
|
||||
u'<script type="text/javascript" src="/media/CACHE/js/770a7311729e.js"></script>',
|
||||
u'<link rel="stylesheet" href="/media/CACHE/css/73e015f740c6.css" type="text/css" />',
|
||||
], result)
|
||||
# Template rendering should use the cache. FIXME: how to make sure of it ? Should we test the cache
|
||||
# key<->values ourselves?
|
||||
rendered_template = self.template.render(Context(settings.COMPRESS_OFFLINE_CONTEXT)).replace("\n", "")
|
||||
self.assertEqual(rendered_template, "".join(result).replace("\n", ""))
|
||||
settings.COMPRESS_OFFLINE_CONTEXT = self._old_offline_context
|
||||
|
||||
def test_get_loaders(self):
|
||||
old_loaders = settings.TEMPLATE_LOADERS
|
||||
settings.TEMPLATE_LOADERS = (
|
||||
@@ -90,4 +133,4 @@ class OfflineGenerationTestCase(TestCase):
|
||||
self.assertTrue(isinstance(loaders[0], FileSystemLoader))
|
||||
self.assertTrue(isinstance(loaders[1], AppDirectoriesLoader))
|
||||
finally:
|
||||
settings.TEMPLATE_LOADERS = old_loaders
|
||||
settings.TEMPLATE_LOADERS = old_loaders
|
||||
|
||||
Reference in New Issue
Block a user