forked from logicalclocks/hopsworks-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_hopswork_udf.py
473 lines (367 loc) · 18.3 KB
/
test_hopswork_udf.py
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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
#
# Copyright 2024 Hopsworks AB
#
# 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.
#
from datetime import date, datetime, time
import pandas as pd
import pytest
from hsfs.client.exceptions import FeatureStoreException
from hsfs.hopsworks_udf import HopsworksUdf, TransformationFeature, udf
class TestHopsworksUdf:
def test_validate_and_convert_output_types_one_elements(self):
assert HopsworksUdf._validate_and_convert_output_types([int]) == ["bigint"]
assert HopsworksUdf._validate_and_convert_output_types([float]) == ["double"]
assert HopsworksUdf._validate_and_convert_output_types([str]) == ["string"]
assert HopsworksUdf._validate_and_convert_output_types([bool]) == ["boolean"]
assert HopsworksUdf._validate_and_convert_output_types([datetime]) == [
"timestamp"
]
assert HopsworksUdf._validate_and_convert_output_types([time]) == ["timestamp"]
assert HopsworksUdf._validate_and_convert_output_types([date]) == ["date"]
with pytest.raises(FeatureStoreException) as exception:
HopsworksUdf._validate_and_convert_output_types([pd.DatetimeTZDtype])
assert (
str(exception.value)
== f"Output type {pd.DatetimeTZDtype} is not supported. Please refer to the documentation to get more information on the supported types."
)
def test_validate_and_convert_output_types_multiple_types(self):
assert HopsworksUdf._validate_and_convert_output_types(
[int, float, str, bool, datetime, date, time]
) == ["bigint", "double", "string", "boolean", "timestamp", "date", "timestamp"]
assert HopsworksUdf._validate_and_convert_output_types(
["bigint", "double", "string", "boolean", "timestamp", "date"]
) == ["bigint", "double", "string", "boolean", "timestamp", "date"]
with pytest.raises(FeatureStoreException) as exception:
HopsworksUdf._validate_and_convert_output_types([pd.DatetimeTZDtype])
assert (
str(exception.value)
== f"Output type {pd.DatetimeTZDtype} is not supported. Please refer to the documentation to get more information on the supported types."
)
def test_validate_and_convert_output_types_invalid_types(self):
with pytest.raises(FeatureStoreException) as exception:
HopsworksUdf._validate_and_convert_output_types([pd.DatetimeTZDtype])
assert (
str(exception.value)
== f"Output type {pd.DatetimeTZDtype} is not supported. Please refer to the documentation to get more information on the supported types."
)
with pytest.raises(FeatureStoreException) as exception:
HopsworksUdf._validate_and_convert_output_types([int, pd.DatetimeTZDtype])
assert (
str(exception.value)
== f"Output type {pd.DatetimeTZDtype} is not supported. Please refer to the documentation to get more information on the supported types."
)
with pytest.raises(FeatureStoreException) as exception:
HopsworksUdf._validate_and_convert_output_types([int, "pd.DatetimeTZDtype"])
assert (
str(exception.value)
== "Output type pd.DatetimeTZDtype is not supported. Please refer to the documentation to get more information on the supported types."
)
def test_get_module_imports(self):
assert HopsworksUdf._get_module_imports(
"python/tests/test_helpers/transformation_test_helper.py"
) == [
"import pandas as pd",
"from hsfs.transformation_statistics import TransformationStatistics",
]
def test_extract_source_code(self):
from .test_helpers.transformation_test_helper import test_function
assert """import pandas as pd
from hsfs.transformation_statistics import TransformationStatistics
def test_function():
return True""" == HopsworksUdf._extract_source_code(test_function).strip()
def test_extract_function_arguments_no_arguments(self):
from .test_helpers.transformation_test_helper import test_function
with pytest.raises(FeatureStoreException) as exception:
HopsworksUdf._extract_function_arguments(test_function)
assert (
str(exception.value)
== "No arguments present in the provided user defined function. Please provide at least one argument in the defined user defined function."
)
def test_extract_function_arguments_one_argument(self):
from .test_helpers.transformation_test_helper import test_function_one_argument
function_argument = HopsworksUdf._extract_function_arguments(
test_function_one_argument
)
assert function_argument == [
TransformationFeature(feature_name="arg1", statistic_argument_name=None)
]
def test_extract_function_arguments_one_argument_with_statistics(self):
from .test_helpers.transformation_test_helper import (
test_function_one_argument_with_statistics,
)
function_argument = HopsworksUdf._extract_function_arguments(
test_function_one_argument_with_statistics
)
assert function_argument == [
TransformationFeature(feature_name="arg1", statistic_argument_name="arg1")
]
def test_extract_function_arguments_one_argument_with_typehint(self):
from .test_helpers.transformation_test_helper import (
test_function_one_argument_with_typehints,
)
function_argument = HopsworksUdf._extract_function_arguments(
test_function_one_argument_with_typehints
)
assert function_argument == [
TransformationFeature(feature_name="arg1", statistic_argument_name=None)
]
def test_extract_function_arguments_one_argument_with_statistics_and_typehints(
self,
):
from .test_helpers.transformation_test_helper import (
test_function_one_argument_with_statistics_and_typehints,
)
function_argument = HopsworksUdf._extract_function_arguments(
test_function_one_argument_with_statistics_and_typehints
)
assert function_argument == [
TransformationFeature(feature_name="arg1", statistic_argument_name="arg1")
]
def test_extract_function_arguments_multiple_argument(self):
from .test_helpers.transformation_test_helper import (
test_function_multiple_argument,
)
function_argument = HopsworksUdf._extract_function_arguments(
test_function_multiple_argument
)
assert function_argument == [
TransformationFeature(feature_name="arg1", statistic_argument_name=None),
TransformationFeature(feature_name="arg2", statistic_argument_name=None),
]
def test_extract_function_arguments_multiple_argument_with_statistics(self):
from .test_helpers.transformation_test_helper import (
test_function_multiple_argument_with_statistics,
)
function_argument = HopsworksUdf._extract_function_arguments(
test_function_multiple_argument_with_statistics
)
assert function_argument == [
TransformationFeature(feature_name="arg1", statistic_argument_name="arg1"),
TransformationFeature(feature_name="arg2", statistic_argument_name=None),
TransformationFeature(feature_name="arg3", statistic_argument_name="arg3"),
]
def test_extract_function_arguments_multiple_argument_with_typehints(self):
from .test_helpers.transformation_test_helper import (
test_function_multiple_argument_with_typehints,
)
function_argument = HopsworksUdf._extract_function_arguments(
test_function_multiple_argument_with_typehints
)
assert function_argument == [
TransformationFeature(feature_name="arg1", statistic_argument_name=None),
TransformationFeature(feature_name="arg2", statistic_argument_name=None),
]
def test_extract_function_arguments_multiple_argument_with_statistics_and_typehints(
self,
):
from .test_helpers.transformation_test_helper import (
test_function_multiple_argument_with_statistics_and_typehints,
)
function_argument = HopsworksUdf._extract_function_arguments(
test_function_multiple_argument_with_statistics_and_typehints
)
assert function_argument == [
TransformationFeature(feature_name="arg1", statistic_argument_name="arg1"),
TransformationFeature(feature_name="arg2", statistic_argument_name="arg2"),
]
def test_extract_function_arguments_multiple_argument_with_mixed_statistics_and_typehints(
self,
):
from .test_helpers.transformation_test_helper import (
test_function_multiple_argument_with_mixed_statistics_and_typehints,
)
function_argument = HopsworksUdf._extract_function_arguments(
test_function_multiple_argument_with_mixed_statistics_and_typehints
)
assert function_argument == [
TransformationFeature(feature_name="arg1", statistic_argument_name="arg1"),
TransformationFeature(feature_name="arg2", statistic_argument_name=None),
TransformationFeature(feature_name="arg3", statistic_argument_name="arg3"),
]
def test_extract_function_arguments_multiple_argument_all_parameter_with_spaces(
self,
):
from .test_helpers.transformation_test_helper import (
test_function_multiple_argument_all_parameter_with_spaces,
)
function_argument = HopsworksUdf._extract_function_arguments(
test_function_multiple_argument_all_parameter_with_spaces
)
assert function_argument == [
TransformationFeature(feature_name="arg1", statistic_argument_name="arg1"),
TransformationFeature(feature_name="arg2", statistic_argument_name="arg2"),
]
def test_extract_function_arguments_multiple_argument_all_parameter_multiline(self):
from .test_helpers.transformation_test_helper import (
test_function_multiple_argument_all_parameter_multiline,
)
function_argument = HopsworksUdf._extract_function_arguments(
test_function_multiple_argument_all_parameter_multiline
)
assert function_argument == [
TransformationFeature(feature_name="arg1", statistic_argument_name="arg1"),
TransformationFeature(feature_name="arg2", statistic_argument_name=None),
TransformationFeature(feature_name="arg3", statistic_argument_name="arg3"),
]
def test_extract_function_arguments_multiple_argumen_all_parameter_multiline_with_comments(
self,
):
from .test_helpers.transformation_test_helper import (
test_function_multiple_argument_all_parameter_multiline_with_comments,
)
function_argument = HopsworksUdf._extract_function_arguments(
test_function_multiple_argument_all_parameter_multiline_with_comments
)
assert function_argument == [
TransformationFeature(feature_name="arg1", statistic_argument_name="arg1"),
TransformationFeature(feature_name="arg2", statistic_argument_name=None),
TransformationFeature(feature_name="arg3", statistic_argument_name="arg3"),
]
def test_extract_function_arguments_statistics_invalid(self):
from .test_helpers.transformation_test_helper import (
test_function_statistics_invalid,
)
with pytest.raises(FeatureStoreException) as exception:
HopsworksUdf._extract_function_arguments(test_function_statistics_invalid)
assert (
str(exception.value)
== "No argument corresponding to statistics parameter 'arg3' present in function definition."
)
def test_format_source_code(self):
from .test_helpers.transformation_test_helper import (
test_function_multiple_argument_all_parameter_multiline_with_comments,
)
function_source = HopsworksUdf._extract_source_code(
test_function_multiple_argument_all_parameter_multiline_with_comments
)
formated_source, module_imports = HopsworksUdf._format_source_code(
function_source
)
assert (
formated_source.strip()
== """def test_function_multiple_argument_all_parameter_multiline_with_comments(arg1, arg2, arg3):
\t pass"""
)
def test_generate_output_column_names_one_argument_one_output_type(self):
@udf(int)
def test_func(col1):
return col1 + 1
assert test_func._get_output_column_names() == ["test_func_col1_"]
def test_generate_output_column_names_multiple_argument_one_output_type(self):
@udf(int)
def test_func(col1, col2, col3):
return col1 + 1
assert test_func._get_output_column_names() == ["test_func_col1_col2_col3_"]
def test_generate_output_column_names_single_argument_multiple_output_type(self):
@udf([int, float, int])
def test_func(col1):
return pd.DataFrame(
{"col1": [col1 + 1], "col2": [col1 + 1], "col3": [col1 + 1]}
)
assert test_func._get_output_column_names() == [
"test_func_col1_0",
"test_func_col1_1",
"test_func_col1_2",
]
def test_generate_output_column_names_multiple_argument_multiple_output_type(self):
@udf([int, float, int])
def test_func(col1, col2, col3):
return pd.DataFrame(
{"col1": [col1 + 1], "col2": [col2 + 1], "col3": [col3 + 1]}
)
assert test_func._get_output_column_names() == [
"test_func_col1_col2_col3_0",
"test_func_col1_col2_col3_1",
"test_func_col1_col2_col3_2",
]
def test_create_pandas_udf_return_schema_from_list_one_output_type(self):
@udf(int)
def test_func(col1):
return col1 + 1
assert test_func._create_pandas_udf_return_schema_from_list() == "bigint"
def test_create_pandas_udf_return_schema_from_list_one_argument_multiple_output_type(
self,
):
@udf([int, float, str, date, datetime, time, bool])
def test_func(col1):
return pd.DataFrame(
{
"col1": [col1 + 1],
"col2": [col1 + 1],
"col3": [col1 + 1],
"col4": [col1 + 1],
"col5": [col1 + 1],
"col6": [True],
}
)
assert (
test_func._create_pandas_udf_return_schema_from_list()
== "`test_func_col1_0` bigint, `test_func_col1_1` double, `test_func_col1_2` string, `test_func_col1_3` date, `test_func_col1_4` timestamp, `test_func_col1_5` timestamp, `test_func_col1_6` boolean"
)
def test_hopsworks_wrapper_single_output(self):
@udf(int)
def test_func(col1):
return col1 + 1
renaming_wrapper_function = test_func.hopsworksUdf_wrapper()
test_dataframe = pd.DataFrame({"col1": [1, 2, 3, 4]})
result = renaming_wrapper_function(test_dataframe["col1"])
assert result.name == "test_func_col1_"
assert result.values.tolist() == [2, 3, 4, 5]
def test_hopsworks_wrapper_multiple_output(self):
@udf([int, float])
def test_func(col1, col2):
return pd.DataFrame({"out1": col1 + 1, "out2": col2 + 2})
renaming_wrapper_function = test_func.hopsworksUdf_wrapper()
test_dataframe = pd.DataFrame(
{"column1": [1, 2, 3, 4], "column2": [10, 20, 30, 40]}
)
result = renaming_wrapper_function(
test_dataframe["column1"], test_dataframe["column2"]
)
assert all(result.columns == ["test_func_col1_col2_0", "test_func_col1_col2_1"])
assert result.values.tolist() == [[2, 12], [3, 22], [4, 32], [5, 42]]
def test_HopsworkUDf_call_one_argument(self):
@udf(int)
def test_func(col1):
return col1 + 1
assert test_func.transformation_features == ["col1"]
assert test_func.statistics_features == []
assert test_func("new_feature").transformation_features == ["new_feature"]
assert test_func("new_feature").statistics_features == []
def test_HopsworkUDf_call_one_argument_statistics(self):
from hsfs.transformation_statistics import TransformationStatistics
stats = TransformationStatistics("col1")
@udf(int)
def test_func(col1, statistics=stats):
return col1 + statistics.col1.mean
assert test_func.transformation_features == ["col1"]
assert test_func.statistics_features == ["col1"]
assert test_func._statistics_argument_names == ["col1"]
assert test_func("new_feature").transformation_features == ["new_feature"]
assert test_func("new_feature").statistics_features == ["new_feature"]
assert test_func("new_feature")._statistics_argument_names == ["col1"]
def test_HopsworkUDf_call_multiple_argument_statistics(self):
from hsfs.transformation_statistics import TransformationStatistics
stats = TransformationStatistics("col1", "col3")
@udf(int)
def test_func(col1, col2, col3, statistics=stats):
return col1 + statistics.col1.mean + statistics.col3.mean
assert test_func.transformation_features == ["col1", "col2", "col3"]
assert test_func.statistics_features == ["col1", "col3"]
assert test_func("f1", "f2", "f3").transformation_features == ["f1", "f2", "f3"]
assert test_func("f1", "f2", "f3").statistics_features == ["f1", "f3"]
assert test_func("f1", "f2", "f3")._statistics_argument_names == [
"col1",
"col3",
]