Exemplo n.º 1
0
async def test_publish(test_case, mocker, mqtt_adapter):
    # pylint: disable=unused-argument
    async def mock_publish(topics, message, options=None, stash=True):
        pass

    mocker.patch('src.iotea.core.util.mqtt_client.MqttClient.publish',
                 wraps=mock_publish)
    message = 'test message'
    # test if publish options are passed to MqttClient correctly
    publish_options = ProtocolGateway.create_publish_options()
    publish_options.retain = not publish_options.retain
    publish_options.stash = not publish_options.stash

    await mqtt_adapter.publish('test', message, publish_options)
    test_case.assertEqual(1, mqtt_adapter.client.publish.call_count)

    mqtt_adapter.client.publish.assert_called_once_with(
        ['iotea/test'], message, {'retain': publish_options.retain},
        publish_options.stash)

    await mqtt_adapter.publish('test', message, None)
    test_case.assertEqual(2, mqtt_adapter.client.publish.call_count)

    mqtt_adapter.client.publish.assert_called_with(
        ['iotea/test'], message,
        {'retain': ProtocolGateway.create_publish_options().retain},
        ProtocolGateway.create_publish_options().stash)
Exemplo n.º 2
0
async def test_publish(test_case, mocker, protocol_gateway, test_message,
                       force_wait):
    test_topic = 'test'

    mocker.patch('src.iotea.core.util.mqtt_client.MqttProtocolAdapter.publish',
                 wraps=mock_publish)

    publish_options = ProtocolGateway.create_publish_options()
    if isinstance(test_message, str):
        await protocol_gateway.publish(test_topic,
                                       test_message,
                                       publish_options,
                                       force_wait=force_wait)
        test_case.assertEqual(
            1, protocol_gateway.adapters[0].instance.publish.call_count)
        protocol_gateway.adapters[0].instance.publish.assert_called_once_with(
            test_topic, test_message, publish_options)
    else:
        await protocol_gateway.publish_json(test_topic,
                                            test_message,
                                            publish_options,
                                            force_wait=force_wait)
        test_case.assertEqual(
            1, protocol_gateway.adapters[0].instance.publish.call_count)
        protocol_gateway.adapters[0].instance.publish.assert_called_once_with(
            test_topic, json.dumps(test_message, separators=(',', ':')),
            publish_options)
Exemplo n.º 3
0
async def test_validate_platform_protocol_usage(test_case):
    mqtt_def_config = MqttProtocolAdapter.create_default_configuration(is_platform_protocol=True)
    protocol_gateway_config = ProtocolGateway.create_default_configuration([mqtt_def_config])
    protocol_gateway = ProtocolGateway(protocol_gateway_config, 'TestProtocolGateway', use_platform_protocol_only=True)
    publish_options = ProtocolGateway.create_publish_options(platform_protocol_only=False)
    with pytest.raises(Exception) as exc_info:
        await protocol_gateway.publish("test_topic", "test message", publish_options)
    test_case.assertEqual(
        f'Gateway is configured to only use the provided platform protocol. Runtime request for all protocols given.',
        str(exc_info.value))
Exemplo n.º 4
0
async def test_publish_options(test_case, mocker, protocol_gateway):
    test_topic = 'test'
    test_message = 'test message'
    mocker.patch('src.iotea.core.util.mqtt_client.MqttProtocolAdapter.publish', wraps=mock_publish)
    # test publish with None pub options
    await protocol_gateway.publish(test_topic, test_message, None)
    test_case.assertEqual(1, protocol_gateway.adapters[0].instance.publish.call_count)
    protocol_gateway.adapters[0].instance.publish.assert_called_once_with(test_topic, test_message, unittest.mock.ANY)
    args, _ = protocol_gateway.adapters[0].instance.publish.call_args
    # check that public options is not None
    test_case.assertIsNotNone(args[2])

    # test publish to platform adapter
    publish_options = ProtocolGateway.create_publish_options(True, None)
    await protocol_gateway.publish(test_topic, test_message, publish_options)
    test_case.assertEqual(2, protocol_gateway.adapters[0].instance.publish.call_count)
    protocol_gateway.adapters[0].instance.publish.assert_called_with(test_topic, test_message, publish_options)