actually fix the test and the swap_indexes name resolution

This commit is contained in:
Irevoire 2022-10-27 00:56:34 +02:00 committed by Clément Renault
parent 54d0aff4cf
commit 6280bd51a9
No known key found for this signature in database
GPG Key ID: 92ADA4E935E71FA4
2 changed files with 36 additions and 38 deletions

View File

@ -7,6 +7,7 @@ use meilisearch_types::error::ResponseError;
use meilisearch_types::tasks::{IndexSwap, KindWithContent}; use meilisearch_types::tasks::{IndexSwap, KindWithContent};
use serde::Deserialize; use serde::Deserialize;
use super::SummarizedTaskView;
use crate::error::MeilisearchHttpError; use crate::error::MeilisearchHttpError;
use crate::extractors::authentication::policies::*; use crate::extractors::authentication::policies::*;
use crate::extractors::authentication::{AuthenticationError, GuardedData}; use crate::extractors::authentication::{AuthenticationError, GuardedData};
@ -31,7 +32,6 @@ pub async fn swap_indexes(
let mut swaps = vec![]; let mut swaps = vec![];
let mut indexes_set = BTreeSet::<String>::default(); let mut indexes_set = BTreeSet::<String>::default();
let mut unauthorized_indexes = BTreeSet::new(); let mut unauthorized_indexes = BTreeSet::new();
let mut unknown_indexes = BTreeSet::new();
let mut duplicate_indexes = BTreeSet::new(); let mut duplicate_indexes = BTreeSet::new();
for SwapIndexesPayload { indexes } in params.into_inner().into_iter() { for SwapIndexesPayload { indexes } in params.into_inner().into_iter() {
let (lhs, rhs) = match indexes.as_slice() { let (lhs, rhs) = match indexes.as_slice() {
@ -46,20 +46,6 @@ pub async fn swap_indexes(
if !search_rules.is_index_authorized(rhs) { if !search_rules.is_index_authorized(rhs) {
unauthorized_indexes.insert(rhs.clone()); unauthorized_indexes.insert(rhs.clone());
} }
match index_scheduler.index(lhs) {
Ok(_) => (),
Err(index_scheduler::Error::IndexNotFound(_)) => {
unknown_indexes.insert(lhs.clone());
}
Err(e) => return Err(e.into()),
}
match index_scheduler.index(rhs) {
Ok(_) => (),
Err(index_scheduler::Error::IndexNotFound(_)) => {
unknown_indexes.insert(rhs.clone());
}
Err(e) => return Err(e.into()),
}
swaps.push(IndexSwap { indexes: (lhs.clone(), rhs.clone()) }); swaps.push(IndexSwap { indexes: (lhs.clone(), rhs.clone()) });
@ -83,19 +69,10 @@ pub async fn swap_indexes(
if !unauthorized_indexes.is_empty() { if !unauthorized_indexes.is_empty() {
return Err(AuthenticationError::InvalidToken.into()); return Err(AuthenticationError::InvalidToken.into());
} }
if !unknown_indexes.is_empty() {
let unknown_indexes: Vec<_> = unknown_indexes.into_iter().collect();
if let [index] = unknown_indexes.as_slice() {
return Err(index_scheduler::Error::IndexNotFound(index.clone()).into());
} else {
return Err(MeilisearchHttpError::IndexesNotFound(unknown_indexes).into());
}
}
let task = KindWithContent::IndexSwap { swaps }; let task = KindWithContent::IndexSwap { swaps };
let task = index_scheduler.register(task)?; let task = index_scheduler.register(task)?;
let task_view = TaskView::from_task(&task); let task: SummarizedTaskView = task.into();
Ok(HttpResponse::Accepted().json(task))
Ok(HttpResponse::Accepted().json(task_view))
} }

View File

@ -677,21 +677,43 @@ async fn test_summarized_index_update() {
} }
#[actix_web::test] #[actix_web::test]
#[ignore]
async fn test_summarized_index_swap() { async fn test_summarized_index_swap() {
let server = Server::new().await; let server = Server::new().await;
let (v, _) = server server
.index_swap(json!([ .index_swap(json!([
{ "indexes": ["doggos", "cattos"] } { "indexes": ["doggos", "cattos"] }
])) ]))
.await; .await;
dbg!(&v); server.wait_task(0).await;
assert_json_snapshot!(v, @r###" let (task, _) = server.get_task(0).await;
assert_json_snapshot!(task,
{ ".duration" => "[duration]", ".enqueuedAt" => "[date]", ".startedAt" => "[date]", ".finishedAt" => "[date]" },
@r###"
{ {
"message": "Indexes `cattos`, `doggos` not found.", "uid": 0,
"indexUid": null,
"status": "failed",
"type": "indexSwap",
"details": {
"swaps": [
{
"indexes": [
"doggos",
"cattos"
]
}
]
},
"error": {
"message": "Index `doggos` not found.",
"code": "index_not_found", "code": "index_not_found",
"type": "invalid_request", "type": "invalid_request",
"link": "https://docs.meilisearch.com/errors#index_not_found" "link": "https://docs.meilisearch.com/errors#index_not_found"
},
"duration": "[duration]",
"enqueuedAt": "[date]",
"startedAt": "[date]",
"finishedAt": "[date]"
} }
"###); "###);
@ -702,14 +724,13 @@ async fn test_summarized_index_swap() {
{ "indexes": ["doggos", "cattos"] } { "indexes": ["doggos", "cattos"] }
])) ]))
.await; .await;
dbg!(ret); server.wait_task(3).await;
server.wait_task(2).await; let (task, _) = server.get_task(3).await;
let (task, _) = server.get_task(2).await;
assert_json_snapshot!(task, assert_json_snapshot!(task,
{ ".duration" => "[duration]", ".enqueuedAt" => "[date]", ".startedAt" => "[date]", ".finishedAt" => "[date]" }, { ".duration" => "[duration]", ".enqueuedAt" => "[date]", ".startedAt" => "[date]", ".finishedAt" => "[date]" },
@r###" @r###"
{ {
"uid": 2, "uid": 3,
"indexUid": null, "indexUid": null,
"status": "succeeded", "status": "succeeded",
"type": "indexSwap", "type": "indexSwap",