Skip to content

Commit

Permalink
Removed wrong mock calls
Browse files Browse the repository at this point in the history
Increased code readability.
  • Loading branch information
Roman Pystin committed Mar 26, 2013
1 parent c552058 commit b028065
Showing 1 changed file with 12 additions and 14 deletions.
26 changes: 12 additions & 14 deletions test/unit/common/test_key_manager.py
Expand Up @@ -46,12 +46,10 @@ def setUp(self):
self.url = "sqlite:///%s" % (self.db_path,)
self.conf = {'crypto_keystore_sql_url': self.url}
self.key_driver = SQLDriver(self.conf)
self.dbapi_connection = mock.Mock()
self.execute = mock.Mock()
self.mock_conn = mock.Mock(OperationalError=exc.OperationalError)

cursor = mock.Mock()
cursor.execute = mock.Mock()
self.dbapi_connection.cursor.return_value = cursor
self.dbapi_connection.OperationalError = exc.OperationalError
self.mock_conn.cursor.return_value = mock.Mock(execute=self.execute)
engine = create_engine(self.url)
meta_test.bind = engine
Session.configure(bind=engine)
Expand Down Expand Up @@ -129,32 +127,32 @@ def test_valid_key_id(self):
self.assertEqual(None, self.key_driver.validate_key_id("42"))

def test_succesful_ping(self):
self.dbapi_connection.cursor().execute.return_value = None
self.execute.return_value = None
self.assertEqual(None,
ping_connection(self.dbapi_connection, None, None))
ping_connection(self.mock_conn, None, None))

def test_connection_problems(self):
error = exc.OperationalError(None, None, None)
#typical situations when connection should be removed from a pool
#typical situation when connection should be removed from a pool
for code in (2006, 2013, 2014, 2045, 2055):
error.args = [code, 'connection need to be removed']
self.dbapi_connection.cursor().execute.side_effect = error
self.execute.side_effect = error
self.assertRaises(exc.DisconnectionError, ping_connection,
self.dbapi_connection, None, None)
self.mock_conn, None, None)

def test_unknown_problem(self):
error = exc.OperationalError(None, None, None)
#expected exception type with unexpected error code, raised further
error.args = [2000, "CR_UNKNOWN_ERROR"]
self.dbapi_connection.cursor().execute.side_effect = error
self.execute.side_effect = error
self.assertRaises(exc.OperationalError, ping_connection,
self.dbapi_connection, None, None)
self.mock_conn, None, None)
error = StandardError()
#unexpected error, shouldn't be catched
error.args = [42, 'all is lost']
self.dbapi_connection.cursor().execute.side_effect = error
self.execute.side_effect = error
self.assertRaises(StandardError, ping_connection,
self.dbapi_connection, None, None)
self.mock_conn, None, None)


if __name__ == '__main__':
Expand Down

0 comments on commit b028065

Please sign in to comment.