|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +from pathlib import Path |
| 4 | + |
| 5 | +import pytest |
| 6 | +from django.utils.safestring import SafeString |
| 7 | + |
| 8 | +from django_bird.staticfiles import Asset |
| 9 | +from django_bird.staticfiles import AssetType |
| 10 | +from django_bird.staticfiles import registry |
| 11 | + |
| 12 | + |
| 13 | +class TestAsset: |
| 14 | + def test_hash(self): |
| 15 | + asset1 = Asset(Path("static.css"), AssetType.CSS) |
| 16 | + asset2 = Asset(Path("static.css"), AssetType.CSS) |
| 17 | + |
| 18 | + assert asset1 == asset2 |
| 19 | + assert hash(asset1) == hash(asset2) |
| 20 | + |
| 21 | + assets = {asset1, asset2, Asset(Path("other.css"), AssetType.CSS)} |
| 22 | + |
| 23 | + assert len(assets) == 2 |
| 24 | + |
| 25 | + def test_exists(self, tmp_path: Path): |
| 26 | + css_file = tmp_path / "test.css" |
| 27 | + css_file.touch() |
| 28 | + |
| 29 | + asset = Asset(css_file, AssetType.CSS) |
| 30 | + |
| 31 | + assert asset.exists() is True |
| 32 | + |
| 33 | + def test_exists_nonexistent(self): |
| 34 | + missing_asset = Asset(Path("missing.css"), AssetType.CSS) |
| 35 | + assert missing_asset.exists() is False |
| 36 | + |
| 37 | + @pytest.mark.parametrize( |
| 38 | + "path,expected", |
| 39 | + [ |
| 40 | + (Path("static.css"), Asset(Path("static.css"), AssetType.CSS)), |
| 41 | + (Path("static.js"), Asset(Path("static.js"), AssetType.JS)), |
| 42 | + ( |
| 43 | + Path("nested/path/style.css"), |
| 44 | + Asset(Path("nested/path/style.css"), AssetType.CSS), |
| 45 | + ), |
| 46 | + ( |
| 47 | + Path("./relative/script.js"), |
| 48 | + Asset(Path("./relative/script.js"), AssetType.JS), |
| 49 | + ), |
| 50 | + (Path("UPPERCASE.CSS"), Asset(Path("UPPERCASE.CSS"), AssetType.CSS)), |
| 51 | + (Path("mixed.Js"), Asset(Path("mixed.Js"), AssetType.JS)), |
| 52 | + ], |
| 53 | + ) |
| 54 | + def test_from_path(self, path, expected): |
| 55 | + assert Asset.from_path(path) == expected |
| 56 | + |
| 57 | + def test_from_path_invalid(self): |
| 58 | + with pytest.raises(ValueError): |
| 59 | + Asset.from_path(Path("static.html")) |
| 60 | + |
| 61 | + |
| 62 | +class TestRegistry: |
| 63 | + @pytest.fixture |
| 64 | + def registry(self): |
| 65 | + registry.clear() |
| 66 | + yield registry |
| 67 | + registry.clear() |
| 68 | + |
| 69 | + def test_register_asset(self, registry, tmp_path): |
| 70 | + css_file = tmp_path / "test.css" |
| 71 | + css_file.touch() |
| 72 | + asset = Asset(css_file, AssetType.CSS) |
| 73 | + |
| 74 | + registry.register(asset) |
| 75 | + |
| 76 | + assert asset in registry.assets |
| 77 | + |
| 78 | + def test_register_path(self, registry, tmp_path): |
| 79 | + css_file = tmp_path / "test.css" |
| 80 | + css_file.touch() |
| 81 | + |
| 82 | + registry.register(css_file) |
| 83 | + |
| 84 | + assert len(registry.assets) == 1 |
| 85 | + assert next(iter(registry.assets)).path == css_file |
| 86 | + |
| 87 | + def test_register_missing_file(self, registry, tmp_path): |
| 88 | + missing_file = tmp_path / "missing.css" |
| 89 | + |
| 90 | + with pytest.raises(FileNotFoundError): |
| 91 | + registry.register(missing_file) |
| 92 | + |
| 93 | + def test_clear(self, registry, tmp_path): |
| 94 | + css_file = tmp_path / "test.css" |
| 95 | + css_file.touch() |
| 96 | + js_file = tmp_path / "test.js" |
| 97 | + js_file.touch() |
| 98 | + registry.register(Asset(css_file, AssetType.CSS)) |
| 99 | + registry.register(Asset(js_file, AssetType.JS)) |
| 100 | + |
| 101 | + assert len(registry.assets) == 2 |
| 102 | + |
| 103 | + registry.clear() |
| 104 | + |
| 105 | + assert len(registry.assets) == 0 |
| 106 | + |
| 107 | + @pytest.mark.parametrize("asset_type", [AssetType.CSS, AssetType.JS]) |
| 108 | + def test_get_assets(self, asset_type, registry): |
| 109 | + css_asset = Asset(Path("test.css"), AssetType.CSS) |
| 110 | + js_asset = Asset(Path("test.js"), AssetType.JS) |
| 111 | + |
| 112 | + registry.assets = {css_asset, js_asset} |
| 113 | + |
| 114 | + assets = registry.get_assets(asset_type) |
| 115 | + |
| 116 | + assert len(assets) == 1 |
| 117 | + assert all(asset.type == asset_type for asset in assets) |
| 118 | + |
| 119 | + @pytest.mark.parametrize( |
| 120 | + "assets,sort_key,expected", |
| 121 | + [ |
| 122 | + ( |
| 123 | + [ |
| 124 | + Asset(Path("test/a.css"), AssetType.CSS), |
| 125 | + Asset(Path("test/b.css"), AssetType.CSS), |
| 126 | + ], |
| 127 | + None, |
| 128 | + ["test/a.css", "test/b.css"], |
| 129 | + ), |
| 130 | + ( |
| 131 | + [ |
| 132 | + Asset(Path("test/b.css"), AssetType.CSS), |
| 133 | + Asset(Path("other/a.css"), AssetType.CSS), |
| 134 | + ], |
| 135 | + lambda a: a.path.name, |
| 136 | + ["other/a.css", "test/b.css"], |
| 137 | + ), |
| 138 | + ([], None, []), |
| 139 | + ( |
| 140 | + [Asset(Path("test.css"), AssetType.CSS)], |
| 141 | + None, |
| 142 | + ["test.css"], |
| 143 | + ), |
| 144 | + ], |
| 145 | + ) |
| 146 | + def test_sort_assets(self, assets, sort_key, expected, registry): |
| 147 | + kwargs = {} |
| 148 | + if sort_key is not None: |
| 149 | + kwargs["key"] = sort_key |
| 150 | + |
| 151 | + sorted_assets = registry.sort_assets(assets, **kwargs) |
| 152 | + |
| 153 | + assert [str(a.path) for a in sorted_assets] == expected |
| 154 | + |
| 155 | + @pytest.mark.parametrize( |
| 156 | + "asset_type,expected", |
| 157 | + [ |
| 158 | + (AssetType.CSS, '<link rel="stylesheet" href="{}" type="text/css">'), |
| 159 | + (AssetType.JS, '<script src="{}" type="text/javascript">'), |
| 160 | + ], |
| 161 | + ) |
| 162 | + def test_get_format_string(self, asset_type, expected, registry): |
| 163 | + assert registry.get_format_string(asset_type) == expected |
| 164 | + |
| 165 | + @pytest.mark.parametrize( |
| 166 | + "assets,asset_type,expected", |
| 167 | + [ |
| 168 | + (set(), AssetType.CSS, ""), |
| 169 | + ( |
| 170 | + {Asset(Path("test.css"), AssetType.CSS)}, |
| 171 | + AssetType.CSS, |
| 172 | + ('rel="stylesheet"', 'href="test.css"'), |
| 173 | + ), |
| 174 | + ( |
| 175 | + {Asset(Path("test.js"), AssetType.JS)}, |
| 176 | + AssetType.JS, |
| 177 | + ('<script src="test.js"',), |
| 178 | + ), |
| 179 | + ( |
| 180 | + { |
| 181 | + Asset(Path("a.css"), AssetType.CSS), |
| 182 | + Asset(Path("b.css"), AssetType.CSS), |
| 183 | + }, |
| 184 | + AssetType.CSS, |
| 185 | + ('stylesheet" href="a.css"', 'stylesheet" href="b.css"'), |
| 186 | + ), |
| 187 | + ], |
| 188 | + ) |
| 189 | + def test_render(self, assets, asset_type, expected, registry): |
| 190 | + registry.assets = assets |
| 191 | + |
| 192 | + rendered = registry.render(asset_type) |
| 193 | + |
| 194 | + assert isinstance(rendered, SafeString) |
| 195 | + for content in expected: |
| 196 | + assert content in rendered |
0 commit comments