Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
406 changes: 246 additions & 160 deletions schema_analysis/tests/shared/mod.rs

Large diffs are not rendered by default.

96 changes: 54 additions & 42 deletions schema_analysis/tests/source_bson.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use bson::{bson, Bson as Value};
use bson::bson;

use schema_analysis::InferredSchema;

Expand All @@ -9,82 +9,94 @@ struct Bson;

test_format!(Bson);

impl FormatTests<Value> for Bson {
fn convert_to_inferred_schema(value: Value) -> InferredSchema {
impl FormatTests for Bson {
type Value = bson::Bson;

fn infer_schema(value: Self::Value) -> InferredSchema {
let bytes = bson::to_vec(&value).unwrap();
let processed_schema: InferredSchema = bson::from_slice(&bytes).unwrap();
processed_schema
}

// Bson doesn't allow top-level primitives
fn null() -> Option<Value> {
None
fn test_null() {}
fn null() -> Self::Value {
unreachable!()
}
fn boolean() -> Option<Value> {
None
fn test_boolean() {}
fn boolean() -> Self::Value {
unreachable!()
}
fn integer() -> Option<Value> {
None
fn test_integer() {}
fn integer() -> Self::Value {
unreachable!()
}
fn float() -> Option<Value> {
None
fn test_float() {}
fn float() -> Self::Value {
unreachable!()
}
fn string() -> Option<Value> {
None
fn test_string() {}
fn string() -> Self::Value {
unreachable!()
}

// Bson doesn't allow top-level arrays
fn empty_sequence() -> Option<Value> {
None
fn test_empty_sequence() {}
fn empty_sequence() -> Self::Value {
unreachable!()
}
fn string_sequence() -> Option<Value> {
None
fn test_string_sequence() {}
fn string_sequence() -> Self::Value {
unreachable!()
}
fn integer_sequence() -> Option<Value> {
None
fn test_integer_sequence() {}
fn integer_sequence() -> Self::Value {
unreachable!()
}
fn mixed_sequence() -> Option<Value> {
None
fn test_mixed_sequence() {}
fn mixed_sequence() -> Self::Value {
unreachable!()
}
fn optional_mixed_sequence() -> Option<Value> {
None
fn test_optional_mixed_sequence() {}
fn optional_mixed_sequence() -> Self::Value {
unreachable!()
}

fn empty_map_struct() -> Option<Value> {
Some(bson!({}))
fn empty_map_struct() -> Self::Value {
bson!({})
}
fn map_struct_single() -> Option<Value> {
Some(bson!({
fn map_struct_single() -> Self::Value {
bson!({
"hello": 1
}))
})
}
fn map_struct_double() -> Option<Value> {
Some(bson!({
fn map_struct_double() -> Self::Value {
bson!({
"hello": 1,
"world": "!"
}))
}
fn sequence_map_struct_mixed() -> Option<Value> {
None // Bson doesn't allow top-level arrays
})
}
fn test_sequence_map_struct_mixed() {}
fn sequence_map_struct_optional_or_missing() -> Option<Value> {
None // Bson doesn't allow top-level arrays
fn sequence_map_struct_mixed() -> Self::Value {
unreachable!() // Bson doesn't allow top-level arrays
}
fn test_sequence_map_struct_optional_or_missing() {}
fn map_struct_mixed_sequence() -> Option<Value> {
Some(bson!({
fn sequence_map_struct_optional_or_missing() -> Self::Value {
unreachable!() // Bson doesn't allow top-level arrays
}
fn map_struct_mixed_sequence() -> Self::Value {
bson!({
"hello": 1,
"world": "!",
"sequence": ["one", "two", "three"]
}))
})
}
fn map_struct_mixed_sequence_optional() -> Option<Value> {
Some(bson!({
fn map_struct_mixed_sequence_optional() -> Self::Value {
bson!({
"hello": 1,
"world": "!",
"optional": null,
"sequence": ["one", "two", "three", null]
}))
})
}
}
88 changes: 40 additions & 48 deletions schema_analysis/tests/source_cbor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,73 +11,71 @@ struct Cbor;

test_format!(Cbor);

impl FormatTests<Value> for Cbor {
fn convert_to_inferred_schema(value: Value) -> InferredSchema {
impl FormatTests for Cbor {
type Value = Value;

fn infer_schema(value: Value) -> InferredSchema {
let vec_value = serde_cbor::to_vec(&value).unwrap();
let processed_schema: InferredSchema = serde_cbor::from_slice(&vec_value).unwrap();
processed_schema
}

fn null() -> Option<Value> {
Some(Value::Null)
fn null() -> Self::Value {
Value::Null
}
fn boolean() -> Option<Value> {
Some(Value::Bool(true))
fn boolean() -> Self::Value {
Value::Bool(true)
}
fn integer() -> Option<Value> {
Some(Value::Integer(123))
fn integer() -> Self::Value {
Value::Integer(123)
}
fn float() -> Option<Value> {
Some(Value::Float(123.123))
fn float() -> Self::Value {
Value::Float(123.123)
}
fn string() -> Option<Value> {
Some(Value::Text("hello".into()))
fn string() -> Self::Value {
Value::Text("hello".into())
}

fn empty_sequence() -> Option<Value> {
Some(Value::Array(vec![]))
fn empty_sequence() -> Self::Value {
Value::Array(vec![])
}
fn string_sequence() -> Option<Value> {
Some(Value::Array(vec![
fn string_sequence() -> Self::Value {
Value::Array(vec![
Value::Text("one".into()),
Value::Text("two".into()),
Value::Text("three".into()),
]))
])
}
fn integer_sequence() -> Option<Value> {
Some(Value::Array(vec![1.into(), 2.into(), 3.into()]))
fn integer_sequence() -> Self::Value {
Value::Array(vec![1.into(), 2.into(), 3.into()])
}
fn mixed_sequence() -> Option<Value> {
Some(Value::Array(vec![
1.into(),
Value::Text("two".into()),
3.into(),
]))
fn mixed_sequence() -> Self::Value {
Value::Array(vec![1.into(), Value::Text("two".into()), 3.into()])
}
fn optional_mixed_sequence() -> Option<Value> {
Some(Value::Array(vec![
fn optional_mixed_sequence() -> Self::Value {
Value::Array(vec![
1.into(),
Value::Text("two".into()),
3.into(),
Value::Null,
]))
])
}

fn empty_map_struct() -> Option<Value> {
Some(Value::Map(BTreeMap::new()))
fn empty_map_struct() -> Self::Value {
Value::Map(BTreeMap::new())
}
fn map_struct_single() -> Option<Value> {
fn map_struct_single() -> Self::Value {
let mut mapping = BTreeMap::new();
mapping.insert(Value::Text("hello".into()), 1.into());
Some(Value::Map(mapping))
Value::Map(mapping)
}
fn map_struct_double() -> Option<Value> {
fn map_struct_double() -> Self::Value {
let mut mapping = BTreeMap::new();
mapping.insert(Value::Text("hello".into()), 1.into());
mapping.insert(Value::Text("world".into()), Value::Text("!".into()));
Some(Value::Map(mapping))
Value::Map(mapping)
}
fn sequence_map_struct_mixed() -> Option<Value> {
fn sequence_map_struct_mixed() -> Self::Value {
let mut mapping_1 = BTreeMap::new();
mapping_1.insert(Value::Text("hello".into()), 1.into());
mapping_1.insert(Value::Text("world".into()), Value::Text("!".into()));
Expand All @@ -88,12 +86,9 @@ impl FormatTests<Value> for Cbor {
mapping_2.insert(Value::Text("world".into()), Value::Text("!".into()));
mapping_2.insert(Value::Text("mixed".into()), Value::Text("1.1".into()));

Some(Value::Array(vec![
Value::Map(mapping_1),
Value::Map(mapping_2),
]))
Value::Array(vec![Value::Map(mapping_1), Value::Map(mapping_2)])
}
fn sequence_map_struct_optional_or_missing() -> Option<Value> {
fn sequence_map_struct_optional_or_missing() -> Self::Value {
let mut mapping_1 = BTreeMap::new();
mapping_1.insert(Value::Text("hello".into()), 1.into());
mapping_1.insert(Value::Text("possibly_null".into()), Value::Text("!".into()));
Expand All @@ -104,12 +99,9 @@ impl FormatTests<Value> for Cbor {
mapping_2.insert(Value::Text("hello".into()), 2.into());
mapping_2.insert(Value::Text("possibly_null".into()), Value::Null);

Some(Value::Array(vec![
Value::Map(mapping_1),
Value::Map(mapping_2),
]))
Value::Array(vec![Value::Map(mapping_1), Value::Map(mapping_2)])
}
fn map_struct_mixed_sequence() -> Option<Value> {
fn map_struct_mixed_sequence() -> Self::Value {
let mut mapping = BTreeMap::new();
mapping.insert(Value::Text("hello".into()), 1.into());
mapping.insert(Value::Text("world".into()), Value::Text("!".into()));
Expand All @@ -121,9 +113,9 @@ impl FormatTests<Value> for Cbor {
Value::Text("three".into()),
]),
);
Some(Value::Map(mapping))
Value::Map(mapping)
}
fn map_struct_mixed_sequence_optional() -> Option<Value> {
fn map_struct_mixed_sequence_optional() -> Self::Value {
let mut mapping = BTreeMap::new();
mapping.insert(Value::Text("hello".into()), 1.into());
mapping.insert(Value::Text("world".into()), Value::Text("!".into()));
Expand All @@ -137,6 +129,6 @@ impl FormatTests<Value> for Cbor {
Value::Null,
]),
);
Some(Value::Map(mapping))
Value::Map(mapping)
}
}
Loading
Loading