You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@beam.apache.org by "bzablocki (via GitHub)" <gi...@apache.org> on 2023/07/03 09:53:48 UTC
[GitHub] [beam] bzablocki opened a new pull request, #27338: [yaml] yaml_transform.py unit tests
bzablocki opened a new pull request, #27338:
URL: https://github.com/apache/beam/pull/27338
Adding more Unit Tests covering yaml_transform.py
Includes Unit Tests from unmerged https://github.com/apache/beam/pull/27086, so please take a look at that first.3
------------------------
Thank you for your contribution! Follow this checklist to help us incorporate your contribution quickly and easily:
- [ ] Mention the appropriate issue in your description (for example: `addresses #123`), if applicable. This will automatically add a link to the pull request in the issue. If you would like the issue to automatically close on merging the pull request, comment `fixes #<ISSUE NUMBER>` instead.
- [ ] Update `CHANGES.md` with noteworthy changes.
- [ ] If this contribution is large, please file an Apache [Individual Contributor License Agreement](https://www.apache.org/licenses/icla.pdf).
See the [Contributor Guide](https://beam.apache.org/contribute) for more tips on [how to make review process smoother](https://beam.apache.org/contribute/get-started-contributing/#make-the-reviewers-job-easier).
To check the build health, please visit [https://github.com/apache/beam/blob/master/.test-infra/BUILD_STATUS.md](https://github.com/apache/beam/blob/master/.test-infra/BUILD_STATUS.md)
GitHub Actions Tests Status (on master branch)
------------------------------------------------------------------------------------------------
[](https://github.com/apache/beam/actions?query=workflow%3A%22Build+python+source+distribution+and+wheels%22+branch%3Amaster+event%3Aschedule)
[](https://github.com/apache/beam/actions?query=workflow%3A%22Python+Tests%22+branch%3Amaster+event%3Aschedule)
[](https://github.com/apache/beam/actions?query=workflow%3A%22Java+Tests%22+branch%3Amaster+event%3Aschedule)
[](https://github.com/apache/beam/actions?query=workflow%3A%22Go+tests%22+branch%3Amaster+event%3Aschedule)
See [CI.md](https://github.com/apache/beam/blob/master/CI.md) for more information about GitHub Actions CI.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@beam.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [beam] robertwb commented on a diff in pull request #27338: [yaml] yaml_transform.py unit tests
Posted by "robertwb (via GitHub)" <gi...@apache.org>.
robertwb commented on code in PR #27338:
URL: https://github.com/apache/beam/pull/27338#discussion_r1260357066
##########
sdks/python/apache_beam/yaml/yaml_transform_scope_test.py:
##########
@@ -0,0 +1,213 @@
+#
+# 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 logging
+import unittest
+
+import yaml
+
+import apache_beam as beam
+from apache_beam.yaml import yaml_provider
+from apache_beam.yaml.yaml_transform import SafeLineLoader
+from apache_beam.yaml.yaml_transform import Scope
+
+
+class ScopeTest(unittest.TestCase):
+ def get_scope_by_spec(self, p, spec):
+ spec = yaml.load(spec, Loader=SafeLineLoader)
+
+ scope = Scope(
+ beam.pvalue.PBegin(p), {},
+ spec['transforms'],
+ yaml_provider.standard_providers(), {})
+ return scope, spec
+
+ def test_get_pcollection_input(self):
+ with beam.Pipeline(options=beam.options.pipeline_options.PipelineOptions(
+ pickle_library='cloudpickle')) as p:
+ elements = p | beam.Create(range(3))
+ scope = Scope(
+ p, {'input': elements},
+ transforms=[],
+ providers=yaml_provider.standard_providers(),
+ input_providers={})
+
+ result = scope.get_pcollection('input')
+ self.assertEqual("PCollection[Create/Map(decode).None]", str(result))
+
+ def test_get_pcollection_output(self):
+ with beam.Pipeline(options=beam.options.pipeline_options.PipelineOptions(
+ pickle_library='cloudpickle')) as p:
+ spec = '''
+ transforms:
+ - type: Create
+ elements: [0, 1, 3, 4]
+ - type: PyMap
+ name: Square
+ input: Create
+ fn: "lambda x: x*x"
+ '''
+
+ scope, spec = self.get_scope_by_spec(p, spec)
+
+ result = scope.get_pcollection("Create")
Review Comment:
Nit: rather than re-assigning result, just do
```
self.assertEqual(
"PCollection[Create/Map(decode).None]",
str(scope.get_pcollection("Create")))
```
##########
sdks/python/apache_beam/yaml/yaml_transform_scope_test.py:
##########
@@ -0,0 +1,213 @@
+#
+# 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 logging
+import unittest
+
+import yaml
+
+import apache_beam as beam
+from apache_beam.yaml import yaml_provider
+from apache_beam.yaml.yaml_transform import SafeLineLoader
+from apache_beam.yaml.yaml_transform import Scope
+
+
+class ScopeTest(unittest.TestCase):
+ def get_scope_by_spec(self, p, spec):
+ spec = yaml.load(spec, Loader=SafeLineLoader)
+
+ scope = Scope(
+ beam.pvalue.PBegin(p), {},
+ spec['transforms'],
+ yaml_provider.standard_providers(), {})
+ return scope, spec
+
+ def test_get_pcollection_input(self):
+ with beam.Pipeline(options=beam.options.pipeline_options.PipelineOptions(
+ pickle_library='cloudpickle')) as p:
+ elements = p | beam.Create(range(3))
+ scope = Scope(
+ p, {'input': elements},
+ transforms=[],
+ providers=yaml_provider.standard_providers(),
+ input_providers={})
+
+ result = scope.get_pcollection('input')
+ self.assertEqual("PCollection[Create/Map(decode).None]", str(result))
+
+ def test_get_pcollection_output(self):
+ with beam.Pipeline(options=beam.options.pipeline_options.PipelineOptions(
+ pickle_library='cloudpickle')) as p:
+ spec = '''
+ transforms:
+ - type: Create
+ elements: [0, 1, 3, 4]
+ - type: PyMap
+ name: Square
+ input: Create
+ fn: "lambda x: x*x"
+ '''
+
+ scope, spec = self.get_scope_by_spec(p, spec)
+
+ result = scope.get_pcollection("Create")
+ self.assertEqual("PCollection[Create/Map(decode).None]", str(result))
+
+ result = scope.get_pcollection("Square")
+ self.assertEqual("PCollection[Square.None]", str(result))
+
+ result = scope.get_pcollection("PyMap")
+ self.assertEqual("PCollection[Square.None]", str(result))
Review Comment:
Maybe test getting the same PCollection twice returns the exact same PCollection?
##########
sdks/python/apache_beam/yaml/yaml_transform_scope_test.py:
##########
@@ -0,0 +1,213 @@
+#
+# 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 logging
+import unittest
+
+import yaml
+
+import apache_beam as beam
+from apache_beam.yaml import yaml_provider
+from apache_beam.yaml.yaml_transform import SafeLineLoader
+from apache_beam.yaml.yaml_transform import Scope
+
+
+class ScopeTest(unittest.TestCase):
+ def get_scope_by_spec(self, p, spec):
+ spec = yaml.load(spec, Loader=SafeLineLoader)
+
+ scope = Scope(
+ beam.pvalue.PBegin(p), {},
+ spec['transforms'],
+ yaml_provider.standard_providers(), {})
+ return scope, spec
+
+ def test_get_pcollection_input(self):
+ with beam.Pipeline(options=beam.options.pipeline_options.PipelineOptions(
+ pickle_library='cloudpickle')) as p:
+ elements = p | beam.Create(range(3))
+ scope = Scope(
+ p, {'input': elements},
+ transforms=[],
+ providers=yaml_provider.standard_providers(),
+ input_providers={})
+
+ result = scope.get_pcollection('input')
+ self.assertEqual("PCollection[Create/Map(decode).None]", str(result))
+
+ def test_get_pcollection_output(self):
+ with beam.Pipeline(options=beam.options.pipeline_options.PipelineOptions(
+ pickle_library='cloudpickle')) as p:
+ spec = '''
+ transforms:
+ - type: Create
+ elements: [0, 1, 3, 4]
+ - type: PyMap
+ name: Square
+ input: Create
+ fn: "lambda x: x*x"
+ '''
+
+ scope, spec = self.get_scope_by_spec(p, spec)
+
+ result = scope.get_pcollection("Create")
+ self.assertEqual("PCollection[Create/Map(decode).None]", str(result))
+
+ result = scope.get_pcollection("Square")
+ self.assertEqual("PCollection[Square.None]", str(result))
+
+ result = scope.get_pcollection("PyMap")
+ self.assertEqual("PCollection[Square.None]", str(result))
+
+ def test_get_pcollection_ambigous_name(self):
+ with beam.Pipeline(options=beam.options.pipeline_options.PipelineOptions(
+ pickle_library='cloudpickle')) as p:
+ spec = '''
+ transforms:
+ - type: Create
+ elements: [0, 1, 3, 4]
+ - type: PyMap
+ fn: "lambda x: x*x"
+ - type: PyMap
+ fn: "lambda x: x*x*x"
+ '''
+
+ scope, spec = self.get_scope_by_spec(p, spec)
+
+ with self.assertRaisesRegex(ValueError, r'Ambiguous.*'):
+ scope.get_pcollection("PyMap")
+
+ def test_unique_name_by_name(self):
+ with beam.Pipeline(options=beam.options.pipeline_options.PipelineOptions(
+ pickle_library='cloudpickle')) as p:
+ spec = '''
+ transforms:
+ - type: Create
+ name: MyElements
+ elements: [0, 1, 3, 4]
+ '''
+ scope, spec = self.get_scope_by_spec(p, spec)
+
+ spec_transform = spec['transforms'][0]
+ p_transform = scope.create_ptransform(spec_transform, [])
+
+ result = scope.unique_name(spec_transform, p_transform)
+ self.assertEqual(result, "MyElements")
+ self.assertIn("MyElements", scope._seen_names)
+
+ result = scope.unique_name(spec_transform, p_transform)
Review Comment:
We probably shouldn't be giving a new name when passing the same spec twice, but I guess that's not a realistic scenario. TBD if we want to disallow transforms with the same explicit name.
##########
sdks/python/apache_beam/yaml/yaml_transform_scope_test.py:
##########
@@ -0,0 +1,213 @@
+#
+# 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 logging
+import unittest
+
+import yaml
+
+import apache_beam as beam
+from apache_beam.yaml import yaml_provider
+from apache_beam.yaml.yaml_transform import SafeLineLoader
+from apache_beam.yaml.yaml_transform import Scope
+
+
+class ScopeTest(unittest.TestCase):
+ def get_scope_by_spec(self, p, spec):
+ spec = yaml.load(spec, Loader=SafeLineLoader)
+
+ scope = Scope(
+ beam.pvalue.PBegin(p), {},
+ spec['transforms'],
+ yaml_provider.standard_providers(), {})
+ return scope, spec
+
+ def test_get_pcollection_input(self):
+ with beam.Pipeline(options=beam.options.pipeline_options.PipelineOptions(
+ pickle_library='cloudpickle')) as p:
+ elements = p | beam.Create(range(3))
+ scope = Scope(
+ p, {'input': elements},
+ transforms=[],
+ providers=yaml_provider.standard_providers(),
+ input_providers={})
+
+ result = scope.get_pcollection('input')
+ self.assertEqual("PCollection[Create/Map(decode).None]", str(result))
+
+ def test_get_pcollection_output(self):
+ with beam.Pipeline(options=beam.options.pipeline_options.PipelineOptions(
+ pickle_library='cloudpickle')) as p:
+ spec = '''
+ transforms:
+ - type: Create
+ elements: [0, 1, 3, 4]
+ - type: PyMap
+ name: Square
+ input: Create
+ fn: "lambda x: x*x"
+ '''
+
+ scope, spec = self.get_scope_by_spec(p, spec)
+
+ result = scope.get_pcollection("Create")
+ self.assertEqual("PCollection[Create/Map(decode).None]", str(result))
+
+ result = scope.get_pcollection("Square")
+ self.assertEqual("PCollection[Square.None]", str(result))
+
+ result = scope.get_pcollection("PyMap")
+ self.assertEqual("PCollection[Square.None]", str(result))
+
+ def test_get_pcollection_ambigous_name(self):
+ with beam.Pipeline(options=beam.options.pipeline_options.PipelineOptions(
+ pickle_library='cloudpickle')) as p:
+ spec = '''
+ transforms:
+ - type: Create
+ elements: [0, 1, 3, 4]
+ - type: PyMap
+ fn: "lambda x: x*x"
+ - type: PyMap
+ fn: "lambda x: x*x*x"
+ '''
+
+ scope, spec = self.get_scope_by_spec(p, spec)
+
+ with self.assertRaisesRegex(ValueError, r'Ambiguous.*'):
+ scope.get_pcollection("PyMap")
+
+ def test_unique_name_by_name(self):
+ with beam.Pipeline(options=beam.options.pipeline_options.PipelineOptions(
+ pickle_library='cloudpickle')) as p:
+ spec = '''
+ transforms:
+ - type: Create
+ name: MyElements
+ elements: [0, 1, 3, 4]
+ '''
+ scope, spec = self.get_scope_by_spec(p, spec)
+
+ spec_transform = spec['transforms'][0]
+ p_transform = scope.create_ptransform(spec_transform, [])
+
+ result = scope.unique_name(spec_transform, p_transform)
+ self.assertEqual(result, "MyElements")
+ self.assertIn("MyElements", scope._seen_names)
+
+ result = scope.unique_name(spec_transform, p_transform)
+ self.assertIn("MyElements@3", scope._seen_names)
+ self.assertEqual(result, "MyElements@3")
+
+ def test_unique_name_by_label(self):
+ with beam.Pipeline(options=beam.options.pipeline_options.PipelineOptions(
+ pickle_library='cloudpickle')) as p:
+ spec = '''
+ transforms:
+ - type: Create
+ elements: [0, 1, 3, 4]
+ '''
+ scope, spec = self.get_scope_by_spec(p, spec)
+
+ spec_transform = spec['transforms'][0]
+ p_transform = scope.create_ptransform(spec_transform, [])
+
+ result = scope.unique_name(spec_transform, p_transform)
+ self.assertEqual(result, "Create")
+ self.assertIn("Create", scope._seen_names)
+
+ result = scope.unique_name(spec_transform, p_transform)
Review Comment:
Again, we shouldn't be this on the same transform twice.
##########
sdks/python/apache_beam/yaml/yaml_transform_ut_test.py:
##########
@@ -0,0 +1,301 @@
+#
Review Comment:
I'm assuming you'll merge with #27086 and move rather than duplicate these?
##########
sdks/python/apache_beam/yaml/yaml_transform_scope_test.py:
##########
@@ -0,0 +1,213 @@
+#
+# 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 logging
+import unittest
+
+import yaml
+
+import apache_beam as beam
+from apache_beam.yaml import yaml_provider
+from apache_beam.yaml.yaml_transform import SafeLineLoader
+from apache_beam.yaml.yaml_transform import Scope
+
+
+class ScopeTest(unittest.TestCase):
+ def get_scope_by_spec(self, p, spec):
+ spec = yaml.load(spec, Loader=SafeLineLoader)
+
+ scope = Scope(
+ beam.pvalue.PBegin(p), {},
+ spec['transforms'],
+ yaml_provider.standard_providers(), {})
+ return scope, spec
+
+ def test_get_pcollection_input(self):
+ with beam.Pipeline(options=beam.options.pipeline_options.PipelineOptions(
+ pickle_library='cloudpickle')) as p:
+ elements = p | beam.Create(range(3))
+ scope = Scope(
+ p, {'input': elements},
+ transforms=[],
+ providers=yaml_provider.standard_providers(),
+ input_providers={})
+
+ result = scope.get_pcollection('input')
+ self.assertEqual("PCollection[Create/Map(decode).None]", str(result))
+
+ def test_get_pcollection_output(self):
+ with beam.Pipeline(options=beam.options.pipeline_options.PipelineOptions(
+ pickle_library='cloudpickle')) as p:
+ spec = '''
+ transforms:
+ - type: Create
+ elements: [0, 1, 3, 4]
+ - type: PyMap
+ name: Square
+ input: Create
+ fn: "lambda x: x*x"
+ '''
+
+ scope, spec = self.get_scope_by_spec(p, spec)
+
+ result = scope.get_pcollection("Create")
+ self.assertEqual("PCollection[Create/Map(decode).None]", str(result))
+
+ result = scope.get_pcollection("Square")
+ self.assertEqual("PCollection[Square.None]", str(result))
+
+ result = scope.get_pcollection("PyMap")
+ self.assertEqual("PCollection[Square.None]", str(result))
+
+ def test_get_pcollection_ambigous_name(self):
+ with beam.Pipeline(options=beam.options.pipeline_options.PipelineOptions(
+ pickle_library='cloudpickle')) as p:
+ spec = '''
+ transforms:
+ - type: Create
+ elements: [0, 1, 3, 4]
+ - type: PyMap
+ fn: "lambda x: x*x"
+ - type: PyMap
+ fn: "lambda x: x*x*x"
+ '''
+
+ scope, spec = self.get_scope_by_spec(p, spec)
+
+ with self.assertRaisesRegex(ValueError, r'Ambiguous.*'):
Review Comment:
I'm on the fence whether this is redundant with the existing test_name_is_ambiguous test (though that could be strengthened to use assertRaisesRegex).
##########
sdks/python/apache_beam/yaml/yaml_transform_ut_test.py:
##########
@@ -0,0 +1,301 @@
+#
Review Comment:
Also, maybe call this file "_unit_test" rather than "ut_test" (took me a bit to figure out what "ut" was).
##########
sdks/python/apache_beam/yaml/yaml_transform_scope_test.py:
##########
@@ -0,0 +1,213 @@
+#
+# 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 logging
+import unittest
+
+import yaml
+
+import apache_beam as beam
+from apache_beam.yaml import yaml_provider
+from apache_beam.yaml.yaml_transform import SafeLineLoader
+from apache_beam.yaml.yaml_transform import Scope
+
+
+class ScopeTest(unittest.TestCase):
+ def get_scope_by_spec(self, p, spec):
+ spec = yaml.load(spec, Loader=SafeLineLoader)
+
+ scope = Scope(
+ beam.pvalue.PBegin(p), {},
+ spec['transforms'],
+ yaml_provider.standard_providers(), {})
+ return scope, spec
+
+ def test_get_pcollection_input(self):
+ with beam.Pipeline(options=beam.options.pipeline_options.PipelineOptions(
+ pickle_library='cloudpickle')) as p:
+ elements = p | beam.Create(range(3))
+ scope = Scope(
+ p, {'input': elements},
+ transforms=[],
+ providers=yaml_provider.standard_providers(),
+ input_providers={})
+
+ result = scope.get_pcollection('input')
+ self.assertEqual("PCollection[Create/Map(decode).None]", str(result))
+
+ def test_get_pcollection_output(self):
+ with beam.Pipeline(options=beam.options.pipeline_options.PipelineOptions(
+ pickle_library='cloudpickle')) as p:
+ spec = '''
+ transforms:
+ - type: Create
+ elements: [0, 1, 3, 4]
+ - type: PyMap
+ name: Square
+ input: Create
+ fn: "lambda x: x*x"
+ '''
+
+ scope, spec = self.get_scope_by_spec(p, spec)
+
+ result = scope.get_pcollection("Create")
+ self.assertEqual("PCollection[Create/Map(decode).None]", str(result))
+
+ result = scope.get_pcollection("Square")
+ self.assertEqual("PCollection[Square.None]", str(result))
+
+ result = scope.get_pcollection("PyMap")
+ self.assertEqual("PCollection[Square.None]", str(result))
+
+ def test_get_pcollection_ambigous_name(self):
+ with beam.Pipeline(options=beam.options.pipeline_options.PipelineOptions(
Review Comment:
Is this needed/used here?
##########
sdks/python/apache_beam/yaml/yaml_transform_ut_test.py:
##########
@@ -0,0 +1,301 @@
+#
+# 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 logging
+import unittest
+
+import yaml
+
+import apache_beam as beam
+from apache_beam.yaml import yaml_provider
+from apache_beam.yaml import yaml_transform
+from apache_beam.yaml.yaml_transform import LightweightScope
+from apache_beam.yaml.yaml_transform import SafeLineLoader
+from apache_beam.yaml.yaml_transform import Scope
+from apache_beam.yaml.yaml_transform import expand_composite_transform
+from apache_beam.yaml.yaml_transform import expand_leaf_transform
+from apache_beam.yaml.yaml_transform import pipeline_as_composite
+
+
+class YamlTransformTest(unittest.TestCase):
+ def test_only_element(self):
+ self.assertEqual(yaml_transform.only_element((1, )), 1)
+
+
+class SafeLineLoaderTest(unittest.TestCase):
+ def test_get_line(self):
+ pipeline_yaml = '''
+ type: composite
+ input:
+ elements: input
+ transforms:
+ - type: PyMap
+ name: Square
+ input: elements
+ fn: "lambda x: x * x"
+ - type: PyMap
+ name: Cube
+ input: elements
+ fn: "lambda x: x * x * x"
+ output:
+ Flatten
+ '''
+ spec = yaml.load(pipeline_yaml, Loader=SafeLineLoader)
+ self.assertEqual(SafeLineLoader.get_line(spec['type']), 2)
+ self.assertEqual(SafeLineLoader.get_line(spec['input']), 4)
+ self.assertEqual(SafeLineLoader.get_line(spec['transforms'][0]), 6)
+ self.assertEqual(SafeLineLoader.get_line(spec['transforms'][0]['type']), 6)
+ self.assertEqual(SafeLineLoader.get_line(spec['transforms'][0]['name']), 7)
+ self.assertEqual(SafeLineLoader.get_line(spec['transforms'][1]), 10)
+ self.assertEqual(SafeLineLoader.get_line(spec['output']), 15)
+ self.assertEqual(SafeLineLoader.get_line(spec['transforms']), "unknown")
+
+ def test_strip_metadata(self):
+ spec_yaml = '''
+ transforms:
+ - type: PyMap
+ name: Square
+ '''
+ spec = yaml.load(spec_yaml, Loader=SafeLineLoader)
+ stripped = SafeLineLoader.strip_metadata(spec['transforms'])
+
+ self.assertFalse(hasattr(stripped[0], '__line__'))
+ self.assertFalse(hasattr(stripped[0], '__uuid__'))
+
+ def test_strip_metadata_nothing_to_strip(self):
+ spec_yaml = 'prop: 123'
+ spec = yaml.load(spec_yaml, Loader=SafeLineLoader)
+ stripped = SafeLineLoader.strip_metadata(spec['prop'])
+
+ self.assertFalse(hasattr(stripped, '__line__'))
+ self.assertFalse(hasattr(stripped, '__uuid__'))
+
+
+class LightweightScopeTest(unittest.TestCase):
+ @staticmethod
+ def get_spec():
+ pipeline_yaml = '''
+ - type: PyMap
+ name: Square
+ input: elements
+ fn: "lambda x: x * x"
+ - type: PyMap
+ name: PyMap
+ input: elements
+ fn: "lambda x: x * x * x"
+ - type: Filter
+ name: FilterOutBigNumbers
+ input: PyMap
+ keep: "lambda x: x<100"
+ '''
+ return yaml.load(pipeline_yaml, Loader=SafeLineLoader)
+
+ def test_init(self):
+ spec = self.get_spec()
+ scope = LightweightScope(spec)
+ self.assertEqual(len(scope._transforms_by_uuid), 3)
+ self.assertCountEqual(
+ list(scope._uuid_by_name.keys()),
+ ["PyMap", "Square", "Filter", "FilterOutBigNumbers"])
+
+ def test_get_transform_id_and_output_name(self):
+ spec = self.get_spec()
+ scope = LightweightScope(spec)
+ transform_id, output = scope.get_transform_id_and_output_name("Square")
+ self.assertEqual(transform_id, spec[0]['__uuid__'])
+ self.assertEqual(output, None)
+
+ def test_get_transform_id_and_output_name_with_dot(self):
+ spec = self.get_spec()
+ scope = LightweightScope(spec)
+ transform_id, output = \
+ scope.get_transform_id_and_output_name("Square.OutputName")
+ self.assertEqual(transform_id, spec[0]['__uuid__'])
+ self.assertEqual(output, "OutputName")
+
+ def test_get_transform_id_by_uuid(self):
+ spec = self.get_spec()
+ scope = LightweightScope(spec)
+ transform_id = scope.get_transform_id(spec[0]['__uuid__'])
+ self.assertEqual(transform_id, spec[0]['__uuid__'])
+
+ def test_get_transform_id_by_unique_name(self):
+ spec = self.get_spec()
+ scope = LightweightScope(spec)
+ transform_id = scope.get_transform_id("Square")
+ self.assertEqual(transform_id, spec[0]['__uuid__'])
+
+ def test_get_transform_id_by_ambiguous_name(self):
+ spec = self.get_spec()
+ scope = LightweightScope(spec)
+ with self.assertRaisesRegex(ValueError, r'Ambiguous.*PyMap'):
+ scope.get_transform_id(scope.get_transform_id(spec[1]['name']))
+
+ def test_get_transform_id_by_unknown_name(self):
+ spec = self.get_spec()
+ scope = LightweightScope(spec)
+ with self.assertRaisesRegex(ValueError, r'Unknown.*NotExistingTransform'):
+ scope.get_transform_id("NotExistingTransform")
+
+
+def new_pipeline():
+ return beam.Pipeline(
+ options=beam.options.pipeline_options.PipelineOptions(
+ pickle_library='cloudpickle'))
+
+
+class MainTest(unittest.TestCase):
+ def get_scope_by_spec(self, p, spec, inputs=None):
+ if inputs is None:
+ inputs = {}
+ spec = yaml.load(spec, Loader=SafeLineLoader)
+
+ scope = Scope(
+ beam.pvalue.PBegin(p),
+ inputs,
+ spec['transforms'],
+ yaml_provider.standard_providers(), {})
+ return scope, spec
+
+ def test_expand_leaf_transform_with_input(self):
+ with new_pipeline() as p:
+ spec = '''
+ transforms:
+ - type: Create
+ elements: [0]
+ - type: PyMap
+ input: Create
+ fn: "lambda x: x*x"
+ '''
+ scope, spec = self.get_scope_by_spec(p, spec)
+ result = expand_leaf_transform(spec['transforms'][1], scope)
Review Comment:
It's a little unclear what you're trying to test here. I am a bit wary of making tests that are to tied to the particular implementation. Generally I think we want to focus on testing things that (1) are part of the public API and/or (2) invariants or behaviors that if broken would cause issues with the feature.
##########
sdks/python/apache_beam/yaml/yaml_transform_scope_test.py:
##########
@@ -0,0 +1,213 @@
+#
+# 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 logging
+import unittest
+
+import yaml
+
+import apache_beam as beam
+from apache_beam.yaml import yaml_provider
+from apache_beam.yaml.yaml_transform import SafeLineLoader
+from apache_beam.yaml.yaml_transform import Scope
+
+
+class ScopeTest(unittest.TestCase):
+ def get_scope_by_spec(self, p, spec):
+ spec = yaml.load(spec, Loader=SafeLineLoader)
+
+ scope = Scope(
+ beam.pvalue.PBegin(p), {},
+ spec['transforms'],
+ yaml_provider.standard_providers(), {})
+ return scope, spec
+
+ def test_get_pcollection_input(self):
+ with beam.Pipeline(options=beam.options.pipeline_options.PipelineOptions(
+ pickle_library='cloudpickle')) as p:
+ elements = p | beam.Create(range(3))
+ scope = Scope(
+ p, {'input': elements},
+ transforms=[],
+ providers=yaml_provider.standard_providers(),
+ input_providers={})
+
+ result = scope.get_pcollection('input')
+ self.assertEqual("PCollection[Create/Map(decode).None]", str(result))
+
+ def test_get_pcollection_output(self):
+ with beam.Pipeline(options=beam.options.pipeline_options.PipelineOptions(
+ pickle_library='cloudpickle')) as p:
+ spec = '''
+ transforms:
+ - type: Create
+ elements: [0, 1, 3, 4]
+ - type: PyMap
+ name: Square
+ input: Create
+ fn: "lambda x: x*x"
+ '''
+
+ scope, spec = self.get_scope_by_spec(p, spec)
+
+ result = scope.get_pcollection("Create")
+ self.assertEqual("PCollection[Create/Map(decode).None]", str(result))
+
+ result = scope.get_pcollection("Square")
+ self.assertEqual("PCollection[Square.None]", str(result))
+
+ result = scope.get_pcollection("PyMap")
+ self.assertEqual("PCollection[Square.None]", str(result))
+
+ def test_get_pcollection_ambigous_name(self):
+ with beam.Pipeline(options=beam.options.pipeline_options.PipelineOptions(
+ pickle_library='cloudpickle')) as p:
+ spec = '''
+ transforms:
+ - type: Create
+ elements: [0, 1, 3, 4]
+ - type: PyMap
+ fn: "lambda x: x*x"
+ - type: PyMap
+ fn: "lambda x: x*x*x"
+ '''
+
+ scope, spec = self.get_scope_by_spec(p, spec)
+
+ with self.assertRaisesRegex(ValueError, r'Ambiguous.*'):
+ scope.get_pcollection("PyMap")
+
+ def test_unique_name_by_name(self):
+ with beam.Pipeline(options=beam.options.pipeline_options.PipelineOptions(
+ pickle_library='cloudpickle')) as p:
+ spec = '''
+ transforms:
+ - type: Create
+ name: MyElements
+ elements: [0, 1, 3, 4]
+ '''
+ scope, spec = self.get_scope_by_spec(p, spec)
+
+ spec_transform = spec['transforms'][0]
+ p_transform = scope.create_ptransform(spec_transform, [])
+
+ result = scope.unique_name(spec_transform, p_transform)
+ self.assertEqual(result, "MyElements")
+ self.assertIn("MyElements", scope._seen_names)
+
+ result = scope.unique_name(spec_transform, p_transform)
+ self.assertIn("MyElements@3", scope._seen_names)
+ self.assertEqual(result, "MyElements@3")
+
+ def test_unique_name_by_label(self):
Review Comment:
_by_type rather than _by_label?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@beam.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [beam] robertwb merged pull request #27338: [yaml] yaml_transform.py unit tests
Posted by "robertwb (via GitHub)" <gi...@apache.org>.
robertwb merged PR #27338:
URL: https://github.com/apache/beam/pull/27338
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@beam.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [beam] bzablocki commented on pull request #27338: [yaml] yaml_transform.py unit tests
Posted by "bzablocki (via GitHub)" <gi...@apache.org>.
bzablocki commented on PR #27338:
URL: https://github.com/apache/beam/pull/27338#issuecomment-1683577828
@robertwb conflicts resolved
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@beam.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [beam] robertwb commented on a diff in pull request #27338: [yaml] yaml_transform.py unit tests
Posted by "robertwb (via GitHub)" <gi...@apache.org>.
robertwb commented on code in PR #27338:
URL: https://github.com/apache/beam/pull/27338#discussion_r1266017973
##########
sdks/python/apache_beam/yaml/yaml_transform_scope_test.py:
##########
@@ -0,0 +1,213 @@
+#
+# 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 logging
+import unittest
+
+import yaml
+
+import apache_beam as beam
+from apache_beam.yaml import yaml_provider
+from apache_beam.yaml.yaml_transform import SafeLineLoader
+from apache_beam.yaml.yaml_transform import Scope
+
+
+class ScopeTest(unittest.TestCase):
+ def get_scope_by_spec(self, p, spec):
+ spec = yaml.load(spec, Loader=SafeLineLoader)
+
+ scope = Scope(
+ beam.pvalue.PBegin(p), {},
+ spec['transforms'],
+ yaml_provider.standard_providers(), {})
+ return scope, spec
+
+ def test_get_pcollection_input(self):
+ with beam.Pipeline(options=beam.options.pipeline_options.PipelineOptions(
+ pickle_library='cloudpickle')) as p:
+ elements = p | beam.Create(range(3))
+ scope = Scope(
+ p, {'input': elements},
+ transforms=[],
+ providers=yaml_provider.standard_providers(),
+ input_providers={})
+
+ result = scope.get_pcollection('input')
+ self.assertEqual("PCollection[Create/Map(decode).None]", str(result))
+
+ def test_get_pcollection_output(self):
+ with beam.Pipeline(options=beam.options.pipeline_options.PipelineOptions(
+ pickle_library='cloudpickle')) as p:
+ spec = '''
+ transforms:
+ - type: Create
+ elements: [0, 1, 3, 4]
+ - type: PyMap
+ name: Square
+ input: Create
+ fn: "lambda x: x*x"
+ '''
+
+ scope, spec = self.get_scope_by_spec(p, spec)
+
+ result = scope.get_pcollection("Create")
+ self.assertEqual("PCollection[Create/Map(decode).None]", str(result))
+
+ result = scope.get_pcollection("Square")
+ self.assertEqual("PCollection[Square.None]", str(result))
+
+ result = scope.get_pcollection("PyMap")
+ self.assertEqual("PCollection[Square.None]", str(result))
+
+ def test_get_pcollection_ambigous_name(self):
+ with beam.Pipeline(options=beam.options.pipeline_options.PipelineOptions(
+ pickle_library='cloudpickle')) as p:
+ spec = '''
+ transforms:
+ - type: Create
+ elements: [0, 1, 3, 4]
+ - type: PyMap
+ fn: "lambda x: x*x"
+ - type: PyMap
+ fn: "lambda x: x*x*x"
+ '''
+
+ scope, spec = self.get_scope_by_spec(p, spec)
+
+ with self.assertRaisesRegex(ValueError, r'Ambiguous.*'):
+ scope.get_pcollection("PyMap")
+
+ def test_unique_name_by_name(self):
+ with beam.Pipeline(options=beam.options.pipeline_options.PipelineOptions(
+ pickle_library='cloudpickle')) as p:
+ spec = '''
+ transforms:
+ - type: Create
+ name: MyElements
+ elements: [0, 1, 3, 4]
+ '''
+ scope, spec = self.get_scope_by_spec(p, spec)
+
+ spec_transform = spec['transforms'][0]
+ p_transform = scope.create_ptransform(spec_transform, [])
+
+ result = scope.unique_name(spec_transform, p_transform)
+ self.assertEqual(result, "MyElements")
+ self.assertIn("MyElements", scope._seen_names)
+
+ result = scope.unique_name(spec_transform, p_transform)
Review Comment:
I wouldn't bother testing this one at all. (Maybe it should be renamed to be private...)
##########
sdks/python/apache_beam/yaml/yaml_transform_ut_test.py:
##########
@@ -0,0 +1,301 @@
+#
+# 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 logging
+import unittest
+
+import yaml
+
+import apache_beam as beam
+from apache_beam.yaml import yaml_provider
+from apache_beam.yaml import yaml_transform
+from apache_beam.yaml.yaml_transform import LightweightScope
+from apache_beam.yaml.yaml_transform import SafeLineLoader
+from apache_beam.yaml.yaml_transform import Scope
+from apache_beam.yaml.yaml_transform import expand_composite_transform
+from apache_beam.yaml.yaml_transform import expand_leaf_transform
+from apache_beam.yaml.yaml_transform import pipeline_as_composite
+
+
+class YamlTransformTest(unittest.TestCase):
+ def test_only_element(self):
+ self.assertEqual(yaml_transform.only_element((1, )), 1)
+
+
+class SafeLineLoaderTest(unittest.TestCase):
+ def test_get_line(self):
+ pipeline_yaml = '''
+ type: composite
+ input:
+ elements: input
+ transforms:
+ - type: PyMap
+ name: Square
+ input: elements
+ fn: "lambda x: x * x"
+ - type: PyMap
+ name: Cube
+ input: elements
+ fn: "lambda x: x * x * x"
+ output:
+ Flatten
+ '''
+ spec = yaml.load(pipeline_yaml, Loader=SafeLineLoader)
+ self.assertEqual(SafeLineLoader.get_line(spec['type']), 2)
+ self.assertEqual(SafeLineLoader.get_line(spec['input']), 4)
+ self.assertEqual(SafeLineLoader.get_line(spec['transforms'][0]), 6)
+ self.assertEqual(SafeLineLoader.get_line(spec['transforms'][0]['type']), 6)
+ self.assertEqual(SafeLineLoader.get_line(spec['transforms'][0]['name']), 7)
+ self.assertEqual(SafeLineLoader.get_line(spec['transforms'][1]), 10)
+ self.assertEqual(SafeLineLoader.get_line(spec['output']), 15)
+ self.assertEqual(SafeLineLoader.get_line(spec['transforms']), "unknown")
+
+ def test_strip_metadata(self):
+ spec_yaml = '''
+ transforms:
+ - type: PyMap
+ name: Square
+ '''
+ spec = yaml.load(spec_yaml, Loader=SafeLineLoader)
+ stripped = SafeLineLoader.strip_metadata(spec['transforms'])
+
+ self.assertFalse(hasattr(stripped[0], '__line__'))
+ self.assertFalse(hasattr(stripped[0], '__uuid__'))
+
+ def test_strip_metadata_nothing_to_strip(self):
+ spec_yaml = 'prop: 123'
+ spec = yaml.load(spec_yaml, Loader=SafeLineLoader)
+ stripped = SafeLineLoader.strip_metadata(spec['prop'])
+
+ self.assertFalse(hasattr(stripped, '__line__'))
+ self.assertFalse(hasattr(stripped, '__uuid__'))
+
+
+class LightweightScopeTest(unittest.TestCase):
+ @staticmethod
+ def get_spec():
+ pipeline_yaml = '''
+ - type: PyMap
+ name: Square
+ input: elements
+ fn: "lambda x: x * x"
+ - type: PyMap
+ name: PyMap
+ input: elements
+ fn: "lambda x: x * x * x"
+ - type: Filter
+ name: FilterOutBigNumbers
+ input: PyMap
+ keep: "lambda x: x<100"
+ '''
+ return yaml.load(pipeline_yaml, Loader=SafeLineLoader)
+
+ def test_init(self):
+ spec = self.get_spec()
+ scope = LightweightScope(spec)
+ self.assertEqual(len(scope._transforms_by_uuid), 3)
+ self.assertCountEqual(
+ list(scope._uuid_by_name.keys()),
+ ["PyMap", "Square", "Filter", "FilterOutBigNumbers"])
+
+ def test_get_transform_id_and_output_name(self):
+ spec = self.get_spec()
+ scope = LightweightScope(spec)
+ transform_id, output = scope.get_transform_id_and_output_name("Square")
+ self.assertEqual(transform_id, spec[0]['__uuid__'])
+ self.assertEqual(output, None)
+
+ def test_get_transform_id_and_output_name_with_dot(self):
+ spec = self.get_spec()
+ scope = LightweightScope(spec)
+ transform_id, output = \
+ scope.get_transform_id_and_output_name("Square.OutputName")
+ self.assertEqual(transform_id, spec[0]['__uuid__'])
+ self.assertEqual(output, "OutputName")
+
+ def test_get_transform_id_by_uuid(self):
+ spec = self.get_spec()
+ scope = LightweightScope(spec)
+ transform_id = scope.get_transform_id(spec[0]['__uuid__'])
+ self.assertEqual(transform_id, spec[0]['__uuid__'])
+
+ def test_get_transform_id_by_unique_name(self):
+ spec = self.get_spec()
+ scope = LightweightScope(spec)
+ transform_id = scope.get_transform_id("Square")
+ self.assertEqual(transform_id, spec[0]['__uuid__'])
+
+ def test_get_transform_id_by_ambiguous_name(self):
+ spec = self.get_spec()
+ scope = LightweightScope(spec)
+ with self.assertRaisesRegex(ValueError, r'Ambiguous.*PyMap'):
+ scope.get_transform_id(scope.get_transform_id(spec[1]['name']))
+
+ def test_get_transform_id_by_unknown_name(self):
+ spec = self.get_spec()
+ scope = LightweightScope(spec)
+ with self.assertRaisesRegex(ValueError, r'Unknown.*NotExistingTransform'):
+ scope.get_transform_id("NotExistingTransform")
+
+
+def new_pipeline():
+ return beam.Pipeline(
+ options=beam.options.pipeline_options.PipelineOptions(
+ pickle_library='cloudpickle'))
+
+
+class MainTest(unittest.TestCase):
+ def get_scope_by_spec(self, p, spec, inputs=None):
+ if inputs is None:
+ inputs = {}
+ spec = yaml.load(spec, Loader=SafeLineLoader)
+
+ scope = Scope(
+ beam.pvalue.PBegin(p),
+ inputs,
+ spec['transforms'],
+ yaml_provider.standard_providers(), {})
+ return scope, spec
+
+ def test_expand_leaf_transform_with_input(self):
+ with new_pipeline() as p:
+ spec = '''
+ transforms:
+ - type: Create
+ elements: [0]
+ - type: PyMap
+ input: Create
+ fn: "lambda x: x*x"
+ '''
+ scope, spec = self.get_scope_by_spec(p, spec)
+ result = expand_leaf_transform(spec['transforms'][1], scope)
Review Comment:
Yeah, I'd remove this test. I think the guiding principle for testing internal things is is "if this changed its behavior, would things break in inscrutable ways?"
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@beam.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [beam] bzablocki commented on pull request #27338: [yaml] yaml_transform.py unit tests
Posted by "bzablocki (via GitHub)" <gi...@apache.org>.
bzablocki commented on PR #27338:
URL: https://github.com/apache/beam/pull/27338#issuecomment-1617758940
R: @robertwb
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@beam.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [beam] bzablocki commented on pull request #27338: [yaml] yaml_transform.py unit tests
Posted by "bzablocki (via GitHub)" <gi...@apache.org>.
bzablocki commented on PR #27338:
URL: https://github.com/apache/beam/pull/27338#issuecomment-1634434673
Thanks @robertwb , I responded and applied the comments
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@beam.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [beam] bzablocki commented on pull request #27338: [yaml] yaml_transform.py unit tests
Posted by "bzablocki (via GitHub)" <gi...@apache.org>.
bzablocki commented on PR #27338:
URL: https://github.com/apache/beam/pull/27338#issuecomment-1651911858
Hi @robertwb, could you take another look at this PR?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@beam.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [beam] bzablocki commented on a diff in pull request #27338: [yaml] yaml_transform.py unit tests
Posted by "bzablocki (via GitHub)" <gi...@apache.org>.
bzablocki commented on code in PR #27338:
URL: https://github.com/apache/beam/pull/27338#discussion_r1262637152
##########
sdks/python/apache_beam/yaml/yaml_transform_ut_test.py:
##########
@@ -0,0 +1,301 @@
+#
Review Comment:
Yes, I'll indeed merge with https://github.com/apache/beam/pull/27086.
Fair point about the naming
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@beam.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [beam] bzablocki commented on a diff in pull request #27338: [yaml] yaml_transform.py unit tests
Posted by "bzablocki (via GitHub)" <gi...@apache.org>.
bzablocki commented on code in PR #27338:
URL: https://github.com/apache/beam/pull/27338#discussion_r1266461085
##########
sdks/python/apache_beam/yaml/yaml_transform_scope_test.py:
##########
@@ -0,0 +1,213 @@
+#
+# 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 logging
+import unittest
+
+import yaml
+
+import apache_beam as beam
+from apache_beam.yaml import yaml_provider
+from apache_beam.yaml.yaml_transform import SafeLineLoader
+from apache_beam.yaml.yaml_transform import Scope
+
+
+class ScopeTest(unittest.TestCase):
+ def get_scope_by_spec(self, p, spec):
+ spec = yaml.load(spec, Loader=SafeLineLoader)
+
+ scope = Scope(
+ beam.pvalue.PBegin(p), {},
+ spec['transforms'],
+ yaml_provider.standard_providers(), {})
+ return scope, spec
+
+ def test_get_pcollection_input(self):
+ with beam.Pipeline(options=beam.options.pipeline_options.PipelineOptions(
+ pickle_library='cloudpickle')) as p:
+ elements = p | beam.Create(range(3))
+ scope = Scope(
+ p, {'input': elements},
+ transforms=[],
+ providers=yaml_provider.standard_providers(),
+ input_providers={})
+
+ result = scope.get_pcollection('input')
+ self.assertEqual("PCollection[Create/Map(decode).None]", str(result))
+
+ def test_get_pcollection_output(self):
+ with beam.Pipeline(options=beam.options.pipeline_options.PipelineOptions(
+ pickle_library='cloudpickle')) as p:
+ spec = '''
+ transforms:
+ - type: Create
+ elements: [0, 1, 3, 4]
+ - type: PyMap
+ name: Square
+ input: Create
+ fn: "lambda x: x*x"
+ '''
+
+ scope, spec = self.get_scope_by_spec(p, spec)
+
+ result = scope.get_pcollection("Create")
+ self.assertEqual("PCollection[Create/Map(decode).None]", str(result))
+
+ result = scope.get_pcollection("Square")
+ self.assertEqual("PCollection[Square.None]", str(result))
+
+ result = scope.get_pcollection("PyMap")
+ self.assertEqual("PCollection[Square.None]", str(result))
+
+ def test_get_pcollection_ambigous_name(self):
+ with beam.Pipeline(options=beam.options.pipeline_options.PipelineOptions(
+ pickle_library='cloudpickle')) as p:
+ spec = '''
+ transforms:
+ - type: Create
+ elements: [0, 1, 3, 4]
+ - type: PyMap
+ fn: "lambda x: x*x"
+ - type: PyMap
+ fn: "lambda x: x*x*x"
+ '''
+
+ scope, spec = self.get_scope_by_spec(p, spec)
+
+ with self.assertRaisesRegex(ValueError, r'Ambiguous.*'):
+ scope.get_pcollection("PyMap")
+
+ def test_unique_name_by_name(self):
+ with beam.Pipeline(options=beam.options.pipeline_options.PipelineOptions(
+ pickle_library='cloudpickle')) as p:
+ spec = '''
+ transforms:
+ - type: Create
+ name: MyElements
+ elements: [0, 1, 3, 4]
+ '''
+ scope, spec = self.get_scope_by_spec(p, spec)
+
+ spec_transform = spec['transforms'][0]
+ p_transform = scope.create_ptransform(spec_transform, [])
+
+ result = scope.unique_name(spec_transform, p_transform)
+ self.assertEqual(result, "MyElements")
+ self.assertIn("MyElements", scope._seen_names)
+
+ result = scope.unique_name(spec_transform, p_transform)
Review Comment:
This function is used outside of the Scope class (in the expand_leaf_transform()), so it can't be private.
I'll just remove the test in this case.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@beam.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [beam] codecov[bot] commented on pull request #27338: [yaml] yaml_transform.py unit tests
Posted by "codecov[bot] (via GitHub)" <gi...@apache.org>.
codecov[bot] commented on PR #27338:
URL: https://github.com/apache/beam/pull/27338#issuecomment-1618216781
## [Codecov](https://app.codecov.io/gh/apache/beam/pull/27338?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) Report
> Merging [#27338](https://app.codecov.io/gh/apache/beam/pull/27338?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) (e5bde65) into [master](https://app.codecov.io/gh/apache/beam/commit/753bae4d9414a96c4d9993b3ec2a07674a5d08b3?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) (753bae4) will **decrease** coverage by `0.01%`.
> The diff coverage is `0.00%`.
```diff
@@ Coverage Diff @@
## master #27338 +/- ##
==========================================
- Coverage 71.26% 71.26% -0.01%
==========================================
Files 854 854
Lines 103712 103712
==========================================
- Hits 73914 73907 -7
- Misses 28249 28256 +7
Partials 1549 1549
```
| Flag | Coverage Δ | |
|---|---|---|
| python | `80.59% <0.00%> (-0.02%)` | :arrow_down: |
Flags with carried forward coverage won't be shown. [Click here](https://docs.codecov.io/docs/carryforward-flags?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache#carryforward-flags-in-the-pull-request-comment) to find out more.
| [Impacted Files](https://app.codecov.io/gh/apache/beam/pull/27338?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) | Coverage Δ | |
|---|---|---|
| [sdks/python/apache\_beam/yaml/yaml\_transform.py](https://app.codecov.io/gh/apache/beam/pull/27338?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache#diff-c2Rrcy9weXRob24vYXBhY2hlX2JlYW0veWFtbC95YW1sX3RyYW5zZm9ybS5weQ==) | `89.12% <0.00%> (+0.92%)` | :arrow_up: |
... and [9 files with indirect coverage changes](https://app.codecov.io/gh/apache/beam/pull/27338/indirect-changes?src=pr&el=tree-more&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache)
:mega: We’re building smart automated test selection to slash your CI/CD build times. [Learn more](https://about.codecov.io/iterative-testing/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache)
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@beam.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [beam] bzablocki commented on pull request #27338: [yaml] yaml_transform.py unit tests
Posted by "bzablocki (via GitHub)" <gi...@apache.org>.
bzablocki commented on PR #27338:
URL: https://github.com/apache/beam/pull/27338#issuecomment-1693159636
Thank you!
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@beam.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [beam] bzablocki commented on a diff in pull request #27338: [yaml] yaml_transform.py unit tests
Posted by "bzablocki (via GitHub)" <gi...@apache.org>.
bzablocki commented on code in PR #27338:
URL: https://github.com/apache/beam/pull/27338#discussion_r1266461507
##########
sdks/python/apache_beam/yaml/yaml_transform_scope_test.py:
##########
@@ -0,0 +1,213 @@
+#
+# 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 logging
+import unittest
+
+import yaml
+
+import apache_beam as beam
+from apache_beam.yaml import yaml_provider
+from apache_beam.yaml.yaml_transform import SafeLineLoader
+from apache_beam.yaml.yaml_transform import Scope
+
+
+class ScopeTest(unittest.TestCase):
+ def get_scope_by_spec(self, p, spec):
+ spec = yaml.load(spec, Loader=SafeLineLoader)
+
+ scope = Scope(
+ beam.pvalue.PBegin(p), {},
+ spec['transforms'],
+ yaml_provider.standard_providers(), {})
+ return scope, spec
+
+ def test_get_pcollection_input(self):
+ with beam.Pipeline(options=beam.options.pipeline_options.PipelineOptions(
+ pickle_library='cloudpickle')) as p:
+ elements = p | beam.Create(range(3))
+ scope = Scope(
+ p, {'input': elements},
+ transforms=[],
+ providers=yaml_provider.standard_providers(),
+ input_providers={})
+
+ result = scope.get_pcollection('input')
+ self.assertEqual("PCollection[Create/Map(decode).None]", str(result))
+
+ def test_get_pcollection_output(self):
+ with beam.Pipeline(options=beam.options.pipeline_options.PipelineOptions(
+ pickle_library='cloudpickle')) as p:
+ spec = '''
+ transforms:
+ - type: Create
+ elements: [0, 1, 3, 4]
+ - type: PyMap
+ name: Square
+ input: Create
+ fn: "lambda x: x*x"
+ '''
+
+ scope, spec = self.get_scope_by_spec(p, spec)
+
+ result = scope.get_pcollection("Create")
+ self.assertEqual("PCollection[Create/Map(decode).None]", str(result))
+
+ result = scope.get_pcollection("Square")
+ self.assertEqual("PCollection[Square.None]", str(result))
+
+ result = scope.get_pcollection("PyMap")
+ self.assertEqual("PCollection[Square.None]", str(result))
+
+ def test_get_pcollection_ambigous_name(self):
+ with beam.Pipeline(options=beam.options.pipeline_options.PipelineOptions(
+ pickle_library='cloudpickle')) as p:
+ spec = '''
+ transforms:
+ - type: Create
+ elements: [0, 1, 3, 4]
+ - type: PyMap
+ fn: "lambda x: x*x"
+ - type: PyMap
+ fn: "lambda x: x*x*x"
+ '''
+
+ scope, spec = self.get_scope_by_spec(p, spec)
+
+ with self.assertRaisesRegex(ValueError, r'Ambiguous.*'):
+ scope.get_pcollection("PyMap")
+
+ def test_unique_name_by_name(self):
+ with beam.Pipeline(options=beam.options.pipeline_options.PipelineOptions(
+ pickle_library='cloudpickle')) as p:
+ spec = '''
+ transforms:
+ - type: Create
+ name: MyElements
+ elements: [0, 1, 3, 4]
+ '''
+ scope, spec = self.get_scope_by_spec(p, spec)
+
+ spec_transform = spec['transforms'][0]
+ p_transform = scope.create_ptransform(spec_transform, [])
+
+ result = scope.unique_name(spec_transform, p_transform)
+ self.assertEqual(result, "MyElements")
+ self.assertIn("MyElements", scope._seen_names)
+
+ result = scope.unique_name(spec_transform, p_transform)
+ self.assertIn("MyElements@3", scope._seen_names)
+ self.assertEqual(result, "MyElements@3")
+
+ def test_unique_name_by_label(self):
Review Comment:
Test removed, see [above](https://github.com/apache/beam/pull/27338#discussion_r1266461085)
##########
sdks/python/apache_beam/yaml/yaml_transform_scope_test.py:
##########
@@ -0,0 +1,213 @@
+#
+# 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 logging
+import unittest
+
+import yaml
+
+import apache_beam as beam
+from apache_beam.yaml import yaml_provider
+from apache_beam.yaml.yaml_transform import SafeLineLoader
+from apache_beam.yaml.yaml_transform import Scope
+
+
+class ScopeTest(unittest.TestCase):
+ def get_scope_by_spec(self, p, spec):
+ spec = yaml.load(spec, Loader=SafeLineLoader)
+
+ scope = Scope(
+ beam.pvalue.PBegin(p), {},
+ spec['transforms'],
+ yaml_provider.standard_providers(), {})
+ return scope, spec
+
+ def test_get_pcollection_input(self):
+ with beam.Pipeline(options=beam.options.pipeline_options.PipelineOptions(
+ pickle_library='cloudpickle')) as p:
+ elements = p | beam.Create(range(3))
+ scope = Scope(
+ p, {'input': elements},
+ transforms=[],
+ providers=yaml_provider.standard_providers(),
+ input_providers={})
+
+ result = scope.get_pcollection('input')
+ self.assertEqual("PCollection[Create/Map(decode).None]", str(result))
+
+ def test_get_pcollection_output(self):
+ with beam.Pipeline(options=beam.options.pipeline_options.PipelineOptions(
+ pickle_library='cloudpickle')) as p:
+ spec = '''
+ transforms:
+ - type: Create
+ elements: [0, 1, 3, 4]
+ - type: PyMap
+ name: Square
+ input: Create
+ fn: "lambda x: x*x"
+ '''
+
+ scope, spec = self.get_scope_by_spec(p, spec)
+
+ result = scope.get_pcollection("Create")
+ self.assertEqual("PCollection[Create/Map(decode).None]", str(result))
+
+ result = scope.get_pcollection("Square")
+ self.assertEqual("PCollection[Square.None]", str(result))
+
+ result = scope.get_pcollection("PyMap")
+ self.assertEqual("PCollection[Square.None]", str(result))
+
+ def test_get_pcollection_ambigous_name(self):
+ with beam.Pipeline(options=beam.options.pipeline_options.PipelineOptions(
+ pickle_library='cloudpickle')) as p:
+ spec = '''
+ transforms:
+ - type: Create
+ elements: [0, 1, 3, 4]
+ - type: PyMap
+ fn: "lambda x: x*x"
+ - type: PyMap
+ fn: "lambda x: x*x*x"
+ '''
+
+ scope, spec = self.get_scope_by_spec(p, spec)
+
+ with self.assertRaisesRegex(ValueError, r'Ambiguous.*'):
+ scope.get_pcollection("PyMap")
+
+ def test_unique_name_by_name(self):
+ with beam.Pipeline(options=beam.options.pipeline_options.PipelineOptions(
+ pickle_library='cloudpickle')) as p:
+ spec = '''
+ transforms:
+ - type: Create
+ name: MyElements
+ elements: [0, 1, 3, 4]
+ '''
+ scope, spec = self.get_scope_by_spec(p, spec)
+
+ spec_transform = spec['transforms'][0]
+ p_transform = scope.create_ptransform(spec_transform, [])
+
+ result = scope.unique_name(spec_transform, p_transform)
+ self.assertEqual(result, "MyElements")
+ self.assertIn("MyElements", scope._seen_names)
+
+ result = scope.unique_name(spec_transform, p_transform)
+ self.assertIn("MyElements@3", scope._seen_names)
+ self.assertEqual(result, "MyElements@3")
+
+ def test_unique_name_by_label(self):
+ with beam.Pipeline(options=beam.options.pipeline_options.PipelineOptions(
+ pickle_library='cloudpickle')) as p:
+ spec = '''
+ transforms:
+ - type: Create
+ elements: [0, 1, 3, 4]
+ '''
+ scope, spec = self.get_scope_by_spec(p, spec)
+
+ spec_transform = spec['transforms'][0]
+ p_transform = scope.create_ptransform(spec_transform, [])
+
+ result = scope.unique_name(spec_transform, p_transform)
+ self.assertEqual(result, "Create")
+ self.assertIn("Create", scope._seen_names)
+
+ result = scope.unique_name(spec_transform, p_transform)
Review Comment:
Test removed, see [above](https://github.com/apache/beam/pull/27338#discussion_r1266461085)
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@beam.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [beam] bzablocki commented on a diff in pull request #27338: [yaml] yaml_transform.py unit tests
Posted by "bzablocki (via GitHub)" <gi...@apache.org>.
bzablocki commented on code in PR #27338:
URL: https://github.com/apache/beam/pull/27338#discussion_r1262680275
##########
sdks/python/apache_beam/yaml/yaml_transform_test.py:
##########
@@ -22,139 +22,13 @@
import tempfile
import unittest
-import yaml
-
import apache_beam as beam
from apache_beam.testing.util import assert_that
from apache_beam.testing.util import equal_to
from apache_beam.yaml import yaml_provider
-from apache_beam.yaml import yaml_transform
-from apache_beam.yaml.yaml_transform import LightweightScope
-from apache_beam.yaml.yaml_transform import SafeLineLoader
from apache_beam.yaml.yaml_transform import YamlTransform
-class YamlTransformTest(unittest.TestCase):
- def test_only_element(self):
- self.assertEqual(yaml_transform.only_element((1, )), 1)
-
-
-class SafeLineLoaderTest(unittest.TestCase):
- def test_get_line(self):
- pipeline_yaml = '''
- type: composite
- input:
- elements: input
- transforms:
- - type: PyMap
- name: Square
- input: elements
- fn: "lambda x: x * x"
- - type: PyMap
- name: Cube
- input: elements
- fn: "lambda x: x * x * x"
- output:
- Flatten
- '''
- spec = yaml.load(pipeline_yaml, Loader=SafeLineLoader)
- self.assertEqual(SafeLineLoader.get_line(spec['type']), 2)
- self.assertEqual(SafeLineLoader.get_line(spec['input']), 4)
- self.assertEqual(SafeLineLoader.get_line(spec['transforms'][0]), 6)
- self.assertEqual(SafeLineLoader.get_line(spec['transforms'][0]['type']), 6)
- self.assertEqual(SafeLineLoader.get_line(spec['transforms'][0]['name']), 7)
- self.assertEqual(SafeLineLoader.get_line(spec['transforms'][1]), 10)
- self.assertEqual(SafeLineLoader.get_line(spec['output']), 15)
- self.assertEqual(SafeLineLoader.get_line(spec['transforms']), "unknown")
-
- def test_strip_metadata(self):
- spec_yaml = '''
- transforms:
- - type: PyMap
- name: Square
- '''
- spec = yaml.load(spec_yaml, Loader=SafeLineLoader)
- stripped = SafeLineLoader.strip_metadata(spec['transforms'])
-
- self.assertFalse(hasattr(stripped[0], '__line__'))
- self.assertFalse(hasattr(stripped[0], '__uuid__'))
-
- def test_strip_metadata_nothing_to_strip(self):
- spec_yaml = 'prop: 123'
- spec = yaml.load(spec_yaml, Loader=SafeLineLoader)
- stripped = SafeLineLoader.strip_metadata(spec['prop'])
-
- self.assertFalse(hasattr(stripped, '__line__'))
- self.assertFalse(hasattr(stripped, '__uuid__'))
Review Comment:
Moved to yaml_transform_unit_test.py
##########
sdks/python/apache_beam/yaml/yaml_transform_test.py:
##########
@@ -22,139 +22,13 @@
import tempfile
import unittest
-import yaml
-
import apache_beam as beam
from apache_beam.testing.util import assert_that
from apache_beam.testing.util import equal_to
from apache_beam.yaml import yaml_provider
-from apache_beam.yaml import yaml_transform
-from apache_beam.yaml.yaml_transform import LightweightScope
-from apache_beam.yaml.yaml_transform import SafeLineLoader
from apache_beam.yaml.yaml_transform import YamlTransform
-class YamlTransformTest(unittest.TestCase):
- def test_only_element(self):
- self.assertEqual(yaml_transform.only_element((1, )), 1)
-
-
-class SafeLineLoaderTest(unittest.TestCase):
- def test_get_line(self):
- pipeline_yaml = '''
- type: composite
- input:
- elements: input
- transforms:
- - type: PyMap
- name: Square
- input: elements
- fn: "lambda x: x * x"
- - type: PyMap
- name: Cube
- input: elements
- fn: "lambda x: x * x * x"
- output:
- Flatten
- '''
- spec = yaml.load(pipeline_yaml, Loader=SafeLineLoader)
- self.assertEqual(SafeLineLoader.get_line(spec['type']), 2)
- self.assertEqual(SafeLineLoader.get_line(spec['input']), 4)
- self.assertEqual(SafeLineLoader.get_line(spec['transforms'][0]), 6)
- self.assertEqual(SafeLineLoader.get_line(spec['transforms'][0]['type']), 6)
- self.assertEqual(SafeLineLoader.get_line(spec['transforms'][0]['name']), 7)
- self.assertEqual(SafeLineLoader.get_line(spec['transforms'][1]), 10)
- self.assertEqual(SafeLineLoader.get_line(spec['output']), 15)
- self.assertEqual(SafeLineLoader.get_line(spec['transforms']), "unknown")
-
- def test_strip_metadata(self):
- spec_yaml = '''
- transforms:
- - type: PyMap
- name: Square
- '''
- spec = yaml.load(spec_yaml, Loader=SafeLineLoader)
- stripped = SafeLineLoader.strip_metadata(spec['transforms'])
-
- self.assertFalse(hasattr(stripped[0], '__line__'))
- self.assertFalse(hasattr(stripped[0], '__uuid__'))
-
- def test_strip_metadata_nothing_to_strip(self):
- spec_yaml = 'prop: 123'
- spec = yaml.load(spec_yaml, Loader=SafeLineLoader)
- stripped = SafeLineLoader.strip_metadata(spec['prop'])
-
- self.assertFalse(hasattr(stripped, '__line__'))
- self.assertFalse(hasattr(stripped, '__uuid__'))
-
-
-class LightweightScopeTest(unittest.TestCase):
- @staticmethod
- def get_spec():
- pipeline_yaml = '''
- - type: PyMap
- name: Square
- input: elements
- fn: "lambda x: x * x"
- - type: PyMap
- name: PyMap
- input: elements
- fn: "lambda x: x * x * x"
- - type: Filter
- name: FilterOutBigNumbers
- input: PyMap
- keep: "lambda x: x<100"
- '''
- return yaml.load(pipeline_yaml, Loader=SafeLineLoader)
-
- def test_init(self):
- spec = self.get_spec()
- scope = LightweightScope(spec)
- self.assertEqual(len(scope._transforms_by_uuid), 3)
- self.assertCountEqual(
- list(scope._uuid_by_name.keys()),
- ["PyMap", "Square", "Filter", "FilterOutBigNumbers"])
-
- def test_get_transform_id_and_output_name(self):
- spec = self.get_spec()
- scope = LightweightScope(spec)
- transform_id, output = scope.get_transform_id_and_output_name("Square")
- self.assertEqual(transform_id, spec[0]['__uuid__'])
- self.assertEqual(output, None)
-
- def test_get_transform_id_and_output_name_with_dot(self):
- spec = self.get_spec()
- scope = LightweightScope(spec)
- transform_id, output = \
- scope.get_transform_id_and_output_name("Square.OutputName")
- self.assertEqual(transform_id, spec[0]['__uuid__'])
- self.assertEqual(output, "OutputName")
-
- def test_get_transform_id_by_uuid(self):
- spec = self.get_spec()
- scope = LightweightScope(spec)
- transform_id = scope.get_transform_id(spec[0]['__uuid__'])
- self.assertEqual(transform_id, spec[0]['__uuid__'])
-
- def test_get_transform_id_by_unique_name(self):
- spec = self.get_spec()
- scope = LightweightScope(spec)
- transform_id = scope.get_transform_id("Square")
- self.assertEqual(transform_id, spec[0]['__uuid__'])
-
- def test_get_transform_id_by_ambiguous_name(self):
- spec = self.get_spec()
- scope = LightweightScope(spec)
- with self.assertRaisesRegex(ValueError, r'Ambiguous.*PyMap'):
- scope.get_transform_id(scope.get_transform_id(spec[1]['name']))
-
- def test_get_transform_id_by_unknown_name(self):
- spec = self.get_spec()
- scope = LightweightScope(spec)
- with self.assertRaisesRegex(ValueError, r'Unknown.*NotExistingTransform'):
- scope.get_transform_id("NotExistingTransform")
Review Comment:
Moved to yaml_transform_scope_test.py
##########
sdks/python/apache_beam/yaml/yaml_transform_scope_test.py:
##########
@@ -0,0 +1,213 @@
+#
+# 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 logging
+import unittest
+
+import yaml
+
+import apache_beam as beam
+from apache_beam.yaml import yaml_provider
+from apache_beam.yaml.yaml_transform import SafeLineLoader
+from apache_beam.yaml.yaml_transform import Scope
+
+
+class ScopeTest(unittest.TestCase):
+ def get_scope_by_spec(self, p, spec):
+ spec = yaml.load(spec, Loader=SafeLineLoader)
+
+ scope = Scope(
+ beam.pvalue.PBegin(p), {},
+ spec['transforms'],
+ yaml_provider.standard_providers(), {})
+ return scope, spec
+
+ def test_get_pcollection_input(self):
+ with beam.Pipeline(options=beam.options.pipeline_options.PipelineOptions(
+ pickle_library='cloudpickle')) as p:
+ elements = p | beam.Create(range(3))
+ scope = Scope(
+ p, {'input': elements},
+ transforms=[],
+ providers=yaml_provider.standard_providers(),
+ input_providers={})
+
+ result = scope.get_pcollection('input')
+ self.assertEqual("PCollection[Create/Map(decode).None]", str(result))
+
+ def test_get_pcollection_output(self):
+ with beam.Pipeline(options=beam.options.pipeline_options.PipelineOptions(
+ pickle_library='cloudpickle')) as p:
+ spec = '''
+ transforms:
+ - type: Create
+ elements: [0, 1, 3, 4]
+ - type: PyMap
+ name: Square
+ input: Create
+ fn: "lambda x: x*x"
+ '''
+
+ scope, spec = self.get_scope_by_spec(p, spec)
+
+ result = scope.get_pcollection("Create")
+ self.assertEqual("PCollection[Create/Map(decode).None]", str(result))
+
+ result = scope.get_pcollection("Square")
+ self.assertEqual("PCollection[Square.None]", str(result))
+
+ result = scope.get_pcollection("PyMap")
+ self.assertEqual("PCollection[Square.None]", str(result))
+
+ def test_get_pcollection_ambigous_name(self):
+ with beam.Pipeline(options=beam.options.pipeline_options.PipelineOptions(
Review Comment:
Unit Test removed - test_name_is_ambiguous tests this
##########
sdks/python/apache_beam/yaml/yaml_transform_ut_test.py:
##########
@@ -0,0 +1,301 @@
+#
+# 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 logging
+import unittest
+
+import yaml
+
+import apache_beam as beam
+from apache_beam.yaml import yaml_provider
+from apache_beam.yaml import yaml_transform
+from apache_beam.yaml.yaml_transform import LightweightScope
+from apache_beam.yaml.yaml_transform import SafeLineLoader
+from apache_beam.yaml.yaml_transform import Scope
+from apache_beam.yaml.yaml_transform import expand_composite_transform
+from apache_beam.yaml.yaml_transform import expand_leaf_transform
+from apache_beam.yaml.yaml_transform import pipeline_as_composite
+
+
+class YamlTransformTest(unittest.TestCase):
+ def test_only_element(self):
+ self.assertEqual(yaml_transform.only_element((1, )), 1)
+
+
+class SafeLineLoaderTest(unittest.TestCase):
+ def test_get_line(self):
+ pipeline_yaml = '''
+ type: composite
+ input:
+ elements: input
+ transforms:
+ - type: PyMap
+ name: Square
+ input: elements
+ fn: "lambda x: x * x"
+ - type: PyMap
+ name: Cube
+ input: elements
+ fn: "lambda x: x * x * x"
+ output:
+ Flatten
+ '''
+ spec = yaml.load(pipeline_yaml, Loader=SafeLineLoader)
+ self.assertEqual(SafeLineLoader.get_line(spec['type']), 2)
+ self.assertEqual(SafeLineLoader.get_line(spec['input']), 4)
+ self.assertEqual(SafeLineLoader.get_line(spec['transforms'][0]), 6)
+ self.assertEqual(SafeLineLoader.get_line(spec['transforms'][0]['type']), 6)
+ self.assertEqual(SafeLineLoader.get_line(spec['transforms'][0]['name']), 7)
+ self.assertEqual(SafeLineLoader.get_line(spec['transforms'][1]), 10)
+ self.assertEqual(SafeLineLoader.get_line(spec['output']), 15)
+ self.assertEqual(SafeLineLoader.get_line(spec['transforms']), "unknown")
+
+ def test_strip_metadata(self):
+ spec_yaml = '''
+ transforms:
+ - type: PyMap
+ name: Square
+ '''
+ spec = yaml.load(spec_yaml, Loader=SafeLineLoader)
+ stripped = SafeLineLoader.strip_metadata(spec['transforms'])
+
+ self.assertFalse(hasattr(stripped[0], '__line__'))
+ self.assertFalse(hasattr(stripped[0], '__uuid__'))
+
+ def test_strip_metadata_nothing_to_strip(self):
+ spec_yaml = 'prop: 123'
+ spec = yaml.load(spec_yaml, Loader=SafeLineLoader)
+ stripped = SafeLineLoader.strip_metadata(spec['prop'])
+
+ self.assertFalse(hasattr(stripped, '__line__'))
+ self.assertFalse(hasattr(stripped, '__uuid__'))
+
+
+class LightweightScopeTest(unittest.TestCase):
+ @staticmethod
+ def get_spec():
+ pipeline_yaml = '''
+ - type: PyMap
+ name: Square
+ input: elements
+ fn: "lambda x: x * x"
+ - type: PyMap
+ name: PyMap
+ input: elements
+ fn: "lambda x: x * x * x"
+ - type: Filter
+ name: FilterOutBigNumbers
+ input: PyMap
+ keep: "lambda x: x<100"
+ '''
+ return yaml.load(pipeline_yaml, Loader=SafeLineLoader)
+
+ def test_init(self):
+ spec = self.get_spec()
+ scope = LightweightScope(spec)
+ self.assertEqual(len(scope._transforms_by_uuid), 3)
+ self.assertCountEqual(
+ list(scope._uuid_by_name.keys()),
+ ["PyMap", "Square", "Filter", "FilterOutBigNumbers"])
+
+ def test_get_transform_id_and_output_name(self):
+ spec = self.get_spec()
+ scope = LightweightScope(spec)
+ transform_id, output = scope.get_transform_id_and_output_name("Square")
+ self.assertEqual(transform_id, spec[0]['__uuid__'])
+ self.assertEqual(output, None)
+
+ def test_get_transform_id_and_output_name_with_dot(self):
+ spec = self.get_spec()
+ scope = LightweightScope(spec)
+ transform_id, output = \
+ scope.get_transform_id_and_output_name("Square.OutputName")
+ self.assertEqual(transform_id, spec[0]['__uuid__'])
+ self.assertEqual(output, "OutputName")
+
+ def test_get_transform_id_by_uuid(self):
+ spec = self.get_spec()
+ scope = LightweightScope(spec)
+ transform_id = scope.get_transform_id(spec[0]['__uuid__'])
+ self.assertEqual(transform_id, spec[0]['__uuid__'])
+
+ def test_get_transform_id_by_unique_name(self):
+ spec = self.get_spec()
+ scope = LightweightScope(spec)
+ transform_id = scope.get_transform_id("Square")
+ self.assertEqual(transform_id, spec[0]['__uuid__'])
+
+ def test_get_transform_id_by_ambiguous_name(self):
+ spec = self.get_spec()
+ scope = LightweightScope(spec)
+ with self.assertRaisesRegex(ValueError, r'Ambiguous.*PyMap'):
+ scope.get_transform_id(scope.get_transform_id(spec[1]['name']))
+
+ def test_get_transform_id_by_unknown_name(self):
+ spec = self.get_spec()
+ scope = LightweightScope(spec)
+ with self.assertRaisesRegex(ValueError, r'Unknown.*NotExistingTransform'):
+ scope.get_transform_id("NotExistingTransform")
+
+
+def new_pipeline():
+ return beam.Pipeline(
+ options=beam.options.pipeline_options.PipelineOptions(
+ pickle_library='cloudpickle'))
+
+
+class MainTest(unittest.TestCase):
+ def get_scope_by_spec(self, p, spec, inputs=None):
+ if inputs is None:
+ inputs = {}
+ spec = yaml.load(spec, Loader=SafeLineLoader)
+
+ scope = Scope(
+ beam.pvalue.PBegin(p),
+ inputs,
+ spec['transforms'],
+ yaml_provider.standard_providers(), {})
+ return scope, spec
+
+ def test_expand_leaf_transform_with_input(self):
+ with new_pipeline() as p:
+ spec = '''
+ transforms:
+ - type: Create
+ elements: [0]
+ - type: PyMap
+ input: Create
+ fn: "lambda x: x*x"
+ '''
+ scope, spec = self.get_scope_by_spec(p, spec)
+ result = expand_leaf_transform(spec['transforms'][1], scope)
Review Comment:
I wanted to test as many functions as possible, but indeed, this made some of the tests tied to the implementation. I wasn't sure if it's ok to ignore the private but important functions, such as this one. Should I remove this test?
##########
sdks/python/apache_beam/yaml/yaml_transform_scope_test.py:
##########
@@ -0,0 +1,213 @@
+#
+# 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 logging
+import unittest
+
+import yaml
+
+import apache_beam as beam
+from apache_beam.yaml import yaml_provider
+from apache_beam.yaml.yaml_transform import SafeLineLoader
+from apache_beam.yaml.yaml_transform import Scope
+
+
+class ScopeTest(unittest.TestCase):
+ def get_scope_by_spec(self, p, spec):
+ spec = yaml.load(spec, Loader=SafeLineLoader)
+
+ scope = Scope(
+ beam.pvalue.PBegin(p), {},
+ spec['transforms'],
+ yaml_provider.standard_providers(), {})
+ return scope, spec
+
+ def test_get_pcollection_input(self):
+ with beam.Pipeline(options=beam.options.pipeline_options.PipelineOptions(
+ pickle_library='cloudpickle')) as p:
+ elements = p | beam.Create(range(3))
+ scope = Scope(
+ p, {'input': elements},
+ transforms=[],
+ providers=yaml_provider.standard_providers(),
+ input_providers={})
+
+ result = scope.get_pcollection('input')
+ self.assertEqual("PCollection[Create/Map(decode).None]", str(result))
+
+ def test_get_pcollection_output(self):
+ with beam.Pipeline(options=beam.options.pipeline_options.PipelineOptions(
+ pickle_library='cloudpickle')) as p:
+ spec = '''
+ transforms:
+ - type: Create
+ elements: [0, 1, 3, 4]
+ - type: PyMap
+ name: Square
+ input: Create
+ fn: "lambda x: x*x"
+ '''
+
+ scope, spec = self.get_scope_by_spec(p, spec)
+
+ result = scope.get_pcollection("Create")
+ self.assertEqual("PCollection[Create/Map(decode).None]", str(result))
+
+ result = scope.get_pcollection("Square")
+ self.assertEqual("PCollection[Square.None]", str(result))
+
+ result = scope.get_pcollection("PyMap")
+ self.assertEqual("PCollection[Square.None]", str(result))
+
+ def test_get_pcollection_ambigous_name(self):
+ with beam.Pipeline(options=beam.options.pipeline_options.PipelineOptions(
+ pickle_library='cloudpickle')) as p:
+ spec = '''
+ transforms:
+ - type: Create
+ elements: [0, 1, 3, 4]
+ - type: PyMap
+ fn: "lambda x: x*x"
+ - type: PyMap
+ fn: "lambda x: x*x*x"
+ '''
+
+ scope, spec = self.get_scope_by_spec(p, spec)
+
+ with self.assertRaisesRegex(ValueError, r'Ambiguous.*'):
+ scope.get_pcollection("PyMap")
+
+ def test_unique_name_by_name(self):
+ with beam.Pipeline(options=beam.options.pipeline_options.PipelineOptions(
+ pickle_library='cloudpickle')) as p:
+ spec = '''
+ transforms:
+ - type: Create
+ name: MyElements
+ elements: [0, 1, 3, 4]
+ '''
+ scope, spec = self.get_scope_by_spec(p, spec)
+
+ spec_transform = spec['transforms'][0]
+ p_transform = scope.create_ptransform(spec_transform, [])
+
+ result = scope.unique_name(spec_transform, p_transform)
+ self.assertEqual(result, "MyElements")
+ self.assertIn("MyElements", scope._seen_names)
+
+ result = scope.unique_name(spec_transform, p_transform)
+ self.assertIn("MyElements@3", scope._seen_names)
+ self.assertEqual(result, "MyElements@3")
+
+ def test_unique_name_by_label(self):
+ with beam.Pipeline(options=beam.options.pipeline_options.PipelineOptions(
+ pickle_library='cloudpickle')) as p:
+ spec = '''
+ transforms:
+ - type: Create
+ elements: [0, 1, 3, 4]
+ '''
+ scope, spec = self.get_scope_by_spec(p, spec)
+
+ spec_transform = spec['transforms'][0]
+ p_transform = scope.create_ptransform(spec_transform, [])
+
+ result = scope.unique_name(spec_transform, p_transform)
+ self.assertEqual(result, "Create")
+ self.assertIn("Create", scope._seen_names)
+
+ result = scope.unique_name(spec_transform, p_transform)
Review Comment:
Should I remove the test? And/Or change the implementation of the unique_name()?
##########
sdks/python/apache_beam/yaml/yaml_transform_scope_test.py:
##########
@@ -0,0 +1,213 @@
+#
+# 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 logging
+import unittest
+
+import yaml
+
+import apache_beam as beam
+from apache_beam.yaml import yaml_provider
+from apache_beam.yaml.yaml_transform import SafeLineLoader
+from apache_beam.yaml.yaml_transform import Scope
+
+
+class ScopeTest(unittest.TestCase):
+ def get_scope_by_spec(self, p, spec):
+ spec = yaml.load(spec, Loader=SafeLineLoader)
+
+ scope = Scope(
+ beam.pvalue.PBegin(p), {},
+ spec['transforms'],
+ yaml_provider.standard_providers(), {})
+ return scope, spec
+
+ def test_get_pcollection_input(self):
+ with beam.Pipeline(options=beam.options.pipeline_options.PipelineOptions(
+ pickle_library='cloudpickle')) as p:
+ elements = p | beam.Create(range(3))
+ scope = Scope(
+ p, {'input': elements},
+ transforms=[],
+ providers=yaml_provider.standard_providers(),
+ input_providers={})
+
+ result = scope.get_pcollection('input')
+ self.assertEqual("PCollection[Create/Map(decode).None]", str(result))
+
+ def test_get_pcollection_output(self):
+ with beam.Pipeline(options=beam.options.pipeline_options.PipelineOptions(
+ pickle_library='cloudpickle')) as p:
+ spec = '''
+ transforms:
+ - type: Create
+ elements: [0, 1, 3, 4]
+ - type: PyMap
+ name: Square
+ input: Create
+ fn: "lambda x: x*x"
+ '''
+
+ scope, spec = self.get_scope_by_spec(p, spec)
+
+ result = scope.get_pcollection("Create")
+ self.assertEqual("PCollection[Create/Map(decode).None]", str(result))
+
+ result = scope.get_pcollection("Square")
+ self.assertEqual("PCollection[Square.None]", str(result))
+
+ result = scope.get_pcollection("PyMap")
+ self.assertEqual("PCollection[Square.None]", str(result))
Review Comment:
Good point, added
##########
sdks/python/apache_beam/yaml/yaml_transform_scope_test.py:
##########
@@ -0,0 +1,213 @@
+#
+# 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 logging
+import unittest
+
+import yaml
+
+import apache_beam as beam
+from apache_beam.yaml import yaml_provider
+from apache_beam.yaml.yaml_transform import SafeLineLoader
+from apache_beam.yaml.yaml_transform import Scope
+
+
+class ScopeTest(unittest.TestCase):
+ def get_scope_by_spec(self, p, spec):
+ spec = yaml.load(spec, Loader=SafeLineLoader)
+
+ scope = Scope(
+ beam.pvalue.PBegin(p), {},
+ spec['transforms'],
+ yaml_provider.standard_providers(), {})
+ return scope, spec
+
+ def test_get_pcollection_input(self):
+ with beam.Pipeline(options=beam.options.pipeline_options.PipelineOptions(
+ pickle_library='cloudpickle')) as p:
+ elements = p | beam.Create(range(3))
+ scope = Scope(
+ p, {'input': elements},
+ transforms=[],
+ providers=yaml_provider.standard_providers(),
+ input_providers={})
+
+ result = scope.get_pcollection('input')
+ self.assertEqual("PCollection[Create/Map(decode).None]", str(result))
+
+ def test_get_pcollection_output(self):
+ with beam.Pipeline(options=beam.options.pipeline_options.PipelineOptions(
+ pickle_library='cloudpickle')) as p:
+ spec = '''
+ transforms:
+ - type: Create
+ elements: [0, 1, 3, 4]
+ - type: PyMap
+ name: Square
+ input: Create
+ fn: "lambda x: x*x"
+ '''
+
+ scope, spec = self.get_scope_by_spec(p, spec)
+
+ result = scope.get_pcollection("Create")
+ self.assertEqual("PCollection[Create/Map(decode).None]", str(result))
+
+ result = scope.get_pcollection("Square")
+ self.assertEqual("PCollection[Square.None]", str(result))
+
+ result = scope.get_pcollection("PyMap")
+ self.assertEqual("PCollection[Square.None]", str(result))
+
+ def test_get_pcollection_ambigous_name(self):
+ with beam.Pipeline(options=beam.options.pipeline_options.PipelineOptions(
+ pickle_library='cloudpickle')) as p:
+ spec = '''
+ transforms:
+ - type: Create
+ elements: [0, 1, 3, 4]
+ - type: PyMap
+ fn: "lambda x: x*x"
+ - type: PyMap
+ fn: "lambda x: x*x*x"
+ '''
+
+ scope, spec = self.get_scope_by_spec(p, spec)
+
+ with self.assertRaisesRegex(ValueError, r'Ambiguous.*'):
+ scope.get_pcollection("PyMap")
+
+ def test_unique_name_by_name(self):
+ with beam.Pipeline(options=beam.options.pipeline_options.PipelineOptions(
+ pickle_library='cloudpickle')) as p:
+ spec = '''
+ transforms:
+ - type: Create
+ name: MyElements
+ elements: [0, 1, 3, 4]
+ '''
+ scope, spec = self.get_scope_by_spec(p, spec)
+
+ spec_transform = spec['transforms'][0]
+ p_transform = scope.create_ptransform(spec_transform, [])
+
+ result = scope.unique_name(spec_transform, p_transform)
+ self.assertEqual(result, "MyElements")
+ self.assertIn("MyElements", scope._seen_names)
+
+ result = scope.unique_name(spec_transform, p_transform)
Review Comment:
This unit test checks against the current implementation. Intuitively, I also think that "unique_name()" should return one name for the same spec, but that's not the current behaviour. Let me know if you want to change it.
##########
sdks/python/apache_beam/yaml/yaml_transform_scope_test.py:
##########
@@ -0,0 +1,213 @@
+#
+# 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 logging
+import unittest
+
+import yaml
+
+import apache_beam as beam
+from apache_beam.yaml import yaml_provider
+from apache_beam.yaml.yaml_transform import SafeLineLoader
+from apache_beam.yaml.yaml_transform import Scope
+
+
+class ScopeTest(unittest.TestCase):
+ def get_scope_by_spec(self, p, spec):
+ spec = yaml.load(spec, Loader=SafeLineLoader)
+
+ scope = Scope(
+ beam.pvalue.PBegin(p), {},
+ spec['transforms'],
+ yaml_provider.standard_providers(), {})
+ return scope, spec
+
+ def test_get_pcollection_input(self):
+ with beam.Pipeline(options=beam.options.pipeline_options.PipelineOptions(
+ pickle_library='cloudpickle')) as p:
+ elements = p | beam.Create(range(3))
+ scope = Scope(
+ p, {'input': elements},
+ transforms=[],
+ providers=yaml_provider.standard_providers(),
+ input_providers={})
+
+ result = scope.get_pcollection('input')
+ self.assertEqual("PCollection[Create/Map(decode).None]", str(result))
+
+ def test_get_pcollection_output(self):
+ with beam.Pipeline(options=beam.options.pipeline_options.PipelineOptions(
+ pickle_library='cloudpickle')) as p:
+ spec = '''
+ transforms:
+ - type: Create
+ elements: [0, 1, 3, 4]
+ - type: PyMap
+ name: Square
+ input: Create
+ fn: "lambda x: x*x"
+ '''
+
+ scope, spec = self.get_scope_by_spec(p, spec)
+
+ result = scope.get_pcollection("Create")
+ self.assertEqual("PCollection[Create/Map(decode).None]", str(result))
+
+ result = scope.get_pcollection("Square")
+ self.assertEqual("PCollection[Square.None]", str(result))
+
+ result = scope.get_pcollection("PyMap")
+ self.assertEqual("PCollection[Square.None]", str(result))
+
+ def test_get_pcollection_ambigous_name(self):
+ with beam.Pipeline(options=beam.options.pipeline_options.PipelineOptions(
+ pickle_library='cloudpickle')) as p:
+ spec = '''
+ transforms:
+ - type: Create
+ elements: [0, 1, 3, 4]
+ - type: PyMap
+ fn: "lambda x: x*x"
+ - type: PyMap
+ fn: "lambda x: x*x*x"
+ '''
+
+ scope, spec = self.get_scope_by_spec(p, spec)
+
+ with self.assertRaisesRegex(ValueError, r'Ambiguous.*'):
Review Comment:
I'll move it to the test_name_is_ambiguous test
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@beam.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [beam] bzablocki commented on a diff in pull request #27338: [yaml] yaml_transform.py unit tests
Posted by "bzablocki (via GitHub)" <gi...@apache.org>.
bzablocki commented on code in PR #27338:
URL: https://github.com/apache/beam/pull/27338#discussion_r1266464237
##########
sdks/python/apache_beam/yaml/yaml_transform_ut_test.py:
##########
@@ -0,0 +1,301 @@
+#
+# 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 logging
+import unittest
+
+import yaml
+
+import apache_beam as beam
+from apache_beam.yaml import yaml_provider
+from apache_beam.yaml import yaml_transform
+from apache_beam.yaml.yaml_transform import LightweightScope
+from apache_beam.yaml.yaml_transform import SafeLineLoader
+from apache_beam.yaml.yaml_transform import Scope
+from apache_beam.yaml.yaml_transform import expand_composite_transform
+from apache_beam.yaml.yaml_transform import expand_leaf_transform
+from apache_beam.yaml.yaml_transform import pipeline_as_composite
+
+
+class YamlTransformTest(unittest.TestCase):
+ def test_only_element(self):
+ self.assertEqual(yaml_transform.only_element((1, )), 1)
+
+
+class SafeLineLoaderTest(unittest.TestCase):
+ def test_get_line(self):
+ pipeline_yaml = '''
+ type: composite
+ input:
+ elements: input
+ transforms:
+ - type: PyMap
+ name: Square
+ input: elements
+ fn: "lambda x: x * x"
+ - type: PyMap
+ name: Cube
+ input: elements
+ fn: "lambda x: x * x * x"
+ output:
+ Flatten
+ '''
+ spec = yaml.load(pipeline_yaml, Loader=SafeLineLoader)
+ self.assertEqual(SafeLineLoader.get_line(spec['type']), 2)
+ self.assertEqual(SafeLineLoader.get_line(spec['input']), 4)
+ self.assertEqual(SafeLineLoader.get_line(spec['transforms'][0]), 6)
+ self.assertEqual(SafeLineLoader.get_line(spec['transforms'][0]['type']), 6)
+ self.assertEqual(SafeLineLoader.get_line(spec['transforms'][0]['name']), 7)
+ self.assertEqual(SafeLineLoader.get_line(spec['transforms'][1]), 10)
+ self.assertEqual(SafeLineLoader.get_line(spec['output']), 15)
+ self.assertEqual(SafeLineLoader.get_line(spec['transforms']), "unknown")
+
+ def test_strip_metadata(self):
+ spec_yaml = '''
+ transforms:
+ - type: PyMap
+ name: Square
+ '''
+ spec = yaml.load(spec_yaml, Loader=SafeLineLoader)
+ stripped = SafeLineLoader.strip_metadata(spec['transforms'])
+
+ self.assertFalse(hasattr(stripped[0], '__line__'))
+ self.assertFalse(hasattr(stripped[0], '__uuid__'))
+
+ def test_strip_metadata_nothing_to_strip(self):
+ spec_yaml = 'prop: 123'
+ spec = yaml.load(spec_yaml, Loader=SafeLineLoader)
+ stripped = SafeLineLoader.strip_metadata(spec['prop'])
+
+ self.assertFalse(hasattr(stripped, '__line__'))
+ self.assertFalse(hasattr(stripped, '__uuid__'))
+
+
+class LightweightScopeTest(unittest.TestCase):
+ @staticmethod
+ def get_spec():
+ pipeline_yaml = '''
+ - type: PyMap
+ name: Square
+ input: elements
+ fn: "lambda x: x * x"
+ - type: PyMap
+ name: PyMap
+ input: elements
+ fn: "lambda x: x * x * x"
+ - type: Filter
+ name: FilterOutBigNumbers
+ input: PyMap
+ keep: "lambda x: x<100"
+ '''
+ return yaml.load(pipeline_yaml, Loader=SafeLineLoader)
+
+ def test_init(self):
+ spec = self.get_spec()
+ scope = LightweightScope(spec)
+ self.assertEqual(len(scope._transforms_by_uuid), 3)
+ self.assertCountEqual(
+ list(scope._uuid_by_name.keys()),
+ ["PyMap", "Square", "Filter", "FilterOutBigNumbers"])
+
+ def test_get_transform_id_and_output_name(self):
+ spec = self.get_spec()
+ scope = LightweightScope(spec)
+ transform_id, output = scope.get_transform_id_and_output_name("Square")
+ self.assertEqual(transform_id, spec[0]['__uuid__'])
+ self.assertEqual(output, None)
+
+ def test_get_transform_id_and_output_name_with_dot(self):
+ spec = self.get_spec()
+ scope = LightweightScope(spec)
+ transform_id, output = \
+ scope.get_transform_id_and_output_name("Square.OutputName")
+ self.assertEqual(transform_id, spec[0]['__uuid__'])
+ self.assertEqual(output, "OutputName")
+
+ def test_get_transform_id_by_uuid(self):
+ spec = self.get_spec()
+ scope = LightweightScope(spec)
+ transform_id = scope.get_transform_id(spec[0]['__uuid__'])
+ self.assertEqual(transform_id, spec[0]['__uuid__'])
+
+ def test_get_transform_id_by_unique_name(self):
+ spec = self.get_spec()
+ scope = LightweightScope(spec)
+ transform_id = scope.get_transform_id("Square")
+ self.assertEqual(transform_id, spec[0]['__uuid__'])
+
+ def test_get_transform_id_by_ambiguous_name(self):
+ spec = self.get_spec()
+ scope = LightweightScope(spec)
+ with self.assertRaisesRegex(ValueError, r'Ambiguous.*PyMap'):
+ scope.get_transform_id(scope.get_transform_id(spec[1]['name']))
+
+ def test_get_transform_id_by_unknown_name(self):
+ spec = self.get_spec()
+ scope = LightweightScope(spec)
+ with self.assertRaisesRegex(ValueError, r'Unknown.*NotExistingTransform'):
+ scope.get_transform_id("NotExistingTransform")
+
+
+def new_pipeline():
+ return beam.Pipeline(
+ options=beam.options.pipeline_options.PipelineOptions(
+ pickle_library='cloudpickle'))
+
+
+class MainTest(unittest.TestCase):
+ def get_scope_by_spec(self, p, spec, inputs=None):
+ if inputs is None:
+ inputs = {}
+ spec = yaml.load(spec, Loader=SafeLineLoader)
+
+ scope = Scope(
+ beam.pvalue.PBegin(p),
+ inputs,
+ spec['transforms'],
+ yaml_provider.standard_providers(), {})
+ return scope, spec
+
+ def test_expand_leaf_transform_with_input(self):
+ with new_pipeline() as p:
+ spec = '''
+ transforms:
+ - type: Create
+ elements: [0]
+ - type: PyMap
+ input: Create
+ fn: "lambda x: x*x"
+ '''
+ scope, spec = self.get_scope_by_spec(p, spec)
+ result = expand_leaf_transform(spec['transforms'][1], scope)
Review Comment:
ok, removing `test_expand_leaf_transform_with_input()` and `test_expand_leaf_transform_without_input()`
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@beam.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [beam] github-actions[bot] commented on pull request #27338: [yaml] yaml_transform.py unit tests
Posted by "github-actions[bot] (via GitHub)" <gi...@apache.org>.
github-actions[bot] commented on PR #27338:
URL: https://github.com/apache/beam/pull/27338#issuecomment-1617761567
Stopping reviewer notifications for this pull request: review requested by someone other than the bot, ceding control
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@beam.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [beam] robertwb commented on pull request #27338: [yaml] yaml_transform.py unit tests
Posted by "robertwb (via GitHub)" <gi...@apache.org>.
robertwb commented on PR #27338:
URL: https://github.com/apache/beam/pull/27338#issuecomment-1692526366
The link failures look unrelated. LGTM.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@beam.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org