Skip to content

Commit d9921bd

Browse files
committed
Add unit test for named tuple and dataclasses encoded by RowCoder and passing through GBK
1 parent bece353 commit d9921bd

1 file changed

Lines changed: 86 additions & 0 deletions

File tree

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
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 Beam Row typing functionality."""
19+
20+
from dataclasses import dataclass
21+
import typing
22+
import unittest
23+
24+
import apache_beam as beam
25+
from apache_beam.testing.test_pipeline import TestPipeline
26+
from apache_beam.testing.util import assert_that
27+
from apache_beam.testing.util import equal_to
28+
from apache_beam.typehints import row_type
29+
30+
31+
class RowTypeTest(unittest.TestCase):
32+
@staticmethod
33+
def _check_key_type_and_count(x) -> int:
34+
key_type = type(x[0])
35+
if not row_type._user_type_is_generated(key_type):
36+
raise RuntimeError("Expect type after GBK to be generated user type")
37+
38+
return len(x[1])
39+
40+
def test_group_by_key_namedtuple(self):
41+
MyNamedTuple = typing.NamedTuple(
42+
"MyNamedTuple", [("id", int), ("name", str)])
43+
44+
beam.coders.typecoders.registry.register_coder(
45+
MyNamedTuple, beam.coders.RowCoder)
46+
47+
def generate(num: int):
48+
for i in range(100):
49+
yield (MyNamedTuple(i, 'a'), num)
50+
51+
pipeline = TestPipeline(is_integration_test=False)
52+
53+
with pipeline as p:
54+
result = (
55+
p
56+
| 'Create' >> beam.Create([i for i in range(10)])
57+
| 'Generate' >> beam.ParDo(generate).with_output_types(
58+
tuple[MyNamedTuple, int])
59+
| 'GBK' >> beam.GroupByKey()
60+
| 'Count Elements' >> beam.Map(self._check_key_type_and_count))
61+
assert_that(result, equal_to([10] * 100))
62+
63+
def test_group_by_key_dataclass(self):
64+
@dataclass
65+
class MyDataClass:
66+
id: int
67+
name: str
68+
69+
beam.coders.typecoders.registry.register_coder(
70+
MyDataClass, beam.coders.RowCoder)
71+
72+
def generate(num: int):
73+
for i in range(100):
74+
yield (MyDataClass(i, 'a'), num)
75+
76+
pipeline = TestPipeline(is_integration_test=False)
77+
78+
with pipeline as p:
79+
result = (
80+
p
81+
| 'Create' >> beam.Create([i for i in range(10)])
82+
| 'Generate' >> beam.ParDo(generate).with_output_types(
83+
tuple[MyDataClass, int])
84+
| 'GBK' >> beam.GroupByKey()
85+
| 'Count Elements' >> beam.Map(self._check_key_type_and_count))
86+
assert_that(result, equal_to([10] * 100))

0 commit comments

Comments
 (0)