def test_invalid_field(): """Test that error is propagated from field parser to json parser. 1. Create json parser. 2. Try to parse a field from invalid data. 3. Check that error is raised. 4. Check that error is the same as one that field parser raises. """ invalid_field_data = object() parser = JSONParser() with pytest.raises(ValueError) as actual_error_info: parser.parse_field(invalid_field_data) with pytest.raises(ValueError) as expected_error_info: parser.create_field_parser(invalid_field_data).parse() assert actual_error_info.value.args[0] == expected_error_info.value.args[ 0], ("Wrong error is raised")
def test_fields(fields_data): """Test that fields are properly parsed. 1. Create an action parser. 2. Replace parse_field of the json parser so that it returns fake data. 3. Parse fields. 4. Check the parsed fields. """ json_parser = JSONParser() json_parser.parse_field = fields_data.index actual_fields = ActionParser(data={ "fields": fields_data }, parser=json_parser).parse_fields() assert actual_fields == tuple(range(len(fields_data))), "Wrong fields"