test_oa_mention_unread.py 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. """OA 提到我的未读计数 / 标记已读 / 再@重置。"""
  2. import unittest
  3. from datetime import datetime
  4. from unittest.mock import patch
  5. class TestMentionUnread(unittest.TestCase):
  6. def setUp(self):
  7. from sqlalchemy import create_engine
  8. from sqlalchemy.orm import sessionmaker
  9. from app.models.models import (
  10. OaAppVersion,
  11. OaInstance,
  12. OaInstanceComment,
  13. OaInstanceViewGrant,
  14. SysUser,
  15. )
  16. self.engine = create_engine("sqlite:///:memory:")
  17. for table in (
  18. OaAppVersion.__table__,
  19. OaInstance.__table__,
  20. OaInstanceComment.__table__,
  21. OaInstanceViewGrant.__table__,
  22. SysUser.__table__,
  23. ):
  24. table.create(self.engine)
  25. Session = sessionmaker(bind=self.engine, autoflush=False, autocommit=False)
  26. self.db = Session()
  27. now = datetime.now()
  28. self.db.add(
  29. OaAppVersion(
  30. id="v1",
  31. app_id="app1",
  32. version_no=1,
  33. status="published",
  34. is_latest_published="1",
  35. form_schema_json="{}",
  36. flow_tree_json="{}",
  37. advanced_settings_json="{}",
  38. tenant_no="zkxw",
  39. create_dept_id="",
  40. create_time=now,
  41. update_time=now,
  42. del_flag=0,
  43. )
  44. )
  45. self.db.add(
  46. OaInstance(
  47. id="i1",
  48. app_id="app1",
  49. app_version_id="v1",
  50. title="t",
  51. status="running",
  52. initiator_user_id="u0",
  53. initiator_dept_id="d1",
  54. form_data_json="{}",
  55. tenant_no="zkxw",
  56. create_dept_id="d1",
  57. create_time=now,
  58. update_time=now,
  59. del_flag=0,
  60. current_round=1,
  61. )
  62. )
  63. for uid, name in (("u1", "张三"), ("u2", "李四")):
  64. self.db.add(
  65. SysUser(
  66. id=uid,
  67. tenant_no="zkxw",
  68. user_no=uid,
  69. nick_name=name,
  70. email="",
  71. phone_no="",
  72. password="",
  73. status="1",
  74. create_dept_id="",
  75. del_flag=0,
  76. create_time=now,
  77. update_time=now,
  78. )
  79. )
  80. self.db.commit()
  81. def tearDown(self):
  82. self.db.close()
  83. self.engine.dispose()
  84. def test_mention_starts_unread_and_count(self):
  85. from app.services import oa_instance_comment as svc
  86. with patch("app.services.oa_wechat_notify.schedule_oa_mention_notifies"):
  87. svc.create_comment(
  88. self.db,
  89. instance_id="i1",
  90. tenant_no="zkxw",
  91. user_id="u1",
  92. dept_id="d1",
  93. content="@你",
  94. attachment_refs=None,
  95. mention_user_ids=["u2"],
  96. )
  97. self.assertEqual(
  98. svc.count_mention_unread(self.db, tenant_no="zkxw", user_id="u2"), 1
  99. )
  100. self.assertEqual(
  101. svc.count_mention_unread(self.db, tenant_no="zkxw", user_id="u1"), 0
  102. )
  103. def test_mark_read_clears_unread(self):
  104. from app.services import oa_instance_comment as svc
  105. with patch("app.services.oa_wechat_notify.schedule_oa_mention_notifies"):
  106. svc.create_comment(
  107. self.db,
  108. instance_id="i1",
  109. tenant_no="zkxw",
  110. user_id="u1",
  111. dept_id="d1",
  112. content="x",
  113. attachment_refs=None,
  114. mention_user_ids=["u2"],
  115. )
  116. out = svc.mark_mention_instance_read(
  117. self.db, tenant_no="zkxw", user_id="u2", instance_id="i1"
  118. )
  119. self.assertTrue(out.get("isRead"))
  120. self.assertEqual(
  121. svc.count_mention_unread(self.db, tenant_no="zkxw", user_id="u2"), 0
  122. )
  123. def test_remention_resets_unread(self):
  124. from app.services import oa_instance_comment as svc
  125. with patch("app.services.oa_wechat_notify.schedule_oa_mention_notifies"):
  126. svc.create_comment(
  127. self.db,
  128. instance_id="i1",
  129. tenant_no="zkxw",
  130. user_id="u1",
  131. dept_id="d1",
  132. content="第一次",
  133. attachment_refs=None,
  134. mention_user_ids=["u2"],
  135. )
  136. svc.mark_mention_instance_read(
  137. self.db, tenant_no="zkxw", user_id="u2", instance_id="i1"
  138. )
  139. self.assertEqual(
  140. svc.count_mention_unread(self.db, tenant_no="zkxw", user_id="u2"), 0
  141. )
  142. svc.create_comment(
  143. self.db,
  144. instance_id="i1",
  145. tenant_no="zkxw",
  146. user_id="u1",
  147. dept_id="d1",
  148. content="再提",
  149. attachment_refs=None,
  150. mention_user_ids=["u2"],
  151. )
  152. self.assertEqual(
  153. svc.count_mention_unread(self.db, tenant_no="zkxw", user_id="u2"), 1
  154. )
  155. if __name__ == "__main__":
  156. unittest.main()