def test_serializes_output_float():
    assert GraphQLFloat.serialize(1) == 1.0
    assert GraphQLFloat.serialize(0) == 0.0
    assert GraphQLFloat.serialize(-1) == -1.0
    assert GraphQLFloat.serialize(0.1) == 0.1
    assert GraphQLFloat.serialize(1.1) == 1.1
    assert GraphQLFloat.serialize(-1.1) == -1.1
    assert GraphQLFloat.serialize('-1.1') == -1.1
    assert GraphQLFloat.serialize('one') is None
    assert GraphQLFloat.serialize(False) == 0
    assert GraphQLFloat.serialize(True) == 1
Esempio n. 2
0
 def _parse_literal(s: str):
     return GraphQLFloat.parse_literal(parse_value_to_ast(s))
    def serializes_output_as_float():
        assert GraphQLFloat.serialize(1) == 1.0
        assert GraphQLFloat.serialize(0) == 0.0
        assert GraphQLFloat.serialize("123.5") == 123.5
        assert GraphQLFloat.serialize(-1) == -1.0
        assert GraphQLFloat.serialize(0.1) == 0.1
        assert GraphQLFloat.serialize(1.1) == 1.1
        assert GraphQLFloat.serialize(-1.1) == -1.1
        assert GraphQLFloat.serialize("-1.1") == -1.1
        assert GraphQLFloat.serialize(False) == 0
        assert GraphQLFloat.serialize(True) == 1

        with raises(GraphQLError) as exc_info:
            GraphQLFloat.serialize(nan)
        assert str(
            exc_info.value) == "Float cannot represent non numeric value: nan"
        with raises(GraphQLError) as exc_info:
            GraphQLFloat.serialize(inf)
        assert str(
            exc_info.value) == "Float cannot represent non numeric value: inf"
        with raises(GraphQLError) as exc_info:
            GraphQLFloat.serialize("one")
        assert str(exc_info.value) == (
            "Float cannot represent non numeric value: 'one'")
        with raises(GraphQLError) as exc_info:
            GraphQLFloat.serialize("")
        assert str(
            exc_info.value) == "Float cannot represent non numeric value: ''"
        with raises(GraphQLError) as exc_info:
            GraphQLFloat.serialize([5])
        assert str(
            exc_info.value) == "Float cannot represent non numeric value: [5]"
    def serializes_output_as_float():
        assert GraphQLFloat.serialize(1) == 1.0
        assert GraphQLFloat.serialize(0) == 0.0
        assert GraphQLFloat.serialize('123.5') == 123.5
        assert GraphQLFloat.serialize(-1) == -1.0
        assert GraphQLFloat.serialize(0.1) == 0.1
        assert GraphQLFloat.serialize(1.1) == 1.1
        assert GraphQLFloat.serialize(-1.1) == -1.1
        assert GraphQLFloat.serialize('-1.1') == -1.1
        assert GraphQLFloat.serialize(False) == 0
        assert GraphQLFloat.serialize(True) == 1

        with raises(Exception) as exc_info:
            GraphQLFloat.serialize(nan)
        assert str(exc_info.value) == (
            'Float cannot represent non numeric value: nan')
        with raises(Exception) as exc_info:
            GraphQLFloat.serialize(inf)
        assert str(exc_info.value) == (
            'Float cannot represent non numeric value: inf')
        with raises(Exception) as exc_info:
            GraphQLFloat.serialize('one')
        assert str(exc_info.value) == (
            "Float cannot represent non numeric value: 'one'")
        with raises(Exception) as exc_info:
            GraphQLFloat.serialize('')
        assert str(
            exc_info.value) == ("Float cannot represent non numeric value: ''")
        with raises(Exception) as exc_info:
            GraphQLFloat.serialize([5])
        assert str(exc_info.value) == (
            'Float cannot represent non numeric value: [5]')
def test_serializes_output_float():
    assert GraphQLFloat.serialize(1) == 1.0
    assert GraphQLFloat.serialize(0) == 0.0
    assert GraphQLFloat.serialize(-1) == -1.0
    assert GraphQLFloat.serialize(0.1) == 0.1
    assert GraphQLFloat.serialize(1.1) == 1.1
    assert GraphQLFloat.serialize(-1.1) == -1.1
    assert GraphQLFloat.serialize('-1.1') == -1.1
    with pytest.raises(Exception):
        GraphQLFloat.serialize('one')
    assert GraphQLFloat.serialize(False) == 0
    assert GraphQLFloat.serialize(True) == 1