test_event_push_actions.py 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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 mock import Mock
  16. from twisted.internet import defer
  17. import tests.unittest
  18. import tests.utils
  19. USER_ID = "@user:example.com"
  20. PlAIN_NOTIF = ["notify", {"set_tweak": "highlight", "value": False}]
  21. HIGHLIGHT = [
  22. "notify",
  23. {"set_tweak": "sound", "value": "default"},
  24. {"set_tweak": "highlight"},
  25. ]
  26. class EventPushActionsStoreTestCase(tests.unittest.TestCase):
  27. @defer.inlineCallbacks
  28. def setUp(self):
  29. hs = yield tests.utils.setup_test_homeserver(self.addCleanup)
  30. self.store = hs.get_datastore()
  31. self.persist_events_store = hs.get_datastores().persist_events
  32. @defer.inlineCallbacks
  33. def test_get_unread_push_actions_for_user_in_range_for_http(self):
  34. yield self.store.get_unread_push_actions_for_user_in_range_for_http(
  35. USER_ID, 0, 1000, 20
  36. )
  37. @defer.inlineCallbacks
  38. def test_get_unread_push_actions_for_user_in_range_for_email(self):
  39. yield self.store.get_unread_push_actions_for_user_in_range_for_email(
  40. USER_ID, 0, 1000, 20
  41. )
  42. @defer.inlineCallbacks
  43. def test_count_aggregation(self):
  44. room_id = "!foo:example.com"
  45. user_id = "@user1235:example.com"
  46. @defer.inlineCallbacks
  47. def _assert_counts(noitf_count, highlight_count):
  48. counts = yield self.store.db.runInteraction(
  49. "", self.store._get_unread_counts_by_pos_txn, room_id, user_id, 0
  50. )
  51. self.assertEquals(
  52. counts,
  53. {"notify_count": noitf_count, "highlight_count": highlight_count},
  54. )
  55. @defer.inlineCallbacks
  56. def _inject_actions(stream, action):
  57. event = Mock()
  58. event.room_id = room_id
  59. event.event_id = "$test:example.com"
  60. event.internal_metadata.stream_ordering = stream
  61. event.depth = stream
  62. yield self.store.add_push_actions_to_staging(
  63. event.event_id, {user_id: action}
  64. )
  65. yield self.store.db.runInteraction(
  66. "",
  67. self.persist_events_store._set_push_actions_for_event_and_users_txn,
  68. [(event, None)],
  69. [(event, None)],
  70. )
  71. def _rotate(stream):
  72. return self.store.db.runInteraction(
  73. "", self.store._rotate_notifs_before_txn, stream
  74. )
  75. def _mark_read(stream, depth):
  76. return self.store.db.runInteraction(
  77. "",
  78. self.store._remove_old_push_actions_before_txn,
  79. room_id,
  80. user_id,
  81. stream,
  82. )
  83. yield _assert_counts(0, 0)
  84. yield _inject_actions(1, PlAIN_NOTIF)
  85. yield _assert_counts(1, 0)
  86. yield _rotate(2)
  87. yield _assert_counts(1, 0)
  88. yield _inject_actions(3, PlAIN_NOTIF)
  89. yield _assert_counts(2, 0)
  90. yield _rotate(4)
  91. yield _assert_counts(2, 0)
  92. yield _inject_actions(5, PlAIN_NOTIF)
  93. yield _mark_read(3, 3)
  94. yield _assert_counts(1, 0)
  95. yield _mark_read(5, 5)
  96. yield _assert_counts(0, 0)
  97. yield _inject_actions(6, PlAIN_NOTIF)
  98. yield _rotate(7)
  99. yield self.store.db.simple_delete(
  100. table="event_push_actions", keyvalues={"1": 1}, desc=""
  101. )
  102. yield _assert_counts(1, 0)
  103. yield _mark_read(7, 7)
  104. yield _assert_counts(0, 0)
  105. yield _inject_actions(8, HIGHLIGHT)
  106. yield _assert_counts(1, 1)
  107. yield _rotate(9)
  108. yield _assert_counts(1, 1)
  109. yield _rotate(10)
  110. yield _assert_counts(1, 1)
  111. @defer.inlineCallbacks
  112. def test_find_first_stream_ordering_after_ts(self):
  113. def add_event(so, ts):
  114. return self.store.db.simple_insert(
  115. "events",
  116. {
  117. "stream_ordering": so,
  118. "received_ts": ts,
  119. "event_id": "event%i" % so,
  120. "type": "",
  121. "room_id": "",
  122. "content": "",
  123. "processed": True,
  124. "outlier": False,
  125. "topological_ordering": 0,
  126. "depth": 0,
  127. },
  128. )
  129. # start with the base case where there are no events in the table
  130. r = yield self.store.find_first_stream_ordering_after_ts(11)
  131. self.assertEqual(r, 0)
  132. # now with one event
  133. yield add_event(2, 10)
  134. r = yield self.store.find_first_stream_ordering_after_ts(9)
  135. self.assertEqual(r, 2)
  136. r = yield self.store.find_first_stream_ordering_after_ts(10)
  137. self.assertEqual(r, 2)
  138. r = yield self.store.find_first_stream_ordering_after_ts(11)
  139. self.assertEqual(r, 3)
  140. # add a bunch of dummy events to the events table
  141. for (stream_ordering, ts) in (
  142. (3, 110),
  143. (4, 120),
  144. (5, 120),
  145. (10, 130),
  146. (20, 140),
  147. ):
  148. yield add_event(stream_ordering, ts)
  149. r = yield self.store.find_first_stream_ordering_after_ts(110)
  150. self.assertEqual(r, 3, "First event after 110ms should be 3, was %i" % r)
  151. # 4 and 5 are both after 120: we want 4 rather than 5
  152. r = yield self.store.find_first_stream_ordering_after_ts(120)
  153. self.assertEqual(r, 4, "First event after 120ms should be 4, was %i" % r)
  154. r = yield self.store.find_first_stream_ordering_after_ts(129)
  155. self.assertEqual(r, 10, "First event after 129ms should be 10, was %i" % r)
  156. # check we can get the last event
  157. r = yield self.store.find_first_stream_ordering_after_ts(140)
  158. self.assertEqual(r, 20, "First event after 14ms should be 20, was %i" % r)
  159. # off the end
  160. r = yield self.store.find_first_stream_ordering_after_ts(160)
  161. self.assertEqual(r, 21)
  162. # check we can find an event at ordering zero
  163. yield add_event(0, 5)
  164. r = yield self.store.find_first_stream_ordering_after_ts(1)
  165. self.assertEqual(r, 0)