test_base.py 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. # -*- coding: utf-8 -*-
  2. # Copyright 2014-2016 OpenMarket 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 collections import OrderedDict
  16. from mock import Mock
  17. from twisted.internet import defer
  18. from synapse.storage._base import SQLBaseStore
  19. from synapse.storage.engines import create_engine
  20. from tests import unittest
  21. from tests.utils import TestHomeServer
  22. class SQLBaseStoreTestCase(unittest.TestCase):
  23. """ Test the "simple" SQL generating methods in SQLBaseStore. """
  24. def setUp(self):
  25. self.db_pool = Mock(spec=["runInteraction"])
  26. self.mock_txn = Mock()
  27. self.mock_conn = Mock(spec_set=["cursor", "rollback", "commit"])
  28. self.mock_conn.cursor.return_value = self.mock_txn
  29. self.mock_conn.rollback.return_value = None
  30. # Our fake runInteraction just runs synchronously inline
  31. def runInteraction(func, *args, **kwargs):
  32. return defer.succeed(func(self.mock_txn, *args, **kwargs))
  33. self.db_pool.runInteraction = runInteraction
  34. def runWithConnection(func, *args, **kwargs):
  35. return defer.succeed(func(self.mock_conn, *args, **kwargs))
  36. self.db_pool.runWithConnection = runWithConnection
  37. config = Mock()
  38. config._disable_native_upserts = True
  39. config.event_cache_size = 1
  40. config.database_config = {"name": "sqlite3"}
  41. engine = create_engine(config.database_config)
  42. fake_engine = Mock(wraps=engine)
  43. fake_engine.can_native_upsert = False
  44. hs = TestHomeServer(
  45. "test",
  46. db_pool=self.db_pool,
  47. config=config,
  48. database_engine=fake_engine,
  49. )
  50. self.datastore = SQLBaseStore(None, hs)
  51. @defer.inlineCallbacks
  52. def test_insert_1col(self):
  53. self.mock_txn.rowcount = 1
  54. yield self.datastore._simple_insert(
  55. table="tablename", values={"columname": "Value"}
  56. )
  57. self.mock_txn.execute.assert_called_with(
  58. "INSERT INTO tablename (columname) VALUES(?)", ("Value",)
  59. )
  60. @defer.inlineCallbacks
  61. def test_insert_3cols(self):
  62. self.mock_txn.rowcount = 1
  63. yield self.datastore._simple_insert(
  64. table="tablename",
  65. # Use OrderedDict() so we can assert on the SQL generated
  66. values=OrderedDict([("colA", 1), ("colB", 2), ("colC", 3)]),
  67. )
  68. self.mock_txn.execute.assert_called_with(
  69. "INSERT INTO tablename (colA, colB, colC) VALUES(?, ?, ?)", (1, 2, 3)
  70. )
  71. @defer.inlineCallbacks
  72. def test_select_one_1col(self):
  73. self.mock_txn.rowcount = 1
  74. self.mock_txn.__iter__ = Mock(return_value=iter([("Value",)]))
  75. value = yield self.datastore._simple_select_one_onecol(
  76. table="tablename", keyvalues={"keycol": "TheKey"}, retcol="retcol"
  77. )
  78. self.assertEquals("Value", value)
  79. self.mock_txn.execute.assert_called_with(
  80. "SELECT retcol FROM tablename WHERE keycol = ?", ["TheKey"]
  81. )
  82. @defer.inlineCallbacks
  83. def test_select_one_3col(self):
  84. self.mock_txn.rowcount = 1
  85. self.mock_txn.fetchone.return_value = (1, 2, 3)
  86. ret = yield self.datastore._simple_select_one(
  87. table="tablename",
  88. keyvalues={"keycol": "TheKey"},
  89. retcols=["colA", "colB", "colC"],
  90. )
  91. self.assertEquals({"colA": 1, "colB": 2, "colC": 3}, ret)
  92. self.mock_txn.execute.assert_called_with(
  93. "SELECT colA, colB, colC FROM tablename WHERE keycol = ?", ["TheKey"]
  94. )
  95. @defer.inlineCallbacks
  96. def test_select_one_missing(self):
  97. self.mock_txn.rowcount = 0
  98. self.mock_txn.fetchone.return_value = None
  99. ret = yield self.datastore._simple_select_one(
  100. table="tablename",
  101. keyvalues={"keycol": "Not here"},
  102. retcols=["colA"],
  103. allow_none=True,
  104. )
  105. self.assertFalse(ret)
  106. @defer.inlineCallbacks
  107. def test_select_list(self):
  108. self.mock_txn.rowcount = 3
  109. self.mock_txn.__iter__ = Mock(return_value=iter([(1,), (2,), (3,)]))
  110. self.mock_txn.description = (("colA", None, None, None, None, None, None),)
  111. ret = yield self.datastore._simple_select_list(
  112. table="tablename", keyvalues={"keycol": "A set"}, retcols=["colA"]
  113. )
  114. self.assertEquals([{"colA": 1}, {"colA": 2}, {"colA": 3}], ret)
  115. self.mock_txn.execute.assert_called_with(
  116. "SELECT colA FROM tablename WHERE keycol = ?", ["A set"]
  117. )
  118. @defer.inlineCallbacks
  119. def test_update_one_1col(self):
  120. self.mock_txn.rowcount = 1
  121. yield self.datastore._simple_update_one(
  122. table="tablename",
  123. keyvalues={"keycol": "TheKey"},
  124. updatevalues={"columnname": "New Value"},
  125. )
  126. self.mock_txn.execute.assert_called_with(
  127. "UPDATE tablename SET columnname = ? WHERE keycol = ?",
  128. ["New Value", "TheKey"],
  129. )
  130. @defer.inlineCallbacks
  131. def test_update_one_4cols(self):
  132. self.mock_txn.rowcount = 1
  133. yield self.datastore._simple_update_one(
  134. table="tablename",
  135. keyvalues=OrderedDict([("colA", 1), ("colB", 2)]),
  136. updatevalues=OrderedDict([("colC", 3), ("colD", 4)]),
  137. )
  138. self.mock_txn.execute.assert_called_with(
  139. "UPDATE tablename SET colC = ?, colD = ? WHERE" " colA = ? AND colB = ?",
  140. [3, 4, 1, 2],
  141. )
  142. @defer.inlineCallbacks
  143. def test_delete_one(self):
  144. self.mock_txn.rowcount = 1
  145. yield self.datastore._simple_delete_one(
  146. table="tablename", keyvalues={"keycol": "Go away"}
  147. )
  148. self.mock_txn.execute.assert_called_with(
  149. "DELETE FROM tablename WHERE keycol = ?", ["Go away"]
  150. )