File: test_decimal.py

package info (click to toggle)
python-graphene 2.1.9-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 1,024 kB
  • sloc: python: 7,295; makefile: 196; sh: 4
file content (43 lines) | stat: -rw-r--r-- 1,228 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import decimal

from ..decimal import Decimal
from ..objecttype import ObjectType
from ..schema import Schema


class Query(ObjectType):
    decimal = Decimal(input=Decimal())

    def resolve_decimal(self, info, input):
        return input


schema = Schema(query=Query)


def test_decimal_string_query():
    decimal_value = decimal.Decimal("1969.1974")
    result = schema.execute("""{ decimal(input: "%s") }""" % decimal_value)
    assert not result.errors
    assert result.data == {"decimal": str(decimal_value)}
    assert decimal.Decimal(result.data["decimal"]) == decimal_value


def test_decimal_string_query_variable():
    decimal_value = decimal.Decimal("1969.1974")

    result = schema.execute(
        """query Test($decimal: Decimal){ decimal(input: $decimal) }""",
        variables={"decimal": decimal_value},
    )
    assert not result.errors
    assert result.data == {"decimal": str(decimal_value)}
    assert decimal.Decimal(result.data["decimal"]) == decimal_value


def test_bad_decimal_query():
    not_a_decimal = "Nobody expects the Spanish Inquisition!"

    result = schema.execute("""{ decimal(input: "%s") }""" % not_a_decimal)
    assert len(result.errors) == 1
    assert result.data is None