def test_parse_form_and_query_params_with_errors(self):
        sig = inspect.signature(logic)
        query_params = {
            'age': 'not an int',
            'color': 'blue',
            'is_deleted': 'not a boolean',
        }
        with pytest.raises(TypeSystemError) as exc:
            parse_form_and_query_params(query_params, sig.parameters)

        assert {
            'age': 'value must be a valid type (integer)',
            'is_deleted': 'value must be a valid type (boolean)',
        } == exc.value.errors
 def test_parse_form_and_query_params_no_errors_with_custom_parser(self):
     sig = inspect.signature(logic2)
     query_params = {
         'foos': 'one,two,three',
     }
     actual = parse_form_and_query_params(query_params, sig.parameters)
     expected = {
         'foos': ['one', 'two', 'three'],
     }
     assert expected == actual
    def test_parse_form_and_query_params_with_union_type(self):
        """
        This test verifies if one of the annotations is a UnionType, that we
        correctly parse the value as it can be one of many types.
        """
        sig = inspect.signature(logic_with_union)
        query_params = {'colors_or_object': '["blue"]'}
        actual = parse_form_and_query_params(query_params, sig.parameters)
        assert {'colors_or_object': ['blue']} == actual

        query_params = {'colors_or_object': json.dumps({'str': 'auth'})}
        actual = parse_form_and_query_params(query_params, sig.parameters)
        expected = {
            'colors_or_object': {'str': 'auth'},
        }
        assert expected == actual

        # An invalid type that isn't one of the accepted types
        query_params = {'colors_or_object': 45}
        with pytest.raises(ValueError):
            parse_form_and_query_params(query_params, sig.parameters)
 def test_parse_form_and_query_params_no_errors(self):
     sig = inspect.signature(logic)
     query_params = {
         'age': '22',
         'color': 'blue',
         'is_deleted': 'true',
     }
     actual = parse_form_and_query_params(query_params, sig.parameters)
     assert {
         'age': 22,
         'color': 'blue',
         'is_deleted': True,
     } == actual
    def test_parse_form_and_query_params_with_custom_parser_not_callable(self):
        """
        This test verifies if a parser is provided that isn't callable that
        we warn the user and fallback to the default parser.
        """
        A = string('str', parser='foo')

        def f(a: A):
            pass

        sig = inspect.signature(f)
        query_params = {'a': 'a'}
        with pytest.warns(UserWarning, match='Parser `foo` is not callable'):
            actual = parse_form_and_query_params(query_params, sig.parameters)
        assert {'a': 'a'} == actual
    def test_parse_form_and_query_params_no_doctor_type_param_in_sig(self):
        """
        This is a regression test for when a logic function has a parameter
        in it's signature that is not annotated by a doctor type and the name
        of the parameter matches a parameter in the request variables.
        Previously this would cause an AttributeError.
        """
        def logic(age: Age, use_cache: bool = False):
            pass

        sig = inspect.signature(logic)
        params = {
            'age': '22',
            'use_cache': '1',
        }
        actual = parse_form_and_query_params(params, sig.parameters)
        expected = {'age': 22}
        assert expected == actual
    def test_parse_form_and_query_params_custom_parser_for_some_params(self):
        """
        This test just verifies that we can have types with custom parser mixed
        with those that use the default parsers.
        """
        def f(age: Age, items: FoosWithParser, color: Color):
            pass

        sig = inspect.signature(f)
        query_params = {
            'age': '22',
            'color': 'green',
            'items': 'item1,item2',
        }
        actual = parse_form_and_query_params(query_params, sig.parameters)
        expected = {
            'age': 22,
            'color': 'green',
            'items': ['item1', 'item2'],
        }
        assert expected == actual