Skip to content

Commit f6c335c

Browse files
gerwyn-ngclaude
andcommitted
Add tests for HistoryStr validation
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent ea0089b commit f6c335c

File tree

1 file changed

+43
-3
lines changed

1 file changed

+43
-3
lines changed

tests/test_coercions.py

Lines changed: 43 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
1-
"""Tests for DateStr and SelectStr coercion types."""
1+
"""Tests for DateStr, SelectStr, and HistoryStr coercion types."""
22

33
import datetime
44

5-
from pydantic import BaseModel
5+
import pytest
6+
from pydantic import BaseModel, ValidationError
67

7-
from ahrefs.types._coercions import DateStr, SelectStr
8+
from ahrefs.types._coercions import DateStr, HistoryStr, SelectStr
89
from ahrefs.types._generated import (
10+
SiteExplorerBestByExternalLinksRequest,
911
SiteExplorerDomainRatingRequest,
1012
SiteExplorerOrganicKeywordsRequest,
1113
)
@@ -19,6 +21,10 @@ class _SelectModel(BaseModel):
1921
select: SelectStr
2022

2123

24+
class _HistoryModel(BaseModel):
25+
history: HistoryStr
26+
27+
2228
class TestDateStr:
2329
def test_string_passthrough(self) -> None:
2430
m = _DateModel(date="2025-01-15")
@@ -39,6 +45,28 @@ def test_list_joined(self) -> None:
3945
assert m.select == "keyword,position"
4046

4147

48+
class TestHistoryStr:
49+
def test_live(self) -> None:
50+
m = _HistoryModel(history="live")
51+
assert m.history == "live"
52+
53+
def test_all_time(self) -> None:
54+
m = _HistoryModel(history="all_time")
55+
assert m.history == "all_time"
56+
57+
def test_since_date(self) -> None:
58+
m = _HistoryModel(history="since:2025-01-01")
59+
assert m.history == "since:2025-01-01"
60+
61+
def test_invalid_value_rejected(self) -> None:
62+
with pytest.raises(ValidationError, match="recent"):
63+
_HistoryModel(history="recent")
64+
65+
def test_invalid_since_format_rejected(self) -> None:
66+
with pytest.raises(ValidationError, match="Invalid history value"):
67+
_HistoryModel(history="since:01-2025")
68+
69+
4270
class TestRequestModelRoundTrip:
4371
def test_date_with_date_object(self) -> None:
4472
req = SiteExplorerDomainRatingRequest(
@@ -69,3 +97,15 @@ def test_select_with_string(self) -> None:
6997
)
7098
data = req.model_dump(mode="json", exclude_none=True)
7199
assert data["select"] == "keyword,position"
100+
101+
def test_history_valid(self) -> None:
102+
req = SiteExplorerBestByExternalLinksRequest(
103+
target="x", select="url_to", history="live"
104+
)
105+
assert req.history == "live"
106+
107+
def test_history_invalid_rejected(self) -> None:
108+
with pytest.raises(ValidationError, match="recent"):
109+
SiteExplorerBestByExternalLinksRequest(
110+
target="x", select="url_to", history="recent"
111+
)

0 commit comments

Comments
 (0)