/
test_model.py
120 lines (97 loc) · 3.38 KB
/
test_model.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
"""Test the models."""
from datetime import datetime
from pathlib import Path
import pytest
from dateutil import tz
from mkdocs_newsletter.model import (
FeedEntry,
LastNewsletter,
Newsletter,
NewsletterSection,
)
def test_last_newsletter_min_returns_the_smallest_date() -> None:
"""
Given: A LastNewsletter object with different dates.
When: min is called
Then: The smallest one is returned
"""
last_changes = LastNewsletter(
yearly=datetime(2020, 1, 1, tzinfo=tz.tzlocal()),
weekly=datetime(2020, 2, 2, tzinfo=tz.tzlocal()),
)
result = last_changes.min()
assert result == datetime(2020, 1, 1, tzinfo=tz.tzlocal())
def test_last_newsletter_min_returns_none_if_all_empty() -> None:
"""
Given: A LastNewsletter object with no last dates.
When: min is called
Then: None is returned
"""
last_changes = LastNewsletter()
result = last_changes.min()
assert result is None
# AAA01: No act block
def test_newslettersection_can_order_objects() -> None: # noqa: AAA01
"""
Given: Two NewsletterSection objects with different orders.
When: They are compared
Then: The return the expected behavior
"""
important = NewsletterSection(title="Important section", order=0)
unimportant = NewsletterSection(title="Unimportant section", order=1)
assert important > unimportant
# SIM204, C0113: We don't want to simplify to important > unimportant because we
# want to test the __lt__ method
assert not important < unimportant # noqa: C0113, SIM204
# AAA01: No act block
def test_newsletter_can_order_objects() -> None: # noqa: AAA01
"""
Given: Two Newsletter objects with different orders.
When: They are compared
Then: The return the expected behavior
"""
greater = Newsletter(file_=Path("2021_01.md"))
smaller = Newsletter(file_=Path("2020_01.md"))
assert greater > smaller
# SIM204, C0113: We don't want to simplify to important > unimportant because we
# want to test the __lt__ method
assert not greater < smaller # noqa: C0113, SIM204
# AAA01: No act block
def test_feedentry_can_order_objects() -> None: # noqa: AAA01
"""
Given: Two FeedEntry objects with different orders.
When: They are compared
Then: The return the expected behavior
"""
greater = FeedEntry(
published=datetime(2021, 1, 1),
title="Greater",
link="https://test.com", # type: ignore
description="",
)
smaller = FeedEntry(
published=datetime(2020, 1, 1),
title="Smaller",
link="https://test.com", # type: ignore
description="",
)
assert greater > smaller
# SIM204, C0113: We don't want to simplify to important > unimportant because we
# want to test the __lt__ method
assert not greater < smaller # noqa: C0113, SIM204
@pytest.mark.parametrize(
("property_", "message"),
[
("type_", "Can't extract type from file path"),
("date", "Can't extract date from file path"),
],
)
def test_newsletter_handles_wrong_path(property_: str, message: str) -> None:
"""
Given: A Newsletter with a path that doesn't have a date.
When: calling the type_ and date methods
Then: Errors are raised
"""
newsletter = Newsletter(file_=Path("wrong_path.md"))
with pytest.raises(ValueError, match=message):
getattr(newsletter, property_)