231 lines
6.7 KiB
Python
231 lines
6.7 KiB
Python
"""Tests for get_saint_of_the_day.py.
|
|
|
|
These tests never touch the real CSV, the real pics/ folder, the real .env,
|
|
the real clock, or the real Mastodon server. Fixtures below swap each of
|
|
those for a stand-in the test controls.
|
|
"""
|
|
|
|
from datetime import datetime
|
|
|
|
import pytest
|
|
|
|
import get_saint_of_the_day as saint_of_the_day
|
|
|
|
CSV_HEADER = "Date,Saint Name,French Name,Notes\n"
|
|
|
|
|
|
# --- Fixtures ---------------------------------------------------------------
|
|
# A fixture is a function pytest runs before a test that asks for it by name
|
|
# (as a parameter). `monkeypatch` and `tmp_path` are built-in fixtures:
|
|
# `tmp_path` is a fresh empty folder per test, and `monkeypatch` swaps an
|
|
# attribute for the length of one test, then puts the original back.
|
|
|
|
|
|
@pytest.fixture
|
|
def project_dir(tmp_path, monkeypatch):
|
|
"""Make the script look for its CSV, pics/ and .env inside tmp_path.
|
|
|
|
The script finds its files with Path(__file__).parent, so pointing the
|
|
module's __file__ into tmp_path moves all three at once.
|
|
"""
|
|
monkeypatch.setattr(
|
|
saint_of_the_day, "__file__", str(tmp_path / "get_saint_of_the_day.py")
|
|
)
|
|
(tmp_path / "pics").mkdir()
|
|
return tmp_path
|
|
|
|
|
|
def write_csv(project_dir, rows):
|
|
"""Write a small saints CSV into the test's project folder."""
|
|
(project_dir / "saints_days_france.csv").write_text(
|
|
CSV_HEADER + "".join(row + "\n" for row in rows), encoding="utf-8"
|
|
)
|
|
|
|
|
|
@pytest.fixture
|
|
def set_today(monkeypatch):
|
|
"""Return a function that freezes 'now' to a chosen date."""
|
|
|
|
def _set_today(year, month, day):
|
|
class FrozenDatetime(datetime):
|
|
@classmethod
|
|
def now(cls, tz=None):
|
|
return cls(year, month, day, 8, 0)
|
|
|
|
monkeypatch.setattr(saint_of_the_day, "datetime", FrozenDatetime)
|
|
|
|
return _set_today
|
|
|
|
|
|
# --- get_standardized_name / get_wikipedia_url -----------------------------
|
|
|
|
|
|
def test_standardized_name_replaces_spaces_with_underscores():
|
|
assert (
|
|
saint_of_the_day.get_standardized_name("Francis de Sales")
|
|
== "Francis_de_Sales"
|
|
)
|
|
|
|
|
|
def test_wikipedia_url_points_at_english_wikipedia_article():
|
|
assert (
|
|
saint_of_the_day.get_wikipedia_url("Francis de Sales")
|
|
== "https://en.wikipedia.org/wiki/Francis_de_Sales"
|
|
)
|
|
|
|
|
|
def test_wikipedia_url_percent_encodes_accents_and_apostrophes():
|
|
assert (
|
|
saint_of_the_day.get_wikipedia_url("Mary Magdalene de' Pazzi of Ávila")
|
|
== "https://en.wikipedia.org/wiki/Mary_Magdalene_de%27_Pazzi_of_%C3%81vila"
|
|
)
|
|
|
|
|
|
# --- get_saint_picture_path ------------------------------------------------
|
|
|
|
|
|
def test_picture_path_finds_jpg_named_after_saint(project_dir):
|
|
picture = project_dir / "pics" / "Francis_de_Sales.jpg"
|
|
picture.write_bytes(b"fake image")
|
|
|
|
assert saint_of_the_day.get_saint_picture_path("Francis de Sales") == picture
|
|
|
|
|
|
def test_picture_path_finds_png_named_after_saint(project_dir):
|
|
picture = project_dir / "pics" / "Aloysius_Gonzaga.png"
|
|
picture.write_bytes(b"fake image")
|
|
|
|
assert saint_of_the_day.get_saint_picture_path("Aloysius Gonzaga") == picture
|
|
|
|
|
|
def test_picture_path_is_none_when_saint_has_no_picture(project_dir):
|
|
(project_dir / "pics" / "Someone_Else.jpg").write_bytes(b"fake image")
|
|
|
|
assert saint_of_the_day.get_saint_picture_path("Francis de Sales") is None
|
|
|
|
|
|
# --- get_saints_for_today --------------------------------------------------
|
|
|
|
|
|
def test_saints_for_today_returns_todays_saint_with_details(project_dir, set_today):
|
|
write_csv(
|
|
project_dir,
|
|
[
|
|
"January 21,Agnes,Sainte Agnès,Virgin and Martyr",
|
|
"January 22,Vincent of Saragossa,Saint Vincent,Deacon and Martyr",
|
|
],
|
|
)
|
|
picture = project_dir / "pics" / "Agnes.jpg"
|
|
picture.write_bytes(b"fake image")
|
|
set_today(2026, 1, 21)
|
|
|
|
assert saint_of_the_day.get_saints_for_today() == [
|
|
{
|
|
"name": "Agnes",
|
|
"french_name": "Sainte Agnès",
|
|
"notes": "Virgin and Martyr",
|
|
"wikipedia_url": "https://en.wikipedia.org/wiki/Agnes",
|
|
"picture_path": picture,
|
|
}
|
|
]
|
|
|
|
|
|
def test_saints_for_today_returns_every_saint_sharing_the_date(
|
|
project_dir, set_today
|
|
):
|
|
write_csv(
|
|
project_dir,
|
|
[
|
|
"January 20,Fabian,Saint Fabien,",
|
|
"January 20,Sebastian,Saint Sébastien,",
|
|
],
|
|
)
|
|
set_today(2026, 1, 20)
|
|
|
|
names = [saint["name"] for saint in saint_of_the_day.get_saints_for_today()]
|
|
|
|
assert names == ["Fabian", "Sebastian"]
|
|
|
|
|
|
def test_saints_for_today_skips_feast_days_that_are_not_people(
|
|
project_dir, set_today
|
|
):
|
|
write_csv(
|
|
project_dir,
|
|
[
|
|
"December 8,Immaculate Conception,Immaculée Conception,Solemnity",
|
|
"December 9,Juan Diego,Saint Juan Diego,",
|
|
],
|
|
)
|
|
set_today(2026, 12, 8)
|
|
|
|
assert saint_of_the_day.get_saints_for_today() == []
|
|
|
|
|
|
def test_saints_for_today_matches_single_digit_day_exactly(project_dir, set_today):
|
|
# "January 2" must match "January 2" only: not "January 02",
|
|
# and not "January 20" or "January 21".
|
|
write_csv(
|
|
project_dir,
|
|
[
|
|
"January 2,Basil the Great and Gregory Nazianzen,Saints Basile et Grégoire,",
|
|
"January 20,Fabian,Saint Fabien,",
|
|
"January 21,Agnes,Sainte Agnès,",
|
|
],
|
|
)
|
|
set_today(2026, 1, 2)
|
|
|
|
names = [saint["name"] for saint in saint_of_the_day.get_saints_for_today()]
|
|
|
|
assert names == ["Basil the Great and Gregory Nazianzen"]
|
|
|
|
|
|
def test_saints_for_today_is_empty_when_no_saint_has_the_date(
|
|
project_dir, set_today
|
|
):
|
|
write_csv(project_dir, ["January 21,Agnes,Sainte Agnès,"])
|
|
set_today(2026, 3, 1)
|
|
|
|
assert saint_of_the_day.get_saints_for_today() == []
|
|
|
|
|
|
def test_saints_for_today_has_no_picture_path_when_picture_missing(
|
|
project_dir, set_today
|
|
):
|
|
write_csv(project_dir, ["January 21,Agnes,Sainte Agnès,"])
|
|
set_today(2026, 1, 21)
|
|
|
|
[saint] = saint_of_the_day.get_saints_for_today()
|
|
|
|
assert saint["picture_path"] is None
|
|
|
|
|
|
# --- format_mastodon_post --------------------------------------------------
|
|
|
|
AGNES = {
|
|
"name": "Agnes",
|
|
"french_name": "Sainte Agnès",
|
|
"notes": "Virgin and Martyr",
|
|
"wikipedia_url": "https://en.wikipedia.org/wiki/Agnes",
|
|
"picture_path": None,
|
|
}
|
|
|
|
|
|
def test_mastodon_post_names_date_saint_link_and_hashtags(set_today):
|
|
set_today(2026, 1, 21)
|
|
|
|
assert saint_of_the_day.format_mastodon_post(AGNES) == (
|
|
"January 21: the Feast of Saint Agnes\n"
|
|
"https://en.wikipedia.org/wiki/Agnes\n"
|
|
"\n"
|
|
"#saints #hagiography"
|
|
)
|
|
|
|
|
|
def test_mastodon_post_does_not_zero_pad_single_digit_days(set_today):
|
|
set_today(2026, 11, 8)
|
|
|
|
first_line = saint_of_the_day.format_mastodon_post(AGNES).splitlines()[0]
|
|
|
|
assert first_line == "November 8: the Feast of Saint Agnes"
|