test_collection_protocol_save_bundle.py 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. """采集协议单创建/更新子表持久化与字段名规范化测试。"""
  2. import unittest
  3. from unittest.mock import MagicMock, patch
  4. from app.crud.collection_protocol import (
  5. _normalize_fields,
  6. attachments_to_urls,
  7. urls_to_attachment_list,
  8. upsert_party_customer,
  9. )
  10. class TestNormalizeFields(unittest.TestCase):
  11. def test_camel_case_to_snake(self):
  12. raw = {
  13. "customerId": "c1",
  14. "motherName": "张三",
  15. "motherBirthYm": "199001",
  16. "apgar1min": 9.5,
  17. "apgar5min": 10,
  18. }
  19. out = _normalize_fields(raw)
  20. self.assertEqual(out["customer_id"], "c1")
  21. self.assertEqual(out["mother_name"], "张三")
  22. self.assertEqual(out["mother_birth_ym"], "199001")
  23. self.assertEqual(out["apgar_1min"], 9.5)
  24. self.assertEqual(out["apgar_5min"], 10)
  25. class TestAttachmentsHelpers(unittest.TestCase):
  26. def test_attachments_to_urls(self):
  27. urls = attachments_to_urls(
  28. [{"fileUrl": "http://a/1.jpg"}, {"fileUrl": "http://a/2.jpg"}]
  29. )
  30. self.assertEqual(urls, "http://a/1.jpg,http://a/2.jpg")
  31. def test_urls_to_attachment_list(self):
  32. items = urls_to_attachment_list("http://x/a.pdf,http://x/b.png")
  33. self.assertEqual(len(items), 2)
  34. self.assertEqual(items[0]["fileUrl"], "http://x/a.pdf")
  35. def test_urls_to_attachment_list_uses_original_name_from_db(self):
  36. db = MagicMock()
  37. att = MagicMock()
  38. att.id = "att-1"
  39. att.original_name = "合同扫描件.pdf"
  40. att.file_url = "protocol/2025/05/16/abc.pdf"
  41. att.file_size = 2048
  42. att.file_type = "application/pdf"
  43. q = MagicMock()
  44. db.query.return_value = q
  45. q.filter.return_value = q
  46. q.first.return_value = att
  47. items = urls_to_attachment_list("protocol/2025/05/16/abc.pdf", db, "t1")
  48. self.assertEqual(len(items), 1)
  49. self.assertEqual(items[0]["fileName"], "合同扫描件.pdf")
  50. self.assertEqual(items[0]["id"], "att-1")
  51. class TestUpsertPartyCustomer(unittest.TestCase):
  52. @patch("app.crud.collection_protocol.get_party_customer")
  53. def test_upsert_applies_camel_case_fields(self, mock_get):
  54. mock_get.return_value = None
  55. db = MagicMock()
  56. fields = {
  57. "motherName": "李四",
  58. "motherPhone": "13800000000",
  59. "motherIdCard": "110101199001011234",
  60. "motherBirthYm": "199001",
  61. "fatherName": "王五",
  62. }
  63. upsert_party_customer(db, "proto-1", "t1", "u1", **fields)
  64. added = db.add.call_args[0][0]
  65. self.assertEqual(added.mother_name, "李四")
  66. self.assertEqual(added.mother_phone, "13800000000")
  67. self.assertEqual(added.mother_id_card, "110101199001011234")
  68. self.assertEqual(added.mother_birth_ym, "199001")
  69. self.assertEqual(added.father_name, "王五")
  70. if __name__ == "__main__":
  71. unittest.main()