test__base.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. # Copyright 2015, 2016 OpenMarket Ltd
  2. # Copyright 2019 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. import secrets
  16. from tests import unittest
  17. class UpsertManyTests(unittest.HomeserverTestCase):
  18. def prepare(self, reactor, clock, hs):
  19. self.storage = hs.get_datastore()
  20. self.table_name = "table_" + secrets.token_hex(6)
  21. self.get_success(
  22. self.storage.db_pool.runInteraction(
  23. "create",
  24. lambda x, *a: x.execute(*a),
  25. "CREATE TABLE %s (id INTEGER, username TEXT, value TEXT)"
  26. % (self.table_name,),
  27. )
  28. )
  29. self.get_success(
  30. self.storage.db_pool.runInteraction(
  31. "index",
  32. lambda x, *a: x.execute(*a),
  33. "CREATE UNIQUE INDEX %sindex ON %s(id, username)"
  34. % (self.table_name, self.table_name),
  35. )
  36. )
  37. def _dump_to_tuple(self, res):
  38. for i in res:
  39. yield (i["id"], i["username"], i["value"])
  40. def test_upsert_many(self):
  41. """
  42. Upsert_many will perform the upsert operation across a batch of data.
  43. """
  44. # Add some data to an empty table
  45. key_names = ["id", "username"]
  46. value_names = ["value"]
  47. key_values = [[1, "user1"], [2, "user2"]]
  48. value_values = [["hello"], ["there"]]
  49. self.get_success(
  50. self.storage.db_pool.runInteraction(
  51. "test",
  52. self.storage.db_pool.simple_upsert_many_txn,
  53. self.table_name,
  54. key_names,
  55. key_values,
  56. value_names,
  57. value_values,
  58. )
  59. )
  60. # Check results are what we expect
  61. res = self.get_success(
  62. self.storage.db_pool.simple_select_list(
  63. self.table_name, None, ["id, username, value"]
  64. )
  65. )
  66. self.assertEqual(
  67. set(self._dump_to_tuple(res)),
  68. {(1, "user1", "hello"), (2, "user2", "there")},
  69. )
  70. # Update only user2
  71. key_values = [[2, "user2"]]
  72. value_values = [["bleb"]]
  73. self.get_success(
  74. self.storage.db_pool.runInteraction(
  75. "test",
  76. self.storage.db_pool.simple_upsert_many_txn,
  77. self.table_name,
  78. key_names,
  79. key_values,
  80. value_names,
  81. value_values,
  82. )
  83. )
  84. # Check results are what we expect
  85. res = self.get_success(
  86. self.storage.db_pool.simple_select_list(
  87. self.table_name, None, ["id, username, value"]
  88. )
  89. )
  90. self.assertEqual(
  91. set(self._dump_to_tuple(res)),
  92. {(1, "user1", "hello"), (2, "user2", "bleb")},
  93. )