Esempio n. 1
0
 def _parse_literal(s: str):
     return GraphQLInt.parse_literal(parse_value_to_ast(s))
def test_serializes_output_int():
    assert GraphQLInt.serialize(1) == 1
    assert GraphQLInt.serialize(0) == 0
    assert GraphQLInt.serialize(-1) == -1
    assert GraphQLInt.serialize(0.1) == 0
    assert GraphQLInt.serialize(1.1) == 1
    assert GraphQLInt.serialize(-1.1) == -1
    assert GraphQLInt.serialize(1e5) == 100000
    assert GraphQLInt.serialize(9876504321) is None
    assert GraphQLInt.serialize(-9876504321) is None
    assert GraphQLInt.serialize(1e100) is None
    assert GraphQLInt.serialize(-1e100) is None
    assert GraphQLInt.serialize('-1.1') == -1
    assert GraphQLInt.serialize('one') is None
    assert GraphQLInt.serialize(False) == 0
    assert GraphQLInt.serialize(True) == 1
    def serializes_output_as_int():
        assert GraphQLInt.serialize(1) == 1
        assert GraphQLInt.serialize("123") == 123
        assert GraphQLInt.serialize(0) == 0
        assert GraphQLInt.serialize(-1) == -1
        assert GraphQLInt.serialize(1e5) == 100000
        assert GraphQLInt.serialize(False) == 0
        assert GraphQLInt.serialize(True) == 1

        # The GraphQL specification does not allow serializing non-integer
        # values as Int to avoid accidental data loss.
        with raises(GraphQLError) as exc_info:
            GraphQLInt.serialize(0.1)
        assert str(
            exc_info.value) == "Int cannot represent non-integer value: 0.1"
        with raises(GraphQLError) as exc_info:
            GraphQLInt.serialize(1.1)
        assert str(
            exc_info.value) == "Int cannot represent non-integer value: 1.1"
        with raises(GraphQLError) as exc_info:
            GraphQLInt.serialize(-1.1)
        assert str(
            exc_info.value) == "Int cannot represent non-integer value: -1.1"
        with raises(GraphQLError) as exc_info:
            GraphQLInt.serialize("-1.1")
        assert str(
            exc_info.value) == "Int cannot represent non-integer value: '-1.1'"
        # Maybe a safe JavaScript int, but bigger than 2^32, so not
        # representable as a GraphQL Int
        with raises(GraphQLError) as exc_info:
            GraphQLInt.serialize(9876504321)
        assert str(exc_info.value) == (
            "Int cannot represent non 32-bit signed integer value: 9876504321")
        with raises(GraphQLError) as exc_info:
            GraphQLInt.serialize(-9876504321)
        assert str(exc_info.value) == (
            "Int cannot represent non 32-bit signed integer value: -9876504321"
        )
        # Too big to represent as an Int in JavaScript or GraphQL
        with raises(GraphQLError) as exc_info:
            GraphQLInt.serialize(1e100)
        assert str(exc_info.value) == (
            "Int cannot represent non 32-bit signed integer value: 1e+100")
        with raises(GraphQLError) as exc_info:
            GraphQLInt.serialize(-1e100)
        assert str(exc_info.value) == (
            "Int cannot represent non 32-bit signed integer value: -1e+100")
        with raises(GraphQLError) as exc_info:
            GraphQLInt.serialize("one")
        assert str(
            exc_info.value) == "Int cannot represent non-integer value: 'one'"
        # Doesn't represent number
        with raises(GraphQLError) as exc_info:
            GraphQLInt.serialize("")
        assert str(
            exc_info.value) == "Int cannot represent non-integer value: ''"
        with raises(GraphQLError) as exc_info:
            GraphQLInt.serialize(nan)
        assert str(
            exc_info.value) == "Int cannot represent non-integer value: nan"
        with raises(GraphQLError) as exc_info:
            GraphQLInt.serialize(inf)
        assert str(
            exc_info.value) == "Int cannot represent non-integer value: inf"
        with raises(GraphQLError) as exc_info:
            GraphQLInt.serialize([5])
        assert str(
            exc_info.value) == "Int cannot represent non-integer value: [5]"
def test_serializes_output_int():
    assert GraphQLInt.serialize(1) == 1
    assert GraphQLInt.serialize(0) == 0
    assert GraphQLInt.serialize(-1) == -1
    assert GraphQLInt.serialize(0.1) == 0
    assert GraphQLInt.serialize(1.1) == 1
    assert GraphQLInt.serialize(-1.1) == -1
    assert GraphQLInt.serialize(1e5) == 100000
    with pytest.raises(Exception):
        GraphQLInt.serialize(9876504321)
    with pytest.raises(Exception):
        GraphQLInt.serialize(-9876504321)
    with pytest.raises(Exception):
        GraphQLInt.serialize(1e100)
    with pytest.raises(Exception):
        GraphQLInt.serialize(-1e100)
    assert GraphQLInt.serialize('-1.1') == -1
    with pytest.raises(Exception):
        GraphQLInt.serialize('one')
    assert GraphQLInt.serialize(False) == 0
    assert GraphQLInt.serialize(True) == 1