forked from apache/beam
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode_object_pickler_test.py
More file actions
570 lines (481 loc) · 21.5 KB
/
Copy pathcode_object_pickler_test.py
File metadata and controls
570 lines (481 loc) · 21.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
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
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
#
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You 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.
#
"""Tests for generating stable identifiers to use for Pickle serialization."""
import hashlib
import unittest
from parameterized import parameterized
# pylint: disable=unused-import
from apache_beam.internal import code_object_pickler
from apache_beam.internal.test_data import module_1
from apache_beam.internal.test_data import module_1_class_added
from apache_beam.internal.test_data import module_1_function_added
from apache_beam.internal.test_data import module_1_global_variable_added
from apache_beam.internal.test_data import module_1_lambda_variable_added
from apache_beam.internal.test_data import module_1_local_variable_added
from apache_beam.internal.test_data import module_1_local_variable_removed
from apache_beam.internal.test_data import module_1_nested_function_2_added
from apache_beam.internal.test_data import module_1_nested_function_added
from apache_beam.internal.test_data import module_2
from apache_beam.internal.test_data import module_2_modified
from apache_beam.internal.test_data import module_3
from apache_beam.internal.test_data import module_3_modified
from apache_beam.internal.test_data import module_with_default_argument
def top_level_function():
return 1
top_level_lambda = lambda x: 1
def get_nested_function():
def nested_function():
return 1
return nested_function
def get_lambda_from_dictionary():
d = {"a": lambda x: 1, "b": lambda y: 2}
return d["a"]
def get_lambda_from_dictionary_same_args():
d = {"a": lambda x: 1, "b": lambda x: x + 1}
return d["a"]
def function_with_lambda_default_argument(fn=lambda x: 1):
return fn
def function_with_function_default_argument(fn=top_level_function):
return fn
def function_decorator(f):
return lambda x: f(f(x))
@function_decorator
def add_one(x):
return x + 1
class ClassWithFunction:
def process(self):
return 1
class ClassWithStaticMethod:
@staticmethod
def static_method():
return 1
class ClassWithClassMethod:
@classmethod
def class_method(cls):
return 1
class ClassWithNestedFunction:
def process(self):
def nested_function():
return 1
return nested_function
class ClassWithLambda:
def process(self):
return lambda: 1
class ClassWithNestedClass:
class InnerClass:
def process(self):
return 1
class ClassWithNestedLambda:
def process(self):
def get_lambda_from_dictionary():
d = {"a": lambda x: 1, "b": lambda y: 2}
return d["a"]
return get_lambda_from_dictionary()
prefix = __name__
test_cases = [
(top_level_function, f"{prefix}.top_level_function"
".__code__"),
(top_level_lambda, f"{prefix}.top_level_lambda"
".__code__"),
(
get_nested_function(), (
f"{prefix}.get_nested_function"
".__code__.co_consts[nested_function]")),
(
get_lambda_from_dictionary(),
(
f"{prefix}"
".get_lambda_from_dictionary.__code__.co_consts[<lambda>, ('x',)]")
),
(
get_lambda_from_dictionary_same_args(),
(
f"{prefix}"
".get_lambda_from_dictionary_same_args.__code__.co_consts"
"[<lambda>, ('x',), " + hashlib.md5(
get_lambda_from_dictionary_same_args().__code__.co_code).
hexdigest() + "]")),
(
function_with_lambda_default_argument(),
(
f"{prefix}"
".function_with_lambda_default_argument.__defaults__[0].__code__")),
(
function_with_function_default_argument(),
f"{prefix}.top_level_function"
".__code__"),
(add_one, f"{prefix}.function_decorator"
".__code__.co_consts[<lambda>]"),
(
ClassWithFunction.process,
f"{prefix}.ClassWithFunction"
".process.__code__"),
(
ClassWithStaticMethod.static_method,
f"{prefix}.ClassWithStaticMethod"
".static_method.__code__"),
(
ClassWithClassMethod.class_method,
f"{prefix}.ClassWithClassMethod"
".class_method.__code__"),
(
ClassWithNestedFunction().process(),
(
f"{prefix}.ClassWithNestedFunction.process.__code__.co_consts"
"[nested_function]")),
(
ClassWithLambda().process(),
f"{prefix}.ClassWithLambda.process.__code__.co_consts[<lambda>]"),
(
ClassWithNestedClass.InnerClass().process,
f"{prefix}.ClassWithNestedClass.InnerClass.process.__code__"),
(
ClassWithNestedLambda().process(),
(
f"{prefix}"
".ClassWithNestedLambda.process.__code__.co_consts"
"[get_lambda_from_dictionary].co_consts[<lambda>, ('x',)]")),
(
ClassWithNestedLambda.process,
f"{prefix}.ClassWithNestedLambda.process.__code__"),
]
class CodeObjectIdentifierGenerationTest(unittest.TestCase):
@parameterized.expand(test_cases)
def test_get_code_object_identifier(self, callable, expected_path):
actual = code_object_pickler.get_code_object_identifier(callable)
self.assertEqual(actual, expected_path)
@parameterized.expand(test_cases)
def test_get_code_from_identifier(self, expected_callable, path):
actual = code_object_pickler.get_code_from_identifier(path)
self.assertEqual(actual, expected_callable.__code__)
@parameterized.expand(test_cases)
def test_roundtrip(self, callable, unused_path):
path = code_object_pickler.get_code_object_identifier(callable)
actual = code_object_pickler.get_code_from_identifier(path)
self.assertEqual(actual, callable.__code__)
class GetCodeFromCodeObjectIdentifierTest(unittest.TestCase):
def test_empty_path_raises_exception(self):
with self.assertRaisesRegex(ValueError, "Path must not be empty"):
code_object_pickler.get_code_from_identifier("")
def test_invalid_default_index_raises_exception(self):
with self.assertRaisesRegex(ValueError, "out of bounds"):
code_object_pickler.get_code_from_identifier(
"apache_beam.internal.test_data.module_with_default_argument."
"function_with_lambda_default_argument.__defaults__[1]")
def test_invalid_single_name_path_raises_exception(self):
with self.assertRaisesRegex(AttributeError,
"Could not find code object with path"):
code_object_pickler.get_code_from_identifier(
"apache_beam.internal.test_data.module_3."
"my_function.__code__.co_consts[something]")
def test_invalid_lambda_with_args_path_raises_exception(self):
with self.assertRaisesRegex(AttributeError,
"Could not find code object with path"):
code_object_pickler.get_code_from_identifier(
"apache_beam.internal.test_data.module_3."
"my_function.__code__.co_consts[<lambda>, ('x',)]")
def test_invalid_lambda_with_hash_path_raises_exception(self):
with self.assertRaisesRegex(AttributeError,
"Could not find code object with path"):
code_object_pickler.get_code_from_identifier(
"apache_beam.internal.test_data.module_3."
"my_function.__code__.co_consts[<lambda>, ('',), 1234567890]")
def test_adding_local_variable_in_class_preserves_object(self):
self.assertEqual(
code_object_pickler.get_code_from_identifier(
code_object_pickler.get_code_object_identifier(
module_2.AddLocalVariable.my_method(self)).replace(
"module_2", "module_2_modified")),
module_2_modified.AddLocalVariable.my_method(self).__code__,
)
def test_removing_local_variable_in_class_preserves_object(self):
self.assertEqual(
code_object_pickler.get_code_from_identifier(
code_object_pickler.get_code_object_identifier(
module_2.RemoveLocalVariable.my_method(self)).replace(
"module_2", "module_2_modified")),
module_2_modified.RemoveLocalVariable.my_method(self).__code__,
)
def test_adding_lambda_variable_in_class_preserves_object(self):
self.assertEqual(
code_object_pickler.get_code_from_identifier(
code_object_pickler.get_code_object_identifier(
module_2.AddLambdaVariable.my_method(self)).replace(
"module_2", "module_2_modified")),
module_2_modified.AddLambdaVariable.my_method(self).__code__,
)
def test_removing_lambda_variable_in_class_preserves_object(self):
self.assertEqual(
code_object_pickler.get_code_from_identifier(
code_object_pickler.get_code_object_identifier(
module_2.RemoveLambdaVariable.my_method(self)).replace(
"module_2", "module_2_modified")),
module_2_modified.RemoveLambdaVariable.my_method(self).__code__,
)
def test_adding_nested_function_in_class_preserves_object(self):
self.assertEqual(
code_object_pickler.get_code_from_identifier(
code_object_pickler.get_code_object_identifier(
module_2.ClassWithNestedFunction.my_method(self)).replace(
"module_2", "module_2_modified")),
module_2_modified.ClassWithNestedFunction.my_method(self).__code__,
)
def test_adding_nested_function_2_in_class_preserves_object(self):
self.assertEqual(
code_object_pickler.get_code_from_identifier(
code_object_pickler.get_code_object_identifier(
module_2.ClassWithNestedFunction2.my_method(self)).replace(
"module_2", "module_2_modified")),
module_2_modified.ClassWithNestedFunction2.my_method(self).__code__,
)
def test_adding_new_function_in_class_preserves_object(self):
self.assertEqual(
code_object_pickler.get_code_from_identifier(
code_object_pickler.get_code_object_identifier(
module_2.ClassWithTwoMethods.my_method(self)).replace(
"module_2", "module_2_modified")),
module_2_modified.ClassWithTwoMethods.my_method(self).__code__,
)
def test_removing_method_in_class_preserves_object(self):
self.assertEqual(
code_object_pickler.get_code_from_identifier(
code_object_pickler.get_code_object_identifier(
module_2.RemoveMethod.my_method(self)).replace(
"module_2", "module_2_modified")),
module_2_modified.RemoveMethod.my_method(self).__code__,
)
def test_adding_global_variable_preserves_object(self):
self.assertEqual(
code_object_pickler.get_code_from_identifier(
code_object_pickler.get_code_object_identifier(
module_1.my_function()).replace(
"module_1",
"module_1_global_variable_added",
)),
module_1_global_variable_added.my_function().__code__,
)
def test_adding_top_level_function_preserves_object(self):
self.assertEqual(
code_object_pickler.get_code_from_identifier(
code_object_pickler.get_code_object_identifier(
module_1.my_function()).replace(
"module_1", "module_1_function_added")),
module_1_function_added.my_function().__code__,
)
def test_adding_local_variable_in_function_preserves_object(self):
self.assertEqual(
code_object_pickler.get_code_from_identifier(
code_object_pickler.get_code_object_identifier(
module_1.my_function()).replace(
"module_1", "module_1_local_variable_added")),
module_1_local_variable_added.my_function().__code__,
)
def test_removing_local_variable_in_function_preserves_object(self):
self.assertEqual(
code_object_pickler.get_code_from_identifier(
code_object_pickler.get_code_object_identifier(
module_1.my_function()).replace(
"module_1", "module_1_local_variable_removed")),
module_1_local_variable_removed.my_function().__code__,
)
def test_adding_nested_function_in_function_preserves_object(self):
self.assertEqual(
code_object_pickler.get_code_from_identifier(
code_object_pickler.get_code_object_identifier(
module_1.my_function()).replace(
"module_1", "module_1_nested_function_added")),
module_1_nested_function_added.my_function().__code__,
)
def test_adding_nested_function_2_in_function_preserves_object(self):
self.assertEqual(
code_object_pickler.get_code_from_identifier(
code_object_pickler.get_code_object_identifier(
module_1.my_function()).replace(
"module_1", "module_1_nested_function_2_added")),
module_1_nested_function_2_added.my_function().__code__,
)
def test_adding_class_preserves_object(self):
self.assertEqual(
code_object_pickler.get_code_from_identifier(
code_object_pickler.get_code_object_identifier(
module_1.my_function()).replace(
"module_1", "module_1_class_added")),
module_1_class_added.my_function().__code__,
)
def test_adding_lambda_variable_in_function_preserves_object(self):
self.assertEqual(
code_object_pickler.get_code_from_identifier(
code_object_pickler.get_code_object_identifier(
module_1.my_function()).replace(
"module_1", "module_1_lambda_variable_added")),
module_1_lambda_variable_added.my_function().__code__,
)
def test_removing_lambda_variable_in_function_preserves_object(self):
self.assertEqual(
code_object_pickler.get_code_from_identifier(
code_object_pickler.get_code_object_identifier(
module_3.my_function()).replace(
"module_3", "module_3_modified")),
module_3_modified.my_function().__code__,
)
class CodePathStabilityTest(unittest.TestCase):
def test_adding_local_variable_in_class_preserves_path(self):
self.assertEqual(
code_object_pickler.get_code_object_identifier(
module_2.AddLocalVariable.my_method(self)).replace(
"module_2", "module_name"),
code_object_pickler.get_code_object_identifier(
module_2_modified.AddLocalVariable.my_method(self)).replace(
"module_2_modified", "module_name"),
)
def test_removing_local_variable_in_class_preserves_path(self):
self.assertEqual(
code_object_pickler.get_code_object_identifier(
module_2.RemoveLocalVariable.my_method(self)).replace(
"module_2", "module_name"),
code_object_pickler.get_code_object_identifier(
module_2_modified.RemoveLocalVariable.my_method(self)).replace(
"module_2_modified", "module_name"),
)
def test_adding_lambda_variable_in_class_changes_path(self):
self.assertNotEqual(
code_object_pickler.get_code_object_identifier(
module_2.AddLambdaVariable.my_method(self)).replace(
"module_2", "module_name"),
code_object_pickler.get_code_object_identifier(
module_2_modified.AddLambdaVariable.my_method(self)).replace(
"module_2_modified", "module_name"),
)
def test_removing_lambda_variable_in_class_changes_path(self):
self.assertNotEqual(
code_object_pickler.get_code_object_identifier(
module_2.RemoveLambdaVariable.my_method(self)).replace(
"module_2", "module_name"),
code_object_pickler.get_code_object_identifier(
module_2_modified.RemoveLambdaVariable.my_method(self)).replace(
"module_2_modified", "module_name"),
)
def test_adding_nested_function_in_class_preserves_path(self):
self.assertEqual(
code_object_pickler.get_code_object_identifier(
module_2.ClassWithNestedFunction.my_method(self)).replace(
"module_2", "module_name"),
code_object_pickler.get_code_object_identifier(
module_2_modified.ClassWithNestedFunction.my_method(self)).replace(
"module_2_modified", "module_name"),
)
def test_adding_nested_function_2_in_class_preserves_path(self):
self.assertEqual(
code_object_pickler.get_code_object_identifier(
module_2.ClassWithNestedFunction2.my_method(self)).replace(
"module_2", "module_name"),
code_object_pickler.get_code_object_identifier(
module_2_modified.ClassWithNestedFunction2.my_method(self)).replace(
"module_2_modified", "module_name"),
)
def test_adding_new_function_in_class_preserves_path(self):
self.assertEqual(
code_object_pickler.get_code_object_identifier(
module_2.ClassWithTwoMethods.my_method(self)).replace(
"module_2", "module_name"),
code_object_pickler.get_code_object_identifier(
module_2_modified.ClassWithTwoMethods.my_method(self)).replace(
"module_2_modified", "module_name"),
)
def test_removing_function_in_class_preserves_path(self):
self.assertEqual(
code_object_pickler.get_code_object_identifier(
module_2.RemoveMethod.my_method(self)).replace(
"module_2", "module_name"),
code_object_pickler.get_code_object_identifier(
module_2_modified.RemoveMethod.my_method(self)).replace(
"module_2_modified", "module_name"),
)
def test_adding_global_variable_preserves_path(self):
self.assertEqual(
code_object_pickler.get_code_object_identifier(
module_1.my_function()).replace("module_1", "module_name"),
code_object_pickler.get_code_object_identifier(
module_1_global_variable_added.my_function()).replace(
"module_1_global_variable_added", "module_name"),
)
def test_adding_top_level_function_preserves_path(self):
self.assertEqual(
code_object_pickler.get_code_object_identifier(
module_1.my_function()).replace("module_1", "module_name"),
code_object_pickler.get_code_object_identifier(
module_1_function_added.my_function()).replace(
"module_1_function_added", "module_name"),
)
def test_adding_local_variable_in_function_preserves_path(self):
self.assertEqual(
code_object_pickler.get_code_object_identifier(
module_1.my_function()).replace("module_1", "module_name"),
code_object_pickler.get_code_object_identifier(
module_1_local_variable_added.my_function()).replace(
"module_1_local_variable_added", "module_name"),
)
def test_removing_local_variable_in_function_preserves_path(self):
self.assertEqual(
code_object_pickler.get_code_object_identifier(
module_1.my_function()).replace("module_1", "module_name"),
code_object_pickler.get_code_object_identifier(
module_1_local_variable_removed.my_function()).replace(
"module_1_local_variable_removed", "module_name"),
)
def test_adding_nested_function_in_function_preserves_path(self):
self.assertEqual(
code_object_pickler.get_code_object_identifier(
module_1.my_function()).replace("module_1", "module_name"),
code_object_pickler.get_code_object_identifier(
module_1_nested_function_added.my_function()).replace(
"module_1_nested_function_added", "module_name"),
)
def test_adding_nested_function_2_in_function_preserves_path(self):
self.assertEqual(
code_object_pickler.get_code_object_identifier(
module_1.my_function()).replace("module_1", "module_name"),
code_object_pickler.get_code_object_identifier(
module_1_nested_function_2_added.my_function()).replace(
"module_1_nested_function_2_added", "module_name"),
)
def test_adding_class_preserves_path(self):
self.assertEqual(
code_object_pickler.get_code_object_identifier(
module_1.my_function()).replace("module_1", "module_name"),
code_object_pickler.get_code_object_identifier(
module_1_class_added.my_function()).replace(
"module_1_class_added", "module_name"),
)
def test_adding_lambda_variable_in_function_changes_path(self):
self.assertNotEqual(
code_object_pickler.get_code_object_identifier(
module_1.my_function()).replace("module_1", "module_name"),
code_object_pickler.get_code_object_identifier(
module_1_lambda_variable_added.my_function()).replace(
"module_1_lambda_variable_added", "module_name"),
)
def test_removing_lambda_variable_in_function_changes_path(self):
self.assertNotEqual(
code_object_pickler.get_code_object_identifier(
module_3.my_function()).replace("module_3", "module_name"),
code_object_pickler.get_code_object_identifier(
module_3_modified.my_function()).replace(
"module_3_modified", "module_name"),
)
if __name__ == "__main__":
unittest.main()