|
4 | 4 | from shutil import rmtree
|
5 | 5 | from subprocess import call
|
6 | 6 | from threading import Thread
|
| 7 | +from unittest.mock import Mock |
| 8 | +from unittest.mock import call as MockCall |
| 9 | +from unittest.mock import patch |
7 | 10 |
|
8 | 11 | from django.conf import settings
|
9 |
| -from django.template import engines |
10 |
| -from django.template.backends.django import Template |
| 12 | +from django.template import Context, Template, engines |
11 | 13 | from django.template.response import TemplateResponse
|
12 | 14 | from django.test.client import RequestFactory
|
13 | 15 | from django.test.testcases import TestCase
|
14 | 16 | from django.views.generic.base import TemplateView
|
| 17 | +from django_jinja.backend import Jinja2 |
| 18 | +from django_jinja.backend import Template as Jinja2Template |
15 | 19 | from django_jinja.builtins import DEFAULT_EXTENSIONS
|
16 | 20 |
|
17 | 21 | from webpack_loader.exceptions import (WebpackBundleLookupError, WebpackError,
|
18 | 22 | WebpackLoaderBadStatsError,
|
19 | 23 | WebpackLoaderTimeoutError)
|
| 24 | +from webpack_loader.templatetags.webpack_loader import _WARNING_MESSAGE |
20 | 25 | from webpack_loader.utils import get_loader
|
21 | 26 |
|
22 | 27 | BUNDLE_PATH = os.path.join(
|
23 | 28 | settings.BASE_DIR, 'assets/django_webpack_loader_bundles/')
|
24 | 29 | DEFAULT_CONFIG = 'DEFAULT'
|
25 | 30 | _OUR_EXTENSION = 'webpack_loader.contrib.jinja2ext.WebpackExtension'
|
26 | 31 |
|
| 32 | +_warn_mock = Mock() |
| 33 | + |
27 | 34 |
|
28 | 35 | class LoaderTestCase(TestCase):
|
29 | 36 | def setUp(self):
|
@@ -333,6 +340,142 @@ def test_request_blocking(self):
|
333 | 340 | elapsed = time.time() - then
|
334 | 341 | self.assertTrue(elapsed < wait_for)
|
335 | 342 |
|
| 343 | + @patch( |
| 344 | + target='webpack_loader.templatetags.webpack_loader.warn', |
| 345 | + new=_warn_mock) |
| 346 | + def test_emits_warning_on_no_request_in_djangoengine(self): |
| 347 | + """ |
| 348 | + Should emit warnings on having no request in context (django |
| 349 | + template). |
| 350 | + """ |
| 351 | + self.compile_bundles('webpack.config.skipCommon.js') |
| 352 | + asset_vendor = ( |
| 353 | + '<script src="/static/django_webpack_loader_bundles/vendors.js" >' |
| 354 | + '</script>') |
| 355 | + asset_app1 = ( |
| 356 | + '<script src="/static/django_webpack_loader_bundles/app1.js" >' |
| 357 | + '</script>') |
| 358 | + asset_app2 = ( |
| 359 | + '<script src="/static/django_webpack_loader_bundles/app2.js" >' |
| 360 | + '</script>') |
| 361 | + |
| 362 | + # Shouldn't call any `warn()` here |
| 363 | + dups_template = Template(template_string=( |
| 364 | + r'{% load render_bundle from webpack_loader %}' |
| 365 | + r'{% render_bundle "app1" %}' |
| 366 | + r'{% render_bundle "app2" %}')) # type: Template |
| 367 | + output = dups_template.render(context=Context()) |
| 368 | + _warn_mock.assert_not_called() |
| 369 | + self.assertEqual(output.count(asset_app1), 1) |
| 370 | + self.assertEqual(output.count(asset_app2), 1) |
| 371 | + self.assertEqual(output.count(asset_vendor), 2) |
| 372 | + |
| 373 | + # Should call `warn()` here |
| 374 | + nodups_template = Template(template_string=( |
| 375 | + r'{% load render_bundle from webpack_loader %}' |
| 376 | + r'{% render_bundle "app1" %}' |
| 377 | + r'{% render_bundle "app2" skip_common_chunks=True %}') |
| 378 | + ) # type: Template |
| 379 | + output = nodups_template.render(context=Context()) |
| 380 | + self.assertEqual(output.count(asset_app1), 1) |
| 381 | + self.assertEqual(output.count(asset_app2), 1) |
| 382 | + self.assertEqual(output.count(asset_vendor), 2) |
| 383 | + _warn_mock.assert_called_once_with( |
| 384 | + message=_WARNING_MESSAGE, category=RuntimeWarning) |
| 385 | + |
| 386 | + # Should NOT call `warn()` here |
| 387 | + _warn_mock.reset_mock() |
| 388 | + nodups_template = Template(template_string=( |
| 389 | + r'{% load render_bundle from webpack_loader %}' |
| 390 | + r'{% render_bundle "app1" %}' |
| 391 | + r'{% render_bundle "app2" skip_common_chunks=True %}') |
| 392 | + ) # type: Template |
| 393 | + request = self.factory.get(path='/') |
| 394 | + output = nodups_template.render(context=Context({'request': request})) |
| 395 | + used_tags = getattr(request, '_webpack_loader_used_tags', None) |
| 396 | + self.assertIsNotNone(used_tags, msg=( |
| 397 | + '_webpack_loader_used_tags should be a property of request!')) |
| 398 | + self.assertEqual(output.count(asset_app1), 1) |
| 399 | + self.assertEqual(output.count(asset_app2), 1) |
| 400 | + self.assertEqual(output.count(asset_vendor), 1) |
| 401 | + _warn_mock.assert_not_called() |
| 402 | + _warn_mock.reset_mock() |
| 403 | + |
| 404 | + @patch( |
| 405 | + target='webpack_loader.templatetags.webpack_loader.warn', |
| 406 | + new=_warn_mock) |
| 407 | + def test_emits_warning_on_no_request_in_jinja2engine(self): |
| 408 | + 'Should emit warnings on having no request in context (Jinja2).' |
| 409 | + self.compile_bundles('webpack.config.skipCommon.js') |
| 410 | + settings = { |
| 411 | + 'TEMPLATES': [ |
| 412 | + { |
| 413 | + 'NAME': 'jinja2', |
| 414 | + 'BACKEND': 'django_jinja.backend.Jinja2', |
| 415 | + 'APP_DIRS': True, |
| 416 | + 'OPTIONS': { |
| 417 | + 'match_extension': '.jinja', |
| 418 | + 'extensions': DEFAULT_EXTENSIONS + [_OUR_EXTENSION], |
| 419 | + } |
| 420 | + }, |
| 421 | + ] |
| 422 | + } |
| 423 | + asset_vendor = ( |
| 424 | + '<script src="/static/django_webpack_loader_bundles/vendors.js" >' |
| 425 | + '</script>') |
| 426 | + asset_app1 = ( |
| 427 | + '<script src="/static/django_webpack_loader_bundles/app1.js" >' |
| 428 | + '</script>') |
| 429 | + asset_app2 = ( |
| 430 | + '<script src="/static/django_webpack_loader_bundles/app2.js" >' |
| 431 | + '</script>') |
| 432 | + warning_call = MockCall( |
| 433 | + message=_WARNING_MESSAGE, category=RuntimeWarning) |
| 434 | + |
| 435 | + # Shouldn't call any `warn()` here |
| 436 | + with self.settings(**settings): |
| 437 | + jinja2_engine = engines['jinja2'] # type: Jinja2 |
| 438 | + dups_template = jinja2_engine.get_template( |
| 439 | + template_name='home-duplicated.jinja') # type: Jinja2Template |
| 440 | + output = dups_template.render() |
| 441 | + _warn_mock.assert_not_called() |
| 442 | + self.assertEqual(output.count(asset_app1), 2) |
| 443 | + self.assertEqual(output.count(asset_app2), 2) |
| 444 | + self.assertEqual(output.count(asset_vendor), 4) |
| 445 | + |
| 446 | + # Should call `warn()` here |
| 447 | + with self.settings(**settings): |
| 448 | + jinja2_engine = engines['jinja2'] # type: Jinja2 |
| 449 | + nodups_template = jinja2_engine.get_template( |
| 450 | + template_name='home-deduplicated.jinja' |
| 451 | + ) # type: Jinja2Template |
| 452 | + output = nodups_template.render() |
| 453 | + self.assertEqual(output.count(asset_app1), 2) |
| 454 | + self.assertEqual(output.count(asset_app2), 2) |
| 455 | + self.assertEqual(output.count(asset_vendor), 4) |
| 456 | + self.assertEqual(_warn_mock.call_count, 3) |
| 457 | + self.assertListEqual( |
| 458 | + _warn_mock.call_args_list, |
| 459 | + [warning_call, warning_call, warning_call]) |
| 460 | + |
| 461 | + # Should NOT call `warn()` here |
| 462 | + _warn_mock.reset_mock() |
| 463 | + request = self.factory.get(path='/') |
| 464 | + with self.settings(**settings): |
| 465 | + jinja2_engine = engines['jinja2'] # type: Jinja2 |
| 466 | + nodups_template = jinja2_engine.get_template( |
| 467 | + template_name='home-deduplicated.jinja' |
| 468 | + ) # type: Jinja2Template |
| 469 | + output = nodups_template.render(request=request) |
| 470 | + used_tags = getattr(request, '_webpack_loader_used_tags', None) |
| 471 | + self.assertIsNotNone(used_tags, msg=( |
| 472 | + '_webpack_loader_used_tags should be a property of request!')) |
| 473 | + self.assertEqual(output.count(asset_app1), 1) |
| 474 | + self.assertEqual(output.count(asset_app2), 1) |
| 475 | + self.assertEqual(output.count(asset_vendor), 1) |
| 476 | + _warn_mock.assert_not_called() |
| 477 | + _warn_mock.reset_mock() |
| 478 | + |
336 | 479 | def test_skip_common_chunks_djangoengine(self):
|
337 | 480 | """Test case for deduplication of modules with the django engine."""
|
338 | 481 | self.compile_bundles('webpack.config.skipCommon.js')
|
|
0 commit comments