Skip to content

Commit 743818c

Browse files
committed
add test cases for deprecation logic
1 parent 70de28d commit 743818c

File tree

3 files changed

+54
-9
lines changed

3 files changed

+54
-9
lines changed

src/fundus/parser/base_parser.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -230,11 +230,11 @@ def _search_members(cls, obj_type: type) -> List[Tuple[str, Any]]:
230230
return members
231231

232232
@classmethod
233-
def attributes(cls) -> AttributeCollection:
233+
def attributes(cls, include_all: bool = False) -> AttributeCollection:
234234
# We exclude both __ld and __meta here to hide them from the autogenerated tables. We do so, because
235235
# we neither want them to appear in the attribute_guidelines nor the supported_publishers tables.
236236
attrs: List[Attribute] = [
237-
func for _, func in cls._search_members(Attribute) if func.__name__ not in ["__ld", "__meta"]
237+
func for _, func in cls._search_members(Attribute) if include_all or func.__name__ not in ["__ld", "__meta"]
238238
]
239239
return AttributeCollection(*attrs)
240240

@@ -247,6 +247,10 @@ def functions(cls) -> FunctionCollection:
247247
def cache(self) -> Optional[Dict[str, Any]]:
248248
return self.precomputed.cache if self.precomputed else None
249249

250+
@property
251+
def registered(self) -> List[RegisteredFunction]:
252+
return self._sorted_registered_functions
253+
250254
def _base_setup(self, html: str) -> None:
251255
doc = lxml.html.document_fromstring(html)
252256
self.precomputed = Precomputed(html, doc, get_meta_content(doc), get_ld_content(doc))

tests/fixtures/fixture_parser.py

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
from datetime import datetime
1+
import datetime
2+
from typing import List
23

34
import pytest
45

@@ -17,15 +18,15 @@ class EmptyParserProxy(ParserProxy):
1718
def parser_proxy_with_version():
1819
class ParserProxyWithVersion(ParserProxy):
1920
class Version(BaseParser):
20-
VALID_UNTIL = datetime.now().date()
21+
VALID_UNTIL = datetime.date.today()
2122

2223
return ParserProxyWithVersion
2324

2425

2526
@pytest.fixture
2627
def parser_with_static_method():
2728
class ParserWithStaticMethod(BaseParser):
28-
VALID_UNTIL = datetime.now().date()
29+
VALID_UNTIL = datetime.date.today()
2930

3031
@staticmethod
3132
def test():
@@ -37,7 +38,7 @@ def test():
3738
@pytest.fixture
3839
def parser_with_function_test():
3940
class ParserWithFunctionTest(BaseParser):
40-
VALID_UNTIL = datetime.now().date()
41+
VALID_UNTIL = datetime.date.today()
4142

4243
@function
4344
def test(self):
@@ -49,7 +50,7 @@ def test(self):
4950
@pytest.fixture
5051
def parser_with_attr_title():
5152
class ParserWithAttrTitle(BaseParser):
52-
VALID_UNTIL = datetime.now().date()
53+
VALID_UNTIL = datetime.date.today()
5354

5455
@attribute
5556
def title(self) -> str:
@@ -62,17 +63,36 @@ def title(self) -> str:
6263
def proxy_with_two_versions_and_different_attrs():
6364
class ProxyWithTwoVersionsAndDifferentAttrs(ParserProxy):
6465
class Later(BaseParser):
65-
VALID_UNTIL = datetime(2023, 1, 2).date()
66+
VALID_UNTIL = datetime.date(2023, 1, 2)
6667

6768
@attribute
6869
def title(self) -> str:
6970
return "This is a title"
7071

7172
class Earlier(BaseParser):
72-
VALID_UNTIL = datetime(2023, 1, 1).date()
73+
VALID_UNTIL = datetime.date(2023, 1, 1)
7374

7475
@attribute
7576
def another_title(self) -> str:
7677
return "This is a another title"
7778

7879
return ProxyWithTwoVersionsAndDifferentAttrs
80+
81+
82+
@pytest.fixture
83+
def proxy_with_two_deprecated_attributes():
84+
class ProxyWithTwoDeprecatedAttributes(ParserProxy):
85+
class ParserWithTwoDeprecatedAttributes(BaseParser):
86+
@attribute
87+
def title(self) -> str:
88+
return "This is a title"
89+
90+
@attribute(deprecated=datetime.date(2024, 1, 1))
91+
def topics(self) -> List[str]:
92+
return ["This is a topic"]
93+
94+
@attribute(deprecated=datetime.date(2024, 4, 1))
95+
def authors(self) -> List[str]:
96+
return ["This is a author"]
97+
98+
return ProxyWithTwoDeprecatedAttributes

tests/test_parser.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
AttributeCollection,
1111
BaseParser,
1212
ParserProxy,
13+
RegisteredFunction,
1314
attribute,
1415
)
1516
from fundus.parser.utility import generic_author_parsing
@@ -174,6 +175,26 @@ def test_mapping(self, proxy_with_two_versions_and_different_attrs):
174175
attrs1, attrs2 = parser_proxy.attribute_mapping.values()
175176
assert attrs1.names != attrs2.names
176177

178+
def test_deprecated(self, proxy_with_two_deprecated_attributes):
179+
def get_initialized_attrs(parser: BaseParser) -> List[RegisteredFunction]:
180+
return parser._sorted_registered_functions
181+
182+
proxy: ParserProxy = proxy_with_two_deprecated_attributes()
183+
184+
number_of_attributes = len(proxy.latest_version.attributes(include_all=True))
185+
186+
parser1 = proxy(datetime.date(2023, 1, 1))
187+
assert len(parser1.registered) == number_of_attributes
188+
189+
parser2 = proxy(datetime.date(2024, 3, 1))
190+
assert len(parser2.registered) == number_of_attributes - 1
191+
192+
parser3 = proxy(datetime.date(2024, 4, 1))
193+
assert len(parser3.registered) == number_of_attributes - 2
194+
195+
assert parser3 == proxy(datetime.date(2024, 5, 1))
196+
assert parser3 != parser2 != parser1
197+
177198

178199
# enforce test coverage for test parsing
179200
# because this is also used for the generate_parser_test_files script we export it here

0 commit comments

Comments
 (0)