def test_parser(connection):
    tree = NodeTree(connection, "DEV1234")

    tree.update_node(
        "demods/0/rate",
        {
            "GetParser": lambda value: value + 10,
            "SetParser": lambda value: value + 10,
        },
    )
    connection.getDouble.return_value = 0
    assert tree.demods[0].rate() == 10
    tree.demods[0].rate(0)
    connection.set.assert_called_with(tree.demods[0].rate.node_info.path, 10)
def test_get_cached(connection):
    tree = NodeTree(connection, "DEV1234")

    tree.demods[0].adcselect()
    connection.getInt.assert_called_with(
        tree.demods[0].adcselect.node_info.path)
    tree.demods[0].rate()
    connection.getDouble.assert_called_with(tree.demods[0].rate.node_info.path)
    tree.features.serial()
    connection.getString.assert_called_with(
        tree.features.serial.node_info.path)

    # Complex Double
    tree.update_node("demods/0/rate", {"Type": "Complex Double"})
    tree.demods[0].rate()
    connection.getComplex.assert_called_with(
        tree.demods[0].rate.node_info.path)
    connection.getComplex = None
    with pytest.raises(TypeError) as e_info:
        tree.demods[0].rate()

    # ZIVectorData
    with pytest.raises(TypeError) as e_info:
        tree.system.impedance.calib.user.data()

    # ZIDemodSample
    tree.demods[0].sample()
    connection.getSample.assert_called_with(
        tree.demods[0].sample.node_info.path)
    connection.getSample = None
    with pytest.raises(TypeError) as e_info:
        tree.demods[0].sample()

    # ZIDIOSample
    tree.dios[0].input()
    connection.getDIO.assert_called_with(tree.dios[0].input.node_info.path)

    # ZIAdvisorWave
    tree.update_node(tree.dios[0].input, {"Type": "ZIAdvisorWave"})
    with pytest.raises(StopIteration):
        tree.dios[0].input()
    connection.get.assert_called_with(tree.dios[0].input.node_info.path,
                                      flat=True)

    # invalid type
    tree.update_node(tree.dios[0].input, {"Type": "InvalidType"})
    with pytest.raises(RuntimeError):
        tree.dios[0].input()
def test_update_node(connection):
    tree = NodeTree(connection, "DEV1234")

    tree.update_node("demods/0/rate", {"Unit": "test"})
    assert tree.demods[0].rate.node_info.unit == "test"
    tree.update_node("demods/0/rate", {"Test": "test"})
    assert (next(iter(tree.get_node_info("demods/0/rate").values())).get(
        "Test", None) == "test")
    tree.update_node("demods/0/rate", {"Unit": "test2", "Test2": True})
    assert tree.demods[0].rate.node_info.unit == "test2"
    assert next(iter(tree.get_node_info("demods/0/rate").values())).get(
        "Test2", None)

    # new Node
    with pytest.raises(KeyError) as e_info:
        tree.update_node("/test", {"Node": "test"})
    assert e_info.value.args[0] == "/test"

    assert str(tree.test) == "/dev1234/test"
    tree.update_node("/test", {"Node": "test"}, add=True)
    tree.get_node_info(tree.test)
    assert tree.test.node_info["Node"] == "test"

    assert str(tree.demods[0].test) == "/dev1234/demods/0/test"
    tree.update_node("demods/0/test", {"Node": "test"}, add=True)
    assert str(tree.demods[0].test) == "test"

    tree.update_node("demods/0/rate", {"Properties": "Write"})
    with pytest.raises(Exception) as e_info:
        tree.demods[0].rate()
    tree.update_node("demods/0/rate", {"Properties": "Read"})
    with pytest.raises(Exception) as e_info:
        tree.demods[0].rate(12)

    with pytest.raises(RuntimeError) as e_info:
        tree.update_node("hello/*/world", {}, add=True)
def test_node(connection):
    tree = NodeTree(connection, "DEV1234")
    # test properties of node
    assert (repr(tree.demods[0].rate.node_info) ==
            'NodeInfo("/dev1234/demods/0/rate",leaf-node)')
    assert (str(tree.demods[0].rate.node_info) == """\
/dev1234/demods/0/rate
Defines the demodulator sampling rate, the number of samples that are sent to the host \
computer per second. A rate of about 7-10 higher as compared to the filter bandwidth \
usually provides sufficient aliasing suppression. This is also the rate of data \
received by LabOne Data Server and saved to the computer hard disk. This setting has \
no impact on the sample rate on the auxiliary outputs connectors. Note: the value \
inserted by the user may be approximated to the nearest value supported by the \
instrument.
Properties: Read, Write, Setting
Type: Double
Unit: 1/s""")

    assert "Options" in str(tree.demods[0].enable.node_info)
    assert "Partial node" in str(tree.demods[0].node_info)
    assert "Contains wildcards" in str(tree.demods["*"].node_info)

    assert "description" in dir(tree.demods[0].rate.node_info)

    assert tree.demods[0].rate.node_info.path
    assert tree.demods[0].rate.node_info.description
    assert tree.demods[0].rate.node_info.type
    assert tree.demods[0].rate.node_info.unit
    assert tree.demods[0].trigger.node_info.options.get(1)
    assert tree.demods[0].trigger.node_info.is_setting
    assert not tree.demods[0].trigger.node_info.is_vector
    assert not tree.system.impedance.calib.user.data.node_info.is_setting
    assert tree.system.impedance.calib.user.data.node_info.is_vector
    assert tree.system.node_info.is_setting is None
    assert tree.system.node_info.is_vector is None

    # dir of nested node
    assert "0" in dir(tree.demods)
    assert "rate" in dir(tree.demods[0])

    # contains
    assert "Description" in tree.demods[0].rate.node_info
    assert "Test" not in tree.demods[0].rate.node_info

    # parsers
    def add_one(value):
        return value + 1

    def sub_one(value):
        return value - 1

    tree.update_node(
        "/DEV1234/test/path/0",
        {
            "SetParser": add_one,
            "GetParser": sub_one,
        },
        add=True,
    )
    assert tree.test.path[0].node_info.get_parser(5) == 4
    assert tree.test.path[0].node_info.set_parser(5) == 6

    tree.update_node(
        "/DEV1234/test/path/0",
        {
            "SetParser": [add_one, add_one],
            "GetParser": [sub_one, sub_one]
        },
        add=True,
    )
    assert tree.test.path[0].node_info.get_parser(5) == 3
    assert tree.test.path[0].node_info.set_parser(5) == 7

    # dynamic node
    with pytest.raises(KeyError):
        tree.demods.waves[0].node_info.unit