Files
drive/tests/test_caption_parser.py
2026-10-18 23:48:22 +05:30

103 lines
3.7 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
from caption_parser import parse_caption
from conftest import EXAMPLE_CAPTION
def test_supplied_example():
p = parse_caption(EXAMPLE_CAPTION)
assert p.indexes == ["001", "002"]
assert p.title == "Class 01 Aman sir.mp4"
assert p.topic == "Misc• Grammar Practice"
assert p.batch == "English ( SSC MAINS )"
assert p.extracted_by == "@PUTINxINDIA"
assert p.missing == []
assert p.warnings == []
assert p.raw == EXAMPLE_CAPTION
def test_index_is_never_used_as_title():
p = parse_caption("Index: 007\nIndex: 008\nTopic: Nouns")
assert p.indexes == ["007", "008"]
assert p.title is None
assert "title" in p.missing
def test_multiple_index_order_preserved():
p = parse_caption("Index: 3\nTitle: A\nIndex: 1\nTopic: T\nIndex: 2")
assert p.indexes == ["3", "1", "2"]
def test_fields_in_any_order_and_whitespace_variants():
caption = " topic : Error Detection \n\n\nTITLE:Class 02.mp4\r\n batch: X \nExtracted by :@someone\n"
p = parse_caption(caption)
assert p.topic == "Error Detection"
assert p.title == "Class 02.mp4"
assert p.batch == "X"
assert p.extracted_by == "@someone"
def test_decorations_and_fullwidth_colon():
p = parse_caption("🎬 Title: Class 05.mp4\n📚 Topic: Reading Comprehension\n• Batch - ignored\n**Batch:** B1")
assert p.title == "Class 05.mp4"
assert p.topic == "Reading Comprehension"
assert p.batch == "** B1" # value text is preserved verbatim apart from trimming
def test_missing_title_and_topic():
p = parse_caption("Index: 001\nBatch: English")
assert p.missing == ["title", "topic"]
assert "Title field is missing" in p.warnings
assert "Topic field is missing" in p.warnings
def test_empty_and_none_caption():
for value in (None, "", " \n "):
p = parse_caption(value)
assert p.missing == ["title", "topic"]
assert "caption is empty" in p.warnings
def test_empty_field_values_are_reported():
p = parse_caption("Title:\nTopic: \nIndex:")
assert p.title is None and p.topic is None
assert "Title field is empty" in p.warnings
assert "empty Index field ignored" in p.warnings
def test_unicode_preserved():
p = parse_caption("Title: कक्षा 01 — व्याकरण (भाग १).mp4\nTopic: हिंदी • व्याकरण\nBatch: Ünïcödé ( 2024 )")
assert p.title == "कक्षा 01 — व्याकरण (भाग १).mp4"
assert p.topic == "हिंदी • व्याकरण"
assert p.batch == "Ünïcödé ( 2024 )"
def test_next_field_is_not_swallowed():
p = parse_caption("Title: Class 01\nTopic: Grammar\nBatch: SSC")
assert p.title == "Class 01"
assert p.topic == "Grammar"
def test_flattened_single_line_caption():
p = parse_caption("Index: 001 Title: Class 01 Aman sir.mp4 Topic: Misc• Grammar Practice | Batch: English ( SSC MAINS )")
assert p.indexes == ["001"]
assert p.title == "Class 01 Aman sir.mp4"
assert p.topic == "Misc• Grammar Practice"
assert p.batch == "English ( SSC MAINS )"
def test_duplicate_field_keeps_first_and_warns():
p = parse_caption("Title: First.mp4\nTitle: Second.mp4\nTopic: T")
assert p.title == "First.mp4"
assert any("duplicate Title" in w for w in p.warnings)
def test_unknown_fields_and_free_text_kept_in_extra_or_ignored():
p = parse_caption("Join: @channel\nSome free text line\nTitle: A.mp4\nTopic: T\nSubtitle: not a title")
assert p.title == "A.mp4"
assert p.extra["Join"] == "@channel"
assert p.extra["Subtitle"] == "not a title"
def test_label_must_be_whole_word():
p = parse_caption("Topics: wrong\nTopic: right")
assert p.topic == "right"