From fce132a21bf6e0d22e848d5a2a57b8ac9157a3d6 Mon Sep 17 00:00:00 2001 From: Kushal Kumar Date: Tue, 17 Dec 2024 03:04:50 +0530 Subject: [PATCH] tests: split tests in separate file Signed-off-by: Kushal Kumar --- crates/meilisearch/src/search/mod.rs | 4 +- crates/meilisearch/src/search/mod_test.rs | 113 ++++++++++++++++++++++ 2 files changed, 116 insertions(+), 1 deletion(-) create mode 100644 crates/meilisearch/src/search/mod_test.rs diff --git a/crates/meilisearch/src/search/mod.rs b/crates/meilisearch/src/search/mod.rs index 7e185e951..8f55e0faf 100644 --- a/crates/meilisearch/src/search/mod.rs +++ b/crates/meilisearch/src/search/mod.rs @@ -30,6 +30,8 @@ use milli::{ use regex::Regex; use serde::Serialize; use serde_json::{json, Value}; +#[cfg(test)] +mod mod_test; use crate::error::MeilisearchHttpError; @@ -1557,7 +1559,7 @@ pub fn perform_similar( Ok(result) } -fn insert_geo_distance(sorts: &[String], document: &mut Document) { +pub fn insert_geo_distance(sorts: &[String], document: &mut Document) { lazy_static::lazy_static! { static ref GEO_REGEX: Regex = Regex::new(r"_geoPoint\(\s*([[:digit:].\-]+)\s*,\s*([[:digit:].\-]+)\s*\)").unwrap(); diff --git a/crates/meilisearch/src/search/mod_test.rs b/crates/meilisearch/src/search/mod_test.rs new file mode 100644 index 000000000..161bdd7c9 --- /dev/null +++ b/crates/meilisearch/src/search/mod_test.rs @@ -0,0 +1,113 @@ +use crate::search::insert_geo_distance; +use meilisearch_types::Document; +use serde_json::json; + +#[test] +fn test_insert_geo_distance() { + let value: Document = serde_json::from_str( + r#"{ + "_geo": { + "lat": 50.629973371633746, + "lng": 3.0569447399419567 + }, + "city": "Lille", + "id": "1" + }"#, + ) + .unwrap(); + + let sorters = &["_geoPoint(50.629973371633746,3.0569447399419567):desc".to_string()]; + let mut document = value.clone(); + insert_geo_distance(sorters, &mut document); + assert_eq!(document.get("_geoDistance"), Some(&json!(0))); + + let sorters = &["_geoPoint(50.629973371633746, 3.0569447399419567):asc".to_string()]; + let mut document = value.clone(); + insert_geo_distance(sorters, &mut document); + assert_eq!(document.get("_geoDistance"), Some(&json!(0))); + + let sorters = &["_geoPoint( 50.629973371633746 , 3.0569447399419567 ):desc".to_string()]; + let mut document = value.clone(); + insert_geo_distance(sorters, &mut document); + assert_eq!(document.get("_geoDistance"), Some(&json!(0))); + + let sorters = &[ + "prix:asc", + "villeneuve:desc", + "_geoPoint(50.629973371633746, 3.0569447399419567):asc", + "ubu:asc", + ] + .map(|s| s.to_string()); + let mut document = value.clone(); + insert_geo_distance(sorters, &mut document); + assert_eq!(document.get("_geoDistance"), Some(&json!(0))); + + // only the first geoPoint is used to compute the distance + let sorters = &[ + "chien:desc", + "_geoPoint(50.629973371633746, 3.0569447399419567):asc", + "pangolin:desc", + "_geoPoint(100.0, -80.0):asc", + "chat:asc", + ] + .map(|s| s.to_string()); + let mut document = value.clone(); + insert_geo_distance(sorters, &mut document); + assert_eq!(document.get("_geoDistance"), Some(&json!(0))); + + // there was no _geoPoint so nothing is inserted in the document + let sorters = &["chien:asc".to_string()]; + let mut document = value; + insert_geo_distance(sorters, &mut document); + assert_eq!(document.get("_geoDistance"), None); +} + +#[test] +fn test_insert_geo_distance_with_coords_as_string() { + let value: Document = serde_json::from_str( + r#"{ + "_geo": { + "lat": "50", + "lng": 3 + } + }"#, + ) + .unwrap(); + + let sorters = &["_geoPoint(50,3):desc".to_string()]; + let mut document = value.clone(); + insert_geo_distance(sorters, &mut document); + assert_eq!(document.get("_geoDistance"), Some(&json!(0))); + + let value: Document = serde_json::from_str( + r#"{ + "_geo": { + "lat": "50", + "lng": "3" + }, + "id": "1" + }"#, + ) + .unwrap(); + + let sorters = &["_geoPoint(50,3):desc".to_string()]; + let mut document = value.clone(); + insert_geo_distance(sorters, &mut document); + assert_eq!(document.get("_geoDistance"), Some(&json!(0))); + + let value: Document = serde_json::from_str( + r#"{ + "_geo": { + "lat": 50, + "lng": "3" + }, + "id": "1" + }"#, + ) + .unwrap(); + + let sorters = &["_geoPoint(50,3):desc".to_string()]; + let mut document = value.clone(); + insert_geo_distance(sorters, &mut document); + assert_eq!(document.get("_geoDistance"), Some(&json!(0))); +} \ No newline at end of file