blob: d2c3420239ab9540d47060722a13784685bbaf95 [file]
#
# 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 unittest
import yaml
from apache_beam.cli import pipe_spec
from apache_beam.cli.pipe_spec import PipeSpecError
from apache_beam.cli.pipe_spec import build_pipe_spec
CSV_TO_PARQUET = '''
pipeline:
type: chain
transforms:
- type: ReadFromCsv
config: {path: users.csv}
- type: WriteToParquet
config: {path: out/users.parquet}
'''
class BuildPipeSpecTest(unittest.TestCase):
def test_csv_to_parquet(self):
pipe = build_pipe_spec('users.csv', 'out/users.parquet')
self.assertEqual(pipe.spec, yaml.safe_load(CSV_TO_PARQUET))
self.assertEqual(pipe.output_prefix, 'out/users.parquet')
def test_formats_come_from_extensions(self):
for path, read_transform, write_transform in [
('a.csv', 'ReadFromCsv', 'WriteToCsv'),
('logs/*.json', 'ReadFromJson', 'WriteToJson'),
('a.jsonl', 'ReadFromJson', 'WriteToJson'),
('gs://bucket/a.parquet', 'ReadFromParquet', 'WriteToParquet'),
('GS://bucket/a.parquet', 'ReadFromParquet', 'WriteToParquet'),
('a.AVRO', 'ReadFromAvro', 'WriteToAvro'),
# Output of an earlier command, read back.
('out/users.parquet*', 'ReadFromParquet', 'WriteToParquet'),
('out/users.avro-*', 'ReadFromAvro', 'WriteToAvro'),
('out/users.json-00000-of-00001', 'ReadFromJson', 'WriteToJson'),
# Windows backslash paths.
(r'C:\Users\Ada\users.csv', 'ReadFromCsv', 'WriteToCsv'),
(
r'C:\Users\Ada\out\users.parquet*',
'ReadFromParquet',
'WriteToParquet'),
]:
with self.subTest(path=path):
pipe = build_pipe_spec(path, path)
self.assertEqual(
[t['type'] for t in pipe.spec['pipeline']['transforms']],
[read_transform, write_transform])
def test_explicit_formats_override_extensions(self):
pipe = build_pipe_spec('data.txt', 'out', 'csv', 'json')
self.assertEqual([t['type'] for t in pipe.spec['pipeline']['transforms']],
['ReadFromCsv', 'WriteToJson'])
def test_errors_name_the_fix(self):
cases = [
(('users', 'out.csv'), {}, 'add --from-format'),
((r'C:\Users\ada.smith\users', 'out.csv'), {}, 'add --from-format'),
(('a.csv', 'out/users'), {}, 'add --to-format'),
(('a.csv', 'b.csv'), {
'from_format': 'xml'
},
"unsupported format 'xml' .* use one of: csv, json, parquet, avro"),
(('a.csv', 'b.csv'), {
'to_format': 'xml'
},
"unsupported format 'xml' .* use one of: csv, json, parquet, avro"),
(('kafka://broker:9092/topic', 'b.csv'), {}, 'unsupported location'),
]
for args, kwargs, message in cases:
with self.subTest(args=args, kwargs=kwargs):
with self.assertRaisesRegex(PipeSpecError, message):
build_pipe_spec(*args, **kwargs)
class ToYamlTest(unittest.TestCase):
def test_starts_with_the_command_and_round_trips(self):
for src, dst in [
('users.csv', 'out/users.avro'),
(
r'C:\Users\runneradmin\AppData\Local\Temp\tmp123\users.csv',
r'C:\Users\runneradmin\AppData\Local\Temp\tmp123\out\users.json',
),
]:
with self.subTest(src=src):
pipe = build_pipe_spec(src, dst)
text = pipe_spec.to_yaml(pipe.spec, ['--from', src, '--to', dst])
self.assertTrue(text.startswith('# Generated by: beam pipe --from '))
self.assertEqual(yaml.safe_load(text), pipe.spec)
if __name__ == '__main__':
unittest.main()