From dcb9b86f90b4e58c2be0093675b7ce7bedc0451d Mon Sep 17 00:00:00 2001 From: Jan Willems Date: Wed, 11 Oct 2023 02:10:38 +0200 Subject: [PATCH] fix: Added float to Argument type. (#26) --- graphql_query/types.py | 20 ++++++++++++++++++++ tests/test_argument/test_argument.py | 11 +++++++++++ 2 files changed, 31 insertions(+) diff --git a/graphql_query/types.py b/graphql_query/types.py index cbb8069..5416df8 100644 --- a/graphql_query/types.py +++ b/graphql_query/types.py @@ -167,11 +167,13 @@ class Argument(_GraphQL2PythonQuery): str, int, bool, + float, 'Argument', Variable, List[str], List[int], List[bool], + List[float], List['Argument'], List[List['Argument']], ] @@ -188,6 +190,10 @@ def _check_is_list_of_int(values: List[Any]) -> TypeGuard[List[int]]: def _check_is_list_of_bool(values: List[Any]) -> TypeGuard[List[bool]]: return all(isinstance(value, bool) for value in values) + @staticmethod + def _check_is_list_of_float(values: List[Any]) -> TypeGuard[List[float]]: + return all(isinstance(value, float) for value in values) + @staticmethod def _check_is_list_of_arguments(values: List[Any]) -> TypeGuard[List['Argument']]: return all(isinstance(value, Argument) for value in values) @@ -204,6 +210,10 @@ def _render_for_str(name: str, value: str) -> str: def _render_for_int(name: str, value: int) -> str: return _template_key_value.render(name=name, value=str(value)) + @staticmethod + def _render_for_float(name: str, value: float) -> str: + return _template_key_value.render(name=name, value=str(value)) + @staticmethod def _render_for_list_str(name: str, value: List[str]) -> str: return _template_key_values.render(name=name, values=value) @@ -212,6 +222,10 @@ def _render_for_list_str(name: str, value: List[str]) -> str: def _render_for_list_bool(name: str, value: List[bool]) -> str: return _template_key_values.render(name=name, values=[str(v).lower() for v in value]) + @staticmethod + def _render_for_list_float(name: str, value: List[float]) -> str: + return _template_key_values.render(name=name, values=[str(v).lower() for v in value]) + @staticmethod def _render_for_variable(name: str, value: Variable) -> str: return _template_key_variable.render(name=name, value=value.name) @@ -242,6 +256,9 @@ def render(self) -> str: if isinstance(self.value, int): return self._render_for_int(self.name, self.value) + if isinstance(self.value, float): + return self._render_for_float(self.name, self.value) + if isinstance(self.value, Argument): return self._render_for_argument(self.name, self.value) @@ -255,6 +272,9 @@ def render(self) -> str: if self._check_is_list_of_bool(self.value): return self._render_for_list_bool(self.name, self.value) + if self._check_is_list_of_float(self.value): + return self._render_for_list_float(self.name, self.value) + if self._check_is_list_of_int(self.value): return self._render_for_list_str(self.name, [str(v) for v in self.value]) diff --git a/tests/test_argument/test_argument.py b/tests/test_argument/test_argument.py index 34b2b26..661f589 100644 --- a/tests/test_argument/test_argument.py +++ b/tests/test_argument/test_argument.py @@ -21,6 +21,11 @@ def test_value_is_bool(): assert Argument(name="some", value=False).render() == 'some: false' +def test_value_is_float(): + assert Argument(name="some", value=1.0).render() == 'some: 1.0' + assert Argument(name="some", value=0.2).render() == 'some: 0.2' + + def test_value_is_list_str(): assert Argument(name="someListArgument", value=['"123"']).render() == 'someListArgument: ["123"]' assert Argument(name="someListArgument", value=[]).render() == 'someListArgument: []' @@ -39,6 +44,11 @@ def test_value_is_list_bool(): assert Argument(name="someListArgument", value=[False]).render() == 'someListArgument: [false]' +def test_value_is_list_float(): + assert Argument(name="someListArgument", value=[1.0, 0.2]).render() == 'someListArgument: [1.0, 0.2]' + assert Argument(name="someListArgument", value=[42.0]).render() == 'someListArgument: [42.0]' + + @pytest.mark.parametrize( "name, value, result", [ @@ -46,6 +56,7 @@ def test_value_is_list_bool(): ("filter", Argument(name="field", value="VALUE"), "filter: {\n field: VALUE\n}"), ("filter", Argument(name="field", value='"value"'), 'filter: {\n field: "value"\n}'), ("filter", Argument(name="field", value=[123, -456]), "filter: {\n field: [123, -456]\n}"), + ("filter", Argument(name="field", value=[42.000, -21.000001]), "filter: {\n field: [42.0, -21.000001]\n}"), ( "filter", Argument(name="field", value=['"value1"', '"value2"']),