[ 
https://issues.apache.org/jira/browse/BEAM-7455?focusedWorklogId=273390&page=com.atlassian.jira.plugin.system.issuetabpanels:worklog-tabpanel#worklog-273390
 ]

ASF GitHub Bot logged work on BEAM-7455:
----------------------------------------

                Author: ASF GitHub Bot
            Created on: 08/Jul/19 17:02
            Start Date: 08/Jul/19 17:02
    Worklog Time Spent: 10m 
      Work Description: tvalentyn commented on pull request #8818: [BEAM-7455] 
Improve Avro IO integration test coverage on Python 3.
URL: https://github.com/apache/beam/pull/8818#discussion_r299590315
 
 

 ##########
 File path: sdks/python/apache_beam/io/avroio_it_test.py
 ##########
 @@ -0,0 +1,236 @@
+#
+# 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.
+#
+
+"""End-to-end test for Avro IO's.
+
+Integration test for Avro IO. Using both the avro and fastavro
+libraries, this test writes avro files, reads them back in,
+and combines the different fields.
+
+Usage:
+
+  DataFlowRunner:
+    python setup.py nosetests --tests apache_beam.io.avro_it_test\
+        --test-pipeline-options="
+          --runner=TestDataflowRunner
+          --project=...
+          --staging_location=gs://...
+          --temp_location=gs://...
+          --output=gs://...
+          --sdk_location=...
+        "
+
+  DirectRunner:
+    python setup.py nosetests --tests apache_beam.io.avro_it_test \
+      --test-pipeline-options="
+        --output=/tmp
+      "
+"""
+
+from __future__ import absolute_import
+from __future__ import division
+
+import json
+import logging
+import os
+import sys
+import unittest
+import uuid
+
+from fastavro import parse_schema
+from nose.plugins.attrib import attr
+
+from apache_beam.io.avroio import ReadFromAvro
+from apache_beam.io.avroio import WriteToAvro
+from apache_beam.runners.runner import PipelineState
+from apache_beam.testing.test_pipeline import TestPipeline
+from apache_beam.testing.test_utils import delete_files
+from apache_beam.testing.util import BeamAssertException
+from apache_beam.transforms import CombineGlobally
+from apache_beam.transforms.combiners import Count
+from apache_beam.transforms.combiners import Mean
+from apache_beam.transforms.combiners import ToList
+from apache_beam.transforms.core import Create
+from apache_beam.transforms.core import FlatMap
+from apache_beam.transforms.core import Map
+
+# pylint: disable=wrong-import-order, wrong-import-position
+try:
+  from avro.schema import Parse  # avro-python3 library for python3
+except ImportError:
+  from avro.schema import parse as Parse  # avro library for python2
+# pylint: enable=wrong-import-order, wrong-import-position
+
+LABELS = ['abc', 'def', 'ghi', 'jkl', 'mno', 'pqr', 'stu', 'vwx']
+COLORS = ['RED', 'ORANGE', 'YELLOW', 'GREEN', 'BLUE', 'PURPLE', None]
+
+
+def record(i):
+  return {
+      'label': LABELS[i % len(LABELS)],
+      'number': i,
+      'number_str': str(i),
+      'color': COLORS[i % len(COLORS)]
+  }
+
+
+class AvroITTestBase(object):
+
+  def setUp(self):
+    self.SCHEMA_STRING = '''
+      {"namespace": "example.avro",
+       "type": "record",
+       "name": "User",
+       "fields": [
+           {"name": "label", "type": "string"},
+           {"name": "number",  "type": ["int", "null"]},
+           {"name": "number_str", "type": ["string", "null"]},
+           {"name": "color", "type": ["string", "null"]}
+       ]
+      }
+      '''
+    self.uuid = str(uuid.uuid4())
+
+  @attr('IT')
+  def avro_it_test(self):
+    write_pipeline = TestPipeline(is_integration_test=True)
+    read_pipeline = TestPipeline(is_integration_test=True)
+    output = '/'.join([write_pipeline.get_option('output'),
+                       self.uuid, 'avro-files'])
+    files_output = '/'.join([output, 'avro'])
+
+    num_records = write_pipeline.get_option('records')
+    num_records = int(num_records) if num_records else 10000
+
+    # Seed a `PCollection` with indices that will each be FlatMap'd into
+    # `batch_size` records, to avoid having a too-large list in memory at
+    # the outset
+    batch_size = write_pipeline.get_option('batch-size')
+    batch_size = int(batch_size) if batch_size else 1000
+
+    assert batch_size < num_records
+    # pylint: disable=range-builtin-not-iterating
+    batches = range(int(num_records / batch_size))
+
+    def batch_indices(start):
+      # pylint: disable=range-builtin-not-iterating
+      return range(start * batch_size, (start + 1) * batch_size)
+
+    # A `PCollection` with `num_records` avro records
+    _ = (write_pipeline
+         | 'CreateBatches' >> Create(batches)
+         | 'ExpandBatches' >> FlatMap(batch_indices)
+         | 'Double' >> Map(lambda x: 2 * x)
+         | 'CreateRecords' >> Map(record)
+         | 'WriteAvro' >> WriteToAvro(files_output,
+                                      self.SCHEMA,
+                                      file_name_suffix=".avro",
+                                      use_fastavro=self.use_fastavro)
+        )
+
+    write_result = write_pipeline.run()
+    write_result.wait_until_finish()
+    assert write_result.state == PipelineState.DONE
+
+    def check(x, mean):
+      if not x == mean:
+        raise BeamAssertException('Assertion failed: %s == %s' % (x, mean))
+
+    def check_distr(list):
+      if not len(set(list)) == 1:
+        raise BeamAssertException(
+            'Labels should be equally distributed: %s ' % (list))
+
+    def check_concat(concatenated_string):
+      split_concatenated_string = concatenated_string.split(" ")
+      for i in range(len(batches) * batch_size):
+        if str(2 * i) not in split_concatenated_string:
+          raise BeamAssertException(
+              'String should be in concatenation: %s' % str(2 * i))
+
+    def check_for_none(list):
+      has_none = False
 
 Review comment:
   We could simplify this to:
   ```
   if None not in list:
     raise ...
   ```
   also, list is also a type, let's chose a different name. 
 
----------------------------------------------------------------
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.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


Issue Time Tracking
-------------------

    Worklog Id:     (was: 273390)
    Time Spent: 4.5h  (was: 4h 20m)

> Improve Avro IO integration test coverage on Python 3.
> ------------------------------------------------------
>
>                 Key: BEAM-7455
>                 URL: https://issues.apache.org/jira/browse/BEAM-7455
>             Project: Beam
>          Issue Type: Sub-task
>          Components: io-python-avro
>            Reporter: Valentyn Tymofieiev
>            Assignee: Frederik Bode
>            Priority: Major
>          Time Spent: 4.5h
>  Remaining Estimate: 0h
>
> It seems that we don't have an integration test for Avro IO on Python 3:
> fastavro_it_test [1] depends on both avro and fastavro, however avro package 
> currently does not work with Beam on Python 3, so we don't have an 
> integration test that exercises Avro IO on Python 3. 
> We should add an integration test for Avro IO that does not need both 
> libraries at the same time, and instead can run using either library. 
> [~frederik] is this something you could help with? 
> cc: [~chamikara] [~Juta]
> [1] 
> https://github.com/apache/beam/blob/master/sdks/python/apache_beam/examples/fastavro_it_test.py



--
This message was sent by Atlassian JIRA
(v7.6.3#76005)

Reply via email to