afterhours-shift-manager/tests/weekly_post/test_pay_math.py
Adam Moussa a16bf6d370 Add pytest suite and wire it into CI
Stands up the first automated tests for the repo (151 tests) and turns on
the CI test step.

- Lift slack-bot handlers out of create_app() closures to module level so
  they're unit-testable; create_app is now a thin Bolt-wiring layer. No
  behavior change (handler entrypoints and create_app signature unchanged).
- tests/ mirrors src/: shared layer (schedule, blocks, 3CX client,
  ring_scheduler, secrets) + all four Lambdas (pay math, drop/swap/pick/
  admin/register/rate, pickup button, roster sync, queue scheduler).
- All boundaries mocked: DynamoDB/SES/Secrets via moto, 3CX HTTP via
  responses, Slack via fakes, time via freezegun. No real network/AWS.
- pyproject.toml pytest config (pythonpath=src/shared, importlib mode);
  per-package conftest loads each app.py under a unique name to avoid the
  four-app.py collision. tests/requirements.txt for test-only deps.
- ci.yaml: run-tests: true (reusable workflow auto-installs deps) and lint
  the tests dir too.
- README Testing section.

Closes #85
2026-06-01 18:50:27 -04:00

83 lines
3.4 KiB
Python

"""Tests for weekly-post pay calculation helpers."""
from datetime import datetime
from decimal import Decimal
# 2026-06-01 is a Monday — required so DAY_ORDER lines up with real weekdays.
WEEK_START = datetime(2026, 6, 1)
class TestAddShiftToPay:
def test_assigned_shift_is_added(self, weeklypost_app, schedule, seed):
seed.config(shift_rate="50")
seed.weekly("Monday", "114", "Alice")
breakdown, totals = [], {}
weeklypost_app._add_shift_to_pay(
schedule, breakdown, totals, datetime(2026, 6, 1), "Monday"
)
assert len(breakdown) == 1
assert breakdown[0]["name"] == "Alice"
assert breakdown[0]["rate"] == Decimal("50")
assert totals["Alice"]["shifts"] == 1
assert totals["Alice"]["total"] == Decimal("50")
def test_available_shift_is_skipped(self, weeklypost_app, schedule, seed):
seed.config(shift_rate="50")
breakdown, totals = [], {}
weeklypost_app._add_shift_to_pay(
schedule, breakdown, totals, datetime(2026, 6, 1), "Monday"
)
assert breakdown == [] and totals == {}
def test_weekend_day_label(self, weeklypost_app, schedule, seed):
seed.config(shift_rate="50")
seed.weekly("Saturday", "200", "Alice", shift_type="day")
breakdown, totals = [], {}
weeklypost_app._add_shift_to_pay(
schedule, breakdown, totals, datetime(2026, 6, 6), "Saturday", "day"
)
assert breakdown[0]["day"] == "Sat (Day)"
class TestCalculateWeeklyPay:
def test_single_weekday_shift(self, weeklypost_app, schedule, seed):
seed.config(shift_rate="50")
seed.weekly("Monday", "114", "Alice")
record = weeklypost_app._calculate_weekly_pay(schedule, WEEK_START)
assert record["week_start"] == "2026-06-01"
assert len(record["breakdown"]) == 1
assert record["totals"]["Alice"]["total"] == Decimal("50")
def test_per_employee_rate_applied(self, weeklypost_app, schedule, seed):
seed.config(shift_rate="50")
seed.roster("114", "Alice", shift_rate="75")
seed.weekly("Monday", "114", "Alice")
record = weeklypost_app._calculate_weekly_pay(schedule, WEEK_START)
assert record["totals"]["Alice"]["total"] == Decimal("75.0")
def test_weekend_counts_day_and_night(self, weeklypost_app, schedule, seed):
seed.config(shift_rate="50")
seed.weekly("Saturday", "200", "Alice", shift_type="day")
seed.weekly("Saturday", "200", "Alice", shift_type="night")
record = weeklypost_app._calculate_weekly_pay(schedule, WEEK_START)
# Alice worked both Saturday shifts → 2 shifts, $100.
assert record["totals"]["Alice"]["shifts"] == 2
assert record["totals"]["Alice"]["total"] == Decimal("100")
def test_empty_week_has_no_breakdown(self, weeklypost_app, schedule, seed):
seed.config(shift_rate="50")
record = weeklypost_app._calculate_weekly_pay(schedule, WEEK_START)
assert record["breakdown"] == [] and record["totals"] == {}
class TestBuildPayEmailHtml:
def test_renders_totals_rows(self, weeklypost_app):
pay_record = {
"totals": {
"Alice": {"total": Decimal("100"), "rate": Decimal("50"), "shifts": 2}
}
}
html = weeklypost_app._build_pay_email_html("Jun 1 to Jun 7", pay_record)
assert "Jun 1 to Jun 7" in html
assert "Alice" in html
assert "$100.00" in html