test_css_files_building.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. import pytest
  2. from ..css import change_css_source, get_theme_media_map, rebuild_css
  3. from ..tasks import build_single_theme_css, build_theme_css
  4. @pytest.fixture
  5. def assert_snapshot_match(snapshot, theme):
  6. def _assert_snapshot_match(result):
  7. result = result.replace(theme.dirname, "themedir")
  8. snapshot.assert_match(result)
  9. return _assert_snapshot_match
  10. @pytest.fixture
  11. def media_map(theme, image):
  12. return get_theme_media_map(theme)
  13. def test_tasks_builds_single_css_file(theme, image, css_needing_build):
  14. build_single_theme_css(css_needing_build.pk)
  15. css_needing_build.refresh_from_db()
  16. assert css_needing_build.build_file
  17. def test_tasks_skips_single_css_file_that_doesnt_require_build(theme, css):
  18. build_single_theme_css(css.pk)
  19. css.refresh_from_db()
  20. assert not css.build_file
  21. def test_tasks_handles_nonexisting_css_file(db):
  22. build_single_theme_css(1)
  23. def test_tasks_builds_theme_css_files_that_require_it(theme, image, css_needing_build):
  24. build_theme_css(theme.pk)
  25. css_needing_build.refresh_from_db()
  26. assert css_needing_build.build_file
  27. def test_tasks_skips_theme_css_files_that_dont_require_build(theme, css):
  28. build_theme_css(theme.pk)
  29. css.refresh_from_db()
  30. assert not css.build_file
  31. def test_tasks_handles_nonexisting_theme(nonexisting_theme):
  32. build_theme_css(nonexisting_theme.pk)
  33. def test_media_map_for_theme_without_any_media_files_returns_empty_dict(theme):
  34. assert get_theme_media_map(theme) == {}
  35. def test_media_map_for_theme_with_media_files_returns_dict_with_data(
  36. theme, image, media
  37. ):
  38. assert get_theme_media_map(theme)
  39. def test_css_file_is_build(media_map, css_needing_build):
  40. rebuild_css(media_map, css_needing_build)
  41. css_needing_build.refresh_from_db()
  42. assert css_needing_build.build_file
  43. def test_build_css_file_is_hashed(media_map, css_needing_build):
  44. rebuild_css(media_map, css_needing_build)
  45. css_needing_build.refresh_from_db()
  46. assert css_needing_build.build_hash
  47. def test_build_css_file_includes_hash_in_filename(media_map, css_needing_build):
  48. rebuild_css(media_map, css_needing_build)
  49. css_needing_build.refresh_from_db()
  50. assert css_needing_build.build_hash in str(css_needing_build.build_file)
  51. def test_build_css_file_has_size_set(media_map, css_needing_build):
  52. rebuild_css(media_map, css_needing_build)
  53. css_needing_build.refresh_from_db()
  54. assert css_needing_build.size
  55. def test_simple_url_to_file_is_replaced_with_valid_url(
  56. assert_snapshot_match, media_map, image
  57. ):
  58. css = ".page-header { background-image: url(%s); }" % image.name
  59. result = change_css_source(media_map, css)
  60. assert_snapshot_match(result)
  61. def test_relative_url_to_file_is_replaced_with_valid_url(
  62. assert_snapshot_match, media_map, image
  63. ):
  64. css = ".page-header { background-image: url(./%s); }" % image.name
  65. result = change_css_source(media_map, css)
  66. assert_snapshot_match(result)
  67. def test_url_to_file_from_create_react_app_is_replaced_with_valid_url(
  68. assert_snapshot_match, media_map, image
  69. ):
  70. hashed_name = str(image.file).split("/")[-1]
  71. css = ".page-header { background-image: url(/static/media/%s); }" % hashed_name
  72. result = change_css_source(media_map, css)
  73. assert_snapshot_match(result)
  74. def test_quoted_url_to_file_is_replaced_with_valid_url(
  75. assert_snapshot_match, media_map, image
  76. ):
  77. css = '.page-header { background-image: url("%s"); }' % image.name
  78. result = change_css_source(media_map, css)
  79. assert_snapshot_match(result)
  80. def test_single_quoted_url_to_file_is_replaced_with_valid_url(
  81. assert_snapshot_match, media_map, image
  82. ):
  83. css = ".page-header { background-image: url('%s'); }" % image.name
  84. result = change_css_source(media_map, css)
  85. assert_snapshot_match(result)
  86. def test_absolute_https_url_to_file_is_not_replaced(media_map):
  87. css = ".page-header { background-image: url(https://cdn.example.com/bg.png); }"
  88. result = change_css_source(media_map, css)
  89. assert result == css
  90. def test_absolute_http_url_to_file_is_not_replaced(media_map):
  91. css = ".page-header { background-image: url(http://cdn.example.com/bg.png); }"
  92. result = change_css_source(media_map, css)
  93. assert result == css
  94. def test_absolute_protocol_relative_url_to_file_is_not_replaced(media_map):
  95. css = ".page-header { background-image: url(://cdn.example.com/bg.png); }"
  96. result = change_css_source(media_map, css)
  97. assert result == css
  98. def test_css_file_with_multiple_different_urls_is_correctly_replaced(
  99. assert_snapshot_match, media_map, image
  100. ):
  101. css = (
  102. ".page-header { background-image: url(http://cdn.example.com/bg.png); }"
  103. '\n.container { background-image: url("%s"); }'
  104. '\n.alert { background-image: url("%s"); }'
  105. ) % (image.name, str(image.file).strip("/")[-1])
  106. result = change_css_source(media_map, css)
  107. assert_snapshot_match(result)