test_event_push_actions.py 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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, 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, depth, 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. @tests.unittest.DEBUG
  107. @defer.inlineCallbacks
  108. def test_find_first_stream_ordering_after_ts(self):
  109. def add_event(so, ts):
  110. return self.store._simple_insert("events", {
  111. "stream_ordering": so,
  112. "received_ts": ts,
  113. "event_id": "event%i" % so,
  114. "type": "",
  115. "room_id": "",
  116. "content": "",
  117. "processed": True,
  118. "outlier": False,
  119. "topological_ordering": 0,
  120. "depth": 0,
  121. })
  122. # start with the base case where there are no events in the table
  123. r = yield self.store.find_first_stream_ordering_after_ts(11)
  124. self.assertEqual(r, 0)
  125. # now with one event
  126. yield add_event(2, 10)
  127. r = yield self.store.find_first_stream_ordering_after_ts(9)
  128. self.assertEqual(r, 2)
  129. r = yield self.store.find_first_stream_ordering_after_ts(10)
  130. self.assertEqual(r, 2)
  131. r = yield self.store.find_first_stream_ordering_after_ts(11)
  132. self.assertEqual(r, 3)
  133. # add a bunch of dummy events to the events table
  134. for (stream_ordering, ts) in (
  135. (3, 110),
  136. (4, 120),
  137. (5, 120),
  138. (10, 130),
  139. (20, 140),
  140. ):
  141. yield add_event(stream_ordering, ts)
  142. r = yield self.store.find_first_stream_ordering_after_ts(110)
  143. self.assertEqual(r, 3,
  144. "First event after 110ms should be 3, was %i" % r)
  145. # 4 and 5 are both after 120: we want 4 rather than 5
  146. r = yield self.store.find_first_stream_ordering_after_ts(120)
  147. self.assertEqual(r, 4,
  148. "First event after 120ms should be 4, was %i" % r)
  149. r = yield self.store.find_first_stream_ordering_after_ts(129)
  150. self.assertEqual(r, 10,
  151. "First event after 129ms should be 10, was %i" % r)
  152. # check we can get the last event
  153. r = yield self.store.find_first_stream_ordering_after_ts(140)
  154. self.assertEqual(r, 20,
  155. "First event after 14ms should be 20, was %i" % r)
  156. # off the end
  157. r = yield self.store.find_first_stream_ordering_after_ts(160)
  158. self.assertEqual(r, 21)
  159. # check we can find an event at ordering zero
  160. yield add_event(0, 5)
  161. r = yield self.store.find_first_stream_ordering_after_ts(1)
  162. self.assertEqual(r, 0)