1
0

test_relations.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. # Copyright 2022 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 twisted.test.proto_helpers import MemoryReactor
  15. from synapse.api.constants import MAIN_TIMELINE
  16. from synapse.server import HomeServer
  17. from synapse.util import Clock
  18. from tests import unittest
  19. class RelationsStoreTestCase(unittest.HomeserverTestCase):
  20. def prepare(self, reactor: MemoryReactor, clock: Clock, hs: HomeServer) -> None:
  21. """
  22. Creates a DAG:
  23. A <---[m.thread]-- B <--[m.annotation]-- C
  24. ^
  25. |--[m.reference]-- D <--[m.annotation]-- E
  26. F <--[m.annotation]-- G
  27. """
  28. self._main_store = self.hs.get_datastores().main
  29. self._create_relation("A", "B", "m.thread")
  30. self._create_relation("B", "C", "m.annotation")
  31. self._create_relation("A", "D", "m.reference")
  32. self._create_relation("D", "E", "m.annotation")
  33. self._create_relation("F", "G", "m.annotation")
  34. def _create_relation(self, parent_id: str, event_id: str, rel_type: str) -> None:
  35. self.get_success(
  36. self._main_store.db_pool.simple_insert(
  37. table="event_relations",
  38. values={
  39. "event_id": event_id,
  40. "relates_to_id": parent_id,
  41. "relation_type": rel_type,
  42. },
  43. )
  44. )
  45. def test_get_thread_id(self) -> None:
  46. """
  47. Ensure that get_thread_id only searches up the tree for threads.
  48. """
  49. # The thread itself and children of it return the thread.
  50. thread_id = self.get_success(self._main_store.get_thread_id("B"))
  51. self.assertEqual("A", thread_id)
  52. thread_id = self.get_success(self._main_store.get_thread_id("C"))
  53. self.assertEqual("A", thread_id)
  54. # But the root and events related to the root do not.
  55. thread_id = self.get_success(self._main_store.get_thread_id("A"))
  56. self.assertEqual(MAIN_TIMELINE, thread_id)
  57. thread_id = self.get_success(self._main_store.get_thread_id("D"))
  58. self.assertEqual(MAIN_TIMELINE, thread_id)
  59. thread_id = self.get_success(self._main_store.get_thread_id("E"))
  60. self.assertEqual(MAIN_TIMELINE, thread_id)
  61. # Events which are not related to a thread at all should return the
  62. # main timeline.
  63. thread_id = self.get_success(self._main_store.get_thread_id("F"))
  64. self.assertEqual(MAIN_TIMELINE, thread_id)
  65. thread_id = self.get_success(self._main_store.get_thread_id("G"))
  66. self.assertEqual(MAIN_TIMELINE, thread_id)
  67. def test_get_thread_id_for_receipts(self) -> None:
  68. """
  69. Ensure that get_thread_id_for_receipts searches up and down the tree for a thread.
  70. """
  71. # All of the events are considered related to this thread.
  72. thread_id = self.get_success(self._main_store.get_thread_id_for_receipts("A"))
  73. self.assertEqual("A", thread_id)
  74. thread_id = self.get_success(self._main_store.get_thread_id_for_receipts("B"))
  75. self.assertEqual("A", thread_id)
  76. thread_id = self.get_success(self._main_store.get_thread_id_for_receipts("C"))
  77. self.assertEqual("A", thread_id)
  78. thread_id = self.get_success(self._main_store.get_thread_id_for_receipts("D"))
  79. self.assertEqual("A", thread_id)
  80. thread_id = self.get_success(self._main_store.get_thread_id_for_receipts("E"))
  81. self.assertEqual("A", thread_id)
  82. # Events which are not related to a thread at all should return the
  83. # main timeline.
  84. thread_id = self.get_success(self._main_store.get_thread_id("F"))
  85. self.assertEqual(MAIN_TIMELINE, thread_id)
  86. thread_id = self.get_success(self._main_store.get_thread_id("G"))
  87. self.assertEqual(MAIN_TIMELINE, thread_id)