loreal-utilisation-dept/backend/tests/test_zoho_parse.py
DJP 04edbfdd2c Initial commit: dockerised FastAPI backend + React/Vite frontend rewrite
Replaces a static SPA that shipped an Airtable PAT in the JS bundle.
The new architecture holds all secrets server-side, fronts the app
behind Apache on optical-dev with the shared-vhost split-build pattern,
and is designed for a later Azure AD/MSAL swap-in.

- backend/   FastAPI + uvicorn, local auth (Azure AD stub), Airtable
             proxy with TTL cache, Zoho .xlsx/.csv parser, merge
             service for utilisation summaries. 28 pytest tests.
- frontend/  React + Vite + TS + Tailwind + Recharts SPA. Login entry
             chunk 12.83 KB gzipped; Recharts lazy-loaded. No tokens
             or Airtable URLs in the built bundle.
- deploy/    Idempotent deploy.sh (port auto-pick 8200-8299,
             .env-persisted) + split-build Apache include template.
- docker-compose.yml pins name: utilisation-dept and binds 127.0.0.1.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-16 12:37:04 -04:00

100 lines
2.8 KiB
Python

"""Tests for the Zoho timelog parser."""
from __future__ import annotations
import io
from datetime import date
from pathlib import Path
import pytest
from openpyxl import Workbook
from app.services.zoho_parse import parse
FIXTURE_CSV = Path(__file__).parent / "fixtures" / "sample_zoho.csv"
def test_canonical_csv_headers():
content = FIXTURE_CSV.read_bytes()
out = parse("sample_zoho.csv", content)
rows = out["rows"]
assert out["content_hash"].startswith("sha256:")
assert out["unrecognised_columns"] == []
assert len(rows) == 4
r0 = rows[0]
assert r0["date"] == date(2026, 5, 4)
assert r0["employee"] == "Bhakti Doshi"
assert r0["project"] == "Acme Spring Launch"
assert r0["hours"] == 7.0
assert r0["billable"] is True
# Idle Time → not billable
assert rows[2]["billable"] is False
# Fee Related → billable
assert rows[3]["billable"] is True
def test_aliased_headers():
csv = (
"Resource,Project,Total Hours,Log Date,Is Billable\n"
"Bhakti Doshi,Acme,7.5,2026-05-04,true\n"
).encode("utf-8")
out = parse("aliased.csv", csv)
assert out["unrecognised_columns"] == []
assert out["rows"][0]["employee"] == "Bhakti Doshi"
assert out["rows"][0]["hours"] == 7.5
assert out["rows"][0]["billable"] is True
assert out["rows"][0]["date"] == date(2026, 5, 4)
def test_unrecognised_header_surfaced():
csv = (
"Date,Resource,Total Hours,Wibble Factor\n"
"2026-05-04,Bhakti,7,5\n"
).encode("utf-8")
out = parse("u.csv", csv)
assert "Wibble Factor" in out["unrecognised_columns"]
# Known columns still parse.
assert out["rows"][0]["employee"] == "Bhakti"
assert out["rows"][0]["hours"] == 7.0
def test_xlsx_path():
wb = Workbook()
ws = wb.active
ws.append(["Date", "Resource Name", "Project Title", "Task", "Hours", "Billable"])
ws.append(["2026-05-04", "Bhakti Doshi", "Acme", "Design", 7.5, "Yes"])
buf = io.BytesIO()
wb.save(buf)
buf.seek(0)
out = parse("up.xlsx", buf.read())
assert out["rows"][0]["employee"] == "Bhakti Doshi"
assert out["rows"][0]["hours"] == 7.5
assert out["rows"][0]["date"] == date(2026, 5, 4)
assert out["rows"][0]["billable"] is True
def test_empty_rows_skipped():
csv = (
"Date,Resource,Hours\n"
"\n"
"2026-05-04,Bhakti,7\n"
",,\n"
).encode("utf-8")
out = parse("blank.csv", csv)
assert len(out["rows"]) == 1
def test_hh_mm_hours_parsed():
csv = (
"Date,Resource,Hours\n"
"2026-05-04,Bhakti,7:30\n"
).encode("utf-8")
out = parse("hhmm.csv", csv)
assert out["rows"][0]["hours"] == pytest.approx(7.5)
def test_content_hash_stable():
out1 = parse("a.csv", FIXTURE_CSV.read_bytes())
out2 = parse("a.csv", FIXTURE_CSV.read_bytes())
assert out1["content_hash"] == out2["content_hash"]