@@ -206,6 +206,54 @@ def test_avro_union_type_to_beam_type_with_string_long(self):
206206 expected_beam_type = schemas .typing_to_runner_api (Any )
207207 hc .assert_that (beam_type , hc .equal_to (expected_beam_type ))
208208
209+ def test_avro_union_type_to_beam_type_with_record_and_null (self ):
210+ record_type = {
211+ 'type' : 'record' ,
212+ 'name' : 'TestRecord' ,
213+ 'fields' : [{
214+ 'name' : 'field1' , 'type' : 'string'
215+ }, {
216+ 'name' : 'field2' , 'type' : 'int'
217+ }]
218+ }
219+ union_type = [record_type , 'null' ]
220+ beam_type = avro_union_type_to_beam_type (union_type )
221+ expected_beam_type = schema_pb2 .FieldType (
222+ row_type = schema_pb2 .RowType (
223+ schema = schema_pb2 .Schema (
224+ fields = [
225+ schemas .schema_field (
226+ 'field1' ,
227+ schema_pb2 .FieldType (atomic_type = schema_pb2 .STRING )),
228+ schemas .schema_field (
229+ 'field2' ,
230+ schema_pb2 .FieldType (atomic_type = schema_pb2 .INT32 ))
231+ ])),
232+ nullable = True )
233+ hc .assert_that (beam_type , hc .equal_to (expected_beam_type ))
234+
235+ def test_avro_union_type_to_beam_type_with_nullable_annotated_string (self ):
236+ annotated_string_type = {"avro.java.string" : "String" , "type" : "string" }
237+ union_type = ['null' , annotated_string_type ]
238+
239+ beam_type = avro_union_type_to_beam_type (union_type )
240+
241+ expected_beam_type = schema_pb2 .FieldType (
242+ atomic_type = schema_pb2 .STRING , nullable = True )
243+ hc .assert_that (beam_type , hc .equal_to (expected_beam_type ))
244+
245+ def test_avro_union_type_to_beam_type_with_only_null (self ):
246+ union_type = ['null' ]
247+ beam_type = avro_union_type_to_beam_type (union_type )
248+ expected_beam_type = schemas .typing_to_runner_api (Any )
249+ hc .assert_that (beam_type , hc .equal_to (expected_beam_type ))
250+
251+ def test_avro_union_type_to_beam_type_with_multiple_types (self ):
252+ union_type = ['null' , 'string' , 'int' ]
253+ beam_type = avro_union_type_to_beam_type (union_type )
254+ expected_beam_type = schemas .typing_to_runner_api (Any )
255+ hc .assert_that (beam_type , hc .equal_to (expected_beam_type ))
256+
209257 def test_avro_schema_to_beam_and_back (self ):
210258 avro_schema = fastavro .parse_schema (json .loads (self .SCHEMA_STRING ))
211259 beam_schema = avro_schema_to_beam_schema (avro_schema )
0 commit comments