Skip to content

Commit daa060e

Browse files
committed
Refactor tests for expiring documents and upcoming changes to use parameterized inputs for better coverage and maintainability
1 parent c0a0199 commit daa060e

2 files changed

Lines changed: 47 additions & 95 deletions

File tree

test/test_find_expiring_docs.py

Lines changed: 15 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -102,57 +102,29 @@ def test_load_json_with_nested_data(self, tmp_path):
102102
class TestHasExpiringDatetime:
103103
"""Test the has_expiring_datetime function."""
104104

105-
def test_has_valid_datetime(self):
106-
"""Test data with valid tidsbegransadDateTime."""
105+
@pytest.mark.parametrize("datetime_value", [
106+
"2025-12-31T23:59:59", # Full datetime
107+
"2025-12-31", # Date only
108+
"2024-01-01T00:00:00", # Another valid datetime
109+
])
110+
def test_has_valid_datetime(self, datetime_value):
111+
"""Test data with valid tidsbegransadDateTime values."""
107112
data = {
108113
"beteckning": "2024:1",
109-
"tidsbegransadDateTime": "2025-12-31T23:59:59"
114+
"tidsbegransadDateTime": datetime_value
110115
}
111116

112117
result = has_expiring_datetime(data)
113118

114119
assert result is True
115120

116-
def test_has_datetime_date_only(self):
117-
"""Test data with date-only tidsbegransadDateTime."""
118-
data = {
119-
"beteckning": "2024:1",
120-
"tidsbegransadDateTime": "2025-12-31"
121-
}
122-
123-
result = has_expiring_datetime(data)
124-
125-
assert result is True
126-
127-
def test_datetime_is_none(self):
128-
"""Test data with None tidsbegransadDateTime."""
129-
data = {
130-
"beteckning": "2024:1",
131-
"tidsbegransadDateTime": None
132-
}
133-
134-
result = has_expiring_datetime(data)
135-
136-
assert result is False
137-
138-
def test_datetime_is_empty_string(self):
139-
"""Test data with empty string tidsbegransadDateTime."""
140-
data = {
141-
"beteckning": "2024:1",
142-
"tidsbegransadDateTime": ""
143-
}
144-
145-
result = has_expiring_datetime(data)
146-
147-
assert result is False
148-
149-
def test_datetime_field_missing(self):
150-
"""Test data without tidsbegransadDateTime field."""
151-
data = {
152-
"beteckning": "2024:1",
153-
"rubrik": "Test"
154-
}
155-
121+
@pytest.mark.parametrize("data,description", [
122+
({"beteckning": "2024:1", "tidsbegransadDateTime": None}, "None value"),
123+
({"beteckning": "2024:1", "tidsbegransadDateTime": ""}, "Empty string"),
124+
({"beteckning": "2024:1", "rubrik": "Test"}, "Missing field"),
125+
])
126+
def test_datetime_falsy_values(self, data, description):
127+
"""Test data with None, empty string, or missing tidsbegransadDateTime."""
156128
result = has_expiring_datetime(data)
157129

158130
assert result is False

test/test_upcoming_changes.py

Lines changed: 32 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,15 @@
2424
class TestIdentifyUpcomingChanges:
2525
"""Test the identify_upcoming_changes function."""
2626

27-
def test_extract_ikraft_datum_from_section(self):
28-
"""Test extracting ikraft_datum from section tag."""
29-
content = '''<section id="1" class="paragraf" selex:ikraft_datum="2025-06-01">
27+
@pytest.mark.parametrize("date_attr,date_value,expected_type,section_id", [
28+
("ikraft_datum", "2025-06-01", "ikraft", "1"),
29+
("upphor_datum", "2025-12-31", "upphor", "2"),
30+
])
31+
def test_extract_date_from_section(self, date_attr, date_value, expected_type, section_id):
32+
"""Test extracting ikraft_datum and upphor_datum from section tag."""
33+
content = f'''<section id="{section_id}" class="paragraf" selex:{date_attr}="{date_value}">
3034
31-
## 1 §
35+
## {section_id} §
3236
3337
Content here
3438
@@ -37,28 +41,11 @@ def test_extract_ikraft_datum_from_section(self):
3741
result = identify_upcoming_changes(content)
3842

3943
assert len(result) == 1
40-
assert result[0]['type'] == 'ikraft'
41-
assert result[0]['date'] == '2025-06-01'
44+
assert result[0]['type'] == expected_type
45+
assert result[0]['date'] == date_value
4246
assert result[0]['source'] == 'section_tag'
43-
assert result[0]['section_id'] == '1'
44-
assert result[0]['section_title'] == '1 §'
45-
46-
def test_extract_upphor_datum_from_section(self):
47-
"""Test extracting upphor_datum from section tag."""
48-
content = '''<section id="2" class="paragraf" selex:upphor_datum="2025-12-31">
49-
50-
## 2 §
51-
52-
This section expires.
53-
54-
</section>'''
55-
56-
result = identify_upcoming_changes(content)
57-
58-
assert len(result) == 1
59-
assert result[0]['type'] == 'upphor'
60-
assert result[0]['date'] == '2025-12-31'
61-
assert result[0]['section_id'] == '2'
47+
assert result[0]['section_id'] == section_id
48+
assert result[0]['section_title'] == f'{section_id} §'
6249

6350
def test_extract_from_kapital_section(self):
6451
"""Test extracting from chapter (kapital) section."""
@@ -77,26 +64,19 @@ def test_extract_from_kapital_section(self):
7764
assert result[0]['class_name'] == 'kapital'
7865
assert result[0]['section_title'] == '1 kap. Inledande bestämmelser'
7966

80-
def test_extract_ikraft_datum_from_article(self):
81-
"""Test extracting ikraft_datum from article tag."""
82-
content = '<article selex:ikraft_datum="2025-03-15">Content</article>'
83-
84-
result = identify_upcoming_changes(content)
85-
86-
assert len(result) == 1
87-
assert result[0]['type'] == 'ikraft'
88-
assert result[0]['date'] == '2025-03-15'
89-
assert result[0]['source'] == 'article_tag'
90-
91-
def test_extract_upphor_datum_from_article(self):
92-
"""Test extracting upphor_datum from article tag."""
93-
content = '<article selex:upphor_datum="2026-12-31">Content</article>'
67+
@pytest.mark.parametrize("date_attr,date_value,expected_type", [
68+
("ikraft_datum", "2025-03-15", "ikraft"),
69+
("upphor_datum", "2026-12-31", "upphor"),
70+
])
71+
def test_extract_date_from_article(self, date_attr, date_value, expected_type):
72+
"""Test extracting ikraft_datum and upphor_datum from article tag."""
73+
content = f'<article selex:{date_attr}="{date_value}">Content</article>'
9474

9575
result = identify_upcoming_changes(content)
9676

9777
assert len(result) == 1
98-
assert result[0]['type'] == 'upphor'
99-
assert result[0]['date'] == '2026-12-31'
78+
assert result[0]['type'] == expected_type
79+
assert result[0]['date'] == date_value
10080
assert result[0]['source'] == 'article_tag'
10181

10282
def test_extract_with_upphavd_flag(self):
@@ -131,20 +111,20 @@ def test_multiple_dates_in_document(self):
131111
assert result[1]['date'] == '2025-06-01'
132112
assert result[2]['date'] == '2025-12-31'
133113

134-
def test_invalid_date_format_ignored(self):
135-
"""Test that invalid date formats are ignored."""
136-
content = '''<section id="1" class="paragraf" selex:ikraft_datum="2025-13-45">
114+
@pytest.mark.parametrize("invalid_date,tag_type", [
115+
("2025-13-45", "section"), # Invalid month/day
116+
("not-a-date", "article"), # Malformed date
117+
("2025-02-30", "section"), # Invalid day for month
118+
])
119+
def test_invalid_dates_ignored(self, invalid_date, tag_type):
120+
"""Test that invalid and malformed dates are ignored."""
121+
if tag_type == "section":
122+
content = f'''<section id="1" class="paragraf" selex:ikraft_datum="{invalid_date}">
137123
## 1 §
138124
Invalid date
139125
</section>'''
140-
141-
result = identify_upcoming_changes(content)
142-
143-
assert len(result) == 0
144-
145-
def test_malformed_date_ignored(self):
146-
"""Test that malformed dates are ignored."""
147-
content = '''<article selex:ikraft_datum="not-a-date">Content</article>'''
126+
else:
127+
content = f'<article selex:ikraft_datum="{invalid_date}">Content</article>'
148128

149129
result = identify_upcoming_changes(content)
150130

0 commit comments

Comments
 (0)