agenda/tests/test_domains.py
Edward Betts e6327780aa Add comprehensive test coverage for 8 modules
Increases overall test coverage from 53% to 56% by adding tests for:
- accommodation.py (60% → 100%)
- birthday.py (24% → 100%)
- calendar.py (19% → 100%)
- carnival.py (33% → 100%)
- domains.py (75% → 100%)
- events_yaml.py (50% → 96%)
- fx.py (14% → 21% for tested functions)
- sun.py (55% → 100%)

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-07-20 11:39:49 +02:00

168 lines
No EOL
6.6 KiB
Python

"""Tests for domain renewal functionality."""
import csv
import os
import tempfile
from datetime import date
from typing import Any
import pytest
from agenda.domains import renewal_dates, url
from agenda.event import Event
class TestRenewalDates:
"""Test the renewal_dates function."""
def test_renewal_dates_empty_directory(self) -> None:
"""Test with empty directory."""
with tempfile.TemporaryDirectory() as temp_dir:
with pytest.raises(ValueError, match="max\\(\\) iterable argument is empty"):
renewal_dates(temp_dir)
def test_renewal_dates_no_matching_files(self) -> None:
"""Test with directory containing no matching files."""
with tempfile.TemporaryDirectory() as temp_dir:
# Create some non-matching files
with open(os.path.join(temp_dir, "other_file.csv"), "w") as f:
f.write("test")
with open(os.path.join(temp_dir, "not_export.csv"), "w") as f:
f.write("test")
with pytest.raises(ValueError, match="max\\(\\) iterable argument is empty"):
renewal_dates(temp_dir)
def test_renewal_dates_single_file(self) -> None:
"""Test with single domain export file."""
with tempfile.TemporaryDirectory() as temp_dir:
# Create a domain export file
filename = "export_domains_01_15_2024_02_30_PM.csv"
filepath = os.path.join(temp_dir, filename)
# Create CSV content
csv_data = [
["fqdn", "date_registry_end_utc"],
["example.com", "2024-06-15T00:00:00Z"],
["test.org", "2024-12-31T00:00:00Z"]
]
with open(filepath, "w", newline="") as f:
writer = csv.writer(f)
writer.writerows(csv_data)
result = renewal_dates(temp_dir)
assert len(result) == 2
# Check first domain
assert result[0].name == "domain"
assert result[0].title == "🌐 example.com"
assert result[0].date == date(2024, 6, 15)
assert result[0].url == url + "example.com"
# Check second domain
assert result[1].name == "domain"
assert result[1].title == "🌐 test.org"
assert result[1].date == date(2024, 12, 31)
assert result[1].url == url + "test.org"
def test_renewal_dates_multiple_files_latest_used(self) -> None:
"""Test that the latest file is used when multiple exist."""
with tempfile.TemporaryDirectory() as temp_dir:
# Create older file
older_filename = "export_domains_01_15_2024_02_30_PM.csv"
older_filepath = os.path.join(temp_dir, older_filename)
older_csv_data = [
["fqdn", "date_registry_end_utc"],
["old.com", "2024-06-15T00:00:00Z"]
]
with open(older_filepath, "w", newline="") as f:
writer = csv.writer(f)
writer.writerows(older_csv_data)
# Create newer file
newer_filename = "export_domains_01_16_2024_03_45_PM.csv"
newer_filepath = os.path.join(temp_dir, newer_filename)
newer_csv_data = [
["fqdn", "date_registry_end_utc"],
["new.com", "2024-08-20T00:00:00Z"]
]
with open(newer_filepath, "w", newline="") as f:
writer = csv.writer(f)
writer.writerows(newer_csv_data)
result = renewal_dates(temp_dir)
# Should only get data from newer file
assert len(result) == 1
assert result[0].title == "🌐 new.com"
assert result[0].date == date(2024, 8, 20)
def test_renewal_dates_empty_csv(self) -> None:
"""Test with empty CSV file."""
with tempfile.TemporaryDirectory() as temp_dir:
filename = "export_domains_01_15_2024_02_30_PM.csv"
filepath = os.path.join(temp_dir, filename)
# Create CSV with only headers
csv_data = [["fqdn", "date_registry_end_utc"]]
with open(filepath, "w", newline="") as f:
writer = csv.writer(f)
writer.writerows(csv_data)
result = renewal_dates(temp_dir)
assert result == []
def test_renewal_dates_directory_not_found(self) -> None:
"""Test error handling when directory doesn't exist."""
with pytest.raises(FileNotFoundError):
renewal_dates("/nonexistent/directory")
def test_renewal_dates_malformed_filename(self) -> None:
"""Test with malformed filename that can't be parsed."""
with tempfile.TemporaryDirectory() as temp_dir:
# Create file with name that starts correctly but can't be parsed as date
filename = "export_domains_invalid_date.csv"
filepath = os.path.join(temp_dir, filename)
with open(filepath, "w") as f:
f.write("test")
# Should raise ValueError when trying to parse the malformed date
with pytest.raises(ValueError, match="time data .* does not match format"):
renewal_dates(temp_dir)
def test_renewal_dates_various_date_formats(self) -> None:
"""Test with various date formats in the CSV."""
with tempfile.TemporaryDirectory() as temp_dir:
filename = "export_domains_01_15_2024_02_30_PM.csv"
filepath = os.path.join(temp_dir, filename)
csv_data = [
["fqdn", "date_registry_end_utc"],
["example1.com", "2024-06-15T12:34:56Z"],
["example2.com", "2024-12-31T00:00:00+00:00"],
["example3.com", "2024-03-20T23:59:59.999Z"]
]
with open(filepath, "w", newline="") as f:
writer = csv.writer(f)
writer.writerows(csv_data)
result = renewal_dates(temp_dir)
assert len(result) == 3
assert result[0].date == date(2024, 6, 15)
assert result[1].date == date(2024, 12, 31)
assert result[2].date == date(2024, 3, 20)
class TestConstants:
"""Test module constants."""
def test_url_constant(self) -> None:
"""Test that URL constant has expected value."""
expected_url = "https://admin.gandi.net/domain/01578ef0-a84b-11e7-bdf3-00163e6dc886/"
assert url == expected_url