|
| 1 | +import pytest |
| 2 | + |
| 3 | +from maps2zim.errors import InvalidFormatError |
| 4 | +from maps2zim.zimconfig import DEFAULT_TAGS, ZimConfig |
| 5 | + |
| 6 | + |
| 7 | +def make_config(tags: list[str] | None = None) -> ZimConfig: |
| 8 | + return ZimConfig( |
| 9 | + file_name="test_{region}", |
| 10 | + name="test_{region}", |
| 11 | + title="Test {region}", |
| 12 | + publisher="Publisher", |
| 13 | + creator="Creator", |
| 14 | + description="Desc {region}", |
| 15 | + long_description=None, |
| 16 | + tags=tags, |
| 17 | + secondary_color="#FFFFFF", |
| 18 | + ) |
| 19 | + |
| 20 | + |
| 21 | +PLACEHOLDERS = {"region": "world"} |
| 22 | + |
| 23 | + |
| 24 | +def test_format_includes_default_tags_when_no_tags(): |
| 25 | + config = make_config(tags=None) |
| 26 | + result = config.format(PLACEHOLDERS) |
| 27 | + assert result.tags == DEFAULT_TAGS |
| 28 | + |
| 29 | + |
| 30 | +def test_format_default_tags_overridden_by_user_tag(): |
| 31 | + config = make_config(tags=["_videos:yes"]) |
| 32 | + result = config.format(PLACEHOLDERS) |
| 33 | + assert result.tags is not None |
| 34 | + # _videos:no from defaults should be replaced by _videos:yes |
| 35 | + assert "_videos:yes" in result.tags |
| 36 | + assert "_videos:no" not in result.tags |
| 37 | + |
| 38 | + |
| 39 | +def test_format_all_default_tags_present_with_override(): |
| 40 | + config = make_config(tags=["_videos:yes"]) |
| 41 | + result = config.format(PLACEHOLDERS) |
| 42 | + assert result.tags is not None |
| 43 | + for default_tag in DEFAULT_TAGS: |
| 44 | + key = default_tag.split(":")[0] |
| 45 | + assert any(t.startswith(key + ":") for t in result.tags) |
| 46 | + |
| 47 | + |
| 48 | +def test_format_extra_user_tag_added(): |
| 49 | + config = make_config(tags=["custom_tag"]) |
| 50 | + result = config.format(PLACEHOLDERS) |
| 51 | + assert result.tags is not None |
| 52 | + assert "custom_tag" in result.tags |
| 53 | + # All defaults still present |
| 54 | + for default_tag in DEFAULT_TAGS: |
| 55 | + assert default_tag in result.tags |
| 56 | + |
| 57 | + |
| 58 | +def test_format_multiple_overrides(): |
| 59 | + config = make_config(tags=["_videos:yes", "_pictures:no"]) |
| 60 | + result = config.format(PLACEHOLDERS) |
| 61 | + assert result.tags is not None |
| 62 | + assert "_videos:yes" in result.tags |
| 63 | + assert "_videos:no" not in result.tags |
| 64 | + assert "_pictures:no" in result.tags |
| 65 | + assert "_pictures:yes" not in result.tags |
| 66 | + |
| 67 | + |
| 68 | +def test_format_tags_with_placeholder(): |
| 69 | + config = make_config(tags=["region_{region}"]) |
| 70 | + result = config.format(PLACEHOLDERS) |
| 71 | + assert result.tags is not None |
| 72 | + assert "region_world" in result.tags |
| 73 | + |
| 74 | + |
| 75 | +def test_format_invalid_placeholder_raises(): |
| 76 | + config = make_config(tags=["_{bad_key}:yes"]) |
| 77 | + with pytest.raises(InvalidFormatError): |
| 78 | + config.format(PLACEHOLDERS) |
0 commit comments