PookieBuns commented on code in PR #458:
URL: https://github.com/apache/avro-rs/pull/458#discussion_r2789342482


##########
avro/tests/nullable_union.rs:
##########
@@ -0,0 +1,982 @@
+use apache_avro::{AvroResult, Reader, Schema, Writer, from_value, 
types::Value};
+use serde::de::DeserializeOwned;
+use serde::{Deserialize, Serialize};
+
+struct TestCase<'a, T> {
+    input: T,
+    schema: &'a Schema,
+    expected_avro: &'a Value,
+}
+
+fn test_serialize<T>(test_case: TestCase<T>) -> AvroResult<()>
+where
+    T: Serialize + std::fmt::Debug,
+{
+    let mut writer = Writer::new(test_case.schema, Vec::new())?;
+    writer.append_ser(&test_case.input)?;
+    let bytes = writer.into_inner()?;
+    let mut reader = Reader::with_schema(test_case.schema, &bytes[..])?;
+    let read_avro_value = reader.next().unwrap()?;
+    assert_eq!(
+        &read_avro_value, test_case.expected_avro,
+        "serialization is not correct: expected: {:?}, got: {:?}, input: {:?}",
+        test_case.expected_avro, read_avro_value, test_case.input
+    );
+    Ok(())
+}
+
+fn test_deserialize<T>(test_case: TestCase<T>) -> AvroResult<()>
+where
+    T: DeserializeOwned + std::fmt::Debug + std::cmp::PartialEq,
+{
+    let deserialized: T = from_value(test_case.expected_avro)?;
+    assert_eq!(
+        deserialized, test_case.input,
+        "deserialization is not correct: expected: {:?}, got: {:?}",
+        test_case.input, deserialized,
+    );
+    Ok(())
+}
+
+mod nullable_enum {
+    use super::*;
+
+    const NULLABLE_ENUM_SCHEMA: &str = r#"
+    {
+        "name": "MyUnion",
+        "type": [
+            "null",
+            {
+                "type": "enum",
+                "name": "MyEnum",
+                "symbols": ["A", "B"]
+            }
+        ]
+    }
+    "#;
+
+    #[derive(Debug, Serialize, Deserialize, PartialEq)]
+    enum MyEnum {
+        A,
+        B,
+    }
+
+    #[derive(Debug, Serialize, Deserialize, PartialEq)]
+    enum MyUnionNullable {
+        Null,
+        MyEnum(MyEnum),
+    }
+
+    #[derive(Debug, Serialize, Deserialize, PartialEq)]
+    enum MyUnionAvroJsonEncoding {
+        MyEnum(MyEnum),
+    }
+
+    fn schema() -> Schema {
+        Schema::parse_str(NULLABLE_ENUM_SCHEMA).unwrap()
+    }
+
+    fn null_variant_expected_avro() -> Value {
+        Value::Union(0, Box::new(Value::Null))
+    }
+
+    fn a_variant_expected_avro() -> Value {
+        Value::Union(1, Box::new(Value::Enum(0, "A".to_string())))
+    }
+
+    #[test]
+    fn serialize_null_variant_enum_null() -> AvroResult<()> {
+        test_serialize(TestCase {
+            input: MyUnionNullable::Null,
+            schema: &schema(),
+            expected_avro: &null_variant_expected_avro(),
+        })
+    }
+
+    #[test]
+    fn deserialize_null_variant_enum_null() -> AvroResult<()> {
+        test_deserialize(TestCase {
+            input: MyUnionNullable::Null,
+            schema: &schema(),
+            expected_avro: &null_variant_expected_avro(),
+        })
+    }
+
+    #[test]
+    fn serialize_rusty_null() -> AvroResult<()> {
+        test_serialize(TestCase {
+            input: None::<MyEnum>,
+            schema: &schema(),
+            expected_avro: &null_variant_expected_avro(),
+        })
+    }
+
+    #[test]
+    fn deserialize_rusty_null() -> AvroResult<()> {
+        test_deserialize(TestCase {
+            input: None::<MyEnum>,
+            schema: &schema(),
+            expected_avro: &null_variant_expected_avro(),
+        })
+    }
+
+    #[test]
+    fn serialize_avro_json_encoding_compatible_null() -> AvroResult<()> {
+        test_serialize(TestCase {
+            input: None::<MyUnionAvroJsonEncoding>,
+            schema: &schema(),
+            expected_avro: &null_variant_expected_avro(),
+        })
+    }
+
+    #[test]
+    fn deserialize_avro_json_encoding_compatible_null() -> AvroResult<()> {
+        test_deserialize(TestCase {
+            input: None::<MyUnionAvroJsonEncoding>,
+            schema: &schema(),
+            expected_avro: &null_variant_expected_avro(),
+        })
+    }
+
+    #[test]
+    fn serialize_null_variant_enum_my_enum_a() -> AvroResult<()> {
+        test_serialize(TestCase {
+            input: MyUnionNullable::MyEnum(MyEnum::A),
+            schema: &schema(),
+            expected_avro: &a_variant_expected_avro(),
+        })
+    }
+
+    #[test]
+    fn deserialize_null_variant_enum_my_enum_a() -> AvroResult<()> {
+        test_deserialize(TestCase {
+            input: MyUnionNullable::MyEnum(MyEnum::A),
+            schema: &schema(),
+            expected_avro: &a_variant_expected_avro(),
+        })
+    }
+
+    #[test]
+    fn serialize_rusty_my_enum_a() -> AvroResult<()> {
+        test_serialize(TestCase {
+            input: Some(MyEnum::A),
+            schema: &schema(),
+            expected_avro: &a_variant_expected_avro(),
+        })
+    }
+
+    #[test]
+    fn deserialize_rusty_my_enum_a() -> AvroResult<()> {
+        test_deserialize(TestCase {
+            input: Some(MyEnum::A),
+            schema: &schema(),
+            expected_avro: &a_variant_expected_avro(),
+        })
+    }
+
+    #[test]
+    fn serialize_avro_json_encoding_compatible_my_enum_a() -> AvroResult<()> {
+        test_serialize(TestCase {
+            input: Some(MyUnionAvroJsonEncoding::MyEnum(MyEnum::A)),
+            schema: &schema(),
+            expected_avro: &a_variant_expected_avro(),
+        })
+    }
+
+    #[test]
+    fn deserialize_avro_json_encoding_compatible_my_enum_a() -> AvroResult<()> 
{
+        test_deserialize(TestCase {
+            input: Some(MyUnionAvroJsonEncoding::MyEnum(MyEnum::A)),
+            schema: &schema(),
+            expected_avro: &a_variant_expected_avro(),
+        })
+    }
+}
+
+mod nullable_primitive_int {
+    use super::*;
+
+    const NULLABLE_INT_SCHEMA: &str = r#"
+    {
+        "name": "MyUnion",
+        "type": [
+            "null",
+            "int"

Review Comment:
   Added tests in 
https://github.com/apache/avro-rs/pull/458/changes/6f691dbbcf2211eb5c112cda90759f7f76c0d44c
 to ensure null as non-first variant when using Option<T> fails with error 
message communicating intended behavior



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

Reply via email to