test_oa_bank_receipt.py 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. """OA 银行回执单:biz_attachment 挂载 instance。"""
  2. from __future__ import annotations
  3. import unittest
  4. from datetime import datetime
  5. from sqlalchemy import create_engine
  6. from sqlalchemy.orm import sessionmaker
  7. from app.models.base import Base
  8. from app.models.models import BizAttachment, OaApp, OaAppVersion, OaInstance
  9. from app.services import oa_bank_receipt as svc
  10. class TestOaBankReceipt(unittest.TestCase):
  11. def setUp(self):
  12. self.engine = create_engine("sqlite:///:memory:")
  13. Base.metadata.create_all(
  14. self.engine,
  15. tables=[
  16. OaApp.__table__,
  17. OaAppVersion.__table__,
  18. OaInstance.__table__,
  19. BizAttachment.__table__,
  20. ],
  21. )
  22. Session = sessionmaker(bind=self.engine, autoflush=False, autocommit=False)
  23. self.db = Session()
  24. t0 = datetime(2026, 10, 16, 10, 0, 0)
  25. self.db.add_all(
  26. [
  27. OaApp(
  28. id="app1",
  29. app_name="付款申请",
  30. initiator_scope="all",
  31. admin_scope="all_oa_admin",
  32. status="1",
  33. tenant_no="zkxw",
  34. create_dept_id="",
  35. create_time=t0,
  36. update_time=t0,
  37. del_flag=0,
  38. ),
  39. OaAppVersion(
  40. id="v1",
  41. app_id="app1",
  42. version_no=1,
  43. status="published",
  44. is_latest_published="1",
  45. form_schema_json="{}",
  46. flow_tree_json="{}",
  47. advanced_settings_json="{}",
  48. tenant_no="zkxw",
  49. create_dept_id="",
  50. create_time=t0,
  51. update_time=t0,
  52. del_flag=0,
  53. ),
  54. OaInstance(
  55. id="inst-1",
  56. app_id="app1",
  57. app_version_id="v1",
  58. title="付款-1",
  59. initiator_user_id="u1",
  60. status="completed",
  61. current_round=1,
  62. tenant_no="zkxw",
  63. create_dept_id="",
  64. create_time=t0,
  65. update_time=t0,
  66. del_flag=0,
  67. ),
  68. OaInstance(
  69. id="inst-other-tenant",
  70. app_id="app1",
  71. app_version_id="v1",
  72. title="付款-x",
  73. initiator_user_id="u1",
  74. status="completed",
  75. current_round=1,
  76. tenant_no="other",
  77. create_dept_id="",
  78. create_time=t0,
  79. update_time=t0,
  80. del_flag=0,
  81. ),
  82. ]
  83. )
  84. self.db.commit()
  85. def tearDown(self):
  86. self.db.close()
  87. self.engine.dispose()
  88. def test_list_empty(self):
  89. items = svc.list_bank_receipts(self.db, instance_id="inst-1", tenant_no="zkxw")
  90. self.assertEqual(items, [])
  91. def test_replace_then_list(self):
  92. out = svc.replace_bank_receipts(
  93. self.db,
  94. instance_id="inst-1",
  95. tenant_no="zkxw",
  96. items=[
  97. {
  98. "fileUrl": "oa-bank-receipt/a.pdf",
  99. "fileName": "回单A.pdf",
  100. "fileType": "application/pdf",
  101. "fileSize": 100,
  102. },
  103. {
  104. "fileUrl": "oa-bank-receipt/b.jpg",
  105. "fileName": "回单B.jpg",
  106. "fileType": "image/jpeg",
  107. "fileSize": 200,
  108. },
  109. ],
  110. user_id="u1",
  111. dept_id="d1",
  112. )
  113. self.db.commit()
  114. self.assertEqual(len(out), 2)
  115. listed = svc.list_bank_receipts(self.db, instance_id="inst-1", tenant_no="zkxw")
  116. self.assertEqual([x["fileUrl"] for x in listed], [
  117. "oa-bank-receipt/a.pdf",
  118. "oa-bank-receipt/b.jpg",
  119. ])
  120. self.assertEqual(listed[0]["fileName"], "回单A.pdf")
  121. def test_replace_removes_missing(self):
  122. svc.replace_bank_receipts(
  123. self.db,
  124. instance_id="inst-1",
  125. tenant_no="zkxw",
  126. items=[
  127. {"fileUrl": "oa-bank-receipt/a.pdf", "fileName": "A.pdf"},
  128. {"fileUrl": "oa-bank-receipt/b.pdf", "fileName": "B.pdf"},
  129. ],
  130. user_id="u1",
  131. dept_id="",
  132. )
  133. self.db.commit()
  134. svc.replace_bank_receipts(
  135. self.db,
  136. instance_id="inst-1",
  137. tenant_no="zkxw",
  138. items=[{"fileUrl": "oa-bank-receipt/b.pdf", "fileName": "B.pdf"}],
  139. user_id="u1",
  140. dept_id="",
  141. )
  142. self.db.commit()
  143. listed = svc.list_bank_receipts(self.db, instance_id="inst-1", tenant_no="zkxw")
  144. self.assertEqual([x["fileUrl"] for x in listed], ["oa-bank-receipt/b.pdf"])
  145. gone = (
  146. self.db.query(BizAttachment)
  147. .filter(BizAttachment.file_url == "oa-bank-receipt/a.pdf")
  148. .first()
  149. )
  150. self.assertEqual(gone.del_flag, 1)
  151. def test_replace_empty_clears_all(self):
  152. svc.replace_bank_receipts(
  153. self.db,
  154. instance_id="inst-1",
  155. tenant_no="zkxw",
  156. items=[{"fileUrl": "oa-bank-receipt/a.pdf", "fileName": "A.pdf"}],
  157. user_id="u1",
  158. dept_id="",
  159. )
  160. self.db.commit()
  161. svc.replace_bank_receipts(
  162. self.db,
  163. instance_id="inst-1",
  164. tenant_no="zkxw",
  165. items=[],
  166. user_id="u1",
  167. dept_id="",
  168. )
  169. self.db.commit()
  170. self.assertEqual(
  171. svc.list_bank_receipts(self.db, instance_id="inst-1", tenant_no="zkxw"),
  172. [],
  173. )
  174. def test_bind_orphan_upload_row(self):
  175. """通用上传已写入无 business_type 的行时,PUT 应绑定而非再插一行。"""
  176. t0 = datetime(2026, 10, 16, 11, 0, 0)
  177. orphan = BizAttachment(
  178. id="att-orphan",
  179. business_type=None,
  180. business_id=None,
  181. original_name="orphan.pdf",
  182. file_ext="pdf",
  183. file_url="oa-bank-receipt/orphan.pdf",
  184. file_size=10,
  185. file_type="application/pdf",
  186. tenant_no="zkxw",
  187. create_dept_id="",
  188. create_time=t0,
  189. update_time=t0,
  190. del_flag=0,
  191. )
  192. self.db.add(orphan)
  193. self.db.commit()
  194. svc.replace_bank_receipts(
  195. self.db,
  196. instance_id="inst-1",
  197. tenant_no="zkxw",
  198. items=[{"fileUrl": "oa-bank-receipt/orphan.pdf", "fileName": "orphan.pdf"}],
  199. user_id="u1",
  200. dept_id="",
  201. )
  202. self.db.commit()
  203. rows = (
  204. self.db.query(BizAttachment)
  205. .filter(BizAttachment.file_url == "oa-bank-receipt/orphan.pdf", BizAttachment.del_flag == 0)
  206. .all()
  207. )
  208. self.assertEqual(len(rows), 1)
  209. self.assertEqual(rows[0].business_type, svc.BUSINESS_TYPE)
  210. self.assertEqual(rows[0].business_id, "inst-1")
  211. def test_missing_instance_raises(self):
  212. with self.assertRaises(svc.OaBankReceiptError):
  213. svc.list_bank_receipts(self.db, instance_id="nope", tenant_no="zkxw")
  214. def test_cross_tenant_raises(self):
  215. with self.assertRaises(svc.OaBankReceiptError):
  216. svc.list_bank_receipts(
  217. self.db, instance_id="inst-other-tenant", tenant_no="zkxw"
  218. )
  219. if __name__ == "__main__":
  220. unittest.main()