--
This commit is contained in:
@@ -7,16 +7,17 @@ from tests.e2e.test_full_approval_flow import _create_masters
|
||||
API = "/api/v1"
|
||||
|
||||
|
||||
async def _draft(client, auth, org):
|
||||
async def _draft(client, auth, org, approved_plan):
|
||||
doctors, chemists = await _create_masters(client, auth, org, n_doctors=2, n_chemists=1)
|
||||
await approved_plan()
|
||||
resp = await client.post(f"{API}/dcr", headers=auth["mr1"], json={"report_date": date.today().isoformat()})
|
||||
assert resp.status_code == 201, resp.text
|
||||
return resp.json()["id"], doctors, chemists
|
||||
|
||||
|
||||
class TestFormFields:
|
||||
async def test_fields_round_trip_through_the_api(self, client, auth, org):
|
||||
dcr_id, doctors, chemists = await _draft(client, auth, org)
|
||||
async def test_fields_round_trip_through_the_api(self, client, auth, org, approved_plan):
|
||||
dcr_id, doctors, chemists = await _draft(client, auth, org, approved_plan)
|
||||
d = await client.post(
|
||||
f"{API}/dcr/{dcr_id}/calls/doctor", headers=auth["mr1"],
|
||||
json={"doctor_id": doctors[0]["id"], "visit_time": "09:30:00", "call_outcome": "MET",
|
||||
@@ -35,27 +36,48 @@ class TestFormFields:
|
||||
assert patched.status_code == 200, patched.text
|
||||
|
||||
report = (await client.get(f"{API}/dcr/{dcr_id}", headers=auth["mr1"])).json()
|
||||
assert report["doctor_calls"][0]["product_promote"] == "Calcium" # trimmed
|
||||
assert report["doctor_calls"][0]["product_sample"] == "Vit D"
|
||||
# A single name is still accepted, and comes back as a one-item list.
|
||||
assert report["doctor_calls"][0]["product_promote"] == ["Calcium"] # trimmed
|
||||
assert report["doctor_calls"][0]["product_sample"] == ["Vit D"]
|
||||
assert report["chemist_calls"][0]["pob"] == "12,500"
|
||||
assert report["stockist_visit"] == "Ganesh Distributors"
|
||||
assert report["order_value"] == "45,000"
|
||||
|
||||
async def test_blank_values_are_stored_as_null_and_can_be_cleared(self, client, auth, org):
|
||||
dcr_id, _, _ = await _draft(client, auth, org)
|
||||
async def test_several_products_per_visit(self, client, auth, org, approved_plan):
|
||||
dcr_id, doctors, _ = await _draft(client, auth, org, approved_plan)
|
||||
d = await client.post(
|
||||
f"{API}/dcr/{dcr_id}/calls/doctor", headers=auth["mr1"],
|
||||
json={"doctor_id": doctors[0]["id"], "visit_time": "09:30:00", "call_outcome": "MET",
|
||||
"product_promote": ["Calcium", " Vit D ", "calcium", "", "Zinc"],
|
||||
"product_sample": ["Iron", "Omega 3", "B12"]},
|
||||
)
|
||||
assert d.status_code == 201, d.text
|
||||
call = d.json()["doctor_calls"][0]
|
||||
assert call["product_promote"] == ["Calcium", "Vit D", "Zinc"] # trimmed, de-duplicated
|
||||
assert call["product_sample"] == ["Iron", "Omega 3", "B12"]
|
||||
|
||||
too_many = await client.post(
|
||||
f"{API}/dcr/{dcr_id}/calls/doctor", headers=auth["mr1"],
|
||||
json={"doctor_id": doctors[1]["id"], "visit_time": "09:40:00", "call_outcome": "MET",
|
||||
"product_promote": [f"P{i}" for i in range(11)]},
|
||||
)
|
||||
assert too_many.status_code == 422
|
||||
|
||||
async def test_blank_values_are_stored_as_null_and_can_be_cleared(self, client, auth, org, approved_plan):
|
||||
dcr_id, _, _ = await _draft(client, auth, org, approved_plan)
|
||||
await client.patch(f"{API}/dcr/{dcr_id}", headers=auth["mr1"], json={"stockist_visit": "X"})
|
||||
cleared = await client.patch(f"{API}/dcr/{dcr_id}", headers=auth["mr1"], json={"stockist_visit": " "})
|
||||
assert cleared.json()["stockist_visit"] is None
|
||||
|
||||
async def test_patching_one_field_leaves_the_others_alone(self, client, auth, org):
|
||||
dcr_id, _, _ = await _draft(client, auth, org)
|
||||
async def test_patching_one_field_leaves_the_others_alone(self, client, auth, org, approved_plan):
|
||||
dcr_id, _, _ = await _draft(client, auth, org, approved_plan)
|
||||
await client.patch(f"{API}/dcr/{dcr_id}", headers=auth["mr1"], json={"mr_remarks": "Good day", "order_value": "10"})
|
||||
again = await client.patch(f"{API}/dcr/{dcr_id}", headers=auth["mr1"], json={"stockist_visit": "S"})
|
||||
body = again.json()
|
||||
assert (body["mr_remarks"], body["order_value"], body["stockist_visit"]) == ("Good day", "10", "S")
|
||||
|
||||
async def test_too_long_values_are_rejected(self, client, auth, org):
|
||||
dcr_id, doctors, _ = await _draft(client, auth, org)
|
||||
async def test_too_long_values_are_rejected(self, client, auth, org, approved_plan):
|
||||
dcr_id, doctors, _ = await _draft(client, auth, org, approved_plan)
|
||||
too_long = await client.patch(f"{API}/dcr/{dcr_id}", headers=auth["mr1"], json={"stockist_visit": "x" * 201})
|
||||
assert too_long.status_code == 422
|
||||
bad_call = await client.post(
|
||||
@@ -64,8 +86,8 @@ class TestFormFields:
|
||||
)
|
||||
assert bad_call.status_code == 422
|
||||
|
||||
async def test_only_the_owner_can_edit_them_and_only_while_editable(self, client, auth, org):
|
||||
dcr_id, doctors, _ = await _draft(client, auth, org)
|
||||
async def test_only_the_owner_can_edit_them_and_only_while_editable(self, client, auth, org, approved_plan):
|
||||
dcr_id, doctors, _ = await _draft(client, auth, org, approved_plan)
|
||||
assert (await client.patch(f"{API}/dcr/{dcr_id}", headers=auth["mr2"], json={"order_value": "1"})).status_code in (403, 404)
|
||||
assert (await client.patch(f"{API}/dcr/{dcr_id}", headers=auth["am1"], json={"order_value": "1"})).status_code == 403
|
||||
await client.post(
|
||||
@@ -76,8 +98,8 @@ class TestFormFields:
|
||||
locked = await client.patch(f"{API}/dcr/{dcr_id}", headers=auth["mr1"], json={"order_value": "1"})
|
||||
assert locked.status_code == 409 # submitted reports are locked
|
||||
|
||||
async def test_approved_pdf_is_generated_with_the_new_layout(self, client, auth, org):
|
||||
dcr_id, doctors, chemists = await _draft(client, auth, org)
|
||||
async def test_approved_pdf_is_generated_with_the_new_layout(self, client, auth, org, approved_plan):
|
||||
dcr_id, doctors, chemists = await _draft(client, auth, org, approved_plan)
|
||||
await client.post(
|
||||
f"{API}/dcr/{dcr_id}/calls/doctor", headers=auth["mr1"],
|
||||
json={"doctor_id": doctors[0]["id"], "visit_time": "09:30:00", "call_outcome": "MET", "product_promote": "Calcium"},
|
||||
@@ -91,8 +113,8 @@ class TestFormFields:
|
||||
class TestResponsesReflectTheChange:
|
||||
"""Regression: the response to an add/remove used to list the visits as they were BEFORE it."""
|
||||
|
||||
async def test_adding_a_visit_returns_it_in_the_same_response(self, client, auth, org):
|
||||
dcr_id, doctors, chemists = await _draft(client, auth, org)
|
||||
async def test_adding_a_visit_returns_it_in_the_same_response(self, client, auth, org, approved_plan):
|
||||
dcr_id, doctors, chemists = await _draft(client, auth, org, approved_plan)
|
||||
added = await client.post(
|
||||
f"{API}/dcr/{dcr_id}/calls/doctor", headers=auth["mr1"],
|
||||
json={"doctor_id": doctors[0]["id"], "visit_time": "09:30:00", "call_outcome": "MET"},
|
||||
@@ -105,8 +127,8 @@ class TestResponsesReflectTheChange:
|
||||
body = second.json()
|
||||
assert len(body["doctor_calls"]) == 1 and len(body["chemist_calls"]) == 1
|
||||
|
||||
async def test_removing_a_visit_returns_the_report_without_it(self, client, auth, org):
|
||||
dcr_id, doctors, _ = await _draft(client, auth, org)
|
||||
async def test_removing_a_visit_returns_the_report_without_it(self, client, auth, org, approved_plan):
|
||||
dcr_id, doctors, _ = await _draft(client, auth, org, approved_plan)
|
||||
for i, doctor in enumerate(doctors):
|
||||
r = await client.post(
|
||||
f"{API}/dcr/{dcr_id}/calls/doctor", headers=auth["mr1"],
|
||||
@@ -123,14 +145,14 @@ class TestResponsesReflectTheChange:
|
||||
|
||||
|
||||
class TestDownloadUsesTheCurrentLayout:
|
||||
async def test_a_report_approved_under_an_older_layout_still_downloads_in_the_new_one(self, client, auth, org, db):
|
||||
async def test_a_report_approved_under_an_older_layout_still_downloads_in_the_new_one(self, client, auth, org, db, approved_plan):
|
||||
from pathlib import Path
|
||||
|
||||
from sqlalchemy import select
|
||||
|
||||
from app.reports.models import DailyCallReport
|
||||
|
||||
dcr_id, doctors, _ = await _draft(client, auth, org)
|
||||
dcr_id, doctors, _ = await _draft(client, auth, org, approved_plan)
|
||||
await client.post(
|
||||
f"{API}/dcr/{dcr_id}/calls/doctor", headers=auth["mr1"],
|
||||
json={"doctor_id": doctors[0]["id"], "visit_time": "09:30:00", "call_outcome": "MET"},
|
||||
@@ -146,14 +168,15 @@ class TestDownloadUsesTheCurrentLayout:
|
||||
assert pdf.status_code == 200
|
||||
assert b"OLD DESIGN" not in pdf.content and pdf.content.startswith(b"%PDF-") and len(pdf.content) > 5000
|
||||
# ...and still only for approved reports.
|
||||
draft_id, _, _ = await _draft_second(client, auth, org)
|
||||
draft_id, _, _ = await _draft_second(client, auth, org, approved_plan)
|
||||
assert (await client.get(f"{API}/dcr/{draft_id}/pdf", headers=auth["mr1"])).status_code == 422
|
||||
|
||||
|
||||
async def _draft_second(client, auth, org):
|
||||
async def _draft_second(client, auth, org, approved_plan):
|
||||
"""A second, unapproved report (yesterday's) for the same MR."""
|
||||
from datetime import timedelta
|
||||
|
||||
await approved_plan(date.today() - timedelta(days=1))
|
||||
resp = await client.post(
|
||||
f"{API}/dcr", headers=auth["mr1"], json={"report_date": (date.today() - timedelta(days=1)).isoformat()}
|
||||
)
|
||||
@@ -162,14 +185,14 @@ async def _draft_second(client, auth, org):
|
||||
|
||||
|
||||
class TestApprovedPdfIsSigned:
|
||||
async def test_the_pdf_names_the_approving_area_manager_from_the_audit_trail(self, client, auth, org, db):
|
||||
async def test_the_pdf_names_the_approving_area_manager_from_the_audit_trail(self, client, auth, org, db, approved_plan):
|
||||
from sqlalchemy import select
|
||||
|
||||
from app.pdf.service import PDFService
|
||||
from app.reports.models import DailyCallReport
|
||||
from app.reports.repository import DCRRepository
|
||||
|
||||
dcr_id, doctors, _ = await _draft(client, auth, org)
|
||||
dcr_id, doctors, _ = await _draft(client, auth, org, approved_plan)
|
||||
await client.post(
|
||||
f"{API}/dcr/{dcr_id}/calls/doctor", headers=auth["mr1"],
|
||||
json={"doctor_id": doctors[0]["id"], "visit_time": "09:30:00", "call_outcome": "MET"},
|
||||
@@ -184,10 +207,10 @@ class TestApprovedPdfIsSigned:
|
||||
assert context["approval"]["name"] == "Suresh Behera"
|
||||
assert len(context["approval"]["at"]) == 16 and context["mr_signed"] == "Ravi Das (MR001)"
|
||||
|
||||
async def test_a_report_that_is_not_approved_has_no_approval_signature(self, client, auth, org, db):
|
||||
async def test_a_report_that_is_not_approved_has_no_approval_signature(self, client, auth, org, db, approved_plan):
|
||||
from app.pdf.service import PDFService
|
||||
from app.reports.repository import DCRRepository
|
||||
|
||||
dcr_id, _, _ = await _draft(client, auth, org)
|
||||
dcr_id, _, _ = await _draft(client, auth, org, approved_plan)
|
||||
report = await DCRRepository(db).get_full(dcr_id if not isinstance(dcr_id, str) else __import__("uuid").UUID(dcr_id))
|
||||
assert (await PDFService(db)._context(report))["approval"] is None
|
||||
|
||||
Reference in New Issue
Block a user