Files
Tagger/tests/test_tag.py

148 lines
4.9 KiB
Python
Raw Permalink Normal View History

2025-12-23 10:41:53 +01:00
import pytest
from src.core.tag import Tag
class TestTag:
"""Testy pro třídu Tag"""
def test_tag_creation(self):
"""Test vytvoření tagu"""
tag = Tag("Kategorie", "Název")
assert tag.category == "Kategorie"
assert tag.name == "Název"
def test_tag_full_path(self):
"""Test full_path property"""
tag = Tag("Video", "HD")
assert tag.full_path == "Video/HD"
def test_tag_str_representation(self):
"""Test string reprezentace"""
tag = Tag("Foto", "Dovolená")
assert str(tag) == "Foto/Dovolená"
def test_tag_repr(self):
"""Test repr reprezentace"""
tag = Tag("Audio", "Hudba")
assert repr(tag) == "Tag(Audio/Hudba)"
def test_tag_equality_same_tags(self):
"""Test rovnosti stejných tagů"""
tag1 = Tag("Kategorie", "Název")
tag2 = Tag("Kategorie", "Název")
assert tag1 == tag2
def test_tag_equality_different_tags(self):
"""Test nerovnosti různých tagů"""
tag1 = Tag("Kategorie1", "Název")
tag2 = Tag("Kategorie2", "Název")
assert tag1 != tag2
tag3 = Tag("Kategorie", "Název1")
tag4 = Tag("Kategorie", "Název2")
assert tag3 != tag4
def test_tag_equality_with_non_tag(self):
"""Test porovnání s ne-Tag objektem"""
tag = Tag("Kategorie", "Název")
assert tag != "Kategorie/Název"
assert tag != 123
assert tag != None
def test_tag_hash(self):
"""Test hashování - důležité pro použití v set/dict"""
tag1 = Tag("Kategorie", "Název")
tag2 = Tag("Kategorie", "Název")
tag3 = Tag("Jiná", "Název")
# Stejné tagy mají stejný hash
assert hash(tag1) == hash(tag2)
# Různé tagy mají různý hash (většinou)
assert hash(tag1) != hash(tag3)
def test_tag_in_set(self):
"""Test použití tagů v set"""
tag1 = Tag("Kategorie", "Název")
tag2 = Tag("Kategorie", "Název")
tag3 = Tag("Jiná", "Název")
tag_set = {tag1, tag2, tag3}
# tag1 a tag2 jsou stejné, takže set obsahuje pouze 2 prvky
assert len(tag_set) == 2
assert tag1 in tag_set
assert tag3 in tag_set
def test_tag_in_dict(self):
"""Test použití tagů jako klíčů v dict"""
tag1 = Tag("Kategorie", "Název")
tag2 = Tag("Kategorie", "Název")
tag_dict = {tag1: "hodnota1"}
tag_dict[tag2] = "hodnota2"
# tag1 a tag2 jsou stejné, takže dict má 1 klíč
assert len(tag_dict) == 1
assert tag_dict[tag1] == "hodnota2"
def test_tag_with_special_characters(self):
"""Test tagů se speciálními znaky"""
tag = Tag("Kategorie/Složitá", "Název s mezerami")
assert tag.category == "Kategorie/Složitá"
assert tag.name == "Název s mezerami"
assert tag.full_path == "Kategorie/Složitá/Název s mezerami"
def test_tag_with_empty_strings(self):
"""Test tagů s prázdnými řetězci"""
tag = Tag("", "")
assert tag.category == ""
assert tag.name == ""
assert tag.full_path == "/"
def test_tag_unicode(self):
"""Test tagů s unicode znaky"""
tag = Tag("Kategorie", "Čeština")
assert tag.category == "Kategorie"
assert tag.name == "Čeština"
assert tag.full_path == "Kategorie/Čeština"
2025-12-30 07:54:30 +01:00
class TestTagFromString:
"""Testy pro Tag.from_string() class method"""
def test_from_string_with_category(self):
"""Test parsování stringu s kategorií"""
tag = Tag.from_string("Stav/Nové")
assert tag.category == "Stav"
assert tag.name == "Nové"
def test_from_string_without_category(self):
"""Test parsování stringu bez kategorie - použije default"""
tag = Tag.from_string("simple")
assert tag.category == "default"
assert tag.name == "simple"
def test_from_string_custom_default_category(self):
"""Test parsování s vlastní default kategorií"""
tag = Tag.from_string("simple", default_category="Custom")
assert tag.category == "Custom"
assert tag.name == "simple"
def test_from_string_multiple_slashes(self):
"""Test parsování stringu s více lomítky"""
tag = Tag.from_string("Kategorie/Název/s/lomítky")
assert tag.category == "Kategorie"
assert tag.name == "Název/s/lomítky"
def test_from_string_unicode(self):
"""Test parsování unicode stringu"""
tag = Tag.from_string("Žánr/Komedie")
assert tag.category == "Žánr"
assert tag.name == "Komedie"
def test_from_string_equality(self):
"""Test že from_string vytváří ekvivalentní tag"""
tag1 = Tag("Stav", "Nové")
tag2 = Tag.from_string("Stav/Nové")
assert tag1 == tag2
assert hash(tag1) == hash(tag2)