Aug 5 - Update code to follow up ST - MT14b

This commit is contained in:
2026-08-05 09:43:00 -04:00
parent 45ad924df8
commit be5484e1fb
9 changed files with 965 additions and 38 deletions
+37 -21
View File
@@ -25,6 +25,18 @@ from datetime import date, datetime, timedelta
import pytest
def _today():
"""Today in the app's timezone, not the machine's.
The routes validate due dates against now_eastern(). Using _today() here
made these tests fail on a UTC host between 20:00 ET and midnight, when the
two calendars disagree — "yesterday" by UTC is still today in Eastern, so a
date the test expected to be rejected was legitimately accepted.
"""
from app.utils.time_utils import now_eastern
return now_eastern().date()
@pytest.fixture
def client(app):
"""Fresh schema + test client for each test (isolated in-memory DB)."""
@@ -81,11 +93,12 @@ def _post_follow_up(client, user, **body):
# ── Creation ─────────────────────────────────────────────────────────────────
def test_follow_up_creates_one_time_plan_schedule_from_the_parent(client):
from app import db
from app.models.inspection_schedule import InspectionSchedule
user, tmpl, fac = _seed('mk', role='project_manager')
parent = _completed_inspection(user, tmpl, fac)
due = date.today() + timedelta(days=7)
due = _today() + timedelta(days=7)
resp = _post_follow_up(client, user,
parent_inspection_id=parent.id,
@@ -95,7 +108,7 @@ def test_follow_up_creates_one_time_plan_schedule_from_the_parent(client):
data = resp.get_json()['data']
assert data['created'] is True
s = InspectionSchedule.query.get(data['scheduled']['id'])
s = db.session.get(InspectionSchedule, data['scheduled']['id'])
assert s.parent_inspection_id == parent.id
assert s.is_follow_up is True
# Everything derived from the parent, nothing taken from the client.
@@ -130,11 +143,12 @@ def test_follow_up_ignores_client_supplied_facility_and_template(client):
resp = _post_follow_up(client, user,
parent_inspection_id=parent.id,
due_date=(date.today() + timedelta(days=3)).isoformat(),
due_date=(_today() + timedelta(days=3)).isoformat(),
facility_id=other_fac.id,
template_id=other_tmpl.id,
frequency='daily', mode='auto')
s = InspectionSchedule.query.get(resp.get_json()['data']['scheduled']['id'])
s = db.session.get(InspectionSchedule,
resp.get_json()['data']['scheduled']['id'])
assert s.facility_id == fac.id
assert s.template_id == tmpl.id
assert s.frequency == 'once'
@@ -146,8 +160,8 @@ def test_follow_up_is_idempotent_on_retry(client):
user, tmpl, fac = _seed('idem', role='admin')
parent = _completed_inspection(user, tmpl, fac)
first_due = date.today() + timedelta(days=5)
second_due = date.today() + timedelta(days=9)
first_due = _today() + timedelta(days=5)
second_due = _today() + timedelta(days=9)
r1 = _post_follow_up(client, user, parent_inspection_id=parent.id,
due_date=first_due.isoformat())
@@ -171,14 +185,14 @@ def test_follow_up_reschedule_rearms_reminders(client):
user, tmpl, fac = _seed('rearm', role='admin')
parent = _completed_inspection(user, tmpl, fac)
_post_follow_up(client, user, parent_inspection_id=parent.id,
due_date=(date.today() + timedelta(days=2)).isoformat())
due_date=(_today() + timedelta(days=2)).isoformat())
s = InspectionSchedule.query.filter_by(parent_inspection_id=parent.id).one()
s.advance_notified = s.due_notified = s.overdue_notified = True
db.session.commit()
_post_follow_up(client, user, parent_inspection_id=parent.id,
due_date=(date.today() + timedelta(days=12)).isoformat())
due_date=(_today() + timedelta(days=12)).isoformat())
db.session.expire_all()
s = InspectionSchedule.query.filter_by(parent_inspection_id=parent.id).one()
assert s.advance_notified is False
@@ -201,14 +215,14 @@ def test_follow_up_requires_a_completed_parent(client):
db.session.commit()
r = _post_follow_up(client, user, parent_inspection_id=draft.id,
due_date=(date.today() + timedelta(days=1)).isoformat())
due_date=(_today() + timedelta(days=1)).isoformat())
assert r.status_code == 400
def test_follow_up_rejects_bad_input(client):
user, tmpl, fac = _seed('bad', role='admin')
parent = _completed_inspection(user, tmpl, fac)
ok_due = (date.today() + timedelta(days=1)).isoformat()
ok_due = (_today() + timedelta(days=1)).isoformat()
# Missing parent id.
assert _post_follow_up(client, user, due_date=ok_due).status_code == 400
@@ -222,7 +236,7 @@ def test_follow_up_rejects_bad_input(client):
assert _post_follow_up(client, user, parent_inspection_id=parent.id,
due_date='next tuesday').status_code == 400
# Past date.
past = (date.today() - timedelta(days=1)).isoformat()
past = (_today() - timedelta(days=1)).isoformat()
assert _post_follow_up(client, user, parent_inspection_id=parent.id,
due_date=past).status_code == 400
@@ -232,7 +246,7 @@ def test_follow_up_allows_today(client):
user, tmpl, fac = _seed('today', role='admin')
parent = _completed_inspection(user, tmpl, fac)
r = _post_follow_up(client, user, parent_inspection_id=parent.id,
due_date=date.today().isoformat())
due_date=_today().isoformat())
assert r.status_code == 201
@@ -241,7 +255,7 @@ def test_follow_up_rejects_auditor(client):
user, tmpl, fac = _seed('aud', role='auditor')
parent = _completed_inspection(user, tmpl, fac)
r = _post_follow_up(client, user, parent_inspection_id=parent.id,
due_date=(date.today() + timedelta(days=1)).isoformat())
due_date=(_today() + timedelta(days=1)).isoformat())
assert r.status_code == 403
@@ -259,7 +273,7 @@ def test_inspector_cannot_follow_up_someone_elses_inspection(client):
db.session.commit()
r = _post_follow_up(client, other, parent_inspection_id=parent.id,
due_date=(date.today() + timedelta(days=1)).isoformat())
due_date=(_today() + timedelta(days=1)).isoformat())
assert r.status_code == 403
@@ -268,7 +282,7 @@ def test_follow_up_requires_auth(client):
parent = _completed_inspection(user, tmpl, fac)
r = client.post('/api/v1/scheduled-inspections/follow-up',
json={'parent_inspection_id': parent.id,
'due_date': date.today().isoformat()})
'due_date': _today().isoformat()})
assert r.status_code == 401
@@ -284,7 +298,7 @@ def test_web_start_inherits_the_parent_link(client):
user, tmpl, fac = _seed('start', role='admin')
parent = _completed_inspection(user, tmpl, fac)
_post_follow_up(client, user, parent_inspection_id=parent.id,
due_date=date.today().isoformat())
due_date=_today().isoformat())
sched = InspectionSchedule.query.filter_by(parent_inspection_id=parent.id).one()
client.post('/auth/login', data={'username': user.username,
@@ -307,7 +321,7 @@ def test_web_start_resumes_instead_of_duplicating(client):
user, tmpl, fac = _seed('resume', role='admin')
parent = _completed_inspection(user, tmpl, fac)
_post_follow_up(client, user, parent_inspection_id=parent.id,
due_date=date.today().isoformat())
due_date=_today().isoformat())
sched = InspectionSchedule.query.filter_by(parent_inspection_id=parent.id).one()
client.post('/auth/login', data={'username': user.username,
@@ -352,13 +366,14 @@ def test_cron_materialiser_inherits_the_parent_link(client):
def test_api_create_infers_parent_from_the_schedule(client):
"""An older iPad build submits the schedule id but no parent. Without the
inference the parent would stay flagged forever."""
from app import db
from app.models.inspection import Inspection
from app.models.inspection_schedule import InspectionSchedule
user, tmpl, fac = _seed('infer', role='admin')
parent = _completed_inspection(user, tmpl, fac)
_post_follow_up(client, user, parent_inspection_id=parent.id,
due_date=date.today().isoformat())
due_date=_today().isoformat())
sched = InspectionSchedule.query.filter_by(parent_inspection_id=parent.id).one()
resp = client.post('/api/v1/inspections', headers=_auth(user), json={
@@ -369,13 +384,14 @@ def test_api_create_infers_parent_from_the_schedule(client):
assert resp.status_code in (200, 201)
new_id = resp.get_json()['data']['inspection_id']
run = Inspection.query.get(new_id)
run = db.session.get(Inspection, new_id)
assert run.parent_inspection_id == parent.id
# And the whole point of the link: the parent's flag is cleared.
assert parent.follow_up_required is False
def test_api_explicit_parent_wins_over_the_schedule(client):
from app import db
from app.models.inspection import Inspection
from app.models.inspection_schedule import InspectionSchedule
@@ -383,7 +399,7 @@ def test_api_explicit_parent_wins_over_the_schedule(client):
parent_a = _completed_inspection(user, tmpl, fac)
parent_b = _completed_inspection(user, tmpl, fac)
_post_follow_up(client, user, parent_inspection_id=parent_a.id,
due_date=date.today().isoformat())
due_date=_today().isoformat())
sched = InspectionSchedule.query.filter_by(parent_inspection_id=parent_a.id).one()
resp = client.post('/api/v1/inspections', headers=_auth(user), json={
@@ -393,7 +409,7 @@ def test_api_explicit_parent_wins_over_the_schedule(client):
'form_data': {'f1': 5},
})
new_id = resp.get_json()['data']['inspection_id']
assert Inspection.query.get(new_id).parent_inspection_id == parent_b.id
assert db.session.get(Inspection, new_id).parent_inspection_id == parent_b.id
def test_ordinary_schedule_produces_no_parent_link(client):