本文整理汇总了Python中unittest.mock.Mock.configure_mock方法的典型用法代码示例。如果您正苦于以下问题:Python Mock.configure_mock方法的具体用法?Python Mock.configure_mock怎么用?Python Mock.configure_mock使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类unittest.mock.Mock
的用法示例。
在下文中一共展示了Mock.configure_mock方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_the_pump_should_dispatch_a_command_processor
# 需要导入模块: from unittest.mock import Mock [as 别名]
# 或者: from unittest.mock.Mock import configure_mock [as 别名]
def test_the_pump_should_dispatch_a_command_processor(self):
"""
Given that I have a message pump for a channel
When I read a message from that channel
Then the message should be dispatched to a handler
"""
handler = MyCommandHandler()
request = MyCommand()
channel = Mock(spec=Channel)
command_processor = Mock(spec=CommandProcessor)
message_pump = MessagePump(command_processor, channel, map_to_message)
header = BrightsideMessageHeader(uuid4(), request.__class__.__name__, BrightsideMessageType.command)
body = BrightsideMessageBody(JsonRequestSerializer(request=request).serialize_to_json(),
BrightsideMessageBodyType.application_json)
message = BrightsideMessage(header, body)
quit_message = BrightsideMessageFactory.create_quit_message()
# add messages to that when channel is called it returns first message then qui tmessage
response_queue = [message, quit_message]
channel_spec = {"receive.side_effect" : response_queue}
channel.configure_mock(**channel_spec)
message_pump.run()
channel_calls = [call.receive(), call.receive]
channel.assert_has_calls(channel_calls)
cp_calls = [call.send(request)]
command_processor.assert_has_calls(cp_calls)
示例2: TestUserController
# 需要导入模块: from unittest.mock import Mock [as 别名]
# 或者: from unittest.mock.Mock import configure_mock [as 别名]
class TestUserController(unittest.TestCase):
def setUp(self):
self.userToTest = User('test', '123456-012e1', '123h4123asdhh123', timezone(timedelta(hours = 5, minutes = 30)), 1)
self.mockUserDataStrategy = Mock()
self.mockUserDataStrategyAttrs = {"saveUser.return_value": 1,
"getUserByUsername.return_value": None,
"getUserById.return_value": self.userToTest,
"updateUser.return_value": True}
self.mockUserDataStrategy.configure_mock(**self.mockUserDataStrategyAttrs)
DataConfig.UserDataStrategy = self.mockUserDataStrategy
@patch.object(tweepy.API, 'me')
def test_userCanBeSaved(self, mePatch):
mePatch.return_value(TwitterUser('example'))
self.assertEqual(UserController.createUser('asd', '123fsd', '5', '30'), {"result": "success", "value": 1, "updated": False})
self.assertTrue(self.mockUserDataStrategy.saveUser.called)
self.assertTrue(mePatch.called)
def test_userTimeZoneCanBeSet(self):
formValue = {'tzhour': 5, 'tzminute': 30}
userId = 1
self.assertEqual(UserController.setTimeZone(formValue, userId),{"result": "success"})
self.assertTrue(self.mockUserDataStrategy.updateUser.called, )
def test_userTimezoneCanBeRetrievedAsDictionary(self):
userDetailsToTest = UserController.getUserTimeZone(1)
self.assertEqual(userDetailsToTest, {"hours": 5, "minutes": 30})
self.userToTest.timeZone = timezone(timedelta(hours=-5, minutes=-30))
userDetailsToTest = UserController.getUserTimeZone(1)
self.assertEqual(userDetailsToTest, {"hours": -5, "minutes": -30})
示例3: test_generate_credentials_set_credentials
# 需要导入模块: from unittest.mock import Mock [as 别名]
# 或者: from unittest.mock.Mock import configure_mock [as 别名]
def test_generate_credentials_set_credentials(self):
url = "http://www.google.com"
payload = {'label': "test",
'scope': "read",
'token_expires_in': "200000"}
test_vals = {'client_id': 'client-id',
'client_secret': 'client-secret'}
response_vals = {'ok': True,
'json.return_value': test_vals}
response = Mock()
response.configure_mock(**response_vals)
with patch.object(requests, 'post', return_value=response) as mocker:
self.access.generate_new_credentials(url, self.label)
self.assertEqual(self.access.client_id,
test_vals['client_id'])
self.assertEqual(self.access.client_secret,
test_vals['client_secret'])
self.assertEqual(self.access.expiration,
payload['token_expires_in'])
response_vals['ok'] = False
response.reset_mock()
response.configure_mock(**response_vals)
with patch.object(requests, 'post', return_value=response) as mocker:
with self.assertRaises(BadRequest):
self.access.generate_new_credentials(url, self.label)
示例4: test_userCanBeRetrieved
# 需要导入模块: from unittest.mock import Mock [as 别名]
# 或者: from unittest.mock.Mock import configure_mock [as 别名]
def test_userCanBeRetrieved(self):
mockUserDataStrategy = Mock()
userToTest = User('test', '123456-012e1', '123h4123asdhh123', timezone(timedelta(hours = 5, minutes = 30)), userId = 1)
mockUserDataStrategyAttrs = {"getUserById.return_value": userToTest
}
mockUserDataStrategy.configure_mock(**mockUserDataStrategyAttrs)
self.assertEqual(UserCRUD.getUserById(1, mockUserDataStrategy), userToTest)
示例5: test_messageIsSentCorrectlyWhenOver140Chars
# 需要导入模块: from unittest.mock import Mock [as 别名]
# 或者: from unittest.mock.Mock import configure_mock [as 别名]
def test_messageIsSentCorrectlyWhenOver140Chars(self, sendReplyPatch, usernameMethod, urlLengthPatch):
sendReplyPatch.side_effect = [1234, 1235, 1236]
urlLengthPatch.return_value = (23,23)
usernameMethod.return_value = "example"
d = datetime.now(tz = timezone(timedelta(hours=5, minutes=30)))
replyToSend = Reply(1,
"@example an example message that is just way too long to be kept inside a single tweet. Therefore it will be broken down into lots of little messages each having the example username on top of it. Sounds cool? Keep going! I'd really like to make this message about 3 tweets long so that I can make sure that the module is working properly. Like really well.",
d, timezone(timedelta(hours=5, minutes=30)), 134953292, replyId = 1)
user = User('test', '123456-012e1', '123h4123asdhh123', timezone(timedelta(hours = 5, minutes = 30)))
mockUserDataStrategy = Mock()
mockUserDataStrategyAttrs = {"getUserById.return_value": user }
mockUserDataStrategy.configure_mock(**mockUserDataStrategyAttrs)
mockReplyDataStrategy = Mock()
mockReplyDataStrategyAttrs = {"getReplyByReplyId.side_effect": [copy.deepcopy(replyToSend), copy.deepcopy(replyToSend)]}
mockReplyDataStrategy.configure_mock(**mockReplyDataStrategyAttrs)
DataConfig.ReplyDataStrategy = mockReplyDataStrategy
DataConfig.UserDataStrategy = mockUserDataStrategy
self.assertEqual(MessageSender.sendMessage(1, ""), {"result": "success", "value": {"tweets": [1234, 1235, 1236]}})
usernameMethod.assert_called_once_with(134953292, 1, mockUserDataStrategy)
t1 = "@example an example message that is just way too long to be kept inside a single tweet. Therefore it will be broken down into lots of little"
t2 = "@example messages each having the example username on top of it. Sounds cool? Keep going! I'd really like to make this message about 3"
t3 = "@example tweets long so that I can make sure that the module is working properly. Like really well."
sendReplyPatch.assert_any_call(t1, 134953292, 1, mockUserDataStrategy)
sendReplyPatch.assert_any_call(t2, 1234, 1, mockUserDataStrategy)
sendReplyPatch.assert_any_call(t3, 1235, 1, mockUserDataStrategy)
self.assertTrue(mockReplyDataStrategy.updateReply.called)
示例6: push_alert
# 需要导入模块: from unittest.mock import Mock [as 别名]
# 或者: from unittest.mock.Mock import configure_mock [as 别名]
async def push_alert(self, alert_type, **kwargs):
alert = Mock(**{
'__class__.__name__': alert_type,
'category.return_value': 0
})
alert.configure_mock(**kwargs)
self._alerts.append(alert)
self.event.set()
示例7: test_replyObjectSavesCorrectly
# 需要导入模块: from unittest.mock import Mock [as 别名]
# 或者: from unittest.mock.Mock import configure_mock [as 别名]
def test_replyObjectSavesCorrectly(self, scheduleReplyPatch):
d = datetime.now(tz = timezone(timedelta(hours=5, minutes=30))) + timedelta(minutes=20)
replyToSave = Reply(1, "@example an example message", d, timezone(timedelta(hours=5, minutes=30)), 134953292)
mockReplyDataStrategy = Mock()
mockReplyDataStrategyAttrs = {"saveReply.return_value": 1}
mockReplyDataStrategy.configure_mock(**mockReplyDataStrategyAttrs)
self.assertEqual(ReplyCRUD.saveReply(replyToSave, mockReplyDataStrategy), {"result": "success", "value": 1})
scheduleReplyPatch.assert_called_once_with(1, d)
示例8: test_replyCanBeRetrievedById
# 需要导入模块: from unittest.mock import Mock [as 别名]
# 或者: from unittest.mock.Mock import configure_mock [as 别名]
def test_replyCanBeRetrievedById(self):
d = datetime.now(tz = timezone(timedelta(hours=5, minutes=30))) + timedelta(minutes=20)
replyToRetrieve = Reply(1, "@example an example message", d, timezone(timedelta(hours=5, minutes=30)), 134953292, replyId = 1)
mockReplyDataStrategy = Mock()
mockReplyDataStrategyAttrs = {"getReplyByReplyId.return_value": replyToRetrieve}
mockReplyDataStrategy.configure_mock(**mockReplyDataStrategyAttrs)
self.assertEqual(ReplyCRUD.getReplyByReplyId(1, mockReplyDataStrategy), replyToRetrieve)
self.assertTrue(mockReplyDataStrategy.getReplyByReplyId.called)
示例9: test_messageIsBrokenDownCorrectlyBeforeSending
# 需要导入模块: from unittest.mock import Mock [as 别名]
# 或者: from unittest.mock.Mock import configure_mock [as 别名]
def test_messageIsBrokenDownCorrectlyBeforeSending(self):
user = User('test', '123456-012e1', '123h4123asdhh123', timezone(timedelta(hours = 5, minutes = 30)))
mockUserDataStrategy = Mock()
mockUserDataStrategyAttrs = {"getUserById.return_value": user }
mockUserDataStrategy.configure_mock(**mockUserDataStrategyAttrs)
d = datetime.now(tz = timezone(timedelta(hours=5, minutes=30))) + timedelta(minutes=20)
replyToSend = Reply(1, "@example an example message", d, timezone(timedelta(hours=5, minutes=30)), 134953292, replyId = 1)
self.assertEqual(MessageBreaker.breakMessage(replyToSend.message, replyToSend.tweetId, 1, mockUserDataStrategy), ["@example an example message"])
示例10: test_validUserCanBeSaved
# 需要导入模块: from unittest.mock import Mock [as 别名]
# 或者: from unittest.mock.Mock import configure_mock [as 别名]
def test_validUserCanBeSaved(self):
mockUserDataStrategy = Mock()
mockUserDataStrategyAttrs = {"saveUser.return_value": 1,
"getUserByUsername.return_value": None
}
mockUserDataStrategy.configure_mock(**mockUserDataStrategyAttrs)
userToTest = User('test', '123456-012e1', '123h4123asdhh123', timezone(timedelta(hours = 5, minutes = 30)))
self.assertEqual(UserCRUD.saveUser(userToTest, mockUserDataStrategy), {"result": "success", "value": 1, "updated": False})
示例11: test_routing_mocked_pfs_bad_http_code
# 需要导入模块: from unittest.mock import Mock [as 别名]
# 或者: from unittest.mock.Mock import configure_mock [as 别名]
def test_routing_mocked_pfs_bad_http_code(
chain_state,
payment_network_state,
token_network_state,
our_address,
):
token_network_state, addresses, channel_states = create_square_network_topology(
payment_network_state=payment_network_state,
token_network_state=token_network_state,
our_address=our_address,
)
address1, address2, address3 = addresses
channel_state1, channel_state2 = channel_states
# test routing with all nodes available
chain_state.nodeaddresses_to_networkstates = {
address1: NODE_NETWORK_REACHABLE,
address2: NODE_NETWORK_REACHABLE,
address3: NODE_NETWORK_REACHABLE,
}
# channel 1 and 2 are flipped here, to see when the PFS gets called
json_data = {
'result': [
{
'path': [to_checksum_address(our_address), to_checksum_address(address2)],
'fees': 0,
},
{
'path': [to_checksum_address(our_address), to_checksum_address(address1)],
'fees': 0,
},
],
}
response = Mock()
response.configure_mock(status_code=400)
response.json = Mock(return_value=json_data)
with patch.object(requests, 'get', return_value=response):
routes = get_best_routes(
chain_state=chain_state,
token_network_id=token_network_state.address,
from_address=our_address,
to_address=address1,
amount=50,
previous_address=None,
config={
'services': {
'pathfinding_service_address': 'my-pfs',
'pathfinding_max_paths': 3,
},
},
)
assert routes[0].node_address == address1
assert routes[0].channel_identifier == channel_state1.identifier
assert routes[1].node_address == address2
assert routes[1].channel_identifier == channel_state2.identifier
示例12: test_userCanBeEdited
# 需要导入模块: from unittest.mock import Mock [as 别名]
# 或者: from unittest.mock.Mock import configure_mock [as 别名]
def test_userCanBeEdited(self):
mockUserDataStrategy = Mock()
mockUserDataStrategyAttrs = {"saveUser.return_value": 1,
"updateUser.return_value": True}
mockUserDataStrategy.configure_mock(**mockUserDataStrategyAttrs)
userToTest = User('test', '123456-012e1', '123h4123asdhh123', timezone(timedelta(hours = 5, minutes = 30)), userId = 1)
self.assertEqual(UserCRUD.updateUser(userToTest, mockUserDataStrategy), {"result": "success"})
userToTest = User('test', '', '123h4123asdhh123', timezone(timedelta(hours = 5, minutes = 30)), userId = 1)
self.assertEqual(UserCRUD.updateUser(userToTest, mockUserDataStrategy), {"result": "error", "value": {"tokenError": "Token cannot be empty"}})
示例13: setUp
# 需要导入模块: from unittest.mock import Mock [as 别名]
# 或者: from unittest.mock.Mock import configure_mock [as 别名]
def setUp(self):
super(TestAppCommandWithFlags, self).setUp()
mock_flag_1 = Mock()
mock_flag_2 = Mock()
mock_flag_1.configure_mock(name='flag1', description='flag 1 desc')
mock_flag_2.configure_mock(name='flag2', description='flag 2 desc')
self.flag_mocks = [mock_flag_1, mock_flag_2]
示例14: test_repliesCanBeRetrievedByUserIdAndStatus
# 需要导入模块: from unittest.mock import Mock [as 别名]
# 或者: from unittest.mock.Mock import configure_mock [as 别名]
def test_repliesCanBeRetrievedByUserIdAndStatus(self):
d = datetime.now(tz = timezone(timedelta(hours=5, minutes=30))) + timedelta(minutes=20)
replies = [
Reply(1, "@example an example message", d, timezone(timedelta(hours=5, minutes=30)), 134953292, replyId = 1),
Reply(1, "@example an example message", d, timezone(timedelta(hours=5, minutes=30)), 134953292, replyId = 2)
]
mockReplyDataStrategy = Mock()
mockReplyDataStrategyAttrs = {"getRepliesByUserIdAndStatus.return_value": replies}
mockReplyDataStrategy.configure_mock(**mockReplyDataStrategyAttrs)
self.assertEqual(ReplyCRUD.getRepliesByUserIdAndStatus(1, "unsent", mockReplyDataStrategy), replies)
示例15: test_map_relations_invalid_id
# 需要导入模块: from unittest.mock import Mock [as 别名]
# 或者: from unittest.mock.Mock import configure_mock [as 别名]
def test_map_relations_invalid_id(self):
from pyramid_skosprovider.renderers import _map_relations
p = Mock()
config = {
'get_by_id.return_value': False,
'get_vocabulary_id.return_value': 'TESTPROVIDER'
}
p.configure_mock(**config)
rels = _map_relations([5], p)
assert len(rels) == 0