Skip to content

Commit

Permalink
fix: Added float to Argument type. (#26)
Browse files Browse the repository at this point in the history
  • Loading branch information
jw authored Oct 11, 2023
1 parent 23dbfcb commit dcb9b86
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
20 changes: 20 additions & 0 deletions graphql_query/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -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']],
]
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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)

Expand All @@ -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])

Expand Down
11 changes: 11 additions & 0 deletions tests/test_argument/test_argument.py
Original file line number Diff line number Diff line change
Expand Up @@ -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: []'
Expand All @@ -39,13 +44,19 @@ 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",
[
("filter", Argument(name="field", value=123), "filter: {\n field: 123\n}"),
("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"']),
Expand Down

0 comments on commit dcb9b86

Please sign in to comment.