Skip to content

accepted.tolerance() not applied when comparing values in nested dictionary #62

@teese

Description

@teese

I'm having trouble adjusting the tolerance when comparing nested dictionaries.
For example, the following pytest is green:

import pytest
from datatest import validate, accepted, ValidationError

def test_datatest():
    dict1 = {"x": {"a": 0.99, "b": 2.0}, "y": 3.0}
    dict2 = {"x": {"a": 1.0, "b": 1.99}, "y": 2.99}

    # validation of full dictionary is unsuccessful, despite a difference of 0.01 in all cases (test red)
    # tolerance is not applied when evaluating values in the nested dictionary with key 'x'
    with pytest.raises(ValidationError) as e:
        with accepted.tolerance(0.01):
            validate(dict1, dict2)

    exception_string = str(e)
    print(exception_string)
    assert "'x'" in exception_string
    assert "'y'" not in exception_string

    # validation of the inner dictionary is successful (test green)
    with accepted.tolerance(0.1):
        validate(dict1["x"], dict2["x"])

The ValidationError caught when evaluating validate(dict1, dict2) suggests that the accepted.tolerance is only applied when evaluating the values in a dictionary, but not the values of nested dictionaries. The exception caught by pytest is <ExceptionInfo ValidationError({'x': Invalid({'a': 0.99, 'b': 2.0}, expected={'a': 1.0, 'b': 1.99})}, 'does not satisfy mapping requirements') tblen=2>.
Is there a workaround, to allow the validation of nested dictionary values with a tolerance?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions