Skip to content

Commit 18f9a05

Browse files
[ASYNC] #2167 Add test_decfloat
1 parent d0341aa commit 18f9a05

File tree

1 file changed

+95
-0
lines changed

1 file changed

+95
-0
lines changed

test/integ/aio/test_decfloat_async.py

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
#!/usr/bin/env python
2+
#
3+
# Copyright (c) 2012-2023 Snowflake Computing Inc. All rights reserved.
4+
#
5+
6+
from __future__ import annotations
7+
8+
import decimal
9+
from decimal import Decimal
10+
11+
import numpy
12+
import pytest
13+
14+
import snowflake.connector
15+
16+
17+
@pytest.mark.skipolddriver
18+
async def test_decfloat_bindings(conn_cnx):
19+
# set required decimal precision
20+
decimal.getcontext().prec = 38
21+
original_style = snowflake.connector.paramstyle
22+
snowflake.connector.paramstyle = "qmark"
23+
try:
24+
async with conn_cnx() as cnx:
25+
cur = cnx.cursor()
26+
await cur.execute("select ?", [("DECFLOAT", Decimal("-1234e4000"))])
27+
ret = await cur.fetchone()
28+
assert isinstance(ret[0], Decimal)
29+
assert ret[0] == Decimal("-1234e4000")
30+
31+
await cur.execute("select ?", [("DECFLOAT", -1e3)])
32+
ret = await cur.fetchone()
33+
assert isinstance(ret[0], Decimal)
34+
assert ret[0] == Decimal("-1e3")
35+
36+
# test 38 digits
37+
await cur.execute(
38+
"select ?",
39+
[("DECFLOAT", Decimal("12345678901234567890123456789012345678"))],
40+
)
41+
ret = await cur.fetchone()
42+
assert isinstance(ret[0], Decimal)
43+
assert ret[0] == Decimal("12345678901234567890123456789012345678")
44+
45+
# test w/o explicit type specification
46+
await cur.execute("select ?", [-1e3])
47+
ret = await cur.fetchone()
48+
assert isinstance(ret[0], float)
49+
50+
await cur.execute("select ?", [Decimal("-1e3")])
51+
ret = await cur.fetchone()
52+
assert isinstance(ret[0], int)
53+
finally:
54+
snowflake.connector.paramstyle = original_style
55+
56+
57+
@pytest.mark.skipolddriver
58+
async def test_decfloat_from_compiler(conn_cnx):
59+
# set required decimal precision
60+
decimal.getcontext().prec = 38
61+
# test both result formats
62+
for fmt in ["json", "arrow"]:
63+
async with conn_cnx(
64+
session_parameters={
65+
"PYTHON_CONNECTOR_QUERY_RESULT_FORMAT": fmt,
66+
"use_cached_result": "false",
67+
}
68+
) as cnx:
69+
cur = cnx.cursor()
70+
# test endianess
71+
await cur.execute("SELECT 555::decfloat")
72+
ret = await cur.fetchone()
73+
assert isinstance(ret[0], Decimal)
74+
assert ret[0] == Decimal("555")
75+
76+
# test with decimal separator
77+
await cur.execute("SELECT 123456789.12345678::decfloat")
78+
ret = await cur.fetchone()
79+
assert isinstance(ret[0], Decimal)
80+
assert ret[0] == Decimal("123456789.12345678")
81+
82+
# test 38 digits
83+
await cur.execute(
84+
"SELECT '12345678901234567890123456789012345678'::decfloat"
85+
)
86+
ret = await cur.fetchone()
87+
assert isinstance(ret[0], Decimal)
88+
assert ret[0] == Decimal("12345678901234567890123456789012345678")
89+
90+
async with conn_cnx(numpy=True) as cnx:
91+
cur = cnx.cursor()
92+
await cur.execute("SELECT 1.234::decfloat", None)
93+
ret = await cur.fetchone()
94+
assert isinstance(ret[0], numpy.float64)
95+
assert ret[0] == numpy.float64("1.234")

0 commit comments

Comments
 (0)