You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@iceberg.apache.org by GitBox <gi...@apache.org> on 2022/06/06 17:25:31 UTC

[GitHub] [iceberg] rdblue commented on a diff in pull request #4972: Python: Add more tests for schema_conversion

rdblue commented on code in PR #4972:
URL: https://github.com/apache/iceberg/pull/4972#discussion_r890369325


##########
python/tests/utils/test_schema_conversion.py:
##########
@@ -229,3 +234,128 @@ 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():
+    """Test raising a ValueError when converting an unknown logical type as part of an Avro schema conversion"""
+    avro_logical_type = {"type": "bytes", "logicalType": "date"}
+    with pytest.raises(ValueError) as exc_info:
+        AvroSchemaConversion()._convert_logical_type(avro_logical_type)
+
+    assert "Unknown logical/physical type combination:" in str(exc_info.value)
+
+
+def test_logical_map_with_invalid_fields():

Review Comment:
   Can you add a test for a valid logical map type?



-- 
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: issues-unsubscribe@iceberg.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org