Beispiel #1
0
def lark_to_int_value_node(tree: "Tree") -> "IntValueNode":
    """
    Creates and returns an IntValueNode 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: an IntValueNode instance extracted from the parsing of the tree
    :rtype: IntValueNode
    """
    return IntValueNode(value=tree.children[0].value,
                        location=lark_to_location_node(tree.meta))
Beispiel #2
0
def _parse_int_value(int_value_ast: dict) -> "IntValueNode":
    """
    Creates and returns an IntValueNode instance from an int value's JSON AST
    libgraphqlparser representation.
    :param int_value_ast: int value's JSON AST libgraphqlparser representation
    :type int_value_ast: dict
    :return: an IntValueNode instance equivalent to the JSON AST representation
    :rtype: IntValueNode
    """
    return IntValueNode(
        value=int(int_value_ast["value"]),
        location=_parse_location(int_value_ast["loc"]),
    )
        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
from tartiflette.language.ast import (
    BooleanValueNode,
    DirectiveDefinitionNode,
    IntValueNode,
    StringValueNode,
)

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
    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
    ],
)
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
Beispiel #7
0
        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
        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), UNDEFINED_VALUE),
        (IntValueNode(value=-(2**100)), UNDEFINED_VALUE),
        (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 Long().parse_literal(input_val) == output_val
Beispiel #9
0
)
def test_coerce_input_output(input_val, exception, output_val):
    scalar = Port()
    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),
        (IntValueNode(value=0), UNDEFINED_VALUE),
        (IntValueNode(value=80), 80),
        (IntValueNode(value=65535), 65535),
    ],
)
def test_parse_literal(input_val, output_val):
    assert Port().parse_literal(input_val) == output_val
def test_intvaluenode__init__():
    int_value_node = IntValueNode(value="intValueValue",
                                  location="intValueLocation")
    assert int_value_node.value == "intValueValue"
    assert int_value_node.location == "intValueLocation"
def test_floatvaluenode__repr__(float_value_node, expected):
    assert float_value_node.__repr__() == expected


def test_intvaluenode__init__():
    int_value_node = IntValueNode(value="intValueValue",
                                  location="intValueLocation")
    assert int_value_node.value == "intValueValue"
    assert int_value_node.location == "intValueLocation"


@pytest.mark.parametrize(
    "int_value_node,other,expected",
    [
        (
            IntValueNode(value="intValueValue", location="intValueLocation"),
            Ellipsis,
            False,
        ),
        (
            IntValueNode(value="intValueValue", location="intValueLocation"),
            IntValueNode(value="intValueValueBis",
                         location="intValueLocation"),
            False,
        ),
        (
            IntValueNode(value="intValueValue", location="intValueLocation"),
            IntValueNode(value="intValueValue",
                         location="intValueLocationBis"),
            False,
        ),