You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@beam.apache.org by GitBox <gi...@apache.org> on 2022/08/31 19:29:51 UTC

[GitHub] [beam] TheNeuralBit commented on a diff in pull request #22233: Trying out property-based tests for Beam python coders

TheNeuralBit commented on code in PR #22233:
URL: https://github.com/apache/beam/pull/22233#discussion_r959943053


##########
sdks/python/apache_beam/coders/coders_property_based_test.py:
##########
@@ -0,0 +1,138 @@
+#
+# 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.
+#
+
+"""Property tests for coders in the Python SDK.
+
+The tests in this file utilize the hypothesis library to generate random test
+cases and run them against Beam's coder implementations.
+
+These tests are similar to fuzzing, except they test invariant properties
+of code.
+"""
+
+import math
+import typing
+import unittest
+# TODO(pabloem): Include other categories of characters
+from datetime import datetime
+from string import ascii_letters
+from string import digits
+
+import numpy as np
+from hypothesis import strategies as st
+from hypothesis import assume
+from hypothesis import given
+from hypothesis import settings
+from pytz import utc
+
+from apache_beam.coders import FloatCoder
+from apache_beam.coders import RowCoder
+from apache_beam.coders import StrUtf8Coder
+from apache_beam.coders.typecoders import registry as coders_registry
+from apache_beam.typehints.schemas import typing_to_runner_api
+from apache_beam.utils.timestamp import Timestamp
+
+SCHEMA_TYPES = [str, bytes, Timestamp, int, np.int32, np.int64, bool]
+
+SCHEMA_TYPES_TO_STRATEGY = {
+    str: st.text(),
+    bytes: st.binary(),
+    # Maximum datetime on year 3000 to conform to Windows OS limits.
+    Timestamp: st.datetimes(
+        min_value=datetime(1970, 1, 1, 1, 1),
+        max_value=datetime(
+            3000, 1, 1, 0,
+            0)).map(lambda dt: Timestamp.from_utc_datetime(dt.astimezone(utc))),
+    int: st.integers(min_value=-(1 << 63 - 1), max_value=1 << 63 - 1),
+    np.int32: st.integers(min_value=-(1 << 31 - 1), max_value=1 << 31 - 1),
+    np.int64: st.integers(min_value=-(1 << 63 - 1), max_value=1 << 63 - 1),
+    np.uint32: st.integers(min_value=0, max_value=1 << 32 - 1),
+    np.uint64: st.integers(min_value=0, max_value=1 << 64 - 1),

Review Comment:
   Is it intentional to have unsigned ints here? They're not supported in Beam schemas.



##########
sdks/python/apache_beam/coders/coders_property_based_test.py:
##########
@@ -0,0 +1,138 @@
+#
+# 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.
+#
+
+"""Property tests for coders in the Python SDK.
+
+The tests in this file utilize the hypothesis library to generate random test
+cases and run them against Beam's coder implementations.
+
+These tests are similar to fuzzing, except they test invariant properties
+of code.
+"""
+
+import math
+import typing
+import unittest
+# TODO(pabloem): Include other categories of characters
+from datetime import datetime
+from string import ascii_letters
+from string import digits
+
+import numpy as np
+from hypothesis import strategies as st
+from hypothesis import assume
+from hypothesis import given
+from hypothesis import settings
+from pytz import utc
+
+from apache_beam.coders import FloatCoder
+from apache_beam.coders import RowCoder
+from apache_beam.coders import StrUtf8Coder
+from apache_beam.coders.typecoders import registry as coders_registry
+from apache_beam.typehints.schemas import typing_to_runner_api
+from apache_beam.utils.timestamp import Timestamp
+
+SCHEMA_TYPES = [str, bytes, Timestamp, int, np.int32, np.int64, bool]

Review Comment:
   It would be nice to have a way to keep this up to date as new types are added in schemas.py. Perhaps a test that checks this set is equivalent to the keys in PRIMITIVE_TO_ATOMIC_TYPE: https://github.com/apache/beam/blob/149ed074428ff9b5344169da7d54e8ee271aaba1/sdks/python/apache_beam/typehints/schemas.py#L104
   
   Plus the registered logical types: https://github.com/apache/beam/blob/149ed074428ff9b5344169da7d54e8ee271aaba1/sdks/python/apache_beam/typehints/schemas.py#L505



##########
sdks/python/apache_beam/coders/coders_property_based_test.py:
##########
@@ -0,0 +1,138 @@
+#
+# 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.
+#
+
+"""Property tests for coders in the Python SDK.
+
+The tests in this file utilize the hypothesis library to generate random test
+cases and run them against Beam's coder implementations.
+
+These tests are similar to fuzzing, except they test invariant properties
+of code.
+"""
+
+import math
+import typing
+import unittest
+# TODO(pabloem): Include other categories of characters
+from datetime import datetime
+from string import ascii_letters
+from string import digits
+
+import numpy as np
+from hypothesis import strategies as st
+from hypothesis import assume
+from hypothesis import given
+from hypothesis import settings
+from pytz import utc
+
+from apache_beam.coders import FloatCoder
+from apache_beam.coders import RowCoder
+from apache_beam.coders import StrUtf8Coder
+from apache_beam.coders.typecoders import registry as coders_registry
+from apache_beam.typehints.schemas import typing_to_runner_api
+from apache_beam.utils.timestamp import Timestamp
+
+SCHEMA_TYPES = [str, bytes, Timestamp, int, np.int32, np.int64, bool]
+
+SCHEMA_TYPES_TO_STRATEGY = {
+    str: st.text(),
+    bytes: st.binary(),
+    # Maximum datetime on year 3000 to conform to Windows OS limits.
+    Timestamp: st.datetimes(
+        min_value=datetime(1970, 1, 1, 1, 1),
+        max_value=datetime(
+            3000, 1, 1, 0,
+            0)).map(lambda dt: Timestamp.from_utc_datetime(dt.astimezone(utc))),
+    int: st.integers(min_value=-(1 << 63 - 1), max_value=1 << 63 - 1),
+    np.int32: st.integers(min_value=-(1 << 31 - 1), max_value=1 << 31 - 1),
+    np.int64: st.integers(min_value=-(1 << 63 - 1), max_value=1 << 63 - 1),
+    np.uint32: st.integers(min_value=0, max_value=1 << 32 - 1),
+    np.uint64: st.integers(min_value=0, max_value=1 << 64 - 1),
+    bool: st.booleans()
+}
+
+# A hypothesis strategy that generates schemas.
+# A schema is a list containing tuples of strings (field names), types (field
+# types) and boolean (nullable or not).
+# This strategy currently generates rows with simple types (i.e. non-list, and
+# non-map fields).
+SCHEMA_GENERATOR_STRATEGY = st.lists(
+    st.tuples(
+        st.text(ascii_letters + digits + '_', min_size=1),
+        st.sampled_from(SCHEMA_TYPES),
+        st.booleans()))
+
+
+class ProperyTestingCoders(unittest.TestCase):
+  @given(st.text())
+  def test_string_coder(self, txt: str):
+    coder = StrUtf8Coder()
+    self.assertEqual(coder.decode(coder.encode(txt)), txt)
+
+  @given(st.floats())
+  def test_float_coder(self, num: float):
+    coder = FloatCoder()
+    test_num = coder.decode(coder.encode(num))
+    if math.isnan(num):
+      # This special branch is needed because by definition
+      # nan != nan.
+      self.assertTrue(math.isnan(test_num))
+    else:
+      self.assertEqual(coder.decode(coder.encode(num)), num)
+
+  @settings(deadline=None, print_blob=True)
+  @given(st.data())
+  def test_row_coder(self, data: st.DataObject):
+    """Generate rows and schemas, and test their encoding/decoding.
+
+    The schemas are generated based on the SCHEMA_GENERATOR_STRATEGY.
+    """
+    schema = data.draw(SCHEMA_GENERATOR_STRATEGY)
+    # Assume that the cardinality of the set of names is the same
+    # as the length of the schema. This means there's no duplicate
+    # names for fields.
+    # If this condition does not hold, then we must not continue the
+    # test.
+    assume(len({name for name, _, _ in schema}) == len(schema))
+    assume(
+        len({n[0]
+             for n, _, _ in schema}.intersection(set(digits + '_'))) == 0)
+    RowType = typing.NamedTuple(  # type: ignore
+        'RandomRowType',
+        [(name, type_ if not nullable else typing.Optional[type_]) for name,
+         type_,
+         nullable in schema])

Review Comment:
   Another potential follow-up: Add a test that's parameterized by the schema proto instead of a generated row type. There are some schemas that we can't generate natively in Python, but we can get from other SDKs in the form of schema protos, and we need to be able to handle them. (For example, the millis_instant type that @Abacn just added).



##########
sdks/python/apache_beam/coders/coders_property_based_test.py:
##########
@@ -0,0 +1,138 @@
+#
+# 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.
+#
+
+"""Property tests for coders in the Python SDK.
+
+The tests in this file utilize the hypothesis library to generate random test
+cases and run them against Beam's coder implementations.
+
+These tests are similar to fuzzing, except they test invariant properties
+of code.
+"""
+
+import math
+import typing
+import unittest
+# TODO(pabloem): Include other categories of characters
+from datetime import datetime
+from string import ascii_letters
+from string import digits
+
+import numpy as np
+from hypothesis import strategies as st
+from hypothesis import assume
+from hypothesis import given
+from hypothesis import settings
+from pytz import utc
+
+from apache_beam.coders import FloatCoder
+from apache_beam.coders import RowCoder
+from apache_beam.coders import StrUtf8Coder
+from apache_beam.coders.typecoders import registry as coders_registry
+from apache_beam.typehints.schemas import typing_to_runner_api
+from apache_beam.utils.timestamp import Timestamp
+
+SCHEMA_TYPES = [str, bytes, Timestamp, int, np.int32, np.int64, bool]
+
+SCHEMA_TYPES_TO_STRATEGY = {
+    str: st.text(),
+    bytes: st.binary(),
+    # Maximum datetime on year 3000 to conform to Windows OS limits.
+    Timestamp: st.datetimes(
+        min_value=datetime(1970, 1, 1, 1, 1),
+        max_value=datetime(
+            3000, 1, 1, 0,
+            0)).map(lambda dt: Timestamp.from_utc_datetime(dt.astimezone(utc))),
+    int: st.integers(min_value=-(1 << 63 - 1), max_value=1 << 63 - 1),
+    np.int32: st.integers(min_value=-(1 << 31 - 1), max_value=1 << 31 - 1),
+    np.int64: st.integers(min_value=-(1 << 63 - 1), max_value=1 << 63 - 1),
+    np.uint32: st.integers(min_value=0, max_value=1 << 32 - 1),
+    np.uint64: st.integers(min_value=0, max_value=1 << 64 - 1),
+    bool: st.booleans()
+}
+
+# A hypothesis strategy that generates schemas.
+# A schema is a list containing tuples of strings (field names), types (field
+# types) and boolean (nullable or not).
+# This strategy currently generates rows with simple types (i.e. non-list, and
+# non-map fields).
+SCHEMA_GENERATOR_STRATEGY = st.lists(
+    st.tuples(
+        st.text(ascii_letters + digits + '_', min_size=1),
+        st.sampled_from(SCHEMA_TYPES),
+        st.booleans()))
+
+
+class ProperyTestingCoders(unittest.TestCase):
+  @given(st.text())
+  def test_string_coder(self, txt: str):
+    coder = StrUtf8Coder()
+    self.assertEqual(coder.decode(coder.encode(txt)), txt)
+
+  @given(st.floats())
+  def test_float_coder(self, num: float):
+    coder = FloatCoder()
+    test_num = coder.decode(coder.encode(num))
+    if math.isnan(num):
+      # This special branch is needed because by definition
+      # nan != nan.
+      self.assertTrue(math.isnan(test_num))
+    else:
+      self.assertEqual(coder.decode(coder.encode(num)), num)
+
+  @settings(deadline=None, print_blob=True)
+  @given(st.data())
+  def test_row_coder(self, data: st.DataObject):
+    """Generate rows and schemas, and test their encoding/decoding.
+
+    The schemas are generated based on the SCHEMA_GENERATOR_STRATEGY.
+    """
+    schema = data.draw(SCHEMA_GENERATOR_STRATEGY)
+    # Assume that the cardinality of the set of names is the same
+    # as the length of the schema. This means there's no duplicate
+    # names for fields.
+    # If this condition does not hold, then we must not continue the
+    # test.
+    assume(len({name for name, _, _ in schema}) == len(schema))
+    assume(
+        len({n[0]
+             for n, _, _ in schema}.intersection(set(digits + '_'))) == 0)
+    RowType = typing.NamedTuple(  # type: ignore
+        'RandomRowType',
+        [(name, type_ if not nullable else typing.Optional[type_]) for name,
+         type_,
+         nullable in schema])
+    coders_registry.register_coder(RowType, RowCoder)
+
+    # TODO(pabloem): Also apply nullability for these schemas.

Review Comment:
   Please file an issue and link here. We should also consider testing the structured types (list, map, nested row, ...) https://github.com/apache/beam/blob/149ed074428ff9b5344169da7d54e8ee271aaba1/model/pipeline/src/main/proto/org/apache/beam/model/pipeline/v1/schema.proto#L68-L72
   
   That could be part of the same issue or another issue



##########
sdks/python/apache_beam/coders/coders_property_based_test.py:
##########
@@ -0,0 +1,138 @@
+#
+# 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.
+#
+
+"""Property tests for coders in the Python SDK.
+
+The tests in this file utilize the hypothesis library to generate random test
+cases and run them against Beam's coder implementations.
+
+These tests are similar to fuzzing, except they test invariant properties
+of code.
+"""
+
+import math
+import typing
+import unittest
+# TODO(pabloem): Include other categories of characters
+from datetime import datetime
+from string import ascii_letters
+from string import digits
+
+import numpy as np
+from hypothesis import strategies as st
+from hypothesis import assume
+from hypothesis import given
+from hypothesis import settings
+from pytz import utc
+
+from apache_beam.coders import FloatCoder
+from apache_beam.coders import RowCoder
+from apache_beam.coders import StrUtf8Coder
+from apache_beam.coders.typecoders import registry as coders_registry
+from apache_beam.typehints.schemas import typing_to_runner_api
+from apache_beam.utils.timestamp import Timestamp
+
+SCHEMA_TYPES = [str, bytes, Timestamp, int, np.int32, np.int64, bool]
+
+SCHEMA_TYPES_TO_STRATEGY = {
+    str: st.text(),
+    bytes: st.binary(),
+    # Maximum datetime on year 3000 to conform to Windows OS limits.
+    Timestamp: st.datetimes(
+        min_value=datetime(1970, 1, 1, 1, 1),
+        max_value=datetime(
+            3000, 1, 1, 0,
+            0)).map(lambda dt: Timestamp.from_utc_datetime(dt.astimezone(utc))),
+    int: st.integers(min_value=-(1 << 63 - 1), max_value=1 << 63 - 1),
+    np.int32: st.integers(min_value=-(1 << 31 - 1), max_value=1 << 31 - 1),
+    np.int64: st.integers(min_value=-(1 << 63 - 1), max_value=1 << 63 - 1),
+    np.uint32: st.integers(min_value=0, max_value=1 << 32 - 1),
+    np.uint64: st.integers(min_value=0, max_value=1 << 64 - 1),
+    bool: st.booleans()
+}
+
+# A hypothesis strategy that generates schemas.
+# A schema is a list containing tuples of strings (field names), types (field
+# types) and boolean (nullable or not).
+# This strategy currently generates rows with simple types (i.e. non-list, and
+# non-map fields).
+SCHEMA_GENERATOR_STRATEGY = st.lists(
+    st.tuples(
+        st.text(ascii_letters + digits + '_', min_size=1),
+        st.sampled_from(SCHEMA_TYPES),
+        st.booleans()))
+
+
+class ProperyTestingCoders(unittest.TestCase):
+  @given(st.text())
+  def test_string_coder(self, txt: str):
+    coder = StrUtf8Coder()
+    self.assertEqual(coder.decode(coder.encode(txt)), txt)
+
+  @given(st.floats())
+  def test_float_coder(self, num: float):
+    coder = FloatCoder()
+    test_num = coder.decode(coder.encode(num))
+    if math.isnan(num):
+      # This special branch is needed because by definition
+      # nan != nan.
+      self.assertTrue(math.isnan(test_num))
+    else:
+      self.assertEqual(coder.decode(coder.encode(num)), num)
+
+  @settings(deadline=None, print_blob=True)
+  @given(st.data())
+  def test_row_coder(self, data: st.DataObject):
+    """Generate rows and schemas, and test their encoding/decoding.
+
+    The schemas are generated based on the SCHEMA_GENERATOR_STRATEGY.
+    """
+    schema = data.draw(SCHEMA_GENERATOR_STRATEGY)
+    # Assume that the cardinality of the set of names is the same
+    # as the length of the schema. This means there's no duplicate
+    # names for fields.
+    # If this condition does not hold, then we must not continue the
+    # test.
+    assume(len({name for name, _, _ in schema}) == len(schema))
+    assume(
+        len({n[0]
+             for n, _, _ in schema}.intersection(set(digits + '_'))) == 0)
+    RowType = typing.NamedTuple(  # type: ignore
+        'RandomRowType',
+        [(name, type_ if not nullable else typing.Optional[type_]) for name,
+         type_,
+         nullable in schema])
+    coders_registry.register_coder(RowType, RowCoder)
+
+    # TODO(pabloem): Also apply nullability for these schemas.
+    row = RowType(  # type: ignore
+        **{
+            name: data.draw(SCHEMA_TYPES_TO_STRATEGY[type_])
+            for name,
+            type_,
+            nullable in schema
+        })
+
+    expected_coder = RowCoder(typing_to_runner_api(RowType).row_type.schema)
+    real_coder = coders_registry.get_coder(RowType)
+    self.assertEqual(expected_coder.decode(expected_coder.encode(row)), row)
+    self.assertEqual(real_coder.decode(real_coder.encode(row)), row)
+    self.assertEqual(real_coder.encode(row), expected_coder.encode(row))

Review Comment:
   If you drop this you won't need to add RowType to the registry either.



##########
sdks/python/apache_beam/coders/coders_property_based_test.py:
##########
@@ -0,0 +1,138 @@
+#
+# 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.
+#
+
+"""Property tests for coders in the Python SDK.
+
+The tests in this file utilize the hypothesis library to generate random test
+cases and run them against Beam's coder implementations.
+
+These tests are similar to fuzzing, except they test invariant properties
+of code.
+"""
+
+import math
+import typing
+import unittest
+# TODO(pabloem): Include other categories of characters
+from datetime import datetime
+from string import ascii_letters
+from string import digits
+
+import numpy as np
+from hypothesis import strategies as st
+from hypothesis import assume
+from hypothesis import given
+from hypothesis import settings
+from pytz import utc
+
+from apache_beam.coders import FloatCoder
+from apache_beam.coders import RowCoder
+from apache_beam.coders import StrUtf8Coder
+from apache_beam.coders.typecoders import registry as coders_registry
+from apache_beam.typehints.schemas import typing_to_runner_api
+from apache_beam.utils.timestamp import Timestamp
+
+SCHEMA_TYPES = [str, bytes, Timestamp, int, np.int32, np.int64, bool]
+
+SCHEMA_TYPES_TO_STRATEGY = {
+    str: st.text(),
+    bytes: st.binary(),
+    # Maximum datetime on year 3000 to conform to Windows OS limits.
+    Timestamp: st.datetimes(
+        min_value=datetime(1970, 1, 1, 1, 1),
+        max_value=datetime(
+            3000, 1, 1, 0,
+            0)).map(lambda dt: Timestamp.from_utc_datetime(dt.astimezone(utc))),
+    int: st.integers(min_value=-(1 << 63 - 1), max_value=1 << 63 - 1),
+    np.int32: st.integers(min_value=-(1 << 31 - 1), max_value=1 << 31 - 1),
+    np.int64: st.integers(min_value=-(1 << 63 - 1), max_value=1 << 63 - 1),
+    np.uint32: st.integers(min_value=0, max_value=1 << 32 - 1),
+    np.uint64: st.integers(min_value=0, max_value=1 << 64 - 1),
+    bool: st.booleans()
+}
+
+# A hypothesis strategy that generates schemas.
+# A schema is a list containing tuples of strings (field names), types (field
+# types) and boolean (nullable or not).
+# This strategy currently generates rows with simple types (i.e. non-list, and
+# non-map fields).
+SCHEMA_GENERATOR_STRATEGY = st.lists(
+    st.tuples(
+        st.text(ascii_letters + digits + '_', min_size=1),
+        st.sampled_from(SCHEMA_TYPES),
+        st.booleans()))
+
+
+class ProperyTestingCoders(unittest.TestCase):
+  @given(st.text())
+  def test_string_coder(self, txt: str):
+    coder = StrUtf8Coder()
+    self.assertEqual(coder.decode(coder.encode(txt)), txt)
+
+  @given(st.floats())
+  def test_float_coder(self, num: float):
+    coder = FloatCoder()
+    test_num = coder.decode(coder.encode(num))
+    if math.isnan(num):
+      # This special branch is needed because by definition
+      # nan != nan.
+      self.assertTrue(math.isnan(test_num))
+    else:
+      self.assertEqual(coder.decode(coder.encode(num)), num)
+
+  @settings(deadline=None, print_blob=True)
+  @given(st.data())
+  def test_row_coder(self, data: st.DataObject):
+    """Generate rows and schemas, and test their encoding/decoding.
+
+    The schemas are generated based on the SCHEMA_GENERATOR_STRATEGY.
+    """
+    schema = data.draw(SCHEMA_GENERATOR_STRATEGY)
+    # Assume that the cardinality of the set of names is the same
+    # as the length of the schema. This means there's no duplicate
+    # names for fields.
+    # If this condition does not hold, then we must not continue the
+    # test.
+    assume(len({name for name, _, _ in schema}) == len(schema))
+    assume(
+        len({n[0]
+             for n, _, _ in schema}.intersection(set(digits + '_'))) == 0)
+    RowType = typing.NamedTuple(  # type: ignore
+        'RandomRowType',
+        [(name, type_ if not nullable else typing.Optional[type_]) for name,
+         type_,
+         nullable in schema])
+    coders_registry.register_coder(RowType, RowCoder)
+
+    # TODO(pabloem): Also apply nullability for these schemas.
+    row = RowType(  # type: ignore
+        **{
+            name: data.draw(SCHEMA_TYPES_TO_STRATEGY[type_])
+            for name,
+            type_,
+            nullable in schema
+        })
+
+    expected_coder = RowCoder(typing_to_runner_api(RowType).row_type.schema)
+    real_coder = coders_registry.get_coder(RowType)
+    self.assertEqual(expected_coder.decode(expected_coder.encode(row)), row)
+    self.assertEqual(real_coder.decode(real_coder.encode(row)), row)
+    self.assertEqual(real_coder.encode(row), expected_coder.encode(row))

Review Comment:
   ```suggestion
       coder = RowCoder(typing_to_runner_api(RowType).row_type.schema)
       self.assertEqual(coder.decode(coder.encode(row)), row)
   ```
   
   I don't think we need to replicate the expected_coder/real_coder silliness here. That was just me being overly cautious to make sure the registration works.



-- 
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