예제 #1
0
def test_recursive_update_with_recursion():
    """Test the 'recursive update' utility with recursion."""
    # here we try to update an integer and add a new value
    to_update = dict(subdict=dict(to_update=1))
    new_values = dict(subdict=dict(to_update=2))

    recursive_update(to_update, new_values)
    assert to_update == dict(subdict=dict(to_update=2))
예제 #2
0
def _override_ledger_configurations(agent_config: AgentConfig) -> None:
    """Override LedgerApis configurations with agent override configurations."""
    ledger_component_id = ComponentId(ComponentType.CONNECTION,
                                      LEDGER_CONNECTION)
    if ledger_component_id not in agent_config.component_configurations:
        return
    ledger_apis_config = agent_config.component_configurations[
        ledger_component_id]["config"].get("ledger_apis", {})
    recursive_update(LedgerApis.ledger_api_configs, ledger_apis_config)
예제 #3
0
def test_recursive_update_negative_unknown_field():
    """Test the 'recursive update' utility, when there are unknown fields."""
    # here we try to update an integer with a boolean - it raises error.
    to_update = dict(subdict=dict(field=1))
    new_values = dict(subdict=dict(new_field=False))

    with pytest.raises(
            ValueError,
            match=
            "Key 'new_field' is not contained in the dictionary to update.",
    ):
        recursive_update(to_update, new_values)
예제 #4
0
def test_recursive_update_negative_different_type():
    """Test the 'recursive update' utility, when the types are different."""
    # here we try to update an integer with a boolean - it raises error.
    to_update = dict(subdict=dict(to_update=1))
    new_values = dict(subdict=dict(to_update=False))

    with pytest.raises(
            ValueError,
            match=
            "Trying to replace value '1' with value 'False' which is of different type.",
    ):
        recursive_update(to_update, new_values)
예제 #5
0
def _override_ledger_configurations(agent_config: AgentConfig) -> None:
    """Override LedgerApis configurations with agent override configurations."""
    ledger_component_id = ComponentId(ComponentType.CONNECTION,
                                      PublicId.from_str(LEDGER_CONNECTION))
    prefix_to_component_configuration = {
        key.component_prefix: value
        for key, value in agent_config.component_configurations.items()
    }
    if ledger_component_id.component_prefix not in prefix_to_component_configuration:
        return
    ledger_apis_config = prefix_to_component_configuration[
        ledger_component_id.component_prefix]["config"].get("ledger_apis", {})
    recursive_update(LedgerApis.ledger_api_configs, ledger_apis_config)
예제 #6
0
def test_recursive_update_new_fields():
    """Test the 'recursive update' utility, with new fields."""
    # here we try to update an integer with a boolean - it raises error.
    to_update = dict(subdict=dict(to_update=1))
    new_values = dict(subdict=dict(to_update2=False))

    with pytest.raises(
            ValueError,
            match=
            "Key 'to_update2' is not contained in the dictionary to update.",
    ):
        recursive_update(to_update, new_values)
    assert "to_update2" not in to_update["subdict"]

    recursive_update(to_update, new_values, allow_new_values=True)
    assert "to_update2" in to_update["subdict"]
예제 #7
0
def test_recursive_update_no_recursion():
    """Test the 'recursive update' utility, in the case there's no recursion."""
    to_update = dict(not_updated=0,
                     an_integer=1,
                     a_list=[1, 2, 3],
                     a_tuple=(1, 2, 3))

    new_integer, new_list, new_tuple = 2, [3], (3, )
    new_values = dict(an_integer=new_integer,
                      a_list=new_list,
                      a_tuple=new_tuple)
    recursive_update(to_update, new_values)
    assert to_update == dict(not_updated=0,
                             an_integer=new_integer,
                             a_list=new_list,
                             a_tuple=new_tuple)