-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c795a11
commit dae1655
Showing
5 changed files
with
97 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
# Generated by Cargo | ||
# will have compiled files and executables | ||
/target/ | ||
|
||
tarpaulin-report.html |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
#[cfg(test)] | ||
mod tests { | ||
use std::{fs, str::FromStr, sync::Arc}; | ||
|
||
use apache_avro::Schema as ApacheAvroSchema; | ||
use async_trait::async_trait; | ||
use serde_json::Value as JsonValue; | ||
|
||
use crate::lib::{ | ||
avro::{error::AvroResult, AvroParser, SchemaProvider}, | ||
schema_registry::ResolvedAvroSchema, | ||
}; | ||
|
||
#[tokio::test] | ||
async fn test_unnested() { | ||
test_parsing_loop("1_schema.json", "1_good_input.json").await | ||
} | ||
|
||
/// test fixture | ||
struct MockSchemaRegistry { | ||
schema: String, | ||
} | ||
|
||
#[async_trait] | ||
impl SchemaProvider for MockSchemaRegistry { | ||
async fn get_schema_by_id(&self, id: i32) -> AvroResult<ResolvedAvroSchema> { | ||
Ok(ResolvedAvroSchema::from( | ||
id, | ||
ApacheAvroSchema::parse_str(&self.schema).unwrap(), | ||
)) | ||
} | ||
async fn get_schema_by_name(&self, _name: &str) -> AvroResult<ResolvedAvroSchema> { | ||
Ok(ResolvedAvroSchema::from( | ||
123, | ||
ApacheAvroSchema::parse_str(&self.schema).unwrap(), | ||
)) | ||
} | ||
} | ||
|
||
async fn test_parsing_loop(schema_file_name: &str, test_file_name: &str) { | ||
let avro_json_in = fs::read_to_string(format!("src/lib/avro/test_files/{}", test_file_name)).unwrap(); | ||
let schema = fs::read_to_string(format!("src/lib/avro/test_files/{}", schema_file_name)).unwrap(); | ||
let sut = AvroParser::new(Arc::new(MockSchemaRegistry { schema })); | ||
|
||
// act/assert | ||
let json_to_avro_result = sut.json_to_avro(&avro_json_in, "schema_name").await; | ||
|
||
assert!(json_to_avro_result.is_ok()); | ||
|
||
let avro_to_json_result = sut.avro_to_json(&json_to_avro_result.unwrap()).await; | ||
|
||
assert!(avro_to_json_result.is_ok()); | ||
|
||
assert_eq!( | ||
JsonValue::from_str(&avro_to_json_result.unwrap()).unwrap(), | ||
JsonValue::from_str(&avro_json_in).unwrap() | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
{ | ||
"NullField": null, | ||
"BooleanField": true, | ||
"IntField": 123, | ||
"LongField": 321123321, | ||
"FloatField": 1111.25, | ||
"DoubleField": 11123.123, | ||
"StringField": "this is a test string", | ||
"EnumField": "DIAMONDS", | ||
"ArrayField": [1, 2, 3, 4, 5, 6, 100], | ||
"MapField": { | ||
"f1": "Example map field 1", | ||
"f2": "Example map field 2" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
{ | ||
"type": "record", | ||
"name": "Record", | ||
"fields": [ | ||
{ "name": "NullField", "type": "null" }, | ||
{ "name": "BooleanField", "type": "boolean" }, | ||
{ "name": "IntField", "type": "int" }, | ||
{ "name": "LongField", "type": "long" }, | ||
{ "name": "FloatField", "type": "float" }, | ||
{ "name": "DoubleField", "type": "double" }, | ||
{ "name": "StringField", "type": "string" }, | ||
{ | ||
"name": "EnumField", | ||
"type": { "type": "enum", "name": "Suit", "symbols": ["SPADES", "HEARTS", "DIAMONDS", "CLUBS"] } | ||
}, | ||
{ "name": "ArrayField", "type": { "type": "array", "items": "long" } }, | ||
{ "name": "MapField", "type": { "type": "map", "values": "string" } } | ||
] | ||
} |