update tests & fix the broken code

This commit is contained in:
Quentin de Quelen 2020-04-16 11:09:47 +02:00 committed by qdequele
parent 5e2861ff55
commit 27b3b53bc5
No known key found for this signature in database
GPG key ID: B3F0A000EBF11745
17 changed files with 554 additions and 555 deletions

View file

@ -26,8 +26,8 @@ pub enum ResponseError {
impl fmt::Display for ResponseError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Internal(err) => write!(f, "Internal server error: {}", err),
Self::BadRequest(err) => write!(f, "Bad request: {}", err),
Self::Internal(err) => write!(f, "{}", err),
Self::BadRequest(err) => write!(f, "{}", err),
Self::MissingAuthorizationHeader => write!(f, "You must have an authorization token"),
Self::InvalidToken(err) => write!(f, "Invalid API key: {}", err),
Self::NotFound(err) => write!(f, "{} not found", err),

View file

@ -28,6 +28,7 @@ pub fn create_app(
> {
App::new()
.app_data(web::Data::new(data.clone()))
.app_data(web::JsonConfig::default().limit(1024 * 1024 * 10)) // Json Limit of 10Mb
.wrap(helpers::Authentication::Public)
.service(routes::load_html)
.service(routes::load_css)

View file

@ -22,7 +22,7 @@ pub struct DocumentParam {
pub async fn get_document(
data: web::Data<Data>,
path: web::Path<DocumentParam>,
) -> aweb::Result<web::Json<Document>> {
) -> aweb::Result<HttpResponse> {
let index = data
.db
.open_index(&path.index_uid)
@ -39,7 +39,7 @@ pub async fn get_document(
.map_err(|_| ResponseError::DocumentNotFound(path.document_id.clone()))?
.ok_or(ResponseError::DocumentNotFound(path.document_id.clone()))?;
Ok(web::Json(response))
Ok(HttpResponse::Ok().json(response))
}
#[delete("/indexes/{index_uid}/documents/{document_id}")]
@ -85,7 +85,7 @@ pub async fn get_all_documents(
data: web::Data<Data>,
path: web::Path<IndexParam>,
params: web::Query<BrowseQuery>,
) -> aweb::Result<web::Json<Vec<Document>>> {
) -> aweb::Result<HttpResponse> {
let index = data
.db
.open_index(&path.index_uid)
@ -114,14 +114,14 @@ pub async fn get_all_documents(
.clone()
.map(|a| a.split(',').map(|a| a.to_string()).collect());
let mut response_body = Vec::<Document>::new();
let mut response = Vec::<Document>::new();
for document_id in documents_ids {
if let Ok(Some(document)) = index.document(&reader, attributes.clone(), document_id) {
response_body.push(document);
response.push(document);
}
}
Ok(web::Json(response_body))
Ok(HttpResponse::Ok().json(response))
}
fn find_primary_key(document: &IndexMap<String, Value>) -> Option<String> {
@ -168,7 +168,7 @@ async fn update_multiple_documents(
let id = match params.primary_key.clone() {
Some(id) => id,
None => body.first().and_then(|docs| find_primary_key(docs)).ok_or(
ResponseError::BadRequest("Impossible to infer the primary key".to_string()),
ResponseError::BadRequest("Could not infer a primary key".to_string()),
)?,
};

View file

@ -1,8 +1,7 @@
use actix_web as aweb;
use actix_web::{delete, get, post, web, HttpResponse};
use actix_web::{delete, get, post, put, web, HttpResponse};
use chrono::{DateTime, Utc};
use log::error;
use meilisearch_core::UpdateStatus;
use rand::seq::SliceRandom;
use serde::{Deserialize, Serialize};
@ -30,13 +29,13 @@ pub struct IndexResponse {
}
#[get("/indexes")]
pub async fn list_indexes(data: web::Data<Data>) -> aweb::Result<web::Json<Vec<IndexResponse>>> {
pub async fn list_indexes(data: web::Data<Data>) -> aweb::Result<HttpResponse> {
let reader = data
.db
.main_read_txn()
.map_err(|err| ResponseError::Internal(err.to_string()))?;
let mut response_body = Vec::new();
let mut response = Vec::new();
for index_uid in data.db.indexes_uids() {
let index = data.db.open_index(&index_uid);
@ -80,7 +79,7 @@ pub async fn list_indexes(data: web::Data<Data>) -> aweb::Result<web::Json<Vec<I
updated_at,
primary_key,
};
response_body.push(index_response);
response.push(index_response);
}
None => error!(
"Index {} is referenced in the indexes list but cannot be found",
@ -89,14 +88,14 @@ pub async fn list_indexes(data: web::Data<Data>) -> aweb::Result<web::Json<Vec<I
}
}
Ok(web::Json(response_body))
Ok(HttpResponse::Ok().json(response))
}
#[get("/indexes/{index_uid}")]
pub async fn get_index(
data: web::Data<Data>,
path: web::Path<IndexParam>,
) -> aweb::Result<web::Json<IndexResponse>> {
) -> aweb::Result<HttpResponse> {
let index = data
.db
.open_index(path.index_uid.clone())
@ -137,7 +136,7 @@ pub async fn get_index(
_ => None,
};
Ok(web::Json(IndexResponse {
Ok(HttpResponse::Ok().json(IndexResponse {
name,
uid: path.index_uid.clone(),
created_at,
@ -158,7 +157,7 @@ pub struct IndexCreateRequest {
pub async fn create_index(
data: web::Data<Data>,
body: web::Json<IndexCreateRequest>,
) -> aweb::Result<web::Json<IndexResponse>> {
) -> aweb::Result<HttpResponse> {
if let (None, None) = (body.name.clone(), body.uid.clone()) {
return Err(
ResponseError::BadRequest("Index creation must have an uid".to_string()).into(),
@ -232,7 +231,7 @@ pub async fn create_index(
.commit()
.map_err(|err| ResponseError::Internal(err.to_string()))?;
Ok(web::Json(IndexResponse {
Ok(HttpResponse::Created().json(IndexResponse {
name,
uid,
created_at,
@ -258,12 +257,12 @@ pub struct UpdateIndexResponse {
primary_key: Option<String>,
}
#[post("/indexes/{index_uid}")]
#[put("/indexes/{index_uid}")]
pub async fn update_index(
data: web::Data<Data>,
path: web::Path<IndexParam>,
body: web::Json<IndexCreateRequest>,
) -> aweb::Result<web::Json<IndexResponse>> {
) -> aweb::Result<HttpResponse> {
let index = data
.db
.open_index(path.index_uid.clone())
@ -350,7 +349,7 @@ pub async fn update_index(
_ => None,
};
Ok(web::Json(IndexResponse {
Ok(HttpResponse::Ok().json(IndexResponse {
name,
uid: path.index_uid.clone(),
created_at,
@ -381,7 +380,7 @@ pub struct UpdateParam {
pub async fn get_update_status(
data: web::Data<Data>,
path: web::Path<UpdateParam>,
) -> aweb::Result<web::Json<UpdateStatus>> {
) -> aweb::Result<HttpResponse> {
let index = data
.db
.open_index(path.index_uid.clone())
@ -397,7 +396,7 @@ pub async fn get_update_status(
.map_err(|e| ResponseError::Internal(e.to_string()))?;
match status {
Some(status) => Ok(web::Json(status)),
Some(status) => Ok(HttpResponse::Ok().json(status)),
None => Err(ResponseError::NotFound(format!("Update {} not found", path.update_id)).into()),
}
}
@ -406,7 +405,7 @@ pub async fn get_update_status(
pub async fn get_all_updates_status(
data: web::Data<Data>,
path: web::Path<IndexParam>,
) -> aweb::Result<web::Json<Vec<UpdateStatus>>> {
) -> aweb::Result<HttpResponse> {
let index = data
.db
.open_index(path.index_uid.clone())
@ -421,5 +420,5 @@ pub async fn get_all_updates_status(
.all_updates_status(&reader)
.map_err(|err| ResponseError::Internal(err.to_string()))?;
Ok(web::Json(response))
Ok(HttpResponse::Ok().json(response))
}

View file

@ -11,6 +11,35 @@ use crate::Data;
pub async fn update_all(
data: web::Data<Data>,
path: web::Path<IndexParam>,
body: web::Json<Settings>,
) -> aweb::Result<HttpResponse> {
let index = data
.db
.open_index(&path.index_uid)
.ok_or(ResponseError::IndexNotFound(path.index_uid.clone()))?;
let mut writer = data
.db
.update_write_txn()
.map_err(|err| ResponseError::Internal(err.to_string()))?;
let settings = body
.into_inner()
.into_update()
.map_err(|e| ResponseError::BadRequest(e.to_string()))?;
let update_id = index
.settings_update(&mut writer, settings)
.map_err(|err| ResponseError::Internal(err.to_string()))?;
writer
.commit()
.map_err(|err| ResponseError::Internal(err.to_string()))?;
Ok(HttpResponse::Accepted().json(IndexUpdateResponse::with_id(update_id)))
}
#[get("/indexes/{index_uid}/settings")]
pub async fn get_all(
data: web::Data<Data>,
path: web::Path<IndexParam>,
) -> aweb::Result<HttpResponse> {
let index = data
.db
@ -106,35 +135,6 @@ pub async fn update_all(
Ok(HttpResponse::Ok().json(settings))
}
#[get("/indexes/{index_uid}/settings")]
pub async fn get_all(
data: web::Data<Data>,
path: web::Path<IndexParam>,
body: web::Json<Settings>,
) -> aweb::Result<HttpResponse> {
let index = data
.db
.open_index(&path.index_uid)
.ok_or(ResponseError::IndexNotFound(path.index_uid.clone()))?;
let mut writer = data
.db
.update_write_txn()
.map_err(|err| ResponseError::Internal(err.to_string()))?;
let settings = body
.into_inner()
.into_update()
.map_err(|e| ResponseError::BadRequest(e.to_string()))?;
let update_id = index
.settings_update(&mut writer, settings)
.map_err(|err| ResponseError::Internal(err.to_string()))?;
writer
.commit()
.map_err(|err| ResponseError::Internal(err.to_string()))?;
Ok(HttpResponse::Accepted().json(IndexUpdateResponse::with_id(update_id)))
}
#[delete("/indexes/{index_uid}/settings")]
pub async fn delete_all(
data: web::Data<Data>,