This repository was archived by the owner on Apr 1, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 67
Expand file tree
/
Copy pathtest_remote_function_utils.py
More file actions
329 lines (275 loc) · 10.5 KB
/
test_remote_function_utils.py
File metadata and controls
329 lines (275 loc) · 10.5 KB
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
# Copyright 2024 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import inspect
from unittest.mock import patch
import bigframes_vendored.constants as constants
import pytest
from bigframes.functions import _utils, function_typing
@pytest.mark.parametrize(
("input_location", "expected_bq_location", "expected_cf_region"),
[
(None, "us", "us-central1"),
("us", "us", "us-central1"),
("eu", "eu", "europe-west1"),
("US-east4", "us-east4", "us-east4"),
],
)
def test_get_remote_function_locations(
input_location, expected_bq_location, expected_cf_region
):
"""Tests getting remote function locations for various locations."""
bq_location, cf_region = _utils.get_remote_function_locations(input_location)
assert bq_location == expected_bq_location
assert cf_region == expected_cf_region
def test_get_updated_package_requirements_no_extra_package():
"""Tests with no extra package."""
result = _utils.get_updated_package_requirements(capture_references=False)
assert result is None
initial_packages = ["xgboost"]
result = _utils.get_updated_package_requirements(
initial_packages, capture_references=False
)
assert result == initial_packages
@patch("bigframes.functions._utils.numpy.__version__", "1.24.4")
@patch("bigframes.functions._utils.pyarrow.__version__", "14.0.1")
@patch("bigframes.functions._utils.pandas.__version__", "2.0.3")
@patch("bigframes.functions._utils.cloudpickle.__version__", "2.2.1")
def test_get_updated_package_requirements_is_row_processor_with_versions():
"""Tests with is_row_processor=True and specific versions."""
expected = [
"cloudpickle==2.2.1",
"numpy==1.24.4",
"pandas==2.0.3",
"pyarrow==14.0.1",
]
result = _utils.get_updated_package_requirements(is_row_processor=True)
assert result == expected
@patch("bigframes.functions._utils.warnings.warn")
@patch("bigframes.functions._utils.cloudpickle.__version__", "2.2.1")
def test_get_updated_package_requirements_ignore_version(mock_warn):
"""
Tests with is_row_processor=True and ignore_package_version=True.
Should add packages without versions and raise a warning.
"""
expected = ["cloudpickle==2.2.1", "numpy", "pandas", "pyarrow"]
result = _utils.get_updated_package_requirements(
is_row_processor=True, ignore_package_version=True
)
assert result == expected
# Verify that a warning was issued.
mock_warn.assert_called_once()
@patch("bigframes.functions._utils.numpy.__version__", "1.24.4")
@patch("bigframes.functions._utils.pyarrow.__version__", "14.0.1")
@patch("bigframes.functions._utils.pandas.__version__", "2.0.3")
def test_get_updated_package_requirements_capture_references_false():
"""
Tests with capture_references=False.
Should not add cloudpickle but should add others if requested.
"""
# Case 1: Only capture_references=False.
result_1 = _utils.get_updated_package_requirements(capture_references=False)
assert result_1 is None
# Case 2: capture_references=False but is_row_processor=True.
expected_2 = ["numpy==1.24.4", "pandas==2.0.3", "pyarrow==14.0.1"]
result_2 = _utils.get_updated_package_requirements(
is_row_processor=True, capture_references=False
)
assert result_2 == expected_2
@patch("bigframes.functions._utils.numpy.__version__", "1.24.4")
@patch("bigframes.functions._utils.pyarrow.__version__", "14.0.1")
@patch("bigframes.functions._utils.pandas.__version__", "2.0.3")
@patch("bigframes.functions._utils.cloudpickle.__version__", "2.2.1")
def test_get_updated_package_requirements_non_overlapping_packages():
"""Tests providing an initial list of packages that do not overlap."""
initial_packages = ["scikit-learn==1.3.0", "xgboost"]
expected = [
"cloudpickle==2.2.1",
"numpy==1.24.4",
"pandas==2.0.3",
"pyarrow==14.0.1",
"scikit-learn==1.3.0",
"xgboost",
]
result = _utils.get_updated_package_requirements(
package_requirements=initial_packages, is_row_processor=True
)
assert result == expected
@patch("bigframes.functions._utils.numpy.__version__", "1.24.4")
@patch("bigframes.functions._utils.pyarrow.__version__", "14.0.1")
@patch("bigframes.functions._utils.pandas.__version__", "2.0.3")
@patch("bigframes.functions._utils.cloudpickle.__version__", "2.2.1")
def test_get_updated_package_requirements_overlapping_packages():
"""Tests that packages are not added if they already exist."""
# The function should respect the pre-existing pandas version.
initial_packages = ["pandas==1.5.3", "numpy"]
expected = [
"cloudpickle==2.2.1",
"numpy",
"pandas==1.5.3",
"pyarrow==14.0.1",
]
result = _utils.get_updated_package_requirements(
package_requirements=initial_packages, is_row_processor=True
)
assert result == expected
@patch("bigframes.functions._utils.cloudpickle.__version__", "2.2.1")
def test_get_updated_package_requirements_with_existing_cloudpickle():
"""Tests that cloudpickle is not added if it already exists."""
initial_packages = ["cloudpickle==2.0.0"]
expected = ["cloudpickle==2.0.0"]
result = _utils.get_updated_package_requirements(
package_requirements=initial_packages
)
assert result == expected
def test_package_existed_helper():
"""Tests the _package_existed helper function directly."""
reqs = ["pandas==1.0", "numpy", "scikit-learn>=1.2.0"]
# Exact match
assert _utils._package_existed(reqs, "pandas==1.0")
# Different version
assert _utils._package_existed(reqs, "pandas==2.0")
# No version specified
assert _utils._package_existed(reqs, "numpy")
# Not in list
assert not _utils._package_existed(reqs, "xgboost")
# Empty list
assert not _utils._package_existed([], "pandas")
def test_has_conflict_output_type_no_conflict():
"""Tests has_conflict_output_type with type annotation."""
# Helper functions with type annotation for has_conflict_output_type.
def _func_with_return_type(x: int) -> int:
return x
signature = inspect.signature(_func_with_return_type)
assert _utils.has_conflict_output_type(signature, output_type=float)
assert not _utils.has_conflict_output_type(signature, output_type=int)
def test_has_conflict_output_type_no_annotation():
"""Tests has_conflict_output_type without type annotation."""
# Helper functions without type annotation for has_conflict_output_type.
def _func_without_return_type(x):
return x
signature = inspect.signature(_func_without_return_type)
assert not _utils.has_conflict_output_type(signature, output_type=int)
assert not _utils.has_conflict_output_type(signature, output_type=float)
@pytest.mark.parametrize(
["metadata_options", "metadata_string"],
(
pytest.param(
{},
'{"value": {}}',
id="empty",
),
pytest.param(
{"python_output_type": None},
'{"value": {}}',
id="None",
),
pytest.param(
{"python_output_type": list[bool]},
'{"value": {"python_array_output_type": "bool"}}',
id="list-bool",
),
pytest.param(
{"python_output_type": list[float]},
'{"value": {"python_array_output_type": "float"}}',
id="list-float",
),
pytest.param(
{"python_output_type": list[int]},
'{"value": {"python_array_output_type": "int"}}',
id="list-int",
),
pytest.param(
{"python_output_type": list[str]},
'{"value": {"python_array_output_type": "str"}}',
id="list-str",
),
),
)
def test_get_bigframes_metadata(metadata_options, metadata_string):
assert _utils.get_bigframes_metadata(**metadata_options) == metadata_string
@pytest.mark.parametrize(
["output_type"],
(
pytest.param(bool),
pytest.param(bytes),
pytest.param(float),
pytest.param(int),
pytest.param(str),
pytest.param(list),
pytest.param(list[bytes], id="list-bytes"),
),
)
def test_get_bigframes_metadata_array_type_not_serializable(output_type):
with pytest.raises(ValueError) as context:
_utils.get_bigframes_metadata(python_output_type=output_type)
assert str(context.value) == (
f"python_output_type {output_type} is not serializable. {constants.FEEDBACK_LINK}"
)
@pytest.mark.parametrize(
["metadata_string", "python_output_type"],
(
pytest.param(
None,
None,
id="None",
),
pytest.param(
"",
None,
id="empty",
),
pytest.param(
"{}",
None,
id="empty-dict",
),
pytest.param(
'{"value": {}}',
None,
id="empty-value",
),
pytest.param(
'{"value": {"python_array_output_type": "bool"}}',
list[bool],
id="list-bool",
),
pytest.param(
'{"value": {"python_array_output_type": "float"}}',
list[float],
id="list-float",
),
pytest.param(
'{"value": {"python_array_output_type": "int"}}',
list[int],
id="list-int",
),
pytest.param(
'{"value": {"python_array_output_type": "str"}}',
list[str],
id="list-str",
),
),
)
def test_get_python_output_type_from_bigframes_metadata(
metadata_string, python_output_type
):
assert (
_utils.get_python_output_type_from_bigframes_metadata(metadata_string)
== python_output_type
)
def test_metadata_roundtrip_supported_array_types():
for array_of in function_typing.RF_SUPPORTED_ARRAY_OUTPUT_PYTHON_TYPES:
ser = _utils.get_bigframes_metadata(python_output_type=list[array_of]) # type: ignore
deser = _utils.get_python_output_type_from_bigframes_metadata(ser)
assert deser == list[array_of] # type: ignore