|
18 | 18 | import io |
19 | 19 | import json |
20 | 20 | import logging |
| 21 | +import os |
| 22 | +import tempfile |
21 | 23 | import unittest |
22 | 24 |
|
23 | 25 | import fastavro |
@@ -543,6 +545,122 @@ def test_read_proto(self): |
543 | 545 | assert_that(result, equal_to(data)) |
544 | 546 |
|
545 | 547 |
|
| 548 | +class YamlMatchAllTest(unittest.TestCase): |
| 549 | + def test_match_all_simple(self): |
| 550 | + with tempfile.TemporaryDirectory() as temp_dir: |
| 551 | + file1 = os.path.join(temp_dir, 'file1.txt') |
| 552 | + file2 = os.path.join(temp_dir, 'file2.txt') |
| 553 | + for f in [file1, file2]: |
| 554 | + with open(f, 'w') as fout: |
| 555 | + fout.write('data') |
| 556 | + |
| 557 | + with beam.Pipeline(options=beam.options.pipeline_options.PipelineOptions( |
| 558 | + pickle_library='cloudpickle')) as p: |
| 559 | + result = ( |
| 560 | + p |
| 561 | + | beam.Create( |
| 562 | + [beam.Row(pattern=os.path.join(temp_dir, 'file*.txt'))]) |
| 563 | + | YamlTransform( |
| 564 | + ''' |
| 565 | + type: MatchAll |
| 566 | + config: |
| 567 | + file_pattern: pattern |
| 568 | + ''')) |
| 569 | + paths = result | beam.Map(lambda row: row.path) |
| 570 | + assert_that(paths, equal_to([file1, file2])) |
| 571 | + |
| 572 | + def test_match_all_single_field_default(self): |
| 573 | + with tempfile.TemporaryDirectory() as temp_dir: |
| 574 | + file1 = os.path.join(temp_dir, 'file1.txt') |
| 575 | + with open(file1, 'w') as fout: |
| 576 | + fout.write('data') |
| 577 | + |
| 578 | + with beam.Pipeline(options=beam.options.pipeline_options.PipelineOptions( |
| 579 | + pickle_library='cloudpickle')) as p: |
| 580 | + result = ( |
| 581 | + p |
| 582 | + | beam.Create([beam.Row(my_sole_pattern=file1)]) |
| 583 | + | YamlTransform( |
| 584 | + ''' |
| 585 | + type: MatchAll |
| 586 | + ''')) |
| 587 | + paths = result | beam.Map(lambda row: row.path) |
| 588 | + assert_that(paths, equal_to([file1])) |
| 589 | + |
| 590 | + def test_match_all_multiple_fields_error(self): |
| 591 | + with self.assertRaises(Exception): |
| 592 | + with beam.Pipeline(options=beam.options.pipeline_options.PipelineOptions( |
| 593 | + pickle_library='cloudpickle')) as p: |
| 594 | + _ = ( |
| 595 | + p |
| 596 | + | beam.Create([beam.Row(pattern='foo', other_field='bar')]) |
| 597 | + | YamlTransform( |
| 598 | + ''' |
| 599 | + type: MatchAll |
| 600 | + ''')) |
| 601 | + |
| 602 | + def test_match_all_empty_match_disallow_error(self): |
| 603 | + with self.assertRaises(Exception): |
| 604 | + with beam.Pipeline(options=beam.options.pipeline_options.PipelineOptions( |
| 605 | + pickle_library='cloudpickle')) as p: |
| 606 | + _ = ( |
| 607 | + p |
| 608 | + | beam.Create([beam.Row(pattern='does_not_exist*.txt')]) |
| 609 | + | YamlTransform( |
| 610 | + ''' |
| 611 | + type: MatchAll |
| 612 | + config: |
| 613 | + empty_match_treatment: DISALLOW |
| 614 | + ''')) |
| 615 | + |
| 616 | + def test_match_all_invalid_field_error(self): |
| 617 | + with self.assertRaisesRegex( |
| 618 | + ValueError, "Field 'invalid_field' not found in input schema fields"): |
| 619 | + with beam.Pipeline(options=beam.options.pipeline_options.PipelineOptions( |
| 620 | + pickle_library='cloudpickle')) as p: |
| 621 | + _ = ( |
| 622 | + p |
| 623 | + | beam.Create([beam.Row(pattern='foo')]) |
| 624 | + | YamlTransform( |
| 625 | + ''' |
| 626 | + type: MatchAll |
| 627 | + config: |
| 628 | + file_pattern: invalid_field |
| 629 | + ''')) |
| 630 | + |
| 631 | + def test_match_all_none_timestamp(self): |
| 632 | + from apache_beam.io.filesystem import FileMetadata |
| 633 | + |
| 634 | + class MockMatchAll(beam.PTransform): |
| 635 | + def expand(self, pcoll): |
| 636 | + return pcoll.pipeline | beam.Create([ |
| 637 | + FileMetadata( |
| 638 | + path='file.txt', |
| 639 | + size_in_bytes=100, |
| 640 | + last_updated_in_seconds=None) |
| 641 | + ]) |
| 642 | + |
| 643 | + with mock.patch('apache_beam.io.fileio.MatchAll', |
| 644 | + return_value=MockMatchAll()): |
| 645 | + with beam.Pipeline(options=beam.options.pipeline_options.PipelineOptions( |
| 646 | + pickle_library='cloudpickle')) as p: |
| 647 | + result = ( |
| 648 | + p |
| 649 | + | beam.Create([beam.Row(pattern='file.txt')]) |
| 650 | + | YamlTransform( |
| 651 | + ''' |
| 652 | + type: MatchAll |
| 653 | + ''')) |
| 654 | + assert_that( |
| 655 | + result, |
| 656 | + equal_to([ |
| 657 | + beam.Row( |
| 658 | + path='file.txt', |
| 659 | + size_in_bytes=100, |
| 660 | + last_updated_in_seconds=None) |
| 661 | + ])) |
| 662 | + |
| 663 | + |
546 | 664 | if __name__ == '__main__': |
547 | 665 | logging.getLogger().setLevel(logging.INFO) |
548 | 666 | unittest.main() |
0 commit comments