Skip to content

Commit caf0840

Browse files
committed
add tests for title resolution
1 parent be9dee4 commit caf0840

File tree

1 file changed

+133
-0
lines changed

1 file changed

+133
-0
lines changed
Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
# docker-compose -f local.yml run --rm django pytest sde_collections/tests/test_title_resolution.py
2+
3+
from unittest.mock import Mock, patch
4+
5+
import pytest
6+
7+
from ..utils.title_resolver import (
8+
clean_text,
9+
is_valid_xpath,
10+
parse_title,
11+
resolve_brace,
12+
resolve_title,
13+
resolve_xpath,
14+
validate_fstring,
15+
)
16+
17+
18+
def test_parse_title():
19+
# Test basic string
20+
assert parse_title("Simple Title") == [("str", "Simple Title")]
21+
22+
# Test f-string
23+
assert parse_title("Hello {title}") == [("str", "Hello "), ("brace", "{title}")]
24+
25+
# Test xpath
26+
assert parse_title("xpath://h1") == [("xpath", "//h1")]
27+
28+
# Test complex pattern
29+
result = parse_title("xpath://h1 | {title} - {collection}")
30+
assert result == [
31+
("xpath", "//h1"),
32+
("str", " | "),
33+
("brace", "{title}"),
34+
("str", " - "),
35+
("brace", "{collection}"),
36+
]
37+
38+
39+
def test_is_valid_xpath():
40+
assert is_valid_xpath("//h1") is True
41+
assert is_valid_xpath("//div[@class='title']") is True
42+
assert is_valid_xpath("invalid xpath") is False
43+
assert is_valid_xpath("//h1[") is False
44+
45+
46+
def test_validate_fstring():
47+
# Valid cases - should not raise
48+
validate_fstring("{title}")
49+
validate_fstring("{url}")
50+
validate_fstring("{collection}")
51+
52+
# Invalid cases
53+
with pytest.raises(ValueError):
54+
validate_fstring("{invalid_var}")
55+
with pytest.raises(ValueError):
56+
validate_fstring("{title.upper()}")
57+
with pytest.raises(ValueError):
58+
validate_fstring("{len(title)}")
59+
60+
61+
def test_resolve_brace():
62+
context = {"title": "Test Title", "url": "https://example.com", "collection": "Test Collection"}
63+
64+
assert resolve_brace("{title}", context) == "Test Title"
65+
assert resolve_brace("{title} - {collection}", context) == "Test Title - Test Collection"
66+
67+
with pytest.raises(ValueError):
68+
resolve_brace("{invalid}", context)
69+
70+
71+
def test_clean_text():
72+
# Test whitespace handling
73+
assert clean_text(" Title \n With\tSpaces ") == "Title With Spaces"
74+
75+
# Test HTML entities
76+
assert clean_text("Title & More") == "Title & More"
77+
78+
# Test unicode normalization
79+
assert clean_text("Café") == "Cafe"
80+
81+
82+
@patch("requests.get")
83+
def test_resolve_xpath(mock_get):
84+
mock_response = Mock()
85+
mock_response.ok = True
86+
mock_response.content = b"""
87+
<html>
88+
<body>
89+
<h1>Test Title</h1>
90+
<div class="content">Inner Content</div>
91+
</body>
92+
</html>
93+
"""
94+
mock_get.return_value = mock_response
95+
96+
# Test basic xpath
97+
assert resolve_xpath("//h1", "https://example.com") == "Test Title"
98+
assert resolve_xpath("//div[@class='content']", "https://example.com") == "Inner Content"
99+
100+
# Test error cases
101+
mock_response.ok = False
102+
with pytest.raises(ValueError):
103+
resolve_xpath("//h1", "https://example.com")
104+
105+
mock_response.ok = True
106+
with pytest.raises(ValueError):
107+
resolve_xpath("//nonexistent", "https://example.com")
108+
109+
110+
@patch("requests.get")
111+
def test_resolve_title(mock_get):
112+
mock_response = Mock()
113+
mock_response.ok = True
114+
mock_response.content = b"""
115+
<html>
116+
<body>
117+
<h1>Dynamic Content</h1>
118+
</body>
119+
</html>
120+
"""
121+
mock_get.return_value = mock_response
122+
123+
context = {"title": "Original Title", "url": "https://example.com", "collection": "Test Collection"}
124+
125+
# Test combination of xpath and f-string
126+
pattern = "xpath://h1 | {title} - {collection}"
127+
assert resolve_title(pattern, context) == "Dynamic Content | Original Title - Test Collection"
128+
129+
# Test simple f-string
130+
assert resolve_title("{title} ({collection})", context) == "Original Title (Test Collection)"
131+
132+
# Test plain string
133+
assert resolve_title("Static Title", context) == "Static Title"

0 commit comments

Comments
 (0)