You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@avro.apache.org by mg...@apache.org on 2022/10/20 08:27:29 UTC

[avro] 01/01: AVRO-3646: Add a unit test for serializing Enum with different kind of variants

This is an automated email from the ASF dual-hosted git repository.

mgrigorov pushed a commit to branch avro-3646-serde-for-enum-mixed-variants
in repository https://gitbox.apache.org/repos/asf/avro.git

commit df6d62868c3221cea2d7848a546e92f27200866d
Author: Martin Tzvetanov Grigorov <mg...@apache.org>
AuthorDate: Thu Oct 20 11:26:44 2022 +0300

    AVRO-3646: Add a unit test for serializing Enum with different kind of variants
    
    Signed-off-by: Martin Tzvetanov Grigorov <mg...@apache.org>
---
 lang/rust/avro/src/ser.rs | 64 +++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 64 insertions(+)

diff --git a/lang/rust/avro/src/ser.rs b/lang/rust/avro/src/ser.rs
index c85c8c4d3..16f56d703 100644
--- a/lang/rust/avro/src/ser.rs
+++ b/lang/rust/avro/src/ser.rs
@@ -998,4 +998,68 @@ mod tests {
             "error serializing tuple untagged enum"
         );
     }
+
+    #[test]
+    fn avro_3646_test_to_value_null_enum() {
+        #[derive(Debug, Deserialize, Serialize, PartialEq, Eq)]
+        struct TestNullExternalEnum2 {
+            a: NullExternalEnum2,
+        }
+
+        #[derive(Debug, Deserialize, Serialize, PartialEq, Eq)]
+        enum NullExternalEnum2 {
+            Val1,
+            Val2(),
+            Val3(u64),
+        }
+
+        let test_val1 = TestNullExternalEnum2 {
+            a: NullExternalEnum2::Val1,
+        };
+
+        let expected_val1 =
+            Value::Record(vec![("a".to_owned(), Value::Enum(0, "Val1".to_owned()))]);
+
+        let actual_val1 = to_value(test_val1).unwrap();
+        assert_eq!(
+            actual_val1, expected_val1,
+            "error serializing null external enum, val1"
+        );
+
+        let test_val2 = TestNullExternalEnum2 {
+            a: NullExternalEnum2::Val2(),
+        };
+
+        let expected_val2 = Value::Record(vec![(
+            "a".to_owned(),
+            Value::Record(vec![
+                ("type".to_owned(), Value::Enum(1, "Val2".to_owned())),
+                ("value".to_owned(), Value::Array(vec![])),
+            ]),
+        )]);
+
+        let actual_val2 = to_value(test_val2).unwrap();
+        assert_eq!(
+            actual_val2, expected_val2,
+            "error serializing null external enum, val2"
+        );
+
+        let test_val3 = TestNullExternalEnum2 {
+            a: NullExternalEnum2::Val3(123),
+        };
+
+        let expected_val3 = Value::Record(vec![(
+            "a".to_owned(),
+            Value::Record(vec![
+                ("type".to_owned(), Value::Enum(2, "Val3".to_owned())),
+                ("value".to_owned(), Value::Union(2, Value::Long(123).into())),
+            ]),
+        )]);
+
+        let actual_val3 = to_value(test_val3).unwrap();
+        assert_eq!(
+            actual_val3, expected_val3,
+            "error serializing null external enum, val3"
+        );
+    }
 }