Skip to content

Commit

Permalink
Edit and add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
boraarslan committed Jun 2, 2022
1 parent 499e25c commit c979d84
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 1 deletion.
38 changes: 38 additions & 0 deletions src/query/query_parser/query_parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -810,6 +810,8 @@ mod test {
schema_builder.add_bytes_field("bytes_not_indexed", STORED);
schema_builder.add_json_field("json", TEXT);
schema_builder.add_json_field("json_not_indexed", STORED);
schema_builder.add_bool_field("bool", INDEXED);
schema_builder.add_bool_field("notindexed_bool", STORED);
schema_builder.build()
}

Expand Down Expand Up @@ -925,6 +927,10 @@ mod test {
is_not_indexed_err("notindexed_i64:-234324"),
Some(String::from("notindexed_i64"))
);
assert_eq!(
is_not_indexed_err("notindexed_bool:true"),
Some(String::from("notindexed_bool"))
);
}

#[test]
Expand Down Expand Up @@ -1006,6 +1012,18 @@ mod test {
);
}

#[test]
fn test_parse_bool() {
test_parse_query_to_logical_ast_helper(
"bool:true",
&format!(
"{:?}",
Term::from_field_bool(Field::from_field_id(16u32), true),
),
false,
);
}

#[test]
fn test_parse_bytes_not_indexed() {
let error = parse_query_to_logical_ast("bytes_not_indexed:aaa", false).unwrap_err();
Expand Down Expand Up @@ -1050,6 +1068,15 @@ mod test {
);
}

#[test]
fn test_json_field_possibly_a_bool() {
test_parse_query_to_logical_ast_helper(
"json.titi:true",
r#"(Term(type=Json, field=14, path=titi, vtype=Bool, true) Term(type=Json, field=14, path=titi, vtype=Str, "true"))"#,
true,
);
}

#[test]
fn test_json_field_not_indexed() {
let error = parse_query_to_logical_ast("json_not_indexed.titi:hello", false).unwrap_err();
Expand Down Expand Up @@ -1299,6 +1326,17 @@ mod test {
);
}

#[test]
pub fn test_query_parser_expected_bool() {
let query_parser = make_query_parser();
assert_matches!(
query_parser.parse_query("bool:brie"),
Err(QueryParserError::ExpectedBool(_))
);
assert!(query_parser.parse_query("bool:\"true\"").is_ok());
assert!(query_parser.parse_query("bool:\"false\"").is_ok());
}

#[test]
pub fn test_query_parser_expected_date() {
let query_parser = make_query_parser();
Expand Down
26 changes: 25 additions & 1 deletion src/schema/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -466,6 +466,9 @@ mod tests {
.set_indexed()
.set_fieldnorm()
.set_fast(Cardinality::SingleValue);
let is_read_options = NumericOptions::default()
.set_stored()
.set_fast(Cardinality::SingleValue);
schema_builder.add_text_field("title", TEXT);
schema_builder.add_text_field(
"author",
Expand All @@ -478,6 +481,7 @@ mod tests {
schema_builder.add_u64_field("count", count_options);
schema_builder.add_i64_field("popularity", popularity_options);
schema_builder.add_f64_field("score", score_options);
schema_builder.add_bool_field("is_read", is_read_options);
let schema = schema_builder.build();
let schema_json = serde_json::to_string_pretty(&schema).unwrap();
let expected = r#"[
Expand Down Expand Up @@ -536,6 +540,16 @@ mod tests {
"fast": "single",
"stored": false
}
},
{
"name": "is_read",
"type": "bool",
"options": {
"indexed": false,
"fieldnorms": false,
"fast": "single",
"stored": true
}
}
]"#;
assert_eq!(schema_json, expected);
Expand Down Expand Up @@ -568,6 +582,11 @@ mod tests {
assert_eq!("score", field_entry.name());
assert_eq!(4, field.field_id());
}
{
let (field, field_entry) = fields.next().unwrap();
assert_eq!("is_read", field_entry.name());
assert_eq!(5, field.field_id());
}
assert!(fields.next().is_none());
}

Expand All @@ -577,14 +596,19 @@ mod tests {
let count_options = NumericOptions::default()
.set_stored()
.set_fast(Cardinality::SingleValue);
let is_read_options = NumericOptions::default()
.set_stored()
.set_fast(Cardinality::SingleValue);
schema_builder.add_text_field("title", TEXT);
schema_builder.add_text_field("author", STRING);
schema_builder.add_u64_field("count", count_options);
schema_builder.add_bool_field("is_read", is_read_options);
let schema = schema_builder.build();
let doc_json = r#"{
"title": "my title",
"author": "fulmicoton",
"count": 4
"count": 4,
"is_read": true
}"#;
let doc = schema.parse_document(doc_json).unwrap();

Expand Down
11 changes: 11 additions & 0 deletions src/schema/term.rs
Original file line number Diff line number Diff line change
Expand Up @@ -458,4 +458,15 @@ mod tests {
assert_eq!(term.as_slice().len(), super::FAST_VALUE_TERM_LEN);
assert_eq!(term.as_u64(), Some(983u64))
}

#[test]
pub fn test_term_bool() {
let mut schema_builder = Schema::builder();
let bool_field = schema_builder.add_bool_field("bool", INDEXED);
let term = Term::from_field_bool(bool_field, true);
assert_eq!(term.field(), bool_field);
assert_eq!(term.typ(), Type::Bool);
assert_eq!(term.as_slice().len(), super::FAST_VALUE_TERM_LEN);
assert_eq!(term.as_bool(), Some(true))
}
}

0 comments on commit c979d84

Please sign in to comment.