From 867bd1ffd7a24d9e4469d26ed595e371104f46e7 Mon Sep 17 00:00:00 2001 From: lironhl Date: Mon, 27 Apr 2020 20:10:40 +0300 Subject: [PATCH] Tests for the new highlight algorithm --- meilisearch-http/src/helpers/meilisearch.rs | 63 +++++++++++++++++++++ 1 file changed, 63 insertions(+) diff --git a/meilisearch-http/src/helpers/meilisearch.rs b/meilisearch-http/src/helpers/meilisearch.rs index 6218c5f2c..a925562be 100644 --- a/meilisearch-http/src/helpers/meilisearch.rs +++ b/meilisearch-http/src/helpers/meilisearch.rs @@ -593,6 +593,35 @@ mod tests { } + #[test] + fn calculate_matches() { + let mut matches = Vec::new(); + matches.push(Highlight { attribute: 0, char_index: 0, char_length: 3}); + matches.push(Highlight { attribute: 0, char_index: 0, char_length: 2}); + + let mut attributes_to_retrieve: HashSet = HashSet::new(); + attributes_to_retrieve.insert("title".to_string()); + + let schema = Schema::with_primary_key("title"); + + let matches_result = super::calculate_matches(matches, Some(attributes_to_retrieve), &schema); + + let mut matches_result_expected: HashMap> = HashMap::new(); + + let mut positions = Vec::new(); + positions.push(MatchPosition { + start: 0, + length: 2, + }); + positions.push(MatchPosition { + start: 0, + length: 3, + }); + matches_result_expected.insert("title".to_string(), positions); + + assert_eq!(matches_result, matches_result_expected); + } + #[test] fn calculate_highlights() { let data = r#"{ @@ -631,4 +660,38 @@ mod tests { assert_eq!(result, result_expected); } + + #[test] + fn highlight_longest_match() { + let data = r#"{ + "title": "Ice" + }"#; + + let document: IndexMap = serde_json::from_str(data).unwrap(); + let mut attributes_to_highlight = HashSet::new(); + attributes_to_highlight.insert("title".to_string()); + + let mut matches = HashMap::new(); + + let mut m = Vec::new(); + m.push(MatchPosition { + start: 0, + length: 2, + }); + m.push(MatchPosition { + start: 0, + length: 3, + }); + matches.insert("title".to_string(), m); + + let result = super::calculate_highlights(&document, &matches, &attributes_to_highlight); + + let mut result_expected = IndexMap::new(); + result_expected.insert( + "title".to_string(), + Value::String("Ice".to_string()), + ); + + assert_eq!(result, result_expected); + } }