|
| 1 | +from decimal import Decimal |
| 2 | +from typing import Any |
| 3 | + |
| 4 | +import pytest |
| 5 | +import strawberry |
| 6 | + |
| 7 | +from orchestrator.graphql.types import SCALAR_OVERRIDES |
| 8 | + |
| 9 | +BIG_INT_VALUE = 2**40 # 1,099,511,627,776 |
| 10 | +DECIMAL_VALUE = Decimal("1234567890.123456789") |
| 11 | + |
| 12 | + |
| 13 | +@strawberry.type |
| 14 | +class ScalarsTest: |
| 15 | + big_int: int |
| 16 | + decimal_value: Decimal |
| 17 | + |
| 18 | + |
| 19 | +@strawberry.type |
| 20 | +class Query: |
| 21 | + @strawberry.field |
| 22 | + def scalars(self) -> ScalarsTest: |
| 23 | + """Return test values for large int and decimal.""" |
| 24 | + return ScalarsTest(big_int=BIG_INT_VALUE, decimal_value=DECIMAL_VALUE) |
| 25 | + |
| 26 | + |
| 27 | +schema = strawberry.Schema(query=Query) |
| 28 | +schema_with_overrides = strawberry.Schema(query=Query, scalar_overrides=SCALAR_OVERRIDES) |
| 29 | + |
| 30 | + |
| 31 | +def test_big_int_and_decimal_handling() -> None: |
| 32 | + """Verify that large integers and Decimal values serialize correctly.""" |
| 33 | + query = """ |
| 34 | + query { |
| 35 | + scalars { |
| 36 | + bigInt |
| 37 | + decimalValue |
| 38 | + } |
| 39 | + } |
| 40 | + """ |
| 41 | + |
| 42 | + result: Any = schema_with_overrides.execute_sync(query) |
| 43 | + assert result.errors is None, f"GraphQL errors occurred: {result.errors}" |
| 44 | + |
| 45 | + data = result.data["scalars"] |
| 46 | + |
| 47 | + assert data["bigInt"] == BIG_INT_VALUE |
| 48 | + decimal_serialized = str(data["decimalValue"]) |
| 49 | + assert decimal_serialized.startswith(str(DECIMAL_VALUE)) |
| 50 | + |
| 51 | + |
| 52 | +@pytest.mark.xfail(reason="Graphql now supports non 32-bit signed integers", strict=False) |
| 53 | +def test_big_int_and_decimal_handling_fails_without_scalar_overrides() -> None: |
| 54 | + """Verify that large integers and Decimal values serialize correctly.""" |
| 55 | + query = """ |
| 56 | + query { |
| 57 | + scalars { |
| 58 | + bigInt |
| 59 | + decimalValue |
| 60 | + } |
| 61 | + } |
| 62 | + """ |
| 63 | + |
| 64 | + # with pytest.raises(GraphQLError, match="Int cannot represent non 32-bit signed integer"): |
| 65 | + result = schema.execute_sync(query) |
| 66 | + assert any("Int cannot represent non 32-bit signed integer" in err.message for err in result.errors or []) |
0 commit comments