예제 #1
0
def test_if_chained():
    """Test that two if (is) statement works."""
    test = [
        'target_value',
        [
            IfStatement(**{
                'condition': 'is',
                'target': 'target_value',
                'then': 'value2',
            }),
            IfStatement(**{
                'condition': 'is',
                'target': 'value2',
                'then': 'value3',
            }),
        ],
    ]
    assert apply_if_statements(*test) == Success('value3')
예제 #2
0
def test_validates():  # noqa: WPS218
    """Test that dict is marshalled to pydantic object."""
    test = IfStatement(
        condition='is',
        target=None,
        then='then',
    )
    assert test.condition == 'is'
    assert test.target is None
    assert test.then == 'then'
    assert test.otherwise is None
예제 #3
0
def test_if_in_condition_array_value():
    """Test that we can do if in statement on arrays."""
    test = [
        ['target_value'],
        [
            IfStatement(**{
                'condition': 'in',
                'target': [['target_value']],
                'then': 'value2',
            }),
        ],
    ]
    assert apply_if_statements(*test) == Success('value2')
예제 #4
0
def test_if_in_condition_false():
    """Test if in condition False."""
    test = [
        'not_target_value',
        [
            IfStatement(**{
                'condition': 'in',
                'target': 'target_value',
                'then': 'value2',
            }),
        ],
    ]
    assert apply_if_statements(*test) == Success('not_target_value')
예제 #5
0
def test_if_in():
    """Test that 1 if (in) statement works."""
    test = [
        'target_value',
        [
            IfStatement(**{
                'condition': 'in',
                'target': ['target_value'],
                'then': 'value2',
            }),
        ],
    ]
    assert apply_if_statements(*test) == Success('value2')
예제 #6
0
def test_if_is_condition_objects_value():
    """Test that we can do if is statement on objectss."""
    test = [
        {'val': 'target'},
        [
            IfStatement(**{
                'condition': 'is',
                'target': {'val': 'target'},
                'then': 'value2',
            }),
        ],
    ]
    assert apply_if_statements(*test) == Success('value2')
예제 #7
0
def test_if_contains_works_with_non_strings():
    """Test that we can do if contains statement on objectss."""
    test = [
        123,
        [
            IfStatement(**{
                'condition': 'contains',
                'target': 123,
                'then': 'value2',
            }),
        ],
    ]
    assert apply_if_statements(*test) == Success('value2')
예제 #8
0
def test_if_contains_objects_in_array_value():
    """Test that we can do if contains statement on objectss."""
    test = [
        [{'val': 'target'}],
        [
            IfStatement(**{
                'condition': 'contains',
                'target': {'val': 'target'},
                'then': 'value2',
            }),
        ],
    ]
    assert apply_if_statements(*test) == Success('value2')
예제 #9
0
def test_if_contains_condition_array_value():
    """Test that we can do if contains statement on arrays."""
    test = [
        ['value', 'target'],
        [
            IfStatement(**{
                'condition': 'contains',
                'target': 'target',
                'then': 'value2',
            }),
        ],
    ]
    assert apply_if_statements(*test) == Success('value2')
예제 #10
0
def test_if_contains():
    """Test that 1 if (contains) statement works."""
    test = [
        'target_value',
        [
            IfStatement(**{
                'condition': 'contains',
                'target': '_value',
                'then': 'value2',
            }),
        ],
    ]
    assert apply_if_statements(*test) == Success('value2')
예제 #11
0
def test_if_not_condition_array_value_with_int():
    """Test that we can do if not statement on arrays."""
    test = [
        [123],
        [
            IfStatement(**{
                'condition': 'is',
                'target': [123],
                'then': 'value2',
            }),
        ],
    ]
    assert apply_if_statements(*test) == Success('value2')
예제 #12
0
def test_if_failed_condition_goes_to_otherwise():
    """Test that we get the then value when condition fails."""
    test = [
        'not_target_value',
        [
            IfStatement(**{
                'condition': 'is',
                'target': 'target_value',
                'then': 'no',
                'otherwise': 'yes',
            }),
        ],
    ]
    assert apply_if_statements(*test) == Success('yes')
예제 #13
0
def test_invalid_bad_condition_enum():  # noqa: WPS218
    """Test that we get validation error with correct message."""
    with pytest.raises(ValidationError) as ve:
        IfStatement(  # type: ignore
            condition='bad',
            otherwise='otherwise',
        )

    condition = ve.value.errors()[0]  # noqa: WPS441
    assert condition['loc'] == ('condition', )
    assert 'not a valid enumeration member' in condition['msg']

    target = ve.value.errors()[1]  # noqa: WPS441
    assert target['loc'] == ('target', )
    assert target['msg'] == 'field required'

    then = ve.value.errors()[2]  # noqa: WPS441
    assert then['loc'] == ('then', )
    assert then['msg'] == 'field required'
예제 #14
0
def test_if_contains_array_does_not_stringify():
    """Test that we can do if contains statement on array[objects].

    However its very important that list and object tests does not do
    string casting for check since it would give the check two possible
    ways to do the check and there should only be one.
    for objects the 'in' checks if the key exist
    for arrays the 'in' checks if the element exist inside the aray
    for everything else we will stringify the test value.
    """
    test = [
        [{'val': 'target'}],
        [
            IfStatement(**{
                'condition': 'contains',
                'target': 'target',
                'then': 'value2',
            }),
        ],
    ]
    assert apply_if_statements(*test) == Success([{'val': 'target'}])