async def test_on_data_error(mocker, sparky): m_logger = mocker.spy(commander, 'LOGGER') # No response pipe sparky.data_callback('1234') assert len(sparky._requests) == 0 m_logger.error.assert_called_with( matching(r'.*not enough values to unpack')) # Valid hex, but not expected sparky.data_callback('BB|AA') assert len(sparky._requests) == 0 m_logger.error.assert_called_with(matching(r'.*Unexpected message'))
async def test_unexpected_message(app, client, mocker): m_log_error = mocker.patch(TESTED + '.LOGGER.error', autospec=True) cmder = commander.fget(app) message = EncodedResponse( msgId=123, error=ErrorCode.OK, payload=[] ) _, enc_message = await codec.fget(app).encode((codec.RESPONSE_TYPE, None), message.dict()) await cmder._data_callback(enc_message) m_log_error.assert_called_with(matching(r'.*Unexpected message'))
async def test_on_message(app, client, mocker): # If we want to check whether a function is called # without mocking its behavior, we can set a spy s_logger = mocker.spy(subscribe_example, 'LOGGER') feature = subscribe_example.fget(app) await feature.on_message('hello', {'to': 'world'}) # The actual call: # LOGGER.info(f'Message on topic {topic} = {message}') # # Let's assume we're not 100% sure how info will be formatted, # just that it should include topic and message body. # The `matching` test helper lets us compare with a regex string. s_logger.info.assert_called_once_with(matching(r'.*hello.*to.*world.*'))