Skip to content

Commit d388e92

Browse files
authored
support for simple str list ['123', '456'] and keep backward compatible (#39)
* support for simple str list ['123', '456'] and keep backward compatible * using isinstance * fixed formatting * fixed type for list of int * using triple ''' for the f string
1 parent 7a93a51 commit d388e92

File tree

2 files changed

+48
-22
lines changed

2 files changed

+48
-22
lines changed

graphql_query/types.py

+15-2
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,20 @@ def _render_for_float(name: str, value: float) -> str:
217217

218218
@staticmethod
219219
def _render_for_list_str(name: str, value: List[str]) -> str:
220-
return _template_key_values.render(name=name, values=value)
220+
clean_list = []
221+
for item in value:
222+
result = item.replace('"', '').split(',')
223+
if isinstance(result, list):
224+
trimmed_result = [i.strip() for i in result]
225+
clean_list.extend(trimmed_result)
226+
else:
227+
clean_list.append(result)
228+
229+
return _template_key_values.render(name=name, values=[f'''\"{v.replace('"', '')}\"''' for v in clean_list])
230+
231+
@staticmethod
232+
def _render_for_list_int(name: str, value: List[int]) -> str:
233+
return _template_key_values.render(name=name, values=[str(v) for v in value])
221234

222235
@staticmethod
223236
def _render_for_list_bool(name: str, value: List[bool]) -> str:
@@ -277,7 +290,7 @@ def render(self) -> str:
277290
return self._render_for_list_float(self.name, self.value)
278291

279292
if self._check_is_list_of_int(self.value):
280-
return self._render_for_list_str(self.name, [str(v) for v in self.value])
293+
return self._render_for_list_int(self.name, self.value)
281294

282295
if self._check_is_list_of_arguments(self.value):
283296
return self._render_for_list_argument(self.name, self.value)

tests/test_argument/test_argument.py

+33-20
Original file line numberDiff line numberDiff line change
@@ -6,47 +6,60 @@
66
def test_value_is_str():
77
assert Argument(name="arg1", value="VALUE1").render() == "arg1: VALUE1"
88
assert Argument(name="id", value='"1000"').render() == 'id: "1000"'
9-
assert Argument(name="length", value='911').render() == 'length: 911'
9+
assert Argument(name="length", value="911").render() == "length: 911"
1010
assert Argument(name="unit", value="null").render() == "unit: null"
1111

1212

1313
def test_value_is_int():
14-
assert Argument(name="length", value=911).render() == 'length: 911'
15-
assert Argument(name="length", value=0).render() == 'length: 0'
16-
assert Argument(name="length", value=-1).render() == 'length: -1'
14+
assert Argument(name="length", value=911).render() == "length: 911"
15+
assert Argument(name="length", value=0).render() == "length: 0"
16+
assert Argument(name="length", value=-1).render() == "length: -1"
1717

1818

1919
def test_value_is_bool():
20-
assert Argument(name="some", value=True).render() == 'some: true'
21-
assert Argument(name="some", value=False).render() == 'some: false'
20+
assert Argument(name="some", value=True).render() == "some: true"
21+
assert Argument(name="some", value=False).render() == "some: false"
2222

2323

2424
def test_value_is_float():
25-
assert Argument(name="some", value=1.0).render() == 'some: 1.0'
26-
assert Argument(name="some", value=0.2).render() == 'some: 0.2'
25+
assert Argument(name="some", value=1.0).render() == "some: 1.0"
26+
assert Argument(name="some", value=0.2).render() == "some: 0.2"
2727

2828

29-
def test_value_is_list_str():
29+
def test_value_is_list_str_with_backward_compatible_workarounds():
3030
assert Argument(name="someListArgument", value=['"123"']).render() == 'someListArgument: ["123"]'
31-
assert Argument(name="someListArgument", value=[]).render() == 'someListArgument: []'
31+
assert Argument(name="someListArgument", value=[]).render() == "someListArgument: []"
3232
assert Argument(name="someListArgument", value=['"123", "456"']).render() == 'someListArgument: ["123", "456"]'
3333

3434

35+
def test_value_is_list_str():
36+
assert Argument(name="someListArgument", value=["123", "456"]).render() == 'someListArgument: ["123", "456"]'
37+
assert (
38+
Argument(name="someListArgument", value=["hello", "world"]).render() == 'someListArgument: ["hello", "world"]'
39+
)
40+
41+
42+
def test_value_is_list_str_with_apostrophe():
43+
assert (
44+
Argument(name="someListArgument", value=["you'r", "isn't"]).render() == 'someListArgument: ["you\'r", "isn\'t"]'
45+
)
46+
47+
3548
def test_value_is_list_int():
36-
assert Argument(name="someListArgument", value=[123]).render() == 'someListArgument: [123]'
37-
assert Argument(name="someListArgument", value=[]).render() == 'someListArgument: []'
38-
assert Argument(name="someListArgument", value=[123, 456]).render() == 'someListArgument: [123, 456]'
49+
assert Argument(name="someListArgument", value=[123]).render() == "someListArgument: [123]"
50+
assert Argument(name="someListArgument", value=[]).render() == "someListArgument: []"
51+
assert Argument(name="someListArgument", value=[123, 456]).render() == "someListArgument: [123, 456]"
3952

4053

4154
def test_value_is_list_bool():
42-
assert Argument(name="someListArgument", value=[True, False]).render() == 'someListArgument: [true, false]'
43-
assert Argument(name="someListArgument", value=[True]).render() == 'someListArgument: [true]'
44-
assert Argument(name="someListArgument", value=[False]).render() == 'someListArgument: [false]'
55+
assert Argument(name="someListArgument", value=[True, False]).render() == "someListArgument: [true, false]"
56+
assert Argument(name="someListArgument", value=[True]).render() == "someListArgument: [true]"
57+
assert Argument(name="someListArgument", value=[False]).render() == "someListArgument: [false]"
4558

4659

4760
def test_value_is_list_float():
48-
assert Argument(name="someListArgument", value=[1.0, 0.2]).render() == 'someListArgument: [1.0, 0.2]'
49-
assert Argument(name="someListArgument", value=[42.0]).render() == 'someListArgument: [42.0]'
61+
assert Argument(name="someListArgument", value=[1.0, 0.2]).render() == "someListArgument: [1.0, 0.2]"
62+
assert Argument(name="someListArgument", value=[42.0]).render() == "someListArgument: [42.0]"
5063

5164

5265
@pytest.mark.parametrize(
@@ -62,7 +75,7 @@ def test_value_is_list_float():
6275
Argument(name="field", value=['"value1"', '"value2"']),
6376
'filter: {\n field: ["value1", "value2"]\n}',
6477
),
65-
("filter", Argument(name="field", value=Variable(name="var", type="Variable")), 'filter: {\n field: $var\n}'),
78+
("filter", Argument(name="field", value=Variable(name="var", type="Variable")), "filter: {\n field: $var\n}"),
6679
],
6780
)
6881
def test_value_is_argument(name: str, value: Argument, result: str):
@@ -73,7 +86,7 @@ def test_value_is_argument(name: str, value: Argument, result: str):
7386
def test_value_is_variable():
7487
var = Variable(name="var", type="Variable")
7588
arg = Argument(name="field", value=var)
76-
assert arg.render() == 'field: $var'
89+
assert arg.render() == "field: $var"
7790

7891

7992
def test_value_is_list_of_args():

0 commit comments

Comments
 (0)