Fokko commented on code in PR #4920:
URL: https://github.com/apache/iceberg/pull/4920#discussion_r890239566
##########
python/tests/utils/test_schema_conversion.py:
##########
@@ -229,3 +234,127 @@ def test_avro_list_required_record():
iceberg_schema = AvroSchemaConversion().avro_to_iceberg(avro_schema)
assert expected_iceberg_schema == iceberg_schema
+
+
+def test_resolve_union():
+ with pytest.raises(TypeError) as exc_info:
+ AvroSchemaConversion()._resolve_union(["null", "string", "long"])
+
+ assert "Non-optional types aren't part of the Iceberg specification" in
str(exc_info.value)
+
+
+def test_nested_type():
+ # In the case a primitive field is nested
+ assert AvroSchemaConversion()._convert_schema({"type": {"type":
"string"}}) == StringType()
+
+
+def test_map_type():
+ avro_type = {
+ "type": "map",
+ "values": ["long", "null"],
+ "key-id": 101,
+ "value-id": 102,
+ }
+ actual = AvroSchemaConversion()._convert_schema(avro_type)
+ expected = MapType(key_id=101, key_type=StringType(), value_id=102,
value_type=LongType(), value_is_optional=True)
+ assert actual == expected
+
+
+def test_fixed_type():
+ avro_type = {"type": "fixed", "size": 22}
+ actual = AvroSchemaConversion()._convert_schema(avro_type)
+ expected = FixedType(22)
+ assert actual == expected
+
+
+def test_unknown_primitive():
+ with pytest.raises(TypeError) as exc_info:
+ avro_type = "UnknownType"
+ AvroSchemaConversion()._convert_schema(avro_type)
+ assert "Unknown type: UnknownType" in str(exc_info.value)
+
+
+def test_unknown_complex_type():
+ with pytest.raises(TypeError) as exc_info:
+ avro_type = {
+ "type": "UnknownType",
+ }
+ AvroSchemaConversion()._convert_schema(avro_type)
+ assert "Unknown type: {'type': 'UnknownType'}" in str(exc_info.value)
+
+
+def test_convert_field_without_field_id():
+ with pytest.raises(ValueError) as exc_info:
+ avro_field = {
+ "name": "contains_null",
+ "type": "boolean",
+ }
+ AvroSchemaConversion()._convert_field(avro_field)
+ assert "Cannot convert field, missing field-id" in str(exc_info.value)
+
+
+def test_convert_record_type_without_record():
+ with pytest.raises(ValueError) as exc_info:
+ avro_field = {"type": "non-record", "name": "avro_schema", "fields":
[]}
+ AvroSchemaConversion()._convert_record_type(avro_field)
+ assert "Expected record type, got" in str(exc_info.value)
+
+
+def test_avro_list_missing_element_id():
+ avro_type = {
+ "name": "array_with_string",
+ "type": {
+ "type": "array",
+ "items": "string",
+ "default": [],
+ # "element-id": 101,
+ },
+ "field-id": 100,
+ }
+
+ with pytest.raises(ValueError) as exc_info:
+ AvroSchemaConversion()._convert_array_type(avro_type)
+
+ assert "Cannot convert array-type, missing element-id:" in
str(exc_info.value)
+
+
+def test_convert_decimal_type():
+ avro_decimal_type = {"type": "bytes", "logicalType": "decimal",
"precision": 19, "scale": 25}
+ actual = AvroSchemaConversion()._convert_logical_type(avro_decimal_type)
+ expected = DecimalType(precision=19, scale=25)
+ assert actual == expected
+
+
+def test_convert_date_type():
+ avro_logical_type = {"type": "int", "logicalType": "date"}
+ actual = AvroSchemaConversion()._convert_logical_type(avro_logical_type)
+ assert actual == DateType()
+
+
+def test_unknown_logical_type():
Review Comment:
Added it, thanks!
--
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: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]