# -*- coding: utf-8 -*- import json from unittest import TestCase, main, mock from zyt_patient_client import ZytPatientClient class _Response: def __init__(self, payload): self.payload = payload def __enter__(self): return self def __exit__(self, *_args): return False def read(self): return json.dumps(self.payload, ensure_ascii=False).encode("utf-8") class ZytPatientClientTest(TestCase): def test_search_deduplicates_diagnoses_and_masks_phone(self): response = _Response( { "code": 1, "data": { "lists": [ { "id": 12, "patient_id": 8, "patient_name": "张三", "phone": "13800138000", "age": 35, }, { "id": 10, "patient_id": 8, "patient_name": "张三", "phone": "13800138000", }, ], "count": 2, }, } ) with mock.patch("urllib.request.urlopen", return_value=response) as opened: result = ZytPatientClient( "https://admin.example.com", "secret-token" ).search_patients("张三") self.assertEqual(len(result["items"]), 1) self.assertEqual(result["items"][0]["diagnosis_id"], 12) self.assertEqual(result["items"][0]["phone_masked"], "138****8000") request = opened.call_args.args[0] self.assertEqual(request.headers["Token"], "secret-token") self.assertIn("/adminapi/tcm.diagnosis/lists?", request.full_url) def test_exact_patient_lookup_uses_the_scoped_diagnosis_list(self): response = _Response( { "code": 1, "data": { "lists": [ { "id": 12, "patient_id": 8, "patient_name": "张三", "phone": "13800138000", } ], "count": 1, }, } ) with mock.patch("urllib.request.urlopen", return_value=response) as opened: patient = ZytPatientClient( "https://admin.example.com", "secret-token" ).get_patient(8) self.assertIsNotNone(patient) self.assertEqual(patient["patient_id"], 8) self.assertIn("patient_id=8", opened.call_args.args[0].full_url) if __name__ == "__main__": main()