|
| 1 | +# SPDX-FileCopyrightText: (c) TagStudio Contributors |
| 2 | +# SPDX-License-Identifier: MIT |
| 3 | + |
| 4 | + |
| 5 | +from pytestqt.exceptions import pytest |
| 6 | + |
| 7 | +from tagstudio.core.media_types import MediaTypes |
| 8 | + |
| 9 | + |
| 10 | +def test_register_and_contains(): |
| 11 | + MediaTypes.register("zzztest.basic", ".zzzfoo", "SEARCH") |
| 12 | + |
| 13 | + assert MediaTypes.contains("zzztest.basic", ".zzzfoo", "SEARCH") |
| 14 | + assert not MediaTypes.contains("zzztest.basic", ".zzzfoo", "RENDER") |
| 15 | + |
| 16 | + |
| 17 | +def test_additive_register(): |
| 18 | + MediaTypes.register("zzztest.additive", ".zzzfoo", "SEARCH") |
| 19 | + MediaTypes.register("zzztest.additive", ".zzzbar", "SEARCH") |
| 20 | + |
| 21 | + assert MediaTypes.contains("zzztest.additive", ".zzzfoo", "SEARCH") |
| 22 | + assert MediaTypes.contains("zzztest.additive", ".zzzbar", "SEARCH") |
| 23 | + |
| 24 | + |
| 25 | +def test_contains_missing_group_raises_error(): |
| 26 | + with pytest.raises(AttributeError, match=r"is not registered"): |
| 27 | + MediaTypes.contains("zzztest.does_not_exist", ".zzzfoo", "SEARCH") |
| 28 | + |
| 29 | + |
| 30 | +def test_dot_notation_chains_to_parents(): |
| 31 | + MediaTypes.register("zzztest.chain.parent.child", ".zzzchild", "SEARCH") |
| 32 | + |
| 33 | + assert MediaTypes.contains("zzztest.chain.parent.child", ".zzzchild", "SEARCH") |
| 34 | + assert MediaTypes.contains("zzztest.chain.parent", ".zzzchild", "SEARCH") |
| 35 | + assert MediaTypes.contains("zzztest.chain", ".zzzchild", "SEARCH") |
| 36 | + |
| 37 | + |
| 38 | +def test_explicit_chain_group(): |
| 39 | + MediaTypes.chain_group("zzztest.composite", ["zzztest.composite_child"]) |
| 40 | + MediaTypes.register("zzztest.composite_child", ".zzzcomposite", "SEARCH") |
| 41 | + |
| 42 | + assert MediaTypes.contains("zzztest.composite", ".zzzcomposite", "SEARCH") |
| 43 | + |
| 44 | + |
| 45 | +def test_equivalent_extensions(): |
| 46 | + MediaTypes.register("zzztest.equiv", [".zzzone", ".zzztwo"], "SEARCH") |
| 47 | + |
| 48 | + assert MediaTypes.get_equivalent_exts(".zzzone") == {".zzzone", ".zzztwo"} |
| 49 | + assert MediaTypes.get_equivalent_exts(".zzztwo") == {".zzzone", ".zzztwo"} |
| 50 | + assert MediaTypes.contains("zzztest.equiv", ".zzzone", "SEARCH") |
| 51 | + assert MediaTypes.contains("zzztest.equiv", ".zzztwo", "SEARCH") |
| 52 | + |
| 53 | + |
| 54 | +def test_get_equivalent_exts_defaults_to_itself(): |
| 55 | + assert MediaTypes.get_equivalent_exts(".zzzunregistered") == {".zzzunregistered"} |
| 56 | + |
| 57 | + |
| 58 | +def test_find(): |
| 59 | + MediaTypes.register("zzztest.find_a", ".zzzfind", "SEARCH") |
| 60 | + MediaTypes.register("zzztest.find_b", ".zzzfind", "RENDER") |
| 61 | + |
| 62 | + search_keys = {group.key for group in MediaTypes.find(".zzzfind", "SEARCH")} |
| 63 | + render_keys = {group.key for group in MediaTypes.find(".zzzfind", "RENDER")} |
| 64 | + |
| 65 | + assert "zzztest.find_a" in search_keys |
| 66 | + assert "zzztest.find_a" not in render_keys |
| 67 | + assert "zzztest.find_b" in render_keys |
| 68 | + assert "zzztest.find_b" not in search_keys |
| 69 | + |
| 70 | + |
| 71 | +def test_add_name_aliases_and_lookup(): |
| 72 | + MediaTypes.register("zzztest.alias_target", ".zzzalias", "SEARCH") |
| 73 | + MediaTypes.add_name_aliases("zzztest.alias_target", ["ZZZ Test Group", "zzztest"]) |
| 74 | + |
| 75 | + assert MediaTypes.get_group_key_from_name("ZZZ Test Group") == "zzztest.alias_target" |
| 76 | + assert MediaTypes.get_group_key_from_name("zzz test group", case_sensitive=False) == ( |
| 77 | + "zzztest.alias_target" |
| 78 | + ) |
| 79 | + assert ( |
| 80 | + MediaTypes.get_group_key_from_name( |
| 81 | + "zzztestGroup", case_sensitive=False, ignore_whitespace=True |
| 82 | + ) |
| 83 | + == "zzztest.alias_target" |
| 84 | + ) |
| 85 | + assert MediaTypes.get_group_key_from_name("Not A Real Alias") is None |
0 commit comments