github.com/apache/beam/sdks/v2@v2.48.2/python/apache_beam/examples/matrix_power_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  """Test for the matrix power integration test."""
    19  
    20  import logging
    21  import tempfile
    22  import unittest
    23  
    24  from apache_beam.examples import matrix_power
    25  
    26  
    27  class MatrixPowerTest(unittest.TestCase):
    28  
    29    MATRIX_INPUT = b'''
    30  0:1 1 1
    31  1:1 1 1
    32  2:1 1 1
    33  '''.strip()
    34    VECTOR_INPUT = b'1 2 3'
    35    EXPONENT = 3
    36    EXPECTED_OUTPUT = '''
    37  (0, 54.0)
    38  (1, 54.0)
    39  (2, 54.0)
    40  '''.lstrip()
    41  
    42    def create_temp_file(self, contents):
    43      with tempfile.NamedTemporaryFile(delete=False) as f:
    44        f.write(contents)
    45        return f.name
    46  
    47    def test_basics(self):
    48      matrix_path = self.create_temp_file(self.MATRIX_INPUT)
    49      vector_path = self.create_temp_file(self.VECTOR_INPUT)
    50      matrix_power.run((
    51          '--input_matrix=%s --input_vector=%s --exponent=%d --output=%s.result' %
    52          (matrix_path, vector_path, self.EXPONENT, vector_path)).split())
    53      # Parse result file and compare.
    54      with open(vector_path + '.result-00000-of-00001') as result_file:
    55        results = result_file.read()
    56        self.assertEqual(sorted(self.EXPECTED_OUTPUT), sorted(results))
    57  
    58  
    59  if __name__ == '__main__':
    60    logging.getLogger().setLevel(logging.INFO)
    61    unittest.main()