test_event_push_actions.py 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. # -*- coding: utf-8 -*-
  2. # Copyright 2016 OpenMarket Ltd
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. from twisted.internet import defer
  16. import tests.unittest
  17. import tests.utils
  18. from mock import Mock
  19. USER_ID = "@user:example.com"
  20. PlAIN_NOTIF = ["notify", {"set_tweak": "highlight", "value": False}]
  21. HIGHLIGHT = [
  22. "notify", {"set_tweak": "sound", "value": "default"}, {"set_tweak": "highlight"}
  23. ]
  24. class EventPushActionsStoreTestCase(tests.unittest.TestCase):
  25. @defer.inlineCallbacks
  26. def setUp(self):
  27. hs = yield tests.utils.setup_test_homeserver()
  28. self.store = hs.get_datastore()
  29. @defer.inlineCallbacks
  30. def test_get_unread_push_actions_for_user_in_range_for_http(self):
  31. yield self.store.get_unread_push_actions_for_user_in_range_for_http(
  32. USER_ID, 0, 1000, 20
  33. )
  34. @defer.inlineCallbacks
  35. def test_get_unread_push_actions_for_user_in_range_for_email(self):
  36. yield self.store.get_unread_push_actions_for_user_in_range_for_email(
  37. USER_ID, 0, 1000, 20
  38. )
  39. @defer.inlineCallbacks
  40. def test_count_aggregation(self):
  41. room_id = "!foo:example.com"
  42. user_id = "@user1235:example.com"
  43. @defer.inlineCallbacks
  44. def _assert_counts(noitf_count, highlight_count):
  45. counts = yield self.store.runInteraction(
  46. "", self.store._get_unread_counts_by_pos_txn,
  47. room_id, user_id, 0
  48. )
  49. self.assertEquals(
  50. counts,
  51. {"notify_count": noitf_count, "highlight_count": highlight_count}
  52. )
  53. @defer.inlineCallbacks
  54. def _inject_actions(stream, action):
  55. event = Mock()
  56. event.room_id = room_id
  57. event.event_id = "$test:example.com"
  58. event.internal_metadata.stream_ordering = stream
  59. event.depth = stream
  60. yield self.store.add_push_actions_to_staging(
  61. event.event_id, {user_id: action},
  62. )
  63. yield self.store.runInteraction(
  64. "", self.store._set_push_actions_for_event_and_users_txn,
  65. [(event, None)], [(event, None)],
  66. )
  67. def _rotate(stream):
  68. return self.store.runInteraction(
  69. "", self.store._rotate_notifs_before_txn, stream
  70. )
  71. def _mark_read(stream, depth):
  72. return self.store.runInteraction(
  73. "", self.store._remove_old_push_actions_before_txn,
  74. room_id, user_id, stream
  75. )
  76. yield _assert_counts(0, 0)
  77. yield _inject_actions(1, PlAIN_NOTIF)
  78. yield _assert_counts(1, 0)
  79. yield _rotate(2)
  80. yield _assert_counts(1, 0)
  81. yield _inject_actions(3, PlAIN_NOTIF)
  82. yield _assert_counts(2, 0)
  83. yield _rotate(4)
  84. yield _assert_counts(2, 0)
  85. yield _inject_actions(5, PlAIN_NOTIF)
  86. yield _mark_read(3, 3)
  87. yield _assert_counts(1, 0)
  88. yield _mark_read(5, 5)
  89. yield _assert_counts(0, 0)
  90. yield _inject_actions(6, PlAIN_NOTIF)
  91. yield _rotate(7)
  92. yield self.store._simple_delete(
  93. table="event_push_actions",
  94. keyvalues={"1": 1},
  95. desc="",
  96. )
  97. yield _assert_counts(1, 0)
  98. yield _mark_read(7, 7)
  99. yield _assert_counts(0, 0)
  100. yield _inject_actions(8, HIGHLIGHT)
  101. yield _assert_counts(1, 1)
  102. yield _rotate(9)
  103. yield _assert_counts(1, 1)
  104. yield _rotate(10)
  105. yield _assert_counts(1, 1)
  106. @defer.inlineCallbacks
  107. def test_find_first_stream_ordering_after_ts(self):
  108. def add_event(so, ts):
  109. return self.store._simple_insert("events", {
  110. "stream_ordering": so,
  111. "received_ts": ts,
  112. "event_id": "event%i" % so,
  113. "type": "",
  114. "room_id": "",
  115. "content": "",
  116. "processed": True,
  117. "outlier": False,
  118. "topological_ordering": 0,
  119. "depth": 0,
  120. })
  121. # start with the base case where there are no events in the table
  122. r = yield self.store.find_first_stream_ordering_after_ts(11)
  123. self.assertEqual(r, 0)
  124. # now with one event
  125. yield add_event(2, 10)
  126. r = yield self.store.find_first_stream_ordering_after_ts(9)
  127. self.assertEqual(r, 2)
  128. r = yield self.store.find_first_stream_ordering_after_ts(10)
  129. self.assertEqual(r, 2)
  130. r = yield self.store.find_first_stream_ordering_after_ts(11)
  131. self.assertEqual(r, 3)
  132. # add a bunch of dummy events to the events table
  133. for (stream_ordering, ts) in (
  134. (3, 110),
  135. (4, 120),
  136. (5, 120),
  137. (10, 130),
  138. (20, 140),
  139. ):
  140. yield add_event(stream_ordering, ts)
  141. r = yield self.store.find_first_stream_ordering_after_ts(110)
  142. self.assertEqual(r, 3,
  143. "First event after 110ms should be 3, was %i" % r)
  144. # 4 and 5 are both after 120: we want 4 rather than 5
  145. r = yield self.store.find_first_stream_ordering_after_ts(120)
  146. self.assertEqual(r, 4,
  147. "First event after 120ms should be 4, was %i" % r)
  148. r = yield self.store.find_first_stream_ordering_after_ts(129)
  149. self.assertEqual(r, 10,
  150. "First event after 129ms should be 10, was %i" % r)
  151. # check we can get the last event
  152. r = yield self.store.find_first_stream_ordering_after_ts(140)
  153. self.assertEqual(r, 20,
  154. "First event after 14ms should be 20, was %i" % r)
  155. # off the end
  156. r = yield self.store.find_first_stream_ordering_after_ts(160)
  157. self.assertEqual(r, 21)
  158. # check we can find an event at ordering zero
  159. yield add_event(0, 5)
  160. r = yield self.store.find_first_stream_ordering_after_ts(1)
  161. self.assertEqual(r, 0)