test_event_federation.py 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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()
  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. "INSERT INTO events ("
  31. " room_id, event_id, type, depth, topological_ordering,"
  32. " content, processed, outlier) "
  33. "VALUES (?, ?, 'm.test', ?, ?, 'test', ?, ?)"
  34. ), (room_id, event_id, i, i, True, False))
  35. txn.execute((
  36. 'INSERT INTO event_forward_extremities (room_id, event_id) '
  37. 'VALUES (?, ?)'
  38. ), (room_id, event_id))
  39. txn.execute((
  40. 'INSERT INTO event_reference_hashes '
  41. '(event_id, algorithm, hash) '
  42. "VALUES (?, 'sha256', ?)"
  43. ), (event_id, 'ffff'))
  44. for i in range(0, 11):
  45. yield self.store.runInteraction("insert", insert_event, i)
  46. # this should get the last five and five others
  47. r = yield self.store.get_prev_events_for_room(room_id)
  48. self.assertEqual(10, len(r))
  49. for i in range(0, 5):
  50. el = r[i]
  51. depth = el[2]
  52. self.assertEqual(10 - i, depth)
  53. for i in range(5, 5):
  54. el = r[i]
  55. depth = el[2]
  56. self.assertLessEqual(5, depth)