Skip to content

Commit

Permalink
Update proptests for bytes field
Browse files Browse the repository at this point in the history
  • Loading branch information
sandreae committed Sep 7, 2023
1 parent d594043 commit 46b4293
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 2 deletions.
12 changes: 12 additions & 0 deletions aquadoggo/src/proptests/document_strategies.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ pub enum FieldValue {
/// String value.
String(String),

/// Hex encoded bytes value.
Bytes(Vec<u8>),

/// Reference to a document.
Relation(DocumentAST),

Expand Down Expand Up @@ -117,6 +120,15 @@ fn values_from_schema(schema: SchemaAST) -> impl Strategy<Value = Vec<DocumentFi
}
})
.boxed(),
SchemaFieldType::Bytes => any::<Vec<u8>>()
.prop_map(move |value| {
let value = FieldValue::Bytes(value);
DocumentFieldValue {
name: field_name.clone(),
value,
}
})
.boxed(),
SchemaFieldType::Relation => values_from_schema(*relation_schema.clone().unwrap())
.prop_map(move |value| {
let schema_id = relation_schema.clone().unwrap().id.clone();
Expand Down
22 changes: 21 additions & 1 deletion aquadoggo/src/proptests/filter_strategies.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use proptest::strategy::{BoxedStrategy, Just, Strategy};
use proptest_derive::Arbitrary;

use crate::proptests::schema_strategies::{SchemaField, SchemaFieldType};
use crate::proptests::utils::FieldName;
use crate::proptests::utils::{FieldName, HexString};

/// Possible values used in filter arguments. `UniqueIdentifier` is a placeholder for values which
/// can be derived at runtime in order to use identifiers which exist in on the node, these include
Expand All @@ -17,6 +17,7 @@ use crate::proptests::utils::FieldName;
pub enum FilterValue {
Boolean(bool),
String(String),
Bytes(HexString),
Integer(i64),
Float(f64),
UniqueIdentifier, // This is a placeholder for a document id, document view id or public key which is selected at testing time
Expand Down Expand Up @@ -87,6 +88,7 @@ fn application_field_filter_strategy(
| SchemaFieldType::Integer
| SchemaFieldType::Float
| SchemaFieldType::String
| SchemaFieldType::Bytes
| SchemaFieldType::Relation
| SchemaFieldType::PinnedRelation => generate_simple_field_filter(field.clone())
.prop_map(|(name, filter)| ((name, filter), Vec::new()))
Expand Down Expand Up @@ -221,6 +223,24 @@ fn generate_simple_field_filter(field: SchemaField) -> BoxedStrategy<(FieldName,
]
.boxed()
}
SchemaFieldType::Bytes => {
let field_clone = field.clone();
prop_oneof![
any::<HexString>()
.prop_map(FilterValue::Bytes)
.prop_map(move |value| (
field.name.clone(),
Filter::Equal(value)
)),
any::<HexString>()
.prop_map(FilterValue::Bytes)
.prop_map(move |value| (
field_clone.name.clone(),
Filter::NotEqual(value)
))
]
.boxed()
}
SchemaFieldType::Relation | SchemaFieldType::PinnedRelation => prop_oneof![
(
Just(field.name.clone()),
Expand Down
8 changes: 8 additions & 0 deletions aquadoggo/src/proptests/schema_strategies.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ pub enum SchemaFieldType {
Integer,
Float,
String,
Bytes,
Relation,
RelationList,
PinnedRelation,
Expand Down Expand Up @@ -107,6 +108,13 @@ fn schema_field() -> impl Strategy<Value = SchemaField> {
relation_schema: None,
}
}),
any::<FieldName>().prop_map(|field_name| {
SchemaField {
name: field_name,
field_type: SchemaFieldType::Bytes,
relation_schema: None,
}
}),
];

// Selections for the recursive fields.
Expand Down
11 changes: 10 additions & 1 deletion aquadoggo/src/proptests/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,8 @@ prop_compose! {
proptest! {
#![proptest_config(Config {
cases: 100,
failure_persistence: Some(Box::new(FileFailurePersistence::WithSource("regressions"))),
failure_persistence: Some(Box::new(FileFailurePersistence::WithSource("query-regressions"))),
max_shrink_iters: 1000,
.. Config::default()
})]
#[test]
Expand Down Expand Up @@ -227,7 +228,15 @@ proptest! {
};
});
}
}

proptest! {
#![proptest_config(Config {
cases: 100,
failure_persistence: Some(Box::new(FileFailurePersistence::WithSource("filtering-regressions"))),
max_shrink_iters: 100,
.. Config::default()
})]
#[test]
/// Test passing different combinations of filter arguments to the root collection query and
/// also to fields which contain relation lists. Filter arguments are generated randomly via
Expand Down
18 changes: 18 additions & 0 deletions aquadoggo/src/proptests/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ use super::filter_strategies::{Filter, FilterValue};
#[derive(Arbitrary, Debug, Clone, PartialEq, Eq, Hash)]
pub struct FieldName(#[proptest(regex = "[A-Za-z]{1}[A-Za-z0-9_]{0,63}")] pub String);

/// A fieldname which will follow the expected regex rules.
#[derive(Arbitrary, Debug, Clone, PartialEq, Eq, Hash)]
pub struct HexString(#[proptest(regex = "([a-fA-F0-9]{2}){0,64}")] pub String);

/// Add schemas from a schema AST to a test node.
#[async_recursion]
pub async fn add_schemas_from_ast(
Expand All @@ -42,6 +46,9 @@ pub async fn add_schemas_from_ast(
SchemaFieldType::String => {
schema_fields.push((field.name, FieldType::String));
}
SchemaFieldType::Bytes => {
schema_fields.push((field.name, FieldType::Bytes));
}
SchemaFieldType::Relation => {
let schema_ast = field.relation_schema.unwrap();
let schema = add_schemas_from_ast(node, &schema_ast, schemas).await;
Expand Down Expand Up @@ -116,6 +123,9 @@ pub async fn add_documents_from_ast(
FieldValue::String(value) => {
operation_fields.push((&field.name.0, value.to_owned().into()));
}
FieldValue::Bytes(value) => {
operation_fields.push((&field.name.0, value[..].into()));
}
FieldValue::Relation(document_ast) => {
let document_view_id = add_documents_from_ast(node, &document_ast, documents).await;
let operation_id = document_view_id.graph_tips().first().unwrap();
Expand Down Expand Up @@ -246,6 +256,9 @@ pub fn parse_filter(filter_args: &mut Vec<String>, name: &FieldName, filter: &Fi
FilterValue::String(value) => {
filter_args.push(format!("{name}: {{ eq: {} }}", escape_string_value(value)))
}
FilterValue::Bytes(value) => {
filter_args.push(format!("{name}: {{ eq: {} }}", value.0))
}
FilterValue::Integer(value) => filter_args.push(format!("{name}: {{ eq: {value} }}")),
FilterValue::Float(value) => filter_args.push(format!("{name}: {{ eq: {value} }}")),
},
Expand All @@ -260,6 +273,9 @@ pub fn parse_filter(filter_args: &mut Vec<String>, name: &FieldName, filter: &Fi
"{name}: {{ notEq: {} }}",
escape_string_value(value)
)),
FilterValue::Bytes(value) => {
filter_args.push(format!("{name}: {{ notEq: {} }}", value.0))
}
FilterValue::Integer(value) => {
filter_args.push(format!("{name}: {{ notEq: {value} }}"))
}
Expand All @@ -276,6 +292,7 @@ pub fn parse_filter(filter_args: &mut Vec<String>, name: &FieldName, filter: &Fi
)),
FilterValue::Integer(value) => filter_args.push(format!("{name}: {{ in: [{value}] }}")),
FilterValue::Float(value) => filter_args.push(format!("{name}: {{ in: [{value}] }}")),
_ => panic!(),
},
Filter::NotIn(value) => match value {
FilterValue::UniqueIdentifier => {
Expand All @@ -294,6 +311,7 @@ pub fn parse_filter(filter_args: &mut Vec<String>, name: &FieldName, filter: &Fi
FilterValue::Float(value) => {
filter_args.push(format!("{name}: {{ notIn: [{value}] }}"))
}
_ => panic!(),
},
Filter::GreaterThan(value) => match value {
FilterValue::String(value) => {
Expand Down

0 comments on commit 46b4293

Please sign in to comment.