@@ -15,23 +15,31 @@ def mock_connection():
15
15
conn .disable_pandas = False
16
16
conn ._arrow_pandas_type_override = {}
17
17
conn ._arrow_to_pandas_kwargs = {}
18
- if not hasattr (conn , ' _arrow_to_pandas_kwargs' ):
18
+ if not hasattr (conn , " _arrow_to_pandas_kwargs" ):
19
19
conn ._arrow_to_pandas_kwargs = {}
20
20
return conn
21
21
22
+
22
23
@pytest .fixture
23
24
def mock_thrift_backend (sample_arrow_table ):
24
25
tb = MagicMock ()
25
- empty_arrays = [pyarrow .array ([], type = field .type ) for field in sample_arrow_table .schema ]
26
- empty_table = pyarrow .Table .from_arrays (empty_arrays , schema = sample_arrow_table .schema )
27
- tb .fetch_results .return_value = (ArrowQueue (empty_table , 0 ) , False )
26
+ empty_arrays = [
27
+ pyarrow .array ([], type = field .type ) for field in sample_arrow_table .schema
28
+ ]
29
+ empty_table = pyarrow .Table .from_arrays (
30
+ empty_arrays , schema = sample_arrow_table .schema
31
+ )
32
+ tb .fetch_results .return_value = (ArrowQueue (empty_table , 0 ), False )
28
33
return tb
29
34
35
+
30
36
@pytest .fixture
31
37
def mock_raw_execute_response ():
32
38
er = MagicMock (spec = ExecuteResponse )
33
- er .description = [("col_int" , "int" , None , None , None , None , None ),
34
- ("col_str" , "string" , None , None , None , None , None )]
39
+ er .description = [
40
+ ("col_int" , "int" , None , None , None , None , None ),
41
+ ("col_str" , "string" , None , None , None , None , None ),
42
+ ]
35
43
er .arrow_schema_bytes = None
36
44
er .arrow_queue = None
37
45
er .has_more_rows = False
@@ -42,27 +50,33 @@ def mock_raw_execute_response():
42
50
er .is_staging_operation = False
43
51
return er
44
52
53
+
45
54
@pytest .fixture
46
55
def sample_arrow_table ():
47
56
data = [
48
57
pyarrow .array ([1 , 2 , 3 ], type = pyarrow .int32 ()),
49
- pyarrow .array (["a" , "b" , "c" ], type = pyarrow .string ())
58
+ pyarrow .array (["a" , "b" , "c" ], type = pyarrow .string ()),
50
59
]
51
- schema = pyarrow .schema ([
52
- ('col_int' , pyarrow .int32 ()),
53
- ('col_str' , pyarrow .string ())
54
- ])
60
+ schema = pyarrow .schema (
61
+ [("col_int" , pyarrow .int32 ()), ("col_str" , pyarrow .string ())]
62
+ )
55
63
return pyarrow .Table .from_arrays (data , schema = schema )
56
64
57
65
58
- def test_convert_arrow_table_default (mock_connection , mock_thrift_backend , mock_raw_execute_response , sample_arrow_table ):
59
- mock_raw_execute_response .arrow_queue = ArrowQueue (sample_arrow_table , sample_arrow_table .num_rows )
66
+ def test_convert_arrow_table_default (
67
+ mock_connection , mock_thrift_backend , mock_raw_execute_response , sample_arrow_table
68
+ ):
69
+ mock_raw_execute_response .arrow_queue = ArrowQueue (
70
+ sample_arrow_table , sample_arrow_table .num_rows
71
+ )
60
72
rs = ResultSet (mock_connection , mock_raw_execute_response , mock_thrift_backend )
61
73
result_one = rs .fetchone ()
62
74
assert isinstance (result_one , Row )
63
75
assert result_one .col_int == 1
64
76
assert result_one .col_str == "a"
65
- mock_raw_execute_response .arrow_queue = ArrowQueue (sample_arrow_table , sample_arrow_table .num_rows )
77
+ mock_raw_execute_response .arrow_queue = ArrowQueue (
78
+ sample_arrow_table , sample_arrow_table .num_rows
79
+ )
66
80
rs = ResultSet (mock_connection , mock_raw_execute_response , mock_thrift_backend )
67
81
result_all = rs .fetchall ()
68
82
assert len (result_all ) == 3
@@ -71,9 +85,13 @@ def test_convert_arrow_table_default(mock_connection, mock_thrift_backend, mock_
71
85
assert result_all [1 ].col_str == "b"
72
86
73
87
74
- def test_convert_arrow_table_disable_pandas (mock_connection , mock_thrift_backend , mock_raw_execute_response , sample_arrow_table ):
88
+ def test_convert_arrow_table_disable_pandas (
89
+ mock_connection , mock_thrift_backend , mock_raw_execute_response , sample_arrow_table
90
+ ):
75
91
mock_connection .disable_pandas = True
76
- mock_raw_execute_response .arrow_queue = ArrowQueue (sample_arrow_table , sample_arrow_table .num_rows )
92
+ mock_raw_execute_response .arrow_queue = ArrowQueue (
93
+ sample_arrow_table , sample_arrow_table .num_rows
94
+ )
77
95
rs = ResultSet (mock_connection , mock_raw_execute_response , mock_thrift_backend )
78
96
result = rs .fetchall ()
79
97
assert len (result ) == 3
@@ -84,9 +102,15 @@ def test_convert_arrow_table_disable_pandas(mock_connection, mock_thrift_backend
84
102
assert isinstance (sample_arrow_table .column (1 )[0 ].as_py (), str )
85
103
86
104
87
- def test_convert_arrow_table_type_override (mock_connection , mock_thrift_backend , mock_raw_execute_response , sample_arrow_table ):
88
- mock_connection ._arrow_pandas_type_override = {pyarrow .int32 (): pandas .Float64Dtype ()}
89
- mock_raw_execute_response .arrow_queue = ArrowQueue (sample_arrow_table , sample_arrow_table .num_rows )
105
+ def test_convert_arrow_table_type_override (
106
+ mock_connection , mock_thrift_backend , mock_raw_execute_response , sample_arrow_table
107
+ ):
108
+ mock_connection ._arrow_pandas_type_override = {
109
+ pyarrow .int32 (): pandas .Float64Dtype ()
110
+ }
111
+ mock_raw_execute_response .arrow_queue = ArrowQueue (
112
+ sample_arrow_table , sample_arrow_table .num_rows
113
+ )
90
114
rs = ResultSet (mock_connection , mock_raw_execute_response , mock_thrift_backend )
91
115
result = rs .fetchall ()
92
116
assert len (result ) == 3
@@ -95,34 +119,44 @@ def test_convert_arrow_table_type_override(mock_connection, mock_thrift_backend,
95
119
assert result [0 ].col_str == "a"
96
120
97
121
98
- def test_convert_arrow_table_to_pandas_kwargs (mock_connection , mock_thrift_backend , mock_raw_execute_response ):
122
+ def test_convert_arrow_table_to_pandas_kwargs (
123
+ mock_connection , mock_thrift_backend , mock_raw_execute_response
124
+ ):
99
125
dt_obj = datetime .datetime (2021 , 1 , 1 , 12 , 0 , 0 , tzinfo = datetime .timezone .utc )
100
- ts_array = pyarrow .array ([dt_obj ], type = pyarrow .timestamp ('us' , tz = ' UTC' ))
101
- ts_schema = pyarrow .schema ([(' col_ts' , pyarrow .timestamp ('us' , tz = ' UTC' ))])
126
+ ts_array = pyarrow .array ([dt_obj ], type = pyarrow .timestamp ("us" , tz = " UTC" ))
127
+ ts_schema = pyarrow .schema ([(" col_ts" , pyarrow .timestamp ("us" , tz = " UTC" ))])
102
128
ts_table = pyarrow .Table .from_arrays ([ts_array ], schema = ts_schema )
103
129
104
- mock_raw_execute_response .description = [("col_ts" , "timestamp" , None , None , None , None , None )]
130
+ mock_raw_execute_response .description = [
131
+ ("col_ts" , "timestamp" , None , None , None , None , None )
132
+ ]
105
133
mock_raw_execute_response .arrow_queue = ArrowQueue (ts_table , ts_table .num_rows )
106
134
107
135
# Scenario 1: timestamp_as_object = True. Observed as datetime.datetime in Row.
108
136
mock_connection ._arrow_to_pandas_kwargs = {"timestamp_as_object" : True }
109
- rs_ts_true = ResultSet (mock_connection , mock_raw_execute_response , mock_thrift_backend )
137
+ rs_ts_true = ResultSet (
138
+ mock_connection , mock_raw_execute_response , mock_thrift_backend
139
+ )
110
140
result_true = rs_ts_true .fetchall ()
111
141
assert len (result_true ) == 1
112
142
assert isinstance (result_true [0 ].col_ts , datetime .datetime )
113
143
114
144
# Scenario 2: timestamp_as_object = False. Observed as pandas.Timestamp in Row for this input.
115
145
mock_raw_execute_response .arrow_queue = ArrowQueue (ts_table , ts_table .num_rows )
116
146
mock_connection ._arrow_to_pandas_kwargs = {"timestamp_as_object" : False }
117
- rs_ts_false = ResultSet (mock_connection , mock_raw_execute_response , mock_thrift_backend )
147
+ rs_ts_false = ResultSet (
148
+ mock_connection , mock_raw_execute_response , mock_thrift_backend
149
+ )
118
150
result_false = rs_ts_false .fetchall ()
119
151
assert len (result_false ) == 1
120
152
assert isinstance (result_false [0 ].col_ts , pandas .Timestamp )
121
153
122
154
# Scenario 3: no override. Observed as datetime.datetime in Row since timestamp_as_object is True by default.
123
155
mock_raw_execute_response .arrow_queue = ArrowQueue (ts_table , ts_table .num_rows )
124
156
mock_connection ._arrow_to_pandas_kwargs = {}
125
- rs_ts_true = ResultSet (mock_connection , mock_raw_execute_response , mock_thrift_backend )
157
+ rs_ts_true = ResultSet (
158
+ mock_connection , mock_raw_execute_response , mock_thrift_backend
159
+ )
126
160
result_true = rs_ts_true .fetchall ()
127
161
assert len (result_true ) == 1
128
162
assert isinstance (result_true [0 ].col_ts , datetime .datetime )
0 commit comments