Files
edx-platform/common/djangoapps/pipeline_mako/tests/test_render.py
Kyle McCormick 151bd13666 Use full names for common.djangoapps imports; warn when using old style (#25477)
* Generate common/djangoapps import shims for LMS
* Generate common/djangoapps import shims for Studio
* Stop appending project root to sys.path
* Stop appending common/djangoapps to sys.path
* Import from common.djangoapps.course_action_state instead of course_action_state
* Import from common.djangoapps.course_modes instead of course_modes
* Import from common.djangoapps.database_fixups instead of database_fixups
* Import from common.djangoapps.edxmako instead of edxmako
* Import from common.djangoapps.entitlements instead of entitlements
* Import from common.djangoapps.pipline_mako instead of pipeline_mako
* Import from common.djangoapps.static_replace instead of static_replace
* Import from common.djangoapps.student instead of student
* Import from common.djangoapps.terrain instead of terrain
* Import from common.djangoapps.third_party_auth instead of third_party_auth
* Import from common.djangoapps.track instead of track
* Import from common.djangoapps.util instead of util
* Import from common.djangoapps.xblock_django instead of xblock_django
* Add empty common/djangoapps/__init__.py to fix pytest collection
* Fix pylint formatting violations
* Exclude import_shims/ directory tree from linting
2020-11-10 07:02:01 -05:00

93 lines
3.4 KiB
Python

""" Tests for rendering functions in the mako pipeline. """
from unittest import skipUnless
import ddt
from django.conf import settings
from django.test import TestCase
from mock import patch
from six.moves import map
from common.djangoapps.pipeline_mako import compressed_css, compressed_js, render_require_js_path_overrides
class RequireJSPathOverridesTest(TestCase):
"""Test RequireJS path overrides. """
OVERRIDES = {
'jquery': 'common/js/vendor/jquery.js',
'text': 'js/vendor/text.js',
'backbone': 'common/js/vendor/backbone.js'
}
OVERRIDES_JS = [
"<script type=\"text/javascript\">",
"(function (require) {",
"require.config({",
"paths: {",
"'jquery': 'common/js/vendor/jquery',",
"'text': 'js/vendor/text',",
"'backbone': 'common/js/vendor/backbone'",
"}",
"});",
"}).call(this, require || RequireJS.require);",
"</script>"
]
def test_requirejs_path_overrides(self):
result = render_require_js_path_overrides(self.OVERRIDES)
# To make the string comparision easy remove the whitespaces
self.assertCountEqual(list(map(str.strip, result.splitlines())), self.OVERRIDES_JS)
@skipUnless(settings.ROOT_URLCONF == 'lms.urls', 'Test only valid in LMS')
@ddt.ddt
class PipelineRenderTest(TestCase):
"""Test individual pipeline rendering functions. """
@staticmethod
def mock_staticfiles_lookup(path):
return '/static/' + path
@patch('common.djangoapps.static_replace.try_staticfiles_lookup', side_effect=mock_staticfiles_lookup)
@ddt.data(
(True,),
(False,),
)
def test_compressed_css(self, pipeline_enabled, mock_staticfiles_lookup):
"""
Verify the behavior of compressed_css, with the pipeline
both enabled and disabled.
"""
pipeline = settings.PIPELINE.copy()
pipeline['PIPELINE_ENABLED'] = pipeline_enabled
with self.settings(PIPELINE=pipeline):
# Verify the default behavior
css_include = compressed_css('style-main-v1')
self.assertIn(u'lms-main-v1.css', css_include)
# Verify that raw keyword causes raw URLs to be emitted
css_include = compressed_css('style-main-v1', raw=True)
self.assertIn(u'lms-main-v1.css?raw', css_include)
@patch('django.contrib.staticfiles.storage.staticfiles_storage.exists', return_value=True)
@patch('common.djangoapps.static_replace.try_staticfiles_lookup', side_effect=mock_staticfiles_lookup)
def test_compressed_js(self, mock_staticfiles_lookup, mock_staticfiles_exists):
"""
Verify the behavior of compressed_css, with the pipeline
both enabled and disabled.
"""
pipeline = settings.PIPELINE.copy()
# Verify that a single JS file is rendered with the pipeline enabled
pipeline['PIPELINE_ENABLED'] = True
with self.settings(PIPELINE=pipeline):
js_include = compressed_js('base_application')
self.assertIn(u'lms-base-application.js', js_include)
# Verify that multiple JS files are rendered with the pipeline disabled
pipeline['PIPELINE_ENABLED'] = False
with self.settings(PIPELINE=pipeline):
js_include = compressed_js('base_application')
self.assertIn(u'/static/js/src/logger.js', js_include)