fix tests + fmt

This commit is contained in:
qdequele 2020-03-10 11:29:56 +01:00
parent c984d8d5a5
commit 179969a9e2
No known key found for this signature in database
GPG Key ID: B3F0A000EBF11745
10 changed files with 57 additions and 128 deletions

View File

@ -359,7 +359,7 @@ mod tests {
use crate::criterion::{self, CriteriaBuilder}; use crate::criterion::{self, CriteriaBuilder};
use crate::update::{ProcessedUpdateResult, UpdateStatus}; use crate::update::{ProcessedUpdateResult, UpdateStatus};
use crate::settings::{Settings, SettingsUpdate, UpdateState}; use crate::settings::Settings;
use crate::{Document, DocumentId}; use crate::{Document, DocumentId};
use serde::de::IgnoredAny; use serde::de::IgnoredAny;
use std::sync::mpsc; use std::sync::mpsc;
@ -379,17 +379,11 @@ mod tests {
database.set_update_callback(Box::new(update_fn)); database.set_update_callback(Box::new(update_fn));
let settings_update = SettingsUpdate{ let mut writer = db.main_write_txn().unwrap();
primary_key: UpdateState::Update("id".to_string()), index.main.put_schema(&mut writer, &Schema::with_primary_key("id")).unwrap();
..SettingsUpdate::default()
};
let mut writer = db.update_write_txn().unwrap();
let update_id = index.settings_update(&mut writer, settings_update).unwrap();
writer.commit().unwrap(); writer.commit().unwrap();
// block until the transaction is processed // block until the transaction is processed
let _ = receiver.iter().find(|id| *id == update_id);
let settings = { let settings = {
let data = r#" let data = r#"
@ -450,18 +444,10 @@ mod tests {
database.set_update_callback(Box::new(update_fn)); database.set_update_callback(Box::new(update_fn));
let settings_update = SettingsUpdate{ let mut writer = db.main_write_txn().unwrap();
primary_key: UpdateState::Update("id".to_string()), index.main.put_schema(&mut writer, &Schema::with_primary_key("id")).unwrap();
..SettingsUpdate::default()
};
let mut writer = db.update_write_txn().unwrap();
let update_id = index.settings_update(&mut writer, settings_update).unwrap();
writer.commit().unwrap(); writer.commit().unwrap();
// block until the transaction is processed
let _ = receiver.iter().find(|id| *id == update_id);
let settings = { let settings = {
let data = r#" let data = r#"
{ {
@ -520,18 +506,10 @@ mod tests {
database.set_update_callback(Box::new(update_fn)); database.set_update_callback(Box::new(update_fn));
let settings_update = SettingsUpdate{ let mut writer = db.main_write_txn().unwrap();
primary_key: UpdateState::Update("id".to_string()), index.main.put_schema(&mut writer, &Schema::with_primary_key("id")).unwrap();
..SettingsUpdate::default()
};
let mut writer = db.update_write_txn().unwrap();
let update_id = index.settings_update(&mut writer, settings_update).unwrap();
writer.commit().unwrap(); writer.commit().unwrap();
// block until the transaction is processed
let _ = receiver.iter().find(|id| *id == update_id);
let settings = { let settings = {
let data = r#" let data = r#"
{ {
@ -583,18 +561,10 @@ mod tests {
database.set_update_callback(Box::new(update_fn)); database.set_update_callback(Box::new(update_fn));
let settings_update = SettingsUpdate{ let mut writer = db.main_write_txn().unwrap();
primary_key: UpdateState::Update("id".to_string()), index.main.put_schema(&mut writer, &Schema::with_primary_key("id")).unwrap();
..SettingsUpdate::default()
};
let mut writer = db.update_write_txn().unwrap();
let update_id = index.settings_update(&mut writer, settings_update).unwrap();
writer.commit().unwrap(); writer.commit().unwrap();
// block until the transaction is processed
let _ = receiver.iter().find(|id| *id == update_id);
let settings = { let settings = {
let data = r#" let data = r#"
{ {
@ -735,18 +705,10 @@ mod tests {
database.set_update_callback(Box::new(update_fn)); database.set_update_callback(Box::new(update_fn));
let settings_update = SettingsUpdate{ let mut writer = db.main_write_txn().unwrap();
primary_key: UpdateState::Update("id".to_string()), index.main.put_schema(&mut writer, &Schema::with_primary_key("id")).unwrap();
..SettingsUpdate::default()
};
let mut writer = db.update_write_txn().unwrap();
let update_id = index.settings_update(&mut writer, settings_update).unwrap();
writer.commit().unwrap(); writer.commit().unwrap();
// block until the transaction is processed
let _ = receiver.iter().find(|id| *id == update_id);
let settings = { let settings = {
let data = r#" let data = r#"
{ {
@ -823,18 +785,10 @@ mod tests {
database.set_update_callback(Box::new(update_fn)); database.set_update_callback(Box::new(update_fn));
let settings_update = SettingsUpdate{ let mut writer = db.main_write_txn().unwrap();
primary_key: UpdateState::Update("id".to_string()), index.main.put_schema(&mut writer, &Schema::with_primary_key("id")).unwrap();
..SettingsUpdate::default()
};
let mut writer = db.update_write_txn().unwrap();
let update_id = index.settings_update(&mut writer, settings_update).unwrap();
writer.commit().unwrap(); writer.commit().unwrap();
// block until the transaction is processed
let _ = receiver.iter().find(|id| *id == update_id);
let settings = { let settings = {
let data = r#" let data = r#"
{ {
@ -970,18 +924,10 @@ mod tests {
database.set_update_callback(Box::new(update_fn)); database.set_update_callback(Box::new(update_fn));
let settings_update = SettingsUpdate{ let mut writer = db.main_write_txn().unwrap();
primary_key: UpdateState::Update("id".to_string()), index.main.put_schema(&mut writer, &Schema::with_primary_key("id")).unwrap();
..SettingsUpdate::default()
};
let mut writer = db.update_write_txn().unwrap();
let update_id = index.settings_update(&mut writer, settings_update).unwrap();
writer.commit().unwrap(); writer.commit().unwrap();
// block until the transaction is processed
let _ = receiver.iter().find(|id| *id == update_id);
let settings = { let settings = {
let data = r#" let data = r#"
{ {
@ -1045,18 +991,10 @@ mod tests {
database.set_update_callback(Box::new(update_fn)); database.set_update_callback(Box::new(update_fn));
let settings_update = SettingsUpdate{ let mut writer = db.main_write_txn().unwrap();
primary_key: UpdateState::Update("id".to_string()), index.main.put_schema(&mut writer, &Schema::with_primary_key("id")).unwrap();
..SettingsUpdate::default()
};
let mut writer = db.update_write_txn().unwrap();
let update_id = index.settings_update(&mut writer, settings_update).unwrap();
writer.commit().unwrap(); writer.commit().unwrap();
// block until the transaction is processed
let _ = receiver.iter().find(|id| *id == update_id);
let settings = { let settings = {
let data = r#" let data = r#"
{ {

View File

@ -135,7 +135,10 @@ async fn update_multiple_documents(mut ctx: Request<Data>, is_partial: bool) ->
let db = &ctx.state().db; let db = &ctx.state().db;
let reader = db.main_read_txn()?; let reader = db.main_read_txn()?;
let mut schema = index.main.schema(&reader)?.ok_or(ResponseError::internal("schema not found"))?; let mut schema = index
.main
.schema(&reader)?
.ok_or(ResponseError::internal("schema not found"))?;
if schema.primary_key().is_none() { if schema.primary_key().is_none() {
let id = match query.document_id { let id = match query.document_id {

View File

@ -42,7 +42,7 @@ pub async fn list_indexes(ctx: Request<Data>) -> SResult<Response> {
let primary_key = match index.main.schema(&reader) { let primary_key = match index.main.schema(&reader) {
Ok(Some(schema)) => match schema.primary_key() { Ok(Some(schema)) => match schema.primary_key() {
Some(primary_key) => Some(primary_key.to_owned()), Some(primary_key) => Some(primary_key.to_owned()),
None => None None => None,
}, },
_ => None, _ => None,
}; };
@ -92,7 +92,7 @@ pub async fn get_index(ctx: Request<Data>) -> SResult<Response> {
let primary_key = match index.main.schema(&reader) { let primary_key = match index.main.schema(&reader) {
Ok(Some(schema)) => match schema.primary_key() { Ok(Some(schema)) => match schema.primary_key() {
Some(primary_key) => Some(primary_key.to_owned()), Some(primary_key) => Some(primary_key.to_owned()),
None => None None => None,
}, },
_ => None, _ => None,
}; };
@ -144,12 +144,15 @@ pub async fn create_index(mut ctx: Request<Data>) -> SResult<Response> {
let uid = match body.uid { let uid = match body.uid {
Some(uid) => { Some(uid) => {
if uid.chars().all(|x| x.is_ascii_alphanumeric() || x == '-' || x == '_') { if uid
.chars()
.all(|x| x.is_ascii_alphanumeric() || x == '-' || x == '_')
{
uid uid
} else { } else {
return Err(ResponseError::InvalidIndexUid) return Err(ResponseError::InvalidIndexUid);
}
} }
},
None => loop { None => loop {
let uid = generate_uid(); let uid = generate_uid();
if db.open_index(&uid).is_none() { if db.open_index(&uid).is_none() {
@ -235,8 +238,10 @@ pub async fn update_index(mut ctx: Request<Data>) -> SResult<Response> {
if let Some(mut schema) = index.main.schema(&mut writer)? { if let Some(mut schema) = index.main.schema(&mut writer)? {
match schema.primary_key() { match schema.primary_key() {
Some(_) => { Some(_) => {
return Err(ResponseError::bad_request("The index primary key cannot be updated")); return Err(ResponseError::bad_request(
}, "The index primary key cannot be updated",
));
}
None => { None => {
if let Ok(_) = schema.set_primary_key(&id) { if let Ok(_) = schema.set_primary_key(&id) {
index.main.put_schema(&mut writer, &schema)?; index.main.put_schema(&mut writer, &schema)?;
@ -255,14 +260,9 @@ pub async fn update_index(mut ctx: Request<Data>) -> SResult<Response> {
let updated_at = index.main.updated_at(&reader)?.into_internal_error()?; let updated_at = index.main.updated_at(&reader)?.into_internal_error()?;
let primary_key = match index.main.schema(&reader) { let primary_key = match index.main.schema(&reader) {
Ok(Some(schema)) => { Ok(Some(schema)) => match schema.primary_key() {
match schema.primary_key() { Some(primary_key) => Some(primary_key.to_owned()),
Some(primary_key) => { None => None,
Some(primary_key.to_owned())
},
None => None
}
}, },
_ => None, _ => None,
}; };

View File

@ -69,9 +69,7 @@ impl Server {
fn get_request(&mut self, url: &str) -> (Value, StatusCode) { fn get_request(&mut self, url: &str) -> (Value, StatusCode) {
eprintln!("get_request: {}", url); eprintln!("get_request: {}", url);
let req = http::Request::get(url) let req = http::Request::get(url).body(Body::empty()).unwrap();
.body(Body::empty())
.unwrap();
let res = self.mock.simulate(req).unwrap(); let res = self.mock.simulate(req).unwrap();
let status_code = res.status().clone(); let status_code = res.status().clone();
@ -133,9 +131,7 @@ impl Server {
fn delete_request(&mut self, url: &str) -> (Value, StatusCode) { fn delete_request(&mut self, url: &str) -> (Value, StatusCode) {
eprintln!("delete_request: {}", url); eprintln!("delete_request: {}", url);
let req = http::Request::delete(url) let req = http::Request::delete(url).body(Body::empty()).unwrap();
.body(Body::empty())
.unwrap();
let res = self.mock.simulate(req).unwrap(); let res = self.mock.simulate(req).unwrap();
let status_code = res.status().clone(); let status_code = res.status().clone();
@ -154,7 +150,6 @@ impl Server {
(response, status_code) (response, status_code)
} }
// // All Routes // // All Routes
pub fn list_indexes(&mut self) -> (Value, StatusCode) { pub fn list_indexes(&mut self) -> (Value, StatusCode) {
@ -221,12 +216,20 @@ impl Server {
} }
pub fn get_document(&mut self, document_id: impl ToString) -> (Value, StatusCode) { pub fn get_document(&mut self, document_id: impl ToString) -> (Value, StatusCode) {
let url = format!("/indexes/{}/documents/{}", self.uid, document_id.to_string()); let url = format!(
"/indexes/{}/documents/{}",
self.uid,
document_id.to_string()
);
self.get_request(&url) self.get_request(&url)
} }
pub fn delete_document(&mut self, document_id: impl ToString) -> (Value, StatusCode) { pub fn delete_document(&mut self, document_id: impl ToString) -> (Value, StatusCode) {
let url = format!("/indexes/{}/documents/{}", self.uid, document_id.to_string()); let url = format!(
"/indexes/{}/documents/{}",
self.uid,
document_id.to_string()
);
self.delete_request_async(&url) self.delete_request_async(&url)
} }
@ -443,5 +446,4 @@ impl Server {
self.add_or_replace_multiple_documents(body); self.add_or_replace_multiple_documents(body);
} }
} }

View File

@ -14,7 +14,6 @@ fn delete() {
assert_eq!(status_code, 404); assert_eq!(status_code, 404);
} }
// Resolve teh issue https://github.com/meilisearch/MeiliSearch/issues/493 // Resolve teh issue https://github.com/meilisearch/MeiliSearch/issues/493
#[test] #[test]
fn delete_batch() { fn delete_batch() {

View File

@ -1,5 +1,5 @@
use serde_json::json;
use assert_json_diff::assert_json_eq; use assert_json_diff::assert_json_eq;
use serde_json::json;
mod common; mod common;
@ -413,8 +413,6 @@ fn create_index_failed() {
assert_eq!(message, "invalid data"); assert_eq!(message, "invalid data");
} }
// Resolve issue https://github.com/meilisearch/MeiliSearch/issues/492 // Resolve issue https://github.com/meilisearch/MeiliSearch/issues/492
#[test] #[test]
fn create_index_with_primary_key_and_index() { fn create_index_with_primary_key_and_index() {
@ -542,7 +540,6 @@ fn create_index_and_add_indentifier_after() {
let (response, status_code) = server.get_index(); let (response, status_code) = server.get_index();
assert_eq!(status_code, 200); assert_eq!(status_code, 200);
assert_eq!(response["primaryKey"].as_str().unwrap(), "id"); assert_eq!(response["primaryKey"].as_str().unwrap(), "id");
} }
// Test that it's impossible to change the primary_key // Test that it's impossible to change the primary_key
@ -576,7 +573,6 @@ fn create_index_and_update_indentifier_after() {
assert_eq!(response["primaryKey"].as_str().unwrap(), "id"); assert_eq!(response["primaryKey"].as_str().unwrap(), "id");
} }
// Test that schema inference work well // Test that schema inference work well
#[test] #[test]
fn create_index_without_primary_key_and_add_document() { fn create_index_without_primary_key_and_add_document() {
@ -607,7 +603,6 @@ fn create_index_without_primary_key_and_add_document() {
assert_eq!(response["primaryKey"].as_str().unwrap(), "id"); assert_eq!(response["primaryKey"].as_str().unwrap(), "id");
} }
// Test search with no primary_key // Test search with no primary_key
#[test] #[test]
fn create_index_without_primary_key_and_search() { fn create_index_without_primary_key_and_search() {

View File

@ -1,6 +1,6 @@
use std::convert::Into;
use serde_json::json;
use assert_json_diff::assert_json_eq; use assert_json_diff::assert_json_eq;
use serde_json::json;
use std::convert::Into;
mod common; mod common;

View File

@ -1,6 +1,6 @@
use std::convert::Into;
use assert_json_diff::assert_json_eq; use assert_json_diff::assert_json_eq;
use serde_json::json; use serde_json::json;
use std::convert::Into;
mod common; mod common;
@ -253,7 +253,6 @@ fn write_all_and_update() {
assert_json_eq!(expected, response, ordered: false); assert_json_eq!(expected, response, ordered: false);
} }
#[test] #[test]
fn test_default_settings() { fn test_default_settings() {
let mut server = common::Server::with_uid("movies"); let mut server = common::Server::with_uid("movies");

View File

@ -115,9 +115,7 @@ fn send_undefined_rule() {
}); });
server.create_index(body); server.create_index(body);
let body = json!([ let body = json!(["typos",]);
"typos",
]);
let (_response, status_code) = server.update_ranking_rules_sync(body); let (_response, status_code) = server.update_ranking_rules_sync(body);
assert_eq!(status_code, 400); assert_eq!(status_code, 400);
@ -132,9 +130,7 @@ fn send_malformed_custom_rule() {
}); });
server.create_index(body); server.create_index(body);
let body = json!([ let body = json!(["dsc(truc)",]);
"dsc(truc)",
]);
let (_response, status_code) = server.update_ranking_rules_sync(body); let (_response, status_code) = server.update_ranking_rules_sync(body);
assert_eq!(status_code, 400); assert_eq!(status_code, 400);

View File

@ -19,10 +19,7 @@ fn update_stop_words() {
// 2 - Update stop words // 2 - Update stop words
let body = json!([ let body = json!(["the", "a"]);
"the",
"a"
]);
server.update_stop_words(body.clone()); server.update_stop_words(body.clone());
// 3 - Get all stop words and compare to the previous one // 3 - Get all stop words and compare to the previous one