test_event_push_actions.py 6.4 KB

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