Browse Source

Test css building

rafalp 6 years ago
parent
commit
3cc59d6a02

+ 8 - 0
misago/themes/admin/tests/conftest.py

@@ -45,6 +45,14 @@ def css_link(admin_client, theme):
 
 
 @pytest.fixture
+def css_needing_build(admin_client, theme):
+    url = reverse("misago:admin:appearance:themes:upload-css", kwargs={"pk": theme.pk})
+    with open(os.path.join(TESTS_DIR, "css", "test.needs-build.css")) as fp:
+        admin_client.post(url, {"assets": [fp]})
+    return theme.css.get(name="test.needs-build.css")
+
+
+@pytest.fixture
 def media(admin_client, theme):
     url = reverse(
         "misago:admin:appearance:themes:upload-media", kwargs={"pk": theme.pk}

+ 0 - 0
misago/themes/admin/tests/css/test.buildable.css → misago/themes/admin/tests/css/test.needs-build.css


+ 68 - 1
misago/themes/admin/tests/test_css_files_building.py

@@ -1,6 +1,7 @@
 import pytest
 
-from ..css import change_css_source, get_theme_media_map
+from ..css import change_css_source, get_theme_media_map, rebuild_css
+from ..tasks import build_single_theme_css, build_theme_css
 
 
 @pytest.fixture
@@ -17,6 +18,72 @@ def media_map(theme, image):
     return get_theme_media_map(theme)
 
 
+def test_tasks_builds_single_css_file(theme, image, css_needing_build):
+    build_single_theme_css(css_needing_build.pk)
+    css_needing_build.refresh_from_db()
+    assert css_needing_build.build_file
+
+
+def test_tasks_skips_single_css_file_that_doesnt_require_build(theme, css):
+    build_single_theme_css(css.pk)
+    css.refresh_from_db()
+    assert not css.build_file
+
+
+def test_tasks_handles_nonexisting_css_file(db):
+    build_single_theme_css(1)
+
+
+def test_tasks_builds_theme_css_files_that_require_it(theme, image, css_needing_build):
+    build_theme_css(theme.pk)
+    css_needing_build.refresh_from_db()
+    assert css_needing_build.build_file
+
+
+def test_tasks_skips_theme_css_files_that_dont_require_build(theme, css):
+    build_theme_css(theme.pk)
+    css.refresh_from_db()
+    assert not css.build_file
+
+
+def test_tasks_handles_nonexisting_theme(nonexisting_theme):
+    build_theme_css(nonexisting_theme.pk)
+
+
+def test_media_map_for_theme_without_any_media_files_returns_empty_dict(theme):
+    assert get_theme_media_map(theme) == {}
+
+
+def test_media_map_for_theme_with_media_files_returns_dict_with_data(
+    theme, image, media
+):
+    assert get_theme_media_map(theme)
+
+
+def test_css_file_is_build(media_map, css_needing_build):
+    rebuild_css(media_map, css_needing_build)
+    css_needing_build.refresh_from_db()
+    assert css_needing_build.build_file
+
+
+def test_build_css_file_is_hashed(media_map, css_needing_build):
+    rebuild_css(media_map, css_needing_build)
+    css_needing_build.refresh_from_db()
+    assert css_needing_build.build_hash
+
+
+def test_build_css_file_includes_hash_in_filename(media_map, css_needing_build):
+    rebuild_css(media_map, css_needing_build)
+    css_needing_build.refresh_from_db()
+    assert css_needing_build.build_hash in str(css_needing_build.build_file)
+
+
+def test_build_css_file_has_size_set(media_map, css_needing_build):
+    rebuild_css(media_map, css_needing_build)
+    css_needing_build.refresh_from_db()
+    assert css_needing_build.size
+
+
 def test_simple_url_to_file_is_replaced_with_valid_url(
     assert_snapshot_match, media_map, image
 ):

+ 3 - 4
misago/themes/admin/tests/test_css_files_creation_and_edition.py

@@ -1,5 +1,3 @@
-import os
-
 import pytest
 from django.urls import reverse
 
@@ -271,12 +269,13 @@ def test_hash_stays_same_if_source_is_not_changed(admin_client, edit_link, css,
 def test_file_is_not_updated_if_form_data_has_no_changes(
     admin_client, edit_link, css, data
 ):
-    original_mtime = os.path.getmtime(css.source_file.path)
+    original_source_file = str(css.source_file)
+    data["name"] = css.name
     data["source"] = css.source_file.read().decode("utf-8")
     admin_client.post(edit_link, data)
 
     css.refresh_from_db()
-    assert original_mtime == os.path.getmtime(css.source_file.path)
+    assert original_source_file == str(css.source_file)
 
 
 def test_adding_image_url_to_edited_file_sets_rebuilding_flag(

+ 1 - 1
misago/themes/admin/tests/test_uploading_css.py

@@ -224,7 +224,7 @@ def test_if_uploaded_css_file_contains_no_image_urls_rebuild_flag_is_not_set(
 
 
 def test_if_uploaded_css_file_contains_image_url_it_has_rebuild_flag_set(upload, theme):
-    css_file = os.path.join(TESTS_DIR, "css", "test.buildable.css")
+    css_file = os.path.join(TESTS_DIR, "css", "test.needs-build.css")
     with open(css_file) as fp:
         upload(theme, fp)