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