github.com/apache/beam/sdks/v2@v2.48.2/python/apache_beam/pvalue_test.py (about)

     1  #
     2  # Licensed to the Apache Software Foundation (ASF) under one or more
     3  # contributor license agreements.  See the NOTICE file distributed with
     4  # this work for additional information regarding copyright ownership.
     5  # The ASF licenses this file to You under the Apache License, Version 2.0
     6  # (the "License"); you may not use this file except in compliance with
     7  # the License.  You may obtain a copy of the License at
     8  #
     9  #    http://www.apache.org/licenses/LICENSE-2.0
    10  #
    11  # Unless required by applicable law or agreed to in writing, software
    12  # distributed under the License is distributed on an "AS IS" BASIS,
    13  # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14  # See the License for the specific language governing permissions and
    15  # limitations under the License.
    16  #
    17  
    18  """Unit tests for the PValue and PCollection classes."""
    19  
    20  # pytype: skip-file
    21  
    22  import unittest
    23  
    24  from apache_beam.pvalue import AsSingleton
    25  from apache_beam.pvalue import PValue
    26  from apache_beam.pvalue import Row
    27  from apache_beam.pvalue import TaggedOutput
    28  from apache_beam.testing.test_pipeline import TestPipeline
    29  
    30  
    31  class PValueTest(unittest.TestCase):
    32    def test_pvalue_expected_arguments(self):
    33      pipeline = TestPipeline()
    34      value = PValue(pipeline)
    35      self.assertEqual(pipeline, value.pipeline)
    36  
    37    def test_assingleton_multi_element(self):
    38      with self.assertRaisesRegex(
    39          ValueError,
    40          'PCollection of size 2 with more than one element accessed as a '
    41          'singleton view. First two elements encountered are \"1\", \"2\".'):
    42        AsSingleton._from_runtime_iterable([1, 2], {})
    43  
    44  
    45  class TaggedValueTest(unittest.TestCase):
    46    def test_passed_tuple_as_tag(self):
    47      with self.assertRaisesRegex(
    48          TypeError,
    49          r'Attempting to create a TaggedOutput with non-string tag \(1, 2, 3\)'):
    50        TaggedOutput((1, 2, 3), 'value')
    51  
    52  
    53  class RowTest(unittest.TestCase):
    54    def test_row_eq(self):
    55      row = Row(a=1, b=2)
    56      same = Row(a=1, b=2)
    57      self.assertEqual(row, same)
    58  
    59    def test_trailing_column_row_neq(self):
    60      row = Row(a=1, b=2)
    61      trail = Row(a=1, b=2, c=3)
    62      self.assertNotEqual(row, trail)
    63  
    64    def test_row_comparison_respects_element_order(self):
    65      row = Row(a=1, b=2)
    66      different = Row(b=2, a=1)
    67      self.assertNotEqual(row, different)
    68  
    69  
    70  if __name__ == '__main__':
    71    unittest.main()