コード例 #1
0
async def test_unset_protocol_version(message, message_schema, node, child,
                                      transport):
    """Test gateway listen."""
    gateway = Gateway(transport)
    gateway.nodes[0] = node
    cmd = message_schema.dump(message)

    async with gateway:
        async for msg in gateway.listen():
            assert message_schema.dump(msg) == cmd
            break

    assert gateway.transport.writes == [
        message_schema.dump(Message(0, 255, 3, 0, 2, ""))
    ]
コード例 #2
0
async def test_presentation_gateway_protocol_version(protocol_version,
                                                     message_schema,
                                                     transport):
    """Test that gateway presentation sets protocol version."""
    gateway = Gateway(transport)
    message = Message(0, 255, 0, 0, 17, protocol_version)
    command = message_schema.dump(message)
    transport.messages.append(command)
    assert gateway.protocol_version is None

    async for msg in gateway.listen():
        assert message_schema.dump(msg) == command
        break

    assert gateway.protocol_version == protocol_version
    assert gateway.protocol is get_protocol(protocol_version)
コード例 #3
0
def test_dump(message_schema):
    """Test dump of message."""
    msg = Message()

    cmd = message_schema.dump(msg)

    assert cmd == "0;0;0;0;0;\n"

    msg.node_id = 1
    msg.child_id = 255
    msg.command = 3
    msg.ack = 0
    msg.message_type = 0
    msg.payload = 57

    cmd = message_schema.dump(msg)

    assert cmd == "1;255;3;0;0;57\n"
コード例 #4
0
@pytest.fixture(name="gateway_cli", autouse=True)
def gateway_cli_fixture():
    """Mock the CLI gateway handler."""
    with patch("aiomysensors.cli.gateway_serial.Gateway",
               autospec=True) as gateway_class:
        gateway = gateway_class.return_value
        yield gateway


@pytest.mark.parametrize(
    "error",
    [
        None,
        MissingNodeError(1),
        MissingChildError(1),
        UnsupportedMessageError(Message()),
        AIOMySensorsError(),
    ],
)
@pytest.mark.parametrize(
    "args",
    [["serial-gateway", "-p", "/test"],
     ["--debug", "serial-gateway", "-p", "/test"]],
)
def test_serial_gateway(gateway_handler, args, error):
    """Test the serial gateway CLI."""
    gateway_handler.side_effect = [error, KeyboardInterrupt()]
    runner = CliRunner()
    result = runner.invoke(cli, args)
    assert result.exit_code == 0
コード例 #5
0
# pylint: disable=too-many-arguments,unused-argument

# All test coroutines will be treated as marked.
pytestmark = pytest.mark.asyncio

PROTOCOL_VERSIONS_2x = list(PROTOCOL_VERSIONS)
PROTOCOL_VERSIONS_2x.remove("1.4")
PROTOCOL_VERSIONS_2x.remove("1.5")


@pytest.mark.parametrize("message_schema", PROTOCOL_VERSIONS_2x, indirect=True)
@pytest.mark.parametrize(
    "command, context, node_before, values_after, writes, reboot",
    [
        (
            Message(0, 0, 1, 0, 0, "25.0"),  # command
            default_context(),  # context
            NODE_CHILD_SERIALIZED,  # node_before
            {
                0: "25.0"
            },  # values_after
            [],  # writes
            False,  # reboot
        ),  # Set message
        (
            Message(0, 0, 1, 0, 0, "25.0"),  # command
            default_context(),  # context
            NODE_CHILD_SERIALIZED,  # node_before
            {
                0: "25.0"
            },  # values_after
コード例 #6
0
pytestmark = pytest.mark.asyncio

HEARTBEAT_PAYLOAD = "1111"
PROTOCOL_VERSIONS_2x = list(PROTOCOL_VERSIONS)
PROTOCOL_VERSIONS_2x.remove("1.4")
PROTOCOL_VERSIONS_2x.remove("1.5")
PROTOCOL_VERSIONS_2x.remove("2.0")
PROTOCOL_VERSIONS_2x.remove("2.1")


@pytest.mark.parametrize("message_schema", PROTOCOL_VERSIONS_2x, indirect=True)
@pytest.mark.parametrize(
    "command, context, node_before, writes, heartbeat",
    [
        (
            Message(0, 255, 3, 0, 22, HEARTBEAT_PAYLOAD),  # command
            default_context(),  # context
            NODE_CHILD_SERIALIZED,  # node_before
            [],  # writes
            int(HEARTBEAT_PAYLOAD),  # heartbeat
        ),  # heartbeat
        (
            Message(0, 255, 3, 0, 22, HEARTBEAT_PAYLOAD),  # command
            pytest.raises(MissingNodeError),  # context
            None,  # node_before
            ["0;255;3;0;19;\n"],  # writes
            None,  # heartbeat
        ),  # missing node
    ],
    indirect=["command", "node_before"],
)
コード例 #7
0
def message_fixture(message_schema, transport):
    """Mock a message."""
    message = Message(0, 0, 1, 0, 0, "20.0")
    cmd = message_schema.dump(message)
    transport.messages.append(cmd)
    return message
コード例 #8
0
@pytest.fixture(name="mock_time")
def mock_time_fixture():
    """Mock time."""
    with patch("aiomysensors.model.protocol.protocol_14.time") as mock_time:
        mock_time.localtime.return_value = time.gmtime(123456789)
        yield mock_time


@pytest.mark.parametrize("message_schema",
                         list(PROTOCOL_VERSIONS),
                         indirect=True)
@pytest.mark.parametrize(
    "command, context, node_before, node_after",
    [
        (
            Message(0, 255, 0, 0, 17, "2.0"),  # command
            default_context(),  # context
            None,  # node_before
            NODE_SERIALIZED,  # node_after
        ),  # Node presentation
        (
            Message(0, 0, 0, 0, 6, "test child 0"),  # command
            default_context(),  # context
            NODE_SERIALIZED,  # node_before
            CHILD_PRESENTATION,  # node_after
        ),  # Child presentation
        (
            Message(0, 1, 0, 0, 0, "child 1"),  # command
            pytest.raises(MissingNodeError),  # context
            None,  # node_before
            None,  # node_after