test_space_summary.py 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. # Copyright 2021 The Matrix.org Foundation C.I.C.
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. from typing import Any, Optional
  15. from unittest import mock
  16. from synapse.handlers.space_summary import _child_events_comparison_key
  17. from tests import unittest
  18. def _create_event(room_id: str, order: Optional[Any] = None):
  19. result = mock.Mock()
  20. result.room_id = room_id
  21. result.content = {}
  22. if order is not None:
  23. result.content["order"] = order
  24. return result
  25. def _order(*events):
  26. return sorted(events, key=_child_events_comparison_key)
  27. class TestSpaceSummarySort(unittest.TestCase):
  28. def test_no_order_last(self):
  29. """An event with no ordering is placed behind those with an ordering."""
  30. ev1 = _create_event("!abc:test")
  31. ev2 = _create_event("!xyz:test", "xyz")
  32. self.assertEqual([ev2, ev1], _order(ev1, ev2))
  33. def test_order(self):
  34. """The ordering should be used."""
  35. ev1 = _create_event("!abc:test", "xyz")
  36. ev2 = _create_event("!xyz:test", "abc")
  37. self.assertEqual([ev2, ev1], _order(ev1, ev2))
  38. def test_order_room_id(self):
  39. """Room ID is a tie-breaker for ordering."""
  40. ev1 = _create_event("!abc:test", "abc")
  41. ev2 = _create_event("!xyz:test", "abc")
  42. self.assertEqual([ev1, ev2], _order(ev1, ev2))
  43. def test_invalid_ordering_type(self):
  44. """Invalid orderings are considered the same as missing."""
  45. ev1 = _create_event("!abc:test", 1)
  46. ev2 = _create_event("!xyz:test", "xyz")
  47. self.assertEqual([ev2, ev1], _order(ev1, ev2))
  48. ev1 = _create_event("!abc:test", {})
  49. self.assertEqual([ev2, ev1], _order(ev1, ev2))
  50. ev1 = _create_event("!abc:test", [])
  51. self.assertEqual([ev2, ev1], _order(ev1, ev2))
  52. ev1 = _create_event("!abc:test", True)
  53. self.assertEqual([ev2, ev1], _order(ev1, ev2))
  54. def test_invalid_ordering_value(self):
  55. """Invalid orderings are considered the same as missing."""
  56. ev1 = _create_event("!abc:test", "foo\n")
  57. ev2 = _create_event("!xyz:test", "xyz")
  58. self.assertEqual([ev2, ev1], _order(ev1, ev2))
  59. ev1 = _create_event("!abc:test", "a" * 51)
  60. self.assertEqual([ev2, ev1], _order(ev1, ev2))