149: Handle star in attributes_to_retrieve r=MarinPostma a=curquiza

Closes #147

Co-authored-by: Clémentine Urquizar <clementine@meilisearch.com>
This commit is contained in:
bors[bot] 2021-04-19 17:29:21 +00:00 committed by GitHub
commit 94c5c5843b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 63 additions and 3 deletions

View File

@ -110,7 +110,16 @@ async fn get_all_documents(
let attributes_to_retrieve = params let attributes_to_retrieve = params
.attributes_to_retrieve .attributes_to_retrieve
.as_ref() .as_ref()
.map(|attrs| attrs.split(',').map(String::from).collect::<Vec<_>>()); .and_then(|attrs| {
let mut names = Vec::new();
for name in attrs.split(',').map(String::from) {
if name == "*" {
return None
}
names.push(name);
}
Some(names)
});
match data match data
.retrieve_documents( .retrieve_documents(

View File

@ -182,6 +182,23 @@ async fn test_get_all_documents_attributes_to_retrieve() {
0 0
); );
let (response, code) = index
.get_all_documents(GetAllDocumentsOptions {
attributes_to_retrieve: Some(vec!["wrong"]),
..Default::default()
})
.await;
assert_eq!(code, 200);
assert_eq!(response.as_array().unwrap().len(), 20);
assert_eq!(
response.as_array().unwrap()[0]
.as_object()
.unwrap()
.keys()
.count(),
0
);
let (response, code) = index let (response, code) = index
.get_all_documents(GetAllDocumentsOptions { .get_all_documents(GetAllDocumentsOptions {
attributes_to_retrieve: Some(vec!["name", "tags"]), attributes_to_retrieve: Some(vec!["name", "tags"]),
@ -198,6 +215,40 @@ async fn test_get_all_documents_attributes_to_retrieve() {
.count(), .count(),
2 2
); );
let (response, code) = index
.get_all_documents(GetAllDocumentsOptions {
attributes_to_retrieve: Some(vec!["*"]),
..Default::default()
})
.await;
assert_eq!(code, 200);
assert_eq!(response.as_array().unwrap().len(), 20);
assert_eq!(
response.as_array().unwrap()[0]
.as_object()
.unwrap()
.keys()
.count(),
16
);
let (response, code) = index
.get_all_documents(GetAllDocumentsOptions {
attributes_to_retrieve: Some(vec!["*", "wrong"]),
..Default::default()
})
.await;
assert_eq!(code, 200);
assert_eq!(response.as_array().unwrap().len(), 20);
assert_eq!(
response.as_array().unwrap()[0]
.as_object()
.unwrap()
.keys()
.count(),
16
);
} }
#[actix_rt::test] #[actix_rt::test]