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')
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
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')
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')
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')
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')
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')
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')
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')
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')
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')
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')
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'
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'}])