Skip to content

Commit

Permalink
Add e2e test
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewinci committed Jan 3, 2023
1 parent c795a11 commit dae1655
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 1 deletion.
2 changes: 1 addition & 1 deletion backend/.gitignore
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
3 changes: 3 additions & 0 deletions backend/src/lib/avro/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ mod helpers;
mod json_to_avro;
mod schema_provider;

#[cfg(test)]
mod parser_e2e_tests;

pub use avro_parser::AvroParser;
pub use error::AvroError;
pub use schema_provider::SchemaProvider;
59 changes: 59 additions & 0 deletions backend/src/lib/avro/parser_e2e_tests.rs
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()
);
}
}
15 changes: 15 additions & 0 deletions backend/src/lib/avro/test_files/1_good_input.json
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"
}
}
19 changes: 19 additions & 0 deletions backend/src/lib/avro/test_files/1_schema.json
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" } }
]
}

0 comments on commit dae1655

Please sign in to comment.