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 2023/04/10 19:32:07 UTC

[avro] 01/01: AVRO-3735: Add a unit test

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

mgrigorov pushed a commit to branch avro-3735-parse-enum-ns
in repository https://gitbox.apache.org/repos/asf/avro.git

commit 580f63137e4a30659b72d439376a2e4b5385ce14
Author: Martin Tzvetanov Grigorov <mg...@apache.org>
AuthorDate: Mon Apr 10 22:31:21 2023 +0300

    AVRO-3735: Add a unit test
    
    Signed-off-by: Martin Tzvetanov Grigorov <mg...@apache.org>
---
 lang/rust/avro/src/schema.rs | 68 ++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 68 insertions(+)

diff --git a/lang/rust/avro/src/schema.rs b/lang/rust/avro/src/schema.rs
index 07f2ac3ac..5c0ce69de 100644
--- a/lang/rust/avro/src/schema.rs
+++ b/lang/rust/avro/src/schema.rs
@@ -4369,4 +4369,72 @@ mod tests {
             panic!("Expected a record schema!");
         }
     }
+
+    #[test]
+    fn avro_3735_parse_enum_namespace() {
+        let schema = r#"
+        {
+            "type": "record",
+            "name": "Foo",
+            "namespace": "name.space",
+            "fields":
+            [
+                {
+                    "name": "barInit",
+                    "type":
+                    {
+                        "type": "enum",
+                        "name": "Bar",
+                        "symbols":
+                        [
+                            "bar0",
+                            "bar1"
+                        ]
+                    }
+                },
+                {
+                    "name": "barUse",
+                    "type": "Bar"
+                }
+            ]
+        } 
+        "#;
+
+        #[derive(
+            Debug, PartialEq, Eq, Hash, PartialOrd, Ord, Clone, serde::Deserialize, serde::Serialize,
+        )]
+        pub enum Bar {
+            #[serde(rename = "bar0")]
+            Bar0,
+            #[serde(rename = "bar1")]
+            Bar1,
+        }
+
+        #[derive(Debug, PartialEq, Eq, Clone, serde::Deserialize, serde::Serialize)]
+        pub struct Foo {
+            #[serde(rename = "barInit")]
+            pub bar_init: Bar,
+            #[serde(rename = "barUse")]
+            pub bar_use: Bar,
+        }
+
+        let schema = Schema::parse_str(schema).unwrap();
+
+        let foo = Foo {
+            bar_init: Bar::Bar0,
+            bar_use: Bar::Bar1,
+        };
+
+        let avro_value = crate::to_value(foo).unwrap();
+        dbg!(&avro_value);
+        println!(
+            "value is valid for schema: {}",
+            avro_value.validate(&schema)
+        );
+
+        let mut writer = crate::Writer::new(&schema, Vec::new());
+
+        // schema validation happens here
+        writer.append(avro_value).unwrap();
+    }
 }