本文整理汇总了Python中mock.MagicMock.execute方法的典型用法代码示例。如果您正苦于以下问题:Python MagicMock.execute方法的具体用法?Python MagicMock.execute怎么用?Python MagicMock.execute使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类mock.MagicMock
的用法示例。
在下文中一共展示了MagicMock.execute方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_execute_query1
# 需要导入模块: from mock import MagicMock [as 别名]
# 或者: from mock.MagicMock import execute [as 别名]
def test_execute_query1(self):
m_execute = Mock()
expected = [(17, 22), (19, 24)]
returns = [(17, 22), (19, 24), None]
def foo(*args):
r = returns.pop(0)
return r
m_fetchone = Mock(side_effect=foo)
m_cursor = MagicMock()
m_cursor.execute = m_execute
m_cursor.fetchone = m_fetchone
conn = MagicMock()
conn.cursor.return_value.__enter__.return_value = m_cursor
zipped = zip(
dbapi2_util.execute_query_iter(
conn,
"select * from somewhere"
),
expected
)
for x, y in zipped:
eq_(x, y)
eq_(conn.cursor.call_count, 1)
eq_(m_cursor.execute.call_count, 1)
m_cursor.execute.assert_called_once_with("select * from somewhere",
None)
示例2: _setup
# 需要导入模块: from mock import MagicMock [as 别名]
# 或者: from mock.MagicMock import execute [as 别名]
def _setup():
global reconnect_handler, session_manager, session_list, path, kernel_id, kernel_manager,\
individual_kernel_manager, response_id, good_msg, client, spark_events, request
# Mock kernel manager
client = MagicMock()
client.execute = MagicMock(return_value=response_id)
client.get_shell_msg = MagicMock(return_value=good_msg)
individual_kernel_manager = MagicMock()
individual_kernel_manager.client = MagicMock(return_value=client)
kernel_manager = MagicMock()
kernel_manager.get_kernel = MagicMock(return_value=individual_kernel_manager)
# Mock session manager
session_list = [create_session_dict(path, kernel_id)]
session_manager = MagicMock()
session_manager.list_sessions = MagicMock(return_value=session_list)
# Mock spark events
spark_events = MagicMock()
# Mock request
request = MagicMock()
request.body = json.dumps({"path": path, "username": username, "password": password, "endpoint": endpoint})
# Create mocked reconnect_handler
ReconnectHandler.__bases__ = (SimpleObject,)
reconnect_handler = ReconnectHandler()
reconnect_handler.spark_events = spark_events
reconnect_handler.session_manager = session_manager
reconnect_handler.kernel_manager = kernel_manager
reconnect_handler.set_status = MagicMock()
reconnect_handler.finish = MagicMock()
reconnect_handler.current_user = 'alex'
reconnect_handler.request = request
示例3: test_procedure
# 需要导入模块: from mock import MagicMock [as 别名]
# 或者: from mock.MagicMock import execute [as 别名]
def test_procedure(self):
manager = MagicMock()
manager.execute = MagicMock(return_value='expected')
with self.assertRaises(TypeError):
Procedure('wrong', manager)
p = Procedure(F('sum'), manager)
ast = p.to_ast()
self.assertEqual(
ast,
{
'name': 'run',
'val': {
'name': 'func_sum',
'val': []
}
}
)
result = p()
self.assertEqual(result, 'expected')
manager.execute.assert_called_with(
p.to_ast(),
scope=[]
)
示例4: setUp
# 需要导入模块: from mock import MagicMock [as 别名]
# 或者: from mock.MagicMock import execute [as 别名]
def setUp(self):
# instantiate broker for testing
self.sommelier_broker = SommelierBroker()
# create mock DB
db = MagicMock()
db.execute = MagicMock()
db.fetch_all = MagicMock()
db.fetch_one = MagicMock()
# inject mock db into broker
self.sommelier_broker.db = db
示例5: testNextTurn
# 需要导入模块: from mock import MagicMock [as 别名]
# 或者: from mock.MagicMock import execute [as 别名]
def testNextTurn(self):
from game import Game
g = Game()
#Create mock objects
helpCommand = MagicMock()
helpCommand.execute = MagicMock()
g._parser.getNextCommand = MagicMock(return_value=helpCommand)
g._nextTurn()
self.assertTrue(helpCommand.execute.called, "Game._nextTurn() failed to execute command")
示例6: SideEffect1
# 需要导入模块: from mock import MagicMock [as 别名]
# 或者: from mock.MagicMock import execute [as 别名]
def SideEffect1(groupKey, pageToken=''):
"""Mock list function to return different mock execute calls."""
# pylint: disable=unused-argument
# pylint: disable=invalid-name
if pageToken == '':
mock_execute.return_value = fake_dictionary_1
else:
mock_execute.return_value = fake_dictionary_2
some_object = MagicMock()
some_object.execute = mock_execute
return some_object
示例7: SideEffect
# 需要导入模块: from mock import MagicMock [as 别名]
# 或者: from mock.MagicMock import execute [as 别名]
def SideEffect(customer, maxResults, pageToken, projection, orderBy):
"""Mock list function to return different mock execute calls."""
# pylint: disable=unused-argument
# pylint: disable=invalid-name
if pageToken == '':
mock_execute.return_value = fake_dictionary_1
else:
mock_execute.return_value = fake_dictionary_2
some_object = MagicMock()
some_object.execute = mock_execute
return some_object
示例8: test_single_value_sql5
# 需要导入模块: from mock import MagicMock [as 别名]
# 或者: from mock.MagicMock import execute [as 别名]
def test_single_value_sql5(self):
m_execute = Mock()
m_fetchall = Mock(return_value=((17, 22),))
m_cursor = MagicMock()
m_cursor.execute = m_execute
m_cursor.fetchall = m_fetchall
conn = MagicMock()
conn.cursor.return_value.__enter__.return_value = m_cursor
dbapi2_util.single_row_sql(conn, "select 17, 22", (1, 2, 3))
eq_(conn.cursor.call_count, 1)
eq_(m_cursor.execute.call_count, 1)
m_cursor.execute.assert_called_once_with("select 17, 22", (1, 2, 3))
示例9: test_single_value_sql2
# 需要导入模块: from mock import MagicMock [as 别名]
# 或者: from mock.MagicMock import execute [as 别名]
def test_single_value_sql2(self):
m_execute = Mock()
m_fetchall = Mock(return_value=((17,),))
m_cursor = MagicMock()
m_cursor.execute = m_execute
m_cursor.fetchall = m_fetchall
conn = MagicMock()
conn.cursor.return_value.__enter__.return_value = m_cursor
dbapi2_util.single_value_sql(conn, "select 17", (1, 2, 3))
assert conn.cursor.call_count == 1
assert m_cursor.execute.call_count == 1
m_cursor.execute.assert_called_once_with('select 17', (1, 2, 3))
示例10: test_single_value_sql1
# 需要导入模块: from mock import MagicMock [as 别名]
# 或者: from mock.MagicMock import execute [as 别名]
def test_single_value_sql1(self):
m_execute = Mock()
m_fetchall = Mock(return_value=((17,),))
m_cursor = MagicMock()
m_cursor.execute = m_execute
m_cursor.fetchall = m_fetchall
conn = MagicMock()
conn.cursor.return_value.__enter__.return_value = m_cursor
r = dbapi2_util.single_value_sql(conn, "select 17")
eq_(r, 17)
eq_(conn.cursor.call_count, 1)
eq_(m_cursor.execute.call_count, 1)
m_cursor.execute.assert_called_once_with('select 17', None)
示例11: test_single_value_sql4
# 需要导入模块: from mock import MagicMock [as 别名]
# 或者: from mock.MagicMock import execute [as 别名]
def test_single_value_sql4(self):
m_execute = Mock()
m_fetchall = Mock(return_value=None)
m_cursor = MagicMock()
m_cursor.execute = m_execute
m_cursor.fetchall = m_fetchall
conn = MagicMock()
conn.cursor.return_value.__enter__.return_value = m_cursor
with pytest.raises(dbapi2_util.SQLDidNotReturnSingleRow):
dbapi2_util.single_row_sql(conn, 'select 17, 22', (1, 2, 3))
assert conn.cursor.call_count == 1
assert m_cursor.execute.call_count == 1
m_cursor.execute.assert_called_once_with("select 17, 22", (1, 2, 3))
示例12: test_single_row_sql1
# 需要导入模块: from mock import MagicMock [as 别名]
# 或者: from mock.MagicMock import execute [as 别名]
def test_single_row_sql1(self):
m_execute = Mock()
m_fetchall = Mock(return_value=((17, 22),))
m_cursor = MagicMock()
m_cursor.execute = m_execute
m_cursor.fetchall = m_fetchall
conn = MagicMock()
conn.cursor.return_value.__enter__.return_value = m_cursor
r = dbapi2_util.single_row_sql(conn, "select 17, 22")
assert r == (17, 22)
assert conn.cursor.call_count == 1
assert m_cursor.execute.call_count == 1
m_cursor.execute.assert_called_once_with('select 17, 22', None)
示例13: test_single_value_sql4
# 需要导入模块: from mock import MagicMock [as 别名]
# 或者: from mock.MagicMock import execute [as 别名]
def test_single_value_sql4(self):
m_execute = Mock()
m_fetchall = Mock(return_value=None)
m_cursor = MagicMock()
m_cursor.execute = m_execute
m_cursor.fetchall = m_fetchall
conn = MagicMock()
conn.cursor.return_value.__enter__.return_value = m_cursor
assert_raises(dbapi2_util.SQLDidNotReturnSingleRow,
dbapi2_util.single_row_sql,
conn,
"select 17, 22",
(1, 2, 3))
eq_(conn.cursor.call_count, 1)
eq_(m_cursor.execute.call_count, 1)
m_cursor.execute.assert_called_once_with("select 17, 22", (1, 2, 3))
示例14: test_execute_no_results
# 需要导入模块: from mock import MagicMock [as 别名]
# 或者: from mock.MagicMock import execute [as 别名]
def test_execute_no_results(self):
m_execute = Mock()
m_cursor = MagicMock()
m_cursor.execute = m_execute
conn = MagicMock()
conn.cursor.return_value.__enter__.return_value = m_cursor
dbapi2_util.execute_no_results(
conn,
"insert into table (a, b, c) values (%s, %s, %s)",
(1, 2, 3)
)
eq_(conn.cursor.call_count, 1)
eq_(m_cursor.execute.call_count, 1)
m_cursor.execute.assert_called_once_with(
"insert into table (a, b, c) values (%s, %s, %s)",
(1, 2, 3)
)
示例15: test_execute_query2
# 需要导入模块: from mock import MagicMock [as 别名]
# 或者: from mock.MagicMock import execute [as 别名]
def test_execute_query2(self):
m_execute = Mock()
expected = []
returns = [None]
def foo(*args):
r = returns.pop(0)
return r
m_fetchone = Mock(side_effect=foo)
m_cursor = MagicMock()
m_cursor.execute = m_execute
m_cursor.fetchone = m_fetchone
conn = MagicMock()
conn.cursor.return_value.__enter__.return_value = m_cursor
zipped = zip(dbutil.execute_query_iter(conn, "select * from somewhere"), expected)
for x, y in zipped:
assert x == y
assert conn.cursor.call_count == 1
assert m_cursor.execute.call_count == 1
m_cursor.execute.assert_called_once_with("select * from somewhere", None)