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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to