-
Notifications
You must be signed in to change notification settings - Fork 4.5k
Expand file tree
/
Copy pathyaml_transform_test.py
More file actions
1490 lines (1396 loc) · 48.9 KB
/
yaml_transform_test.py
File metadata and controls
1490 lines (1396 loc) · 48.9 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
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#
# 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.
#
import collections
import glob
import logging
import os
import shutil
import tempfile
import unittest
import apache_beam as beam
from apache_beam.testing.util import assert_that
from apache_beam.testing.util import equal_to
from apache_beam.utils import python_callable
from apache_beam.yaml import yaml_provider
from apache_beam.yaml.yaml_transform import YamlTransform
try:
import jsonschema
except ImportError:
jsonschema = None
_LOGGER = logging.getLogger(__name__)
class CreateTimestamped(beam.PTransform):
_yaml_requires_inputs = False
def __init__(self, elements):
self._elements = elements
def expand(self, p):
return (
p
| beam.Create(self._elements)
| beam.Map(lambda x: beam.transforms.window.TimestampedValue(x, x)))
class CreateInts(beam.PTransform):
_yaml_requires_inputs = False
def __init__(self, elements):
self._elements = elements
def expand(self, p):
return p | beam.Create(self._elements)
class SumGlobally(beam.PTransform):
def expand(self, pcoll):
return pcoll | beam.CombineGlobally(sum).without_defaults()
class SizeLimiter(beam.PTransform):
def __init__(self, limit, error_handling):
self._limit = limit
self._error_handling = error_handling
def expand(self, pcoll):
def raise_on_big(row):
if len(row.element) > self._limit:
raise ValueError(row.element)
else:
return row.element
good, bad = pcoll | beam.Map(raise_on_big).with_exception_handling()
return {'small_elements': good, self._error_handling['output']: bad}
TEST_PROVIDERS = {
'CreateInts': CreateInts,
'CreateTimestamped': CreateTimestamped,
'SumGlobally': SumGlobally,
'SizeLimiter': SizeLimiter,
'PyMap': lambda fn: beam.Map(python_callable.PythonCallableWithSource(fn)),
}
@unittest.skipIf(jsonschema is None, "Yaml dependencies not installed")
class YamlTransformE2ETest(unittest.TestCase):
def test_composite(self):
with beam.Pipeline(options=beam.options.pipeline_options.PipelineOptions(
pickle_library='cloudpickle')) as p:
elements = p | beam.Create([1, 2, 3])
# TODO(robertwb): Consider making the input implicit (and below).
result = elements | YamlTransform(
'''
type: composite
input:
elements: input
transforms:
- type: PyMap
name: Square
input: elements
config:
fn: "lambda x: x * x"
- type: PyMap
name: Cube
input: elements
config:
fn: "lambda x: x * x * x"
- type: Flatten
input: [Square, Cube]
output:
Flatten
''',
providers=TEST_PROVIDERS)
assert_that(result, equal_to([1, 4, 9, 1, 8, 27]))
def test_composite_implicit_input_chaining(self):
with beam.Pipeline(options=beam.options.pipeline_options.PipelineOptions(
pickle_library='cloudpickle')) as p:
elements = p | beam.Create([1, 2, 3])
result = elements | YamlTransform(
'''
type: composite
transforms:
- type: PyMap
name: Square
config:
fn: "lambda x: x * x"
- type: PyMap
name: Increment
config:
fn: "lambda x: x + 1"
''',
providers=TEST_PROVIDERS)
assert_that(result, equal_to([2, 5, 10]))
def test_chain_with_input(self):
with beam.Pipeline(options=beam.options.pipeline_options.PipelineOptions(
pickle_library='cloudpickle')) as p:
elements = p | beam.Create(range(10))
result = elements | YamlTransform(
'''
type: chain
input:
elements: input
transforms:
- type: PyMap
config:
fn: "lambda x: x * x + x"
- type: PyMap
config:
fn: "lambda x: x + 41"
''',
providers=TEST_PROVIDERS)
assert_that(result, equal_to([41, 43, 47, 53, 61, 71, 83, 97, 113, 131]))
def test_chain_with_source_sink(self):
with beam.Pipeline(options=beam.options.pipeline_options.PipelineOptions(
pickle_library='cloudpickle')) as p:
result = p | YamlTransform(
'''
type: chain
source:
type: CreateInts
config:
elements: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
transforms:
- type: PyMap
config:
fn: "lambda x: x * x + x"
sink:
type: PyMap
config:
fn: "lambda x: x + 41"
''',
providers=TEST_PROVIDERS)
assert_that(result, equal_to([41, 43, 47, 53, 61, 71, 83, 97, 113, 131]))
def test_chain_with_root(self):
with beam.Pipeline(options=beam.options.pipeline_options.PipelineOptions(
pickle_library='cloudpickle')) as p:
result = p | YamlTransform(
'''
type: chain
transforms:
- type: CreateInts
config:
elements: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
- type: PyMap
config:
fn: "lambda x: x * x + x"
- type: PyMap
config:
fn: "lambda x: x + 41"
''',
providers=TEST_PROVIDERS)
assert_that(result, equal_to([41, 43, 47, 53, 61, 71, 83, 97, 113, 131]))
def create_has_schema(self):
with beam.Pipeline(options=beam.options.pipeline_options.PipelineOptions(
pickle_library='cloudpickle')) as p:
result = p | YamlTransform(
'''
type: chain
transforms:
- type: Create
config:
elements: [{a: 1, b: 'x'}, {a: 2, b: 'y'}]
- type: MapToFields
config:
language: python
fields:
repeated: a * b
''') | beam.Map(lambda x: x.repeated)
assert_that(result, equal_to(['x', 'yy']))
def test_implicit_flatten(self):
with beam.Pipeline(options=beam.options.pipeline_options.PipelineOptions(
pickle_library='cloudpickle')) as p:
result = p | YamlTransform(
'''
type: composite
transforms:
- type: Create
name: CreateSmall
config:
elements: [1, 2, 3]
- type: Create
name: CreateBig
config:
elements: [100, 200]
- type: PyMap
input: [CreateBig, CreateSmall]
config:
fn: "lambda x: x.element * x.element"
output: PyMap
''',
providers=TEST_PROVIDERS)
assert_that(result, equal_to([1, 4, 9, 10000, 40000]))
def test_csv_to_json(self):
try:
import pandas as pd
except ImportError:
raise unittest.SkipTest('Pandas not available.')
with tempfile.TemporaryDirectory() as tmpdir:
data = pd.DataFrame([
{
'label': '11a', 'rank': 0
},
{
'label': '37a', 'rank': 1
},
{
'label': '389a', 'rank': 2
},
])
input = os.path.join(tmpdir, 'input.csv')
output = os.path.join(tmpdir, 'output.json')
data.to_csv(input, index=False)
with open(input, 'r') as f:
lines = f.readlines()
_LOGGER.debug("input.csv has these {lines} lines.")
self.assertEqual(len(lines), len(data) + 1) # +1 for header
with beam.Pipeline() as p:
result = p | YamlTransform(
'''
type: chain
transforms:
- type: ReadFromCsv
config:
path: %s
- type: WriteToJson
config:
path: %s
num_shards: 1
- type: LogForTesting
''' % (repr(input), repr(output)))
all_output = list(glob.glob(output + "*"))
self.assertEqual(len(all_output), 1)
output_shard = list(glob.glob(output + "*"))[0]
result = pd.read_json(
output_shard, orient='records',
lines=True).sort_values('rank').reindex()
pd.testing.assert_frame_equal(data, result)
def test_circular_reference_validation(self):
with beam.Pipeline(options=beam.options.pipeline_options.PipelineOptions(
pickle_library='cloudpickle')) as p:
# pylint: disable=expression-not-assigned
with self.assertRaisesRegex(ValueError, r'Circular reference detected.*'):
p | YamlTransform(
'''
type: composite
transforms:
- type: Create
name: Create
config:
elements: [0, 1, 3, 4]
input: Create
- type: PyMap
name: PyMap
config:
fn: "lambda row: row.element * row.element"
input: Create
output: PyMap
''',
providers=TEST_PROVIDERS)
def test_circular_reference_multi_inputs_validation(self):
with beam.Pipeline(options=beam.options.pipeline_options.PipelineOptions(
pickle_library='cloudpickle')) as p:
# pylint: disable=expression-not-assigned
with self.assertRaisesRegex(ValueError, r'Circular reference detected.*'):
p | YamlTransform(
'''
type: composite
transforms:
- type: Create
name: Create
config:
elements: [0, 1, 3, 4]
- type: PyMap
name: PyMap
config:
fn: "lambda row: row.element * row.element"
input: [Create, PyMap]
output: PyMap
''',
providers=TEST_PROVIDERS)
def test_name_is_not_ambiguous(self):
with beam.Pipeline(options=beam.options.pipeline_options.PipelineOptions(
pickle_library='cloudpickle')) as p:
result = p | YamlTransform(
'''
type: composite
transforms:
- type: Create
name: Create
config:
elements: [0, 1, 3, 4]
- type: PyMap
name: PyMap
config:
fn: "lambda row: row.element * row.element"
input: Create
output: PyMap
''',
providers=TEST_PROVIDERS)
# No exception raised
assert_that(result, equal_to([0, 1, 9, 16]))
def test_name_is_ambiguous(self):
with beam.Pipeline(options=beam.options.pipeline_options.PipelineOptions(
pickle_library='cloudpickle')) as p:
# pylint: disable=expression-not-assigned
with self.assertRaisesRegex(ValueError, r'Circular reference detected.*'):
p | YamlTransform(
'''
type: composite
transforms:
- type: Create
name: CreateData
config:
elements: [0, 1, 3, 4]
- type: PyMap
name: PyMap
config:
fn: "lambda elem: elem + 2"
input: CreateData
- type: PyMap
name: AnotherMap
config:
fn: "lambda elem: elem + 3"
input: PyMap
output: AnotherMap
''',
providers=TEST_PROVIDERS)
def test_empty_inputs_throws_error(self):
with beam.Pipeline(options=beam.options.pipeline_options.PipelineOptions(
pickle_library='cloudpickle')) as p:
with self.assertRaisesRegex(ValueError,
'Missing inputs for transform at '
'"EmptyInputOkButYamlDoesntKnow" at line .*'):
_ = p | YamlTransform(
'''
type: composite
transforms:
- type: PyTransform
name: EmptyInputOkButYamlDoesntKnow
config:
constructor: apache_beam.Impulse
''')
def test_empty_inputs_ok_in_source(self):
with beam.Pipeline(options=beam.options.pipeline_options.PipelineOptions(
pickle_library='cloudpickle')) as p:
# Does not throw an error like it does above.
_ = p | YamlTransform(
'''
type: composite
source:
type: PyTransform
name: EmptyInputOkButYamlDoesntKnow
config:
constructor: apache_beam.Impulse
''')
def test_empty_inputs_ok_if_explicit(self):
with beam.Pipeline(options=beam.options.pipeline_options.PipelineOptions(
pickle_library='cloudpickle')) as p:
# Does not throw an error like it does above.
_ = p | YamlTransform(
'''
type: composite
transforms:
- type: PyTransform
name: EmptyInputOkButYamlDoesntKnow
input: {}
config:
constructor: apache_beam.Impulse
''')
def test_annotations(self):
t = LinearTransform(5, b=100)
annotations = t.annotations()
with beam.Pipeline(options=beam.options.pipeline_options.PipelineOptions(
pickle_library='cloudpickle')) as p:
result = p | YamlTransform(
'''
type: chain
transforms:
- type: Create
config:
elements: [0, 1, 2, 3]
- type: %r
config: %s
''' % (annotations['yaml_type'], annotations['yaml_args']))
assert_that(result, equal_to([100, 105, 110, 115]))
def test_resource_hints(self):
t = LinearTransform(5, b=100)
with beam.Pipeline(options=beam.options.pipeline_options.PipelineOptions(
pickle_library='cloudpickle')) as p:
result = p | YamlTransform(
'''
type: chain
transforms:
- type: Create
config:
elements: [0, 1, 2, 3]
- type: MapToFields
name: WithResourceHints
config:
language: python
fields:
square: element * element
resource_hints:
min_ram: 1GB
''')
assert_that(result | beam.Map(lambda x: x.square), equal_to([0, 1, 4, 9]))
proto = p.to_runner_api()
transform, = [
t for t in proto.components.transforms.values()
if t.unique_name == 'YamlTransform/Chain/WithResourceHints']
self.assertEqual(
proto.components.environments[transform.environment_id].
resource_hints['beam:resources:min_ram_bytes:v1'],
b'1000000000',
proto)
def test_composite_resource_hints(self):
t = LinearTransform(5, b=100)
with beam.Pipeline(options=beam.options.pipeline_options.PipelineOptions(
pickle_library='cloudpickle')) as p:
result = p | YamlTransform(
'''
type: chain
transforms:
- type: Create
config:
elements: [0, 1, 2, 3]
- type: MapToFields
name: WithInheritedResourceHints
config:
language: python
fields:
square: element * element
resource_hints:
min_ram: 1GB
''')
assert_that(result | beam.Map(lambda x: x.square), equal_to([0, 1, 4, 9]))
proto = p.to_runner_api()
transform, = [
t for t in proto.components.transforms.values()
if t.unique_name == 'YamlTransform/Chain/WithInheritedResourceHints']
self.assertEqual(
proto.components.environments[transform.environment_id].
resource_hints['beam:resources:min_ram_bytes:v1'],
b'1000000000',
proto)
def test_flatten_unifies_schemas(self):
with beam.Pipeline(options=beam.options.pipeline_options.PipelineOptions(
pickle_library='cloudpickle')) as p:
_ = p | YamlTransform(
'''
type: composite
transforms:
- type: Create
name: Create1
config:
elements:
- {ride_id: '1', passenger_count: 1}
- {ride_id: '2', passenger_count: 2}
- type: Create
name: Create2
config:
elements:
- {ride_id: '3'}
- {ride_id: '4'}
- type: Flatten
input: [Create1, Create2]
- type: AssertEqual
input: Flatten
config:
elements:
- {ride_id: '1', passenger_count: 1}
- {ride_id: '2', passenger_count: 2}
- {ride_id: '3'}
- {ride_id: '4'}
''')
def test_flatten_unifies_optional_fields(self):
"""Test that Flatten correctly unifies schemas with optional fields."""
with beam.Pipeline(options=beam.options.pipeline_options.PipelineOptions(
pickle_library='cloudpickle')) as p:
_ = p | YamlTransform(
'''
type: composite
transforms:
- type: Create
name: Create1
config:
elements:
- {id: '1', name: 'Alice', age: 30}
- {id: '2', name: 'Bob', age: 25}
- type: Create
name: Create2
config:
elements:
- {id: '3', name: 'Charlie'}
- {id: '4', name: 'Diana'}
- type: Flatten
input: [Create1, Create2]
- type: AssertEqual
input: Flatten
config:
elements:
- {id: '1', name: 'Alice', age: 30}
- {id: '2', name: 'Bob', age: 25}
- {id: '3', name: 'Charlie'}
- {id: '4', name: 'Diana'}
''')
def test_flatten_unifies_different_types(self):
"""Test that Flatten correctly unifies schemas with different
field types."""
with beam.Pipeline(options=beam.options.pipeline_options.PipelineOptions(
pickle_library='cloudpickle')) as p:
_ = p | YamlTransform(
'''
type: composite
transforms:
- type: Create
name: Create1
config:
elements:
- {id: 1, value: 100}
- {id: 2, value: 200}
- type: Create
name: Create2
config:
elements:
- {id: '3', value: 'text'}
- {id: '4', value: 'data'}
- type: Flatten
input: [Create1, Create2]
- type: AssertEqual
input: Flatten
config:
elements:
- {id: 1, value: 100}
- {id: 2, value: 200}
- {id: '3', value: 'text'}
- {id: '4', value: 'data'}
''')
def test_flatten_unifies_list_fields(self):
"""Test that Flatten correctly unifies schemas with list fields."""
with beam.Pipeline(options=beam.options.pipeline_options.PipelineOptions(
pickle_library='cloudpickle')) as p:
_ = p | YamlTransform(
'''
type: composite
transforms:
- type: Create
name: Create1
config:
elements:
- {id: '1', tags: ['red', 'blue']}
- {id: '2', tags: ['green']}
- type: Create
name: Create2
config:
elements:
- {id: '3', tags: ['yellow', 'purple', 'orange']}
- {id: '4', tags: []}
- type: Flatten
input: [Create1, Create2]
- type: AssertEqual
input: Flatten
config:
elements:
- {id: '1', tags: ['red', 'blue']}
- {id: '2', tags: ['green']}
- {id: '3', tags: ['yellow', 'purple', 'orange']}
- {id: '4', tags: []}
''')
def test_flatten_unifies_with_missing_fields(self):
"""Test that Flatten correctly unifies schemas when some inputs have
missing fields."""
with beam.Pipeline(options=beam.options.pipeline_options.PipelineOptions(
pickle_library='cloudpickle')) as p:
_ = p | YamlTransform(
'''
type: composite
transforms:
- type: Create
name: Create1
config:
elements:
- {id: '1', name: 'Alice', department: 'Engineering',
salary: 75000}
- {id: '2', name: 'Bob', department: 'Marketing',
salary: 65000}
- type: Create
name: Create2
config:
elements:
- {id: '3', name: 'Charlie', department: 'Sales'}
- {id: '4', name: 'Diana'}
- type: Flatten
input: [Create1, Create2]
- type: AssertEqual
input: Flatten
config:
elements:
- {id: '1', name: 'Alice', department: 'Engineering',
salary: 75000}
- {id: '2', name: 'Bob', department: 'Marketing',
salary: 65000}
- {id: '3', name: 'Charlie', department: 'Sales'}
- {id: '4', name: 'Diana'}
''')
def test_flatten_unifies_complex_mixed_schemas(self):
"""Test that Flatten correctly unifies complex mixed
schemas."""
with beam.Pipeline(options=beam.options.pipeline_options.PipelineOptions(
pickle_library='cloudpickle')) as p:
_ = p | YamlTransform(
'''
type: composite
transforms:
- type: Create
name: Create1
config:
elements:
- {id: 1, name: 'Product A', price: 29.99,
categories: ['electronics', 'gadgets']}
- {id: 2, name: 'Product B', price: 15.50,
categories: ['books']}
- type: Create
name: Create2
config:
elements:
- {id: 3, name: 'Product C', categories: ['clothing']}
- {id: 4, name: 'Product D', price: 99.99}
- type: Create
name: Create3
config:
elements:
- {id: 5, name: 'Product E', price: 5.00,
categories: []}
- type: Flatten
input: [Create1, Create2, Create3]
- type: AssertEqual
input: Flatten
config:
elements:
- {id: 1, name: 'Product A', price: 29.99,
categories: ['electronics', 'gadgets']}
- {id: 2, name: 'Product B', price: 15.50,
categories: ['books']}
- {id: 3, name: 'Product C', categories: ['clothing']}
- {id: 4, name: 'Product D', price: 99.99}
- {id: 5, name: 'Product E', price: 5.00,
categories: []}
''')
def test_output_schema_success(self):
"""Test that optional output_schema works."""
with beam.Pipeline(options=beam.options.pipeline_options.PipelineOptions(
pickle_library='cloudpickle')) as p:
_ = p | YamlTransform(
'''
type: composite
transforms:
- type: Create
name: MyCreate
config:
elements:
- {sdk: 'Beam', year: 2016}
- {sdk: 'Flink', year: 2015}
output_schema:
type: object
properties:
sdk:
type: string
year:
type: integer
- type: AssertEqual
name: CheckGood
input: MyCreate
config:
elements:
- {sdk: 'Beam', year: 2016}
- {sdk: 'Flink', year: 2015}
''')
def test_output_schema_fails(self):
"""
Test that optional output_schema works by failing the pipeline since main
transform doesn't have error_handling config.
"""
with self.assertRaises(Exception) as e:
with beam.Pipeline(options=beam.options.pipeline_options.PipelineOptions(
pickle_library='cloudpickle')) as p:
_ = p | YamlTransform(
'''
type: composite
transforms:
- type: Create
name: MyCreate
config:
elements:
- {sdk: 'Beam', year: 2016}
- {sdk: 'Spark', year: 'date'}
- {sdk: 'Flink', year: 2015}
output_schema:
type: object
properties:
sdk:
type: string
year:
type: integer
- type: AssertEqual
name: CheckGood
input: MyCreate
config:
elements:
- {sdk: 'Beam', year: 2016}
- {sdk: 'Flink', year: 2015}
''')
self.assertIn("'date' is not of type 'integer'", str(e.exception))
def test_output_schema_with_main_transform_error_handling_success(self):
"""Test that optional output_schema works in conjunction with main transform
error handling."""
with beam.Pipeline(options=beam.options.pipeline_options.PipelineOptions(
pickle_library='cloudpickle')) as p:
_ = p | YamlTransform(
'''
type: composite
transforms:
- type: Create
name: CreateVisits
config:
elements:
- {user: alice, timestamp: "not-valid"}
- {user: bob, timestamp: 3}
- type: AssignTimestamps
input: CreateVisits
config:
timestamp: timestamp
error_handling:
output: invalid_rows
output_schema:
type: object
properties:
user:
type: string
timestamp:
type: boolean
- type: MapToFields
name: ExtractInvalidTimestamp
input: AssignTimestamps.invalid_rows
config:
language: python
fields:
user: "element.user"
timestamp: "element.timestamp"
- type: AssertEqual
input: ExtractInvalidTimestamp
config:
elements:
- {user: "alice", timestamp: "not-valid"}
- {user: bob, timestamp: 3}
- type: AssertEqual
input: AssignTimestamps
config:
elements: []
''')
class ErrorHandlingTest(unittest.TestCase):
def test_error_handling_outputs(self):
with beam.Pipeline(options=beam.options.pipeline_options.PipelineOptions(
pickle_library='cloudpickle')) as p:
result = p | YamlTransform(
'''
type: composite
transforms:
- type: Create
config:
elements: ['a', 'b', 'biiiiig']
- type: SizeLimiter
input: Create
config:
limit: 5
error_handling:
output: errors
- name: TrimErrors
type: PyMap
input: SizeLimiter.errors
config:
fn: "lambda x: x[1][1]"
output:
good: SizeLimiter
bad: TrimErrors
''',
providers=TEST_PROVIDERS)
assert_that(result['good'], equal_to(['a', 'b']), label="CheckGood")
assert_that(result['bad'], equal_to(["ValueError('biiiiig')"]))
def test_strip_error_metadata(self):
with beam.Pipeline(options=beam.options.pipeline_options.PipelineOptions(
pickle_library='cloudpickle')) as p:
result = p | YamlTransform(
'''
type: composite
transforms:
- type: Create
config:
elements: ['a', 'b', 'biiiiig']
- type: SizeLimiter
input: Create
config:
limit: 5
error_handling:
output: errors
- type: StripErrorMetadata
name: StripErrorMetadata1
input: SizeLimiter.errors
- type: MapToFields
input: Create
config:
language: python
fields:
out: "1.0/(1-len(element))"
error_handling:
output: errors
- type: StripErrorMetadata
name: StripErrorMetadata2
input: MapToFields.errors
output:
good: SizeLimiter
bad1: StripErrorMetadata1
bad2: StripErrorMetadata2
''',
providers=TEST_PROVIDERS)
assert_that(result['good'], equal_to(['a', 'b']), label="CheckGood")
assert_that(
result['bad1'] | beam.Map(lambda x: x.element), equal_to(['biiiiig']))
assert_that(
result['bad2'] | beam.Map(lambda x: x.element), equal_to(['a', 'b']))
def test_must_handle_error_output(self):
with self.assertRaisesRegex(Exception, 'Unconsumed error output .*line 7'):
with beam.Pipeline(options=beam.options.pipeline_options.PipelineOptions(
pickle_library='cloudpickle')) as p:
_ = p | YamlTransform(
'''
type: composite
transforms:
- type: Create
config:
elements: ['a', 'b', 'biiiiig']
- type: SizeLimiter
input: Create
config:
limit: 5
error_handling:
output: errors
''',
providers=TEST_PROVIDERS)
def test_error_handling_log_combined_errors(self):
with beam.Pipeline(options=beam.options.pipeline_options.PipelineOptions(
pickle_library='cloudpickle')) as p:
result = p | YamlTransform(
'''
type: composite
transforms:
- type: Create
name: Input1
config:
elements: [1, 2, 0]
- type: Create
name: Input2
config:
elements: [3, 'a', 5]
- type: MapToFields
name: Inverse
input: Input1
config:
language: python
fields:
inverse: "1 / element"
error_handling:
output: errors
- type: MapToFields
name: Square
input: Input2
config:
language: python
fields:
square: "element * element"
error_handling:
output: errors
- type: LogForTesting
input:
- Inverse.errors
- Square.errors
- type: Flatten
name: GoodData
input:
- Inverse
- Square
output: GoodData
''',
providers=TEST_PROVIDERS)
assert_that(
result,
equal_to([
beam.Row(inverse=1.0, square=None),
beam.Row(inverse=0.5, square=None),
beam.Row(square=9, inverse=None),
beam.Row(square=25, inverse=None)
]))
def test_mapping_errors(self):
with beam.Pipeline(options=beam.options.pipeline_options.PipelineOptions(
pickle_library='cloudpickle')) as p:
result = p | YamlTransform(
'''
type: composite
transforms:
- type: Create
config:
elements: [0, 1, 2, 4]
- type: MapToFields
name: ToRow