Skip to content

Commit

Permalink
Add test to ensure nested field support (#273)
Browse files Browse the repository at this point in the history
  • Loading branch information
bidoubiwa authored Apr 26, 2022
1 parent a596a19 commit 413b04a
Showing 1 changed file with 73 additions and 21 deletions.
94 changes: 73 additions & 21 deletions src/search.rs
Original file line number Diff line number Diff line change
Expand Up @@ -283,13 +283,19 @@ mod tests {
use crate::{client::*, search::*};
use meilisearch_test_macro::meilisearch_test;
use serde::{Deserialize, Serialize};
use serde_json::{Map, Value, json};
use serde_json::{json, Map, Value};

#[derive(Debug, Serialize, Deserialize, PartialEq)]
struct Nested {
child: String,
}

#[derive(Debug, Serialize, Deserialize, PartialEq)]
struct Document {
id: usize,
value: String,
kind: String,
nested: Nested,
}

impl PartialEq<Map<String, Value>> for Document {
Expand All @@ -302,16 +308,16 @@ mod tests {

async fn setup_test_index(client: &Client, index: &Index) -> Result<(), Error> {
let t0 = index.add_documents(&[
Document { id: 0, kind: "text".into(), value: "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.".to_string() },
Document { id: 1, kind: "text".into(), value: "dolor sit amet, consectetur adipiscing elit".to_string() },
Document { id: 2, kind: "title".into(), value: "The Social Network".to_string() },
Document { id: 3, kind: "title".into(), value: "Harry Potter and the Sorcerer's Stone".to_string() },
Document { id: 4, kind: "title".into(), value: "Harry Potter and the Chamber of Secrets".to_string() },
Document { id: 5, kind: "title".into(), value: "Harry Potter and the Prisoner of Azkaban".to_string() },
Document { id: 6, kind: "title".into(), value: "Harry Potter and the Goblet of Fire".to_string() },
Document { id: 7, kind: "title".into(), value: "Harry Potter and the Order of the Phoenix".to_string() },
Document { id: 8, kind: "title".into(), value: "Harry Potter and the Half-Blood Prince".to_string() },
Document { id: 9, kind: "title".into(), value: "Harry Potter and the Deathly Hallows".to_string() },
Document { id: 0, kind: "text".into(), value: "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.".to_string(), nested: Nested { child: "first".to_string() } },
Document { id: 1, kind: "text".into(), value: "dolor sit amet, consectetur adipiscing elit".to_string(), nested: Nested { child: "second".to_string() } },
Document { id: 2, kind: "title".into(), value: "The Social Network".to_string(), nested: Nested { child: "third".to_string() } },
Document { id: 3, kind: "title".into(), value: "Harry Potter and the Sorcerer's Stone".to_string(), nested: Nested { child: "fourth".to_string() } },
Document { id: 4, kind: "title".into(), value: "Harry Potter and the Chamber of Secrets".to_string(), nested: Nested { child: "fift".to_string() } },
Document { id: 5, kind: "title".into(), value: "Harry Potter and the Prisoner of Azkaban".to_string(), nested: Nested { child: "sixth".to_string() } },
Document { id: 6, kind: "title".into(), value: "Harry Potter and the Goblet of Fire".to_string(), nested: Nested { child: "seventh".to_string() } },
Document { id: 7, kind: "title".into(), value: "Harry Potter and the Order of the Phoenix".to_string(), nested: Nested { child: "eighth".to_string() } },
Document { id: 8, kind: "title".into(), value: "Harry Potter and the Half-Blood Prince".to_string(), nested: Nested { child: "ninth".to_string() } },
Document { id: 9, kind: "title".into(), value: "Harry Potter and the Deathly Hallows".to_string(), nested: Nested { child: "tenth".to_string() } },
], None).await?;
let t1 = index.set_filterable_attributes(["kind", "value"]).await?;
let t2 = index.set_sortable_attributes(["title"]).await?;
Expand All @@ -332,6 +338,28 @@ mod tests {
Ok(())
}

#[meilisearch_test]
async fn test_query_string_on_nested_field(client: Client, index: Index) -> Result<(), Error> {
setup_test_index(&client, &index).await?;

let results: SearchResults<Document> =
index.search().with_query("second").execute().await?;

assert_eq!(
&Document {
id: 1,
value: "dolor sit amet, consectetur adipiscing elit".to_string(),
kind: "text".to_string(),
nested: Nested {
child: "second".to_string()
}
},
&results.hits[0].result
);

Ok(())
}

#[meilisearch_test]
async fn test_query_limit(client: Client, index: Index) -> Result<(), Error> {
setup_test_index(&client, &index).await?;
Expand Down Expand Up @@ -457,7 +485,10 @@ mod tests {
id: 0,
value: "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do…"
.to_string(),
kind: "text".to_string()
kind: "text".to_string(),
nested: Nested {
child: "first".to_string()
}
},
results.hits[0].formatted_result.as_ref().unwrap()
);
Expand All @@ -470,7 +501,10 @@ mod tests {
&Document {
id: 0,
value: "Lorem ipsum dolor sit amet…".to_string(),
kind: "text".to_string()
kind: "text".to_string(),
nested: Nested {
child: "first".to_string()
}
},
results.hits[0].formatted_result.as_ref().unwrap()
);
Expand All @@ -490,6 +524,7 @@ mod tests {
id: 0,
value: "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.".to_string(),
kind: "text".to_string(),
nested: Nested { child: "first".to_string() }
},
results.hits[0].formatted_result.as_ref().unwrap());

Expand All @@ -502,7 +537,10 @@ mod tests {
&Document {
id: 0,
value: "Lorem ipsum dolor sit amet…".to_string(),
kind: "text".to_string()
kind: "text".to_string(),
nested: Nested {
child: "first".to_string()
}
},
results.hits[0].formatted_result.as_ref().unwrap()
);
Expand All @@ -521,7 +559,10 @@ mod tests {
&Document {
id: 1,
value: "<em>dolor</em> sit amet, consectetur adipiscing elit".to_string(),
kind: "<em>text</em>".to_string()
kind: "<em>text</em>".to_string(),
nested: Nested {
child: "first".to_string()
}
},
results.hits[0].formatted_result.as_ref().unwrap(),
);
Expand All @@ -534,7 +575,10 @@ mod tests {
&Document {
id: 1,
value: "<em>dolor</em> sit amet, consectetur adipiscing elit".to_string(),
kind: "text".to_string()
kind: "text".to_string(),
nested: Nested {
child: "first".to_string()
}
},
results.hits[0].formatted_result.as_ref().unwrap()
);
Expand Down Expand Up @@ -577,15 +621,20 @@ mod tests {
}

#[meilisearch_test]
async fn test_generate_tenant_token_from_client(client: Client, index: Index) -> Result<(), Error> {
use crate::key::{KeyBuilder, Action};
async fn test_generate_tenant_token_from_client(
client: Client,
index: Index,
) -> Result<(), Error> {
use crate::key::{Action, KeyBuilder};

setup_test_index(&client, &index).await?;

let key = KeyBuilder::new("key for generate_tenant_token test")
.with_action(Action::All)
.with_index("*")
.create(&client).await.unwrap();
.create(&client)
.await
.unwrap();
let allowed_client = Client::new("http://localhost:7700", key.key);

let search_rules = vec![
Expand All @@ -597,10 +646,13 @@ mod tests {
];

for rules in search_rules {
let token = allowed_client.generate_tenant_token(rules, None, None).expect("Cannot generate tenant token.");
let token = allowed_client
.generate_tenant_token(rules, None, None)
.expect("Cannot generate tenant token.");
let new_client = Client::new("http://localhost:7700", token);

let result: SearchResults<Document> = new_client.index(index.uid.to_string())
let result: SearchResults<Document> = new_client
.index(index.uid.to_string())
.search()
.execute()
.await?;
Expand Down

0 comments on commit 413b04a

Please sign in to comment.