def lark_to_string_value_node(tree: "Tree") -> "StringValueNode": """ Creates and returns a StringValueNode instance extracted from the parsing of the tree instance. :param tree: the Tree to parse in order to extract the proper node :type tree: Tree :return: a StringValueNode instance extracted from the parsing of the tree :rtype: StringValueNode """ return StringValueNode(value=tree.children[0].value, location=lark_to_location_node(tree.meta))
def _parse_string_value(string_value_ast: dict) -> "StringValueNode": """ Creates and returns a StringValueNode instance from a string value's JSON AST libgraphqlparser representation. :param string_value_ast: string value's JSON AST libgraphqlparser representation :type string_value_ast: dict :return: a StringValueNode instance equivalent to the JSON AST representation :rtype: StringValueNode """ return StringValueNode( value=string_value_ast["value"], location=_parse_location(string_value_ast["loc"]), )
@pytest.mark.parametrize( "input_val,output_val", [ (ipaddress.ip_address("127.0.0.1"), "127.0.0.1"), (ipaddress.ip_address("8.8.8.8"), "8.8.8.8"), ], ) def test_coerce_output(input_val, output_val): scalar = IPv4() assert scalar.coerce_output(input_val) == output_val @pytest.mark.parametrize( "input_val,output_val", [ ( DirectiveDefinitionNode( arguments=[], name="directive", locations=None), UNDEFINED_VALUE, ), (StringValueNode(value="nok"), UNDEFINED_VALUE), ( StringValueNode(value="127.0.0.1"), ipaddress.ip_address("127.0.0.1"), ), ], ) def test_parse_literal(input_val, output_val): assert IPv4().parse_literal(input_val) == output_val
], ) def test_coerce_output(input_val, output_val): scalar = NaiveDateTime() assert scalar.coerce_output(input_val) == output_val @pytest.mark.parametrize( "input_val,output_val", [ ( DirectiveDefinitionNode( arguments=[], name="directive", locations=None ), UNDEFINED_VALUE, ), (StringValueNode(value="nok"), UNDEFINED_VALUE), (IntValueNode(value=2 ** 128), UNDEFINED_VALUE), ( StringValueNode(value="2019-09-20T14:30:28+00:00"), datetime.datetime(2019, 9, 20, 14, 30, 28, tzinfo=tzutc()), ), ( IntValueNode(value=1568988000), datetime.datetime(2019, 9, 20, 14, 0, 0), ), ], ) def test_parse_literal(input_val, output_val): assert NaiveDateTime().parse_literal(input_val) == output_val
with pytest.raises(exception): scalar.coerce_input(input_val) else: assert scalar.coerce_input(input_val) == output_val @pytest.mark.parametrize( "input_val,output_val", [(5000, "$50.00"), (0, "$0.00")] ) def test_coerce_output(input_val, output_val): scalar = USCurrency() assert scalar.coerce_output(input_val) == output_val @pytest.mark.parametrize( "input_val,output_val", [ ( DirectiveDefinitionNode( arguments=[], name="directive", locations=None ), UNDEFINED_VALUE, ), (StringValueNode(value="nok"), UNDEFINED_VALUE), (IntValueNode(value=10), UNDEFINED_VALUE), (StringValueNode(value="$50.00"), 5000), ], ) def test_parse_literal(input_val, output_val): assert USCurrency().parse_literal(input_val) == output_val
"input_val,output_val", [ (False, "false"), ("ok", '"ok"'), ({"key": "value"}, '{"key": "value"}'), ([1, 2, 3], "[1, 2, 3]"), (1, "1"), (None, "null"), ], ) def test_coerce_output(input_val, output_val): scalar = JSON() assert scalar.coerce_output(input_val) == output_val @pytest.mark.parametrize( "input_val,output_val", [ ( DirectiveDefinitionNode( arguments=[], name="directive", locations=None ), UNDEFINED_VALUE, ), (StringValueNode(value='"ok"'), "ok"), (StringValueNode(value="true"), True), ], ) def test_parse_literal(input_val, output_val): assert JSON().parse_literal(input_val) == output_val
], ) def test_coerce_input_output(input_val, exception, output_val): scalar = MAC() if exception: with pytest.raises(exception): scalar.coerce_input(input_val) with pytest.raises(exception): scalar.coerce_output(input_val) else: assert scalar.coerce_output(input_val) == output_val assert scalar.coerce_input(input_val) == output_val @pytest.mark.parametrize( "input_val,output_val", [ ( DirectiveDefinitionNode( arguments=[], name="directive", locations=None ), UNDEFINED_VALUE, ), (StringValueNode(value="nok"), UNDEFINED_VALUE), (StringValueNode(value=None), UNDEFINED_VALUE), (StringValueNode(value="00:0a:95:9d:68:16"), "00:0a:95:9d:68:16"), ], ) def test_parse_literal(input_val, output_val): assert MAC().parse_literal(input_val) == output_val
from tartiflette_plugin_scalars.duration import Duration # parse literal @pytest.mark.parametrize( "input_value,output_value", [ (BooleanValueNode(value=True), UNDEFINED_VALUE), (IntValueNode(value=12345), UNDEFINED_VALUE), ( DirectiveDefinitionNode( arguments=[], name="directive", locations=[]), UNDEFINED_VALUE, ), ( StringValueNode(value="days=1, seconds=20"), timedelta(days=1, seconds=20), ), (StringValueNode(value="bad_value"), UNDEFINED_VALUE), ], ) def test_parse_literal(input_value, output_value): assert Duration().parse_literal(input_value) == output_value # coerce input @pytest.mark.parametrize( "input_value,exception,output_value", [ # BAD VALUES (False, TypeError, None),
) def test_coerce_input_output(input_val, exception, output_val): scalar = PhoneNumber() if exception: with pytest.raises(exception): scalar.coerce_input(input_val) with pytest.raises(exception): scalar.coerce_output(input_val) else: assert scalar.coerce_output(input_val) == output_val assert scalar.coerce_input(input_val) == output_val @pytest.mark.parametrize( "input_val,output_val", [ ( DirectiveDefinitionNode( arguments=[], name="directive", locations=None ), UNDEFINED_VALUE, ), (StringValueNode(value="nok"), UNDEFINED_VALUE), (StringValueNode(value=None), UNDEFINED_VALUE), (StringValueNode(value="+33177351100"), "+33177351100"), (StringValueNode(value="+16502530001"), "+16502530001"), ], ) def test_parse_literal_email_address(input_val, output_val): assert PhoneNumber().parse_literal(input_val) == output_val
scalar.coerce_input(input_val) with pytest.raises(exception): scalar.coerce_output(input_val) else: assert scalar.coerce_output(input_val) == output_val assert scalar.coerce_input(input_val) == output_val @pytest.mark.parametrize( "input_val,output_val", [ ( DirectiveDefinitionNode( arguments=[], name="directive", locations=None), UNDEFINED_VALUE, ), (StringValueNode(value="nok"), UNDEFINED_VALUE), (StringValueNode(value=None), UNDEFINED_VALUE), ( StringValueNode(value="rgba(51, 170, 51, .1)"), "rgba(51, 170, 51, .1)", ), ( StringValueNode(value="rgba(51, 170, 51, 1)"), "rgba(51, 170, 51, 1)", ), ], ) def test_parse_literal(input_val, output_val): assert RGBA().parse_literal(input_val) == output_val
if exception: with pytest.raises(exception): scalar.coerce_output(input_val) else: assert scalar.coerce_output(input_val) == output_val @pytest.mark.parametrize( "input_val,output_val", [ ( DirectiveDefinitionNode( arguments=[], name="directive", locations=None), UNDEFINED_VALUE, ), (StringValueNode(value="nok"), UNDEFINED_VALUE), ( StringValueNode(value="https://www.dailymotion.com/play"), ParseResult( scheme="https", netloc="www.dailymotion.com", path="/play", params="", query="", fragment="", ), ), ], ) def test_parse_literal_email_address(input_val, output_val): assert URL().parse_literal(input_val) == output_val
) def test_coerce_input_output(input_val, exception, output_val): scalar = HexColorCode() if exception: with pytest.raises(exception): scalar.coerce_input(input_val) with pytest.raises(exception): scalar.coerce_output(input_val) else: assert scalar.coerce_output(input_val) == output_val assert scalar.coerce_input(input_val) == output_val @pytest.mark.parametrize( "input_val,output_val", [ ( DirectiveDefinitionNode( arguments=[], name="directive", locations=None ), UNDEFINED_VALUE, ), (StringValueNode(value="nok"), UNDEFINED_VALUE), (StringValueNode(value=None), UNDEFINED_VALUE), (StringValueNode(value="#0099CC"), "#0099CC"), (StringValueNode(value="#ffffff"), "#ffffff"), ], ) def test_parse_literal(input_val, output_val): assert HexColorCode().parse_literal(input_val) == output_val
def test_stringvaluenode__init__(): string_value_node = StringValueNode(value="stringValueValue", location="stringValueLocation") assert string_value_node.value == "stringValueValue" assert string_value_node.location == "stringValueLocation"
def test_nullvaluenode__repr__(null_value_node, expected): assert null_value_node.__repr__() == expected def test_stringvaluenode__init__(): string_value_node = StringValueNode(value="stringValueValue", location="stringValueLocation") assert string_value_node.value == "stringValueValue" assert string_value_node.location == "stringValueLocation" @pytest.mark.parametrize( "string_value_node,other,expected", [ ( StringValueNode(value="stringValueValue", location="stringValueLocation"), Ellipsis, False, ), ( StringValueNode(value="stringValueValue", location="stringValueLocation"), StringValueNode(value="stringValueValueBis", location="stringValueLocation"), False, ), ( StringValueNode(value="stringValueValue", location="stringValueLocation"), StringValueNode(value="stringValueValue", location="stringValueLocationBis"),
], ) def test_coerce_input_output(input_val, exception, output_val): scalar = PostalCode() if exception: with pytest.raises(exception): scalar.coerce_input(input_val) with pytest.raises(exception): scalar.coerce_output(input_val) else: assert scalar.coerce_output(input_val) == output_val assert scalar.coerce_input(input_val) == output_val @pytest.mark.parametrize( "input_val,output_val", [ ( DirectiveDefinitionNode( arguments=[], name="directive", locations=None), UNDEFINED_VALUE, ), (StringValueNode(value="nok"), UNDEFINED_VALUE), (StringValueNode(value=None), UNDEFINED_VALUE), (StringValueNode(value="75017"), "75017"), (StringValueNode(value="K1N 9N1"), "K1N 9N1"), ], ) def test_parse_literal_email_address(input_val, output_val): assert PostalCode().parse_literal(input_val) == output_val
scalar.coerce_input(input_val) with pytest.raises(exception): scalar.coerce_output(input_val) else: assert scalar.coerce_output(input_val) == output_val assert scalar.coerce_input(input_val) == output_val @pytest.mark.parametrize( "input_val,output_val", [ ( DirectiveDefinitionNode( arguments=[], name="directive", locations=None), UNDEFINED_VALUE, ), (StringValueNode(value="nok"), UNDEFINED_VALUE), (StringValueNode(value=None), UNDEFINED_VALUE), ( StringValueNode(value="*****@*****.**"), "*****@*****.**", ), ( StringValueNode(value="*****@*****.**"), "*****@*****.**", ), ], ) def test_parse_literal_email_address(input_val, output_val): assert EmailAddress().parse_literal(input_val) == output_val
], ) def test_coerce_input_output(input_val, exception, output_val): scalar = RGB() if exception: with pytest.raises(exception): scalar.coerce_input(input_val) with pytest.raises(exception): scalar.coerce_output(input_val) else: assert scalar.coerce_output(input_val) == output_val assert scalar.coerce_input(input_val) == output_val @pytest.mark.parametrize( "input_val,output_val", [ ( DirectiveDefinitionNode( arguments=[], name="directive", locations=None), UNDEFINED_VALUE, ), (StringValueNode(value="nok"), UNDEFINED_VALUE), (StringValueNode(value=None), UNDEFINED_VALUE), (StringValueNode(value="rgb(255,0,153)"), "rgb(255,0,153)"), (StringValueNode(value="rgb(100%, 0%, 60%)"), "rgb(100%, 0%, 60%)"), ], ) def test_parse_literal(input_val, output_val): assert RGB().parse_literal(input_val) == output_val
scalar.coerce_input(input_val) with pytest.raises(exception): scalar.coerce_output(input_val) else: assert scalar.coerce_output(input_val) == output_val assert scalar.coerce_input(input_val) == output_val @pytest.mark.parametrize( "input_val,output_val", [ ( DirectiveDefinitionNode( arguments=[], name="directive", locations=None), UNDEFINED_VALUE, ), (StringValueNode(value="nok"), UNDEFINED_VALUE), (StringValueNode(value=None), UNDEFINED_VALUE), ( StringValueNode(value="5591b4e9-c747-45ae-8abf-d9cd1a17081e"), "5591b4e9-c747-45ae-8abf-d9cd1a17081e", ), ( StringValueNode(value="74af0909-3a1a-4c6b-b3d3-6e76c10348e5"), "74af0909-3a1a-4c6b-b3d3-6e76c10348e5", ), ], ) def test_parse_literal(input_val, output_val): assert GUID().parse_literal(input_val) == output_val
if exception: with pytest.raises(exception): scalar.coerce_input(input_val) with pytest.raises(exception): scalar.coerce_output(input_val) else: assert scalar.coerce_output(input_val) == output_val assert scalar.coerce_input(input_val) == output_val @pytest.mark.parametrize( "input_val,output_val", [ ( DirectiveDefinitionNode( arguments=[], name="directive", locations=None), UNDEFINED_VALUE, ), (StringValueNode(value="nok"), UNDEFINED_VALUE), (StringValueNode(value=None), UNDEFINED_VALUE), (StringValueNode(value="-12.3"), UNDEFINED_VALUE), (IntValueNode(value=-15), UNDEFINED_VALUE), (IntValueNode(value=0), 0), (FloatValueNode(value=16.0), 16), (IntValueNode(value=15), 15), (StringValueNode(value="15"), 15), ], ) def test_parse_literal(input_val, output_val): assert NonNegativeInt().parse_literal(input_val) == output_val
scalar.coerce_input(input_val) with pytest.raises(exception): scalar.coerce_output(input_val) else: assert scalar.coerce_output(input_val) == output_val assert scalar.coerce_input(input_val) == output_val @pytest.mark.parametrize( "input_val,output_val", [ ( DirectiveDefinitionNode( arguments=[], name="directive", locations=None), UNDEFINED_VALUE, ), (StringValueNode(value="nok"), UNDEFINED_VALUE), (StringValueNode(value=None), UNDEFINED_VALUE), ( StringValueNode(value="hsla(270, 60%, 50%, .05)"), "hsla(270, 60%, 50%, .05)", ), ( StringValueNode(value="hsla(4.71239rad, 60%, 70%, 1)"), "hsla(4.71239rad, 60%, 70%, 1)", ), ], ) def test_parse_literal(input_val, output_val): assert HSLA().parse_literal(input_val) == output_val
with pytest.raises(exception): scalar.coerce_input(input_val) with pytest.raises(exception): scalar.coerce_output(input_val) else: assert scalar.coerce_output(input_val) == output_val assert scalar.coerce_input(input_val) == output_val @pytest.mark.parametrize( "input_val,output_val", [ ( DirectiveDefinitionNode( arguments=[], name="directive", locations=None), UNDEFINED_VALUE, ), (StringValueNode(value="nok"), UNDEFINED_VALUE), (StringValueNode(value=None), UNDEFINED_VALUE), (IntValueNode(value=2**100), 2**100), (IntValueNode(value=-(2**100)), -(2**100)), (IntValueNode(value=0), 0), (IntValueNode(value=-15), -15), (FloatValueNode(value=16.0), 16), (StringValueNode(value="12"), 12), (IntValueNode(value=15), 15), ], ) def test_parse_literal(input_val, output_val): assert BigInt().parse_literal(input_val) == output_val
(1, TypeError, "1"), (None, TypeError, "null"), ({"key": "value"}, None, '{"key": "value"}'), ], ) def test_coerce_output(input_val, exception, output_val): scalar = JSONObject() if exception: with pytest.raises(exception): scalar.coerce_output(input_val) else: assert scalar.coerce_output(input_val) == output_val @pytest.mark.parametrize( "input_val,output_val", [ ( DirectiveDefinitionNode( arguments=[], name="directive", locations=None ), UNDEFINED_VALUE, ), (StringValueNode(value='"ok"'), UNDEFINED_VALUE), (StringValueNode(value="true"), UNDEFINED_VALUE), (StringValueNode(value='{"key": "value"}'), {"key": "value"}), ], ) def test_parse_literal(input_val, output_val): assert JSONObject().parse_literal(input_val) == output_val
@pytest.mark.parametrize( "input_val,output_val", [ ( ipaddress.ip_address("2001:0db8:0000:0000:0000:8a2e:0370:7334"), "2001:db8::8a2e:370:7334", ), (ipaddress.ip_address("::1"), "::1"), ], ) def test_coerce_output(input_val, output_val): scalar = IPv6() assert scalar.coerce_output(input_val) == output_val @pytest.mark.parametrize( "input_val,output_val", [ ( DirectiveDefinitionNode( arguments=[], name="directive", locations=None ), UNDEFINED_VALUE, ), (StringValueNode(value="nok"), UNDEFINED_VALUE), (StringValueNode(value="::1"), ipaddress.ip_address("::1")), ], ) def test_parse_literal(input_val, output_val): assert IPv6().parse_literal(input_val) == output_val
], ) def test_coerce_input_output(input_val, exception, output_val): scalar = ISBN() if exception: with pytest.raises(exception): scalar.coerce_input(input_val) with pytest.raises(exception): scalar.coerce_output(input_val) else: assert scalar.coerce_output(input_val) == output_val assert scalar.coerce_input(input_val) == output_val @pytest.mark.parametrize( "input_val,output_val", [ ( DirectiveDefinitionNode( arguments=[], name="directive", locations=None), UNDEFINED_VALUE, ), (StringValueNode(value="nok"), UNDEFINED_VALUE), (StringValueNode(value=None), UNDEFINED_VALUE), (StringValueNode(value="ISBN 0-06-059518-3"), "ISBN 0-06-059518-3"), (StringValueNode(value="ISBN 0553078143"), "ISBN 0553078143"), ], ) def test_parse_literal_email_address(input_val, output_val): assert ISBN().parse_literal(input_val) == output_val