test_event_federation.py 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. # -*- coding: utf-8 -*-
  2. # Copyright 2018 New Vector 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. class EventFederationWorkerStoreTestCase(tests.unittest.TestCase):
  19. @defer.inlineCallbacks
  20. def setUp(self):
  21. hs = yield tests.utils.setup_test_homeserver(self.addCleanup)
  22. self.store = hs.get_datastore()
  23. @defer.inlineCallbacks
  24. def test_get_prev_events_for_room(self):
  25. room_id = '@ROOM:local'
  26. # add a bunch of events and hashes to act as forward extremities
  27. def insert_event(txn, i):
  28. event_id = '$event_%i:local' % i
  29. txn.execute(
  30. (
  31. "INSERT INTO events ("
  32. " room_id, event_id, type, depth, topological_ordering,"
  33. " content, processed, outlier) "
  34. "VALUES (?, ?, 'm.test', ?, ?, 'test', ?, ?)"
  35. ),
  36. (room_id, event_id, i, i, True, False),
  37. )
  38. txn.execute(
  39. (
  40. 'INSERT INTO event_forward_extremities (room_id, event_id) '
  41. 'VALUES (?, ?)'
  42. ),
  43. (room_id, event_id),
  44. )
  45. txn.execute(
  46. (
  47. 'INSERT INTO event_reference_hashes '
  48. '(event_id, algorithm, hash) '
  49. "VALUES (?, 'sha256', ?)"
  50. ),
  51. (event_id, b'ffff'),
  52. )
  53. for i in range(0, 11):
  54. yield self.store.runInteraction("insert", insert_event, i)
  55. # this should get the last five and five others
  56. r = yield self.store.get_prev_events_for_room(room_id)
  57. self.assertEqual(10, len(r))
  58. for i in range(0, 5):
  59. el = r[i]
  60. depth = el[2]
  61. self.assertEqual(10 - i, depth)
  62. for i in range(5, 5):
  63. el = r[i]
  64. depth = el[2]
  65. self.assertLessEqual(5, depth)