update test

This commit is contained in:
qdequele 2020-03-04 14:18:07 +01:00 committed by Quentin de Quelen
parent 6016f2e941
commit 81ce90e57f
6 changed files with 640 additions and 776 deletions

View File

@ -1,10 +1,9 @@
#![allow(dead_code)] #![allow(dead_code)]
use http::StatusCode;
use serde_json::Value; use serde_json::Value;
use std::error::Error;
use std::time::Duration; use std::time::Duration;
use assert_json_diff::assert_json_eq;
use async_std::io::prelude::*; use async_std::io::prelude::*;
use async_std::task::{block_on, sleep}; use async_std::task::{block_on, sleep};
use http_service::Body; use http_service::Body;
@ -16,8 +15,14 @@ use serde_json::json;
use tempdir::TempDir; use tempdir::TempDir;
use tide::server::Service; use tide::server::Service;
pub fn setup_server() -> Result<TestBackend<Service<Data>>, Box<dyn Error>> { pub struct Server {
let tmp_dir = TempDir::new("meilisearch")?; uid: String,
mock: TestBackend<Service<Data>>,
}
impl Server {
pub fn with_uid(uid: &str) -> Server {
let tmp_dir = TempDir::new("meilisearch").unwrap();
let opt = Opt { let opt = Opt {
db_path: tmp_dir.path().to_str().unwrap().to_string(), db_path: tmp_dir.path().to_str().unwrap().to_string(),
@ -31,31 +36,363 @@ pub fn setup_server() -> Result<TestBackend<Service<Data>>, Box<dyn Error>> {
let mut app = tide::with_state(data); let mut app = tide::with_state(data);
routes::load_routes(&mut app); routes::load_routes(&mut app);
let http_server = app.into_http_service(); let http_server = app.into_http_service();
Ok(make_server(http_server)?) let mock = make_server(http_server).unwrap();
}
pub fn enrich_server_with_movies_index( Server {
server: &mut TestBackend<Service<Data>>, uid: uid.to_string(),
) -> Result<(), Box<dyn Error>> { mock,
}
}
fn wait_update_id(&mut self, update_id: u64) {
loop {
let req = http::Request::get(format!("/indexes/{}/updates/{}", self.uid, update_id))
.body(Body::empty())
.unwrap();
let res = self.mock.simulate(req).unwrap();
assert_eq!(res.status(), 200);
let mut buf = Vec::new();
block_on(res.into_body().read_to_end(&mut buf)).unwrap();
let response: Value = serde_json::from_slice(&buf).unwrap();
if response["status"] == "processed" {
return;
}
block_on(sleep(Duration::from_secs(1)));
}
}
// // Global Http request GET/POST/DELETE async or sync
fn get_request(&mut self, url: &str) -> (Value, StatusCode) {
eprintln!("get_request: {}", url);
let req = http::Request::get(url)
.body(Body::empty())
.unwrap();
let res = self.mock.simulate(req).unwrap();
let status_code = res.status().clone();
let mut buf = Vec::new();
block_on(res.into_body().read_to_end(&mut buf)).unwrap();
let response = serde_json::from_slice(&buf).unwrap_or_default();
(response, status_code)
}
fn post_request(&mut self, url: &str, body: Value) -> (Value, StatusCode) {
eprintln!("post_request: {}", url);
let body_bytes = body.to_string().into_bytes();
let req = http::Request::post(url)
.body(Body::from(body_bytes))
.unwrap();
let res = self.mock.simulate(req).unwrap();
let status_code = res.status().clone();
let mut buf = Vec::new();
block_on(res.into_body().read_to_end(&mut buf)).unwrap();
let response = serde_json::from_slice(&buf).unwrap_or_default();
(response, status_code)
}
fn post_request_async(&mut self, url: &str, body: Value) -> (Value, StatusCode) {
eprintln!("post_request_async: {}", url);
let (response, status_code) = self.post_request(url, body);
assert_eq!(status_code, 202);
assert!(response["updateId"].as_u64().is_some());
self.wait_update_id(response["updateId"].as_u64().unwrap());
(response, status_code)
}
fn put_request(&mut self, url: &str, body: Value) -> (Value, StatusCode) {
eprintln!("put_request: {}", url);
let body_bytes = body.to_string().into_bytes();
let req = http::Request::put(url)
.body(Body::from(body_bytes))
.unwrap();
let res = self.mock.simulate(req).unwrap();
let status_code = res.status().clone();
let mut buf = Vec::new();
block_on(res.into_body().read_to_end(&mut buf)).unwrap();
let response = serde_json::from_slice(&buf).unwrap_or_default();
(response, status_code)
}
fn put_request_async(&mut self, url: &str, body: Value) -> (Value, StatusCode) {
eprintln!("put_request_async: {}", url);
let (response, status_code) = self.put_request(url, body);
assert!(response["updateId"].as_u64().is_some());
assert_eq!(status_code, 202);
self.wait_update_id(response["updateId"].as_u64().unwrap());
(response, status_code)
}
fn delete_request(&mut self, url: &str) -> (Value, StatusCode) {
eprintln!("delete_request: {}", url);
let req = http::Request::delete(url)
.body(Body::empty())
.unwrap();
let res = self.mock.simulate(req).unwrap();
let status_code = res.status().clone();
let mut buf = Vec::new();
block_on(res.into_body().read_to_end(&mut buf)).unwrap();
let response = serde_json::from_slice(&buf).unwrap_or_default();
(response, status_code)
}
fn delete_request_async(&mut self, url: &str) -> (Value, StatusCode) {
eprintln!("delete_request_async: {}", url);
let (response, status_code) = self.delete_request(url);
assert!(response["updateId"].as_u64().is_some());
assert_eq!(status_code, 202);
self.wait_update_id(response["updateId"].as_u64().unwrap());
(response, status_code)
}
// // All Routes
pub fn list_indexes(&mut self) -> (Value, StatusCode) {
self.get_request("/indexes")
}
pub fn create_index(&mut self, body: Value) -> (Value, StatusCode) {
self.post_request("/indexes", body)
}
pub fn search_multi_index(&mut self, query: &str) -> (Value, StatusCode) {
let url = format!("/indexes/search?{}", query);
self.get_request(&url)
}
pub fn get_index(&mut self) -> (Value, StatusCode) {
let url = format!("/indexes/{}", self.uid);
self.get_request(&url)
}
pub fn update_index(&mut self, body: Value) -> (Value, StatusCode) {
let url = format!("/indexes/{}", self.uid);
self.put_request(&url, body)
}
pub fn delete_index(&mut self) -> (Value, StatusCode) {
let url = format!("/indexes/{}", self.uid);
self.delete_request(&url)
}
pub fn search(&mut self, query: &str) -> (Value, StatusCode) {
let url = format!("/indexes/{}/search?{}", self.uid, query);
self.get_request(&url)
}
pub fn get_all_updates_status(&mut self) -> (Value, StatusCode) {
let url = format!("/indexes/{}/updates", self.uid);
self.get_request(&url)
}
pub fn get_update_status(&mut self, update_id: u64) -> (Value, StatusCode) {
let url = format!("/indexes/{}/updates/{}", self.uid, update_id);
self.get_request(&url)
}
pub fn get_all_documents(&mut self) -> (Value, StatusCode) {
let url = format!("/indexes/{}/documents", self.uid);
self.get_request(&url)
}
pub fn add_or_replace_multiple_documents(&mut self, body: Value) {
let url = format!("/indexes/{}/documents", self.uid);
self.post_request_async(&url, body);
}
pub fn add_or_update_multiple_documents(&mut self, body: Value) {
let url = format!("/indexes/{}/documents", self.uid);
self.put_request_async(&url, body);
}
pub fn clear_all_documents(&mut self) {
let url = format!("/indexes/{}/documents", self.uid);
self.delete_request_async(&url);
}
pub fn get_document(&mut self, document_id: u64) -> (Value, StatusCode) {
let url = format!("/indexes/{}/documents/{}", self.uid, document_id);
self.get_request(&url)
}
pub fn delete_document(&mut self, document_id: u64) -> (Value, StatusCode) {
let url = format!("/indexes/{}/documents/{}", self.uid, document_id);
self.delete_request(&url)
}
pub fn delete_multiple_documents(&mut self, body: Value) {
let url = format!("/indexes/{}/documents/delete-batch", self.uid);
self.post_request_async(&url, body);
}
pub fn get_all_settings(&mut self) -> (Value, StatusCode) {
let url = format!("/indexes/{}/settings", self.uid);
self.get_request(&url)
}
pub fn update_all_settings(&mut self, body: Value) {
let url = format!("/indexes/{}/settings", self.uid);
self.post_request_async(&url, body);
}
pub fn delete_all_settings(&mut self) -> (Value, StatusCode) {
let url = format!("/indexes/{}/settings", self.uid);
self.delete_request_async(&url)
}
pub fn get_ranking_rules(&mut self) -> (Value, StatusCode) {
let url = format!("/indexes/{}/settings/ranking-rules", self.uid);
self.get_request(&url)
}
pub fn update_ranking_rules(&mut self, body: Value) {
let url = format!("/indexes/{}/settings/ranking-rules", self.uid);
self.post_request_async(&url, body);
}
pub fn delete_ranking_rules(&mut self) -> (Value, StatusCode) {
let url = format!("/indexes/{}/settings/ranking-rules", self.uid);
self.delete_request_async(&url)
}
pub fn get_distinct_attribute(&mut self) -> (Value, StatusCode) {
let url = format!("/indexes/{}/settings/distinct-attribute", self.uid);
self.get_request(&url)
}
pub fn update_distinct_attribute(&mut self, body: Value) {
let url = format!("/indexes/{}/settings/distinct-attribute", self.uid);
self.post_request_async(&url, body);
}
pub fn delete_distinct_attribute(&mut self) -> (Value, StatusCode) {
let url = format!("/indexes/{}/settings/distinct-attribute", self.uid);
self.delete_request_async(&url)
}
pub fn get_identifier(&mut self) -> (Value, StatusCode) {
let url = format!("/indexes/{}/settings/identifier", self.uid);
self.get_request(&url)
}
pub fn get_searchable_attributes(&mut self) -> (Value, StatusCode) {
let url = format!("/indexes/{}/settings/searchable-attributes", self.uid);
self.get_request(&url)
}
pub fn update_searchable_attributes(&mut self, body: Value) {
let url = format!("/indexes/{}/settings/searchable-attributes", self.uid);
self.post_request_async(&url, body);
}
pub fn delete_searchable_attributes(&mut self) -> (Value, StatusCode) {
let url = format!("/indexes/{}/settings/searchable-attributes", self.uid);
self.delete_request_async(&url)
}
pub fn get_displayed_attributes(&mut self) -> (Value, StatusCode) {
let url = format!("/indexes/{}/settings/displayed-attributes", self.uid);
self.get_request(&url)
}
pub fn update_displayed_attributes(&mut self, body: Value) {
let url = format!("/indexes/{}/settings/displayed-attributes", self.uid);
self.post_request_async(&url, body);
}
pub fn delete_displayed_attributes(&mut self) -> (Value, StatusCode) {
let url = format!("/indexes/{}/settings/displayed-attributes", self.uid);
self.delete_request_async(&url)
}
pub fn get_accept_new_fields(&mut self) -> (Value, StatusCode) {
let url = format!("/indexes/{}/settings/accept-new-fields", self.uid);
self.get_request(&url)
}
pub fn update_accept_new_fields(&mut self, body: Value) {
let url = format!("/indexes/{}/settings/accept-new-fields", self.uid);
self.post_request_async(&url, body);
}
pub fn get_synonyms(&mut self) -> (Value, StatusCode) {
let url = format!("/indexes/{}/settings/synonyms", self.uid);
self.get_request(&url)
}
pub fn update_synonyms(&mut self, body: Value) {
let url = format!("/indexes/{}/settings/synonyms", self.uid);
self.post_request_async(&url, body);
}
pub fn delete_synonyms(&mut self) -> (Value, StatusCode) {
let url = format!("/indexes/{}/settings/synonyms", self.uid);
self.delete_request_async(&url)
}
pub fn get_stop_words(&mut self) -> (Value, StatusCode) {
let url = format!("/indexes/{}/settings/stop-words", self.uid);
self.get_request(&url)
}
pub fn update_stop_words(&mut self, body: Value) {
let url = format!("/indexes/{}/settings/stop-words", self.uid);
self.post_request_async(&url, body);
}
pub fn delete_stop_words(&mut self) -> (Value, StatusCode) {
let url = format!("/indexes/{}/settings/stop-words", self.uid);
self.delete_request_async(&url)
}
pub fn get_index_stats(&mut self) -> (Value, StatusCode) {
let url = format!("/indexes/{}/stats", self.uid);
self.get_request(&url)
}
pub fn list_keys(&mut self) -> (Value, StatusCode) {
self.get_request("/keys")
}
pub fn get_health(&mut self) -> (Value, StatusCode) {
self.get_request("/health")
}
pub fn update_health(&mut self, body: Value) -> (Value, StatusCode) {
self.put_request("/health", body)
}
pub fn get_version(&mut self) -> (Value, StatusCode) {
self.get_request("/version")
}
pub fn get_sys_info(&mut self) -> (Value, StatusCode) {
self.get_request("/sys-info")
}
pub fn get_sys_info_pretty(&mut self) -> (Value, StatusCode) {
self.get_request("/sys-info/pretty")
}
// Populate routes
pub fn populate_movies(&mut self) {
let body = json!({ let body = json!({
"uid": "movies", "uid": "movies",
"identifier": "id", "identifier": "id",
}) });
.to_string() self.create_index(body);
.into_bytes();
let req = http::Request::post("/indexes") let body = json!({
.body(Body::from(body))
.unwrap();
let _res = server.simulate(req).unwrap();
Ok(())
}
pub fn enrich_server_with_movies_settings(
server: &mut TestBackend<Service<Data>>,
) -> Result<(), Box<dyn Error>> {
let json = json!({
"rankingRules": [ "rankingRules": [
"typo", "typo",
"words", "words",
@ -66,7 +403,6 @@ pub fn enrich_server_with_movies_settings(
"exactness", "exactness",
"desc(vote_average)", "desc(vote_average)",
], ],
"distinctAttribute": null,
"searchableAttributes": [ "searchableAttributes": [
"title", "title",
"tagline", "tagline",
@ -90,97 +426,16 @@ pub fn enrich_server_with_movies_settings(
"poster_path", "poster_path",
"popularity", "popularity",
], ],
"stopWords": null,
"synonyms": null,
"acceptNewFields": false, "acceptNewFields": false,
}); });
let body = json.to_string().into_bytes(); self.update_all_settings(body);
let req = http::Request::post("/indexes/movies/settings") let dataset = include_bytes!("assets/movies.json");
.body(Body::from(body))
.unwrap();
let res = server.simulate(req).unwrap();
let mut buf = Vec::new(); let body: Value = serde_json::from_slice(dataset).unwrap();
block_on(res.into_body().read_to_end(&mut buf)).unwrap();
let response: Value = serde_json::from_slice(&buf).unwrap();
assert!(response["updateId"].as_u64().is_some()); self.add_or_replace_multiple_documents(body);
wait_update_id(server, response["updateId"].as_u64().unwrap());
Ok(())
}
pub fn enrich_server_with_movies_documents(
server: &mut TestBackend<Service<Data>>,
) -> Result<(), Box<dyn Error>> {
let body = include_bytes!("assets/movies.json").to_vec();
let req = http::Request::post("/indexes/movies/documents")
.body(Body::from(body))
.unwrap();
let res = server.simulate(req).unwrap();
let mut buf = Vec::new();
block_on(res.into_body().read_to_end(&mut buf)).unwrap();
let response: Value = serde_json::from_slice(&buf).unwrap();
assert!(response["updateId"].as_u64().is_some());
wait_update_id(server, response["updateId"].as_u64().unwrap());
Ok(())
}
pub fn search(server: &mut TestBackend<Service<Data>>, query: &str, expect: Value) {
let req = http::Request::get(format!("/indexes/movies/search?{}", query))
.body(Body::empty())
.unwrap();
let res = server.simulate(req).unwrap();
let mut buf = Vec::new();
block_on(res.into_body().read_to_end(&mut buf)).unwrap();
let response: Value = serde_json::from_slice(&buf).unwrap();
assert_json_eq!(expect, response["hits"].clone(), ordered: false)
}
pub fn update_config(server: &mut TestBackend<Service<Data>>, config: Value) {
let body = config.to_string().into_bytes();
let req = http::Request::post("/indexes/movies/settings")
.body(Body::from(body))
.unwrap();
let res = server.simulate(req).unwrap();
assert_eq!(res.status(), 202);
let mut buf = Vec::new();
block_on(res.into_body().read_to_end(&mut buf)).unwrap();
let response: Value = serde_json::from_slice(&buf).unwrap();
assert!(response["updateId"].as_u64().is_some());
wait_update_id(server, response["updateId"].as_u64().unwrap());
}
pub fn wait_update_id(server: &mut TestBackend<Service<Data>>, update_id: u64) {
loop {
let req = http::Request::get(format!("/indexes/movies/updates/{}", update_id))
.body(Body::empty())
.unwrap();
let res = server.simulate(req).unwrap();
assert_eq!(res.status(), 200);
let mut buf = Vec::new();
block_on(res.into_body().read_to_end(&mut buf)).unwrap();
let response: Value = serde_json::from_slice(&buf).unwrap();
if response["status"] == "processed" {
return;
}
block_on(sleep(Duration::from_secs(1)));
} }
} }

View File

@ -1,4 +1,3 @@
use http_service::Body;
use serde_json::json; use serde_json::json;
use std::convert::Into; use std::convert::Into;
@ -6,51 +5,34 @@ mod common;
#[test] #[test]
fn test_healthyness() { fn test_healthyness() {
let mut server = common::setup_server().unwrap(); let mut server = common::Server::with_uid("movies");
// Check that the server is healthy // Check that the server is healthy
let req = http::Request::get("/health").body(Body::empty()).unwrap(); let (_response, status_code) = server.get_health();
let res = server.simulate(req).unwrap(); assert_eq!(status_code, 200);
assert_eq!(res.status(), 200);
// Set the serve Unhealthy // Set the serve Unhealthy
let body = json!({ let body = json!({
"health": false, "health": false,
}) });
.to_string() let (_response, status_code) = server.update_health(body);
.into_bytes(); assert_eq!(status_code, 200);
let req = http::Request::put("/health")
.body(Body::from(body))
.unwrap();
let res = server.simulate(req).unwrap();
assert_eq!(res.status(), 200);
// Check that the server is unhealthy // Check that the server is unhealthy
let req = http::Request::get("/health").body(Body::empty()).unwrap(); let (_response, status_code) = server.get_health();
let res = server.simulate(req).unwrap(); assert_eq!(status_code, 503);
assert_eq!(res.status(), 503);
// Set the server healthy // Set the server healthy
let body = json!({ let body = json!({
"health": true, "health": true,
}) });
.to_string() let (_response, status_code) = server.update_health(body);
.into_bytes(); assert_eq!(status_code, 200);
let req = http::Request::put("/health")
.body(Body::from(body))
.unwrap();
let res = server.simulate(req).unwrap();
assert_eq!(res.status(), 200);
// Check if the server is healthy // Check if the server is healthy
let req = http::Request::get("/health").body(Body::empty()).unwrap(); let (_response, status_code) = server.get_health();
let res = server.simulate(req).unwrap(); assert_eq!(status_code, 200);
assert_eq!(res.status(), 200);
} }

View File

@ -1,14 +1,10 @@
use async_std::io::prelude::*;
use async_std::task::block_on;
use http_service::Body;
use serde_json::json; use serde_json::json;
use serde_json::Value;
mod common; mod common;
#[test] #[test]
fn create_index_with_name() { fn create_index_with_name() {
let mut server = common::setup_server().unwrap(); let mut server = common::Server::with_uid("movies");
// 1 - Create a new index // 1 - Create a new index
// Index with only a name "movies" // Index with only a name "movies"
@ -16,20 +12,10 @@ fn create_index_with_name() {
let body = json!({ let body = json!({
"name": "movies", "name": "movies",
}) });
.to_string()
.into_bytes();
let req = http::Request::post("/indexes")
.body(Body::from(body))
.unwrap();
let res = server.simulate(req).unwrap();
assert_eq!(res.status(), 201);
let mut buf = Vec::new();
block_on(res.into_body().read_to_end(&mut buf)).unwrap();
let res1_value: Value = serde_json::from_slice(&buf).unwrap();
let (res1_value, status_code) = server.create_index(body);
assert_eq!(status_code, 201);
assert_eq!(res1_value.as_object().unwrap().len(), 5); assert_eq!(res1_value.as_object().unwrap().len(), 5);
let r1_name = res1_value["name"].as_str().unwrap(); let r1_name = res1_value["name"].as_str().unwrap();
let r1_uid = res1_value["uid"].as_str().unwrap(); let r1_uid = res1_value["uid"].as_str().unwrap();
@ -45,14 +31,8 @@ fn create_index_with_name() {
// Must have 1 index with the exact same content that the request 1 // Must have 1 index with the exact same content that the request 1
// GET: /indexes // GET: /indexes
let req = http::Request::get("/indexes").body(Body::empty()).unwrap(); let (res2_value, status_code) = server.list_indexes();
let res = server.simulate(req).unwrap(); assert_eq!(status_code, 200);
assert_eq!(res.status(), 200);
let mut buf = Vec::new();
block_on(res.into_body().read_to_end(&mut buf)).unwrap();
let res2_value: Value = serde_json::from_slice(&buf).unwrap();
assert_eq!(res2_value.as_array().unwrap().len(), 1); assert_eq!(res2_value.as_array().unwrap().len(), 1);
assert_eq!(res2_value[0].as_object().unwrap().len(), 5); assert_eq!(res2_value[0].as_object().unwrap().len(), 5);
let r2_name = res2_value[0]["name"].as_str().unwrap(); let r2_name = res2_value[0]["name"].as_str().unwrap();
@ -68,7 +48,7 @@ fn create_index_with_name() {
#[test] #[test]
fn create_index_with_uid() { fn create_index_with_uid() {
let mut server = common::setup_server().unwrap(); let mut server = common::Server::with_uid("movies");
// 1 - Create a new index // 1 - Create a new index
// Index with only an uid "movies" // Index with only an uid "movies"
@ -76,20 +56,10 @@ fn create_index_with_uid() {
let body = json!({ let body = json!({
"uid": "movies", "uid": "movies",
}) });
.to_string()
.into_bytes();
let req = http::Request::post("/indexes")
.body(Body::from(body))
.unwrap();
let res = server.simulate(req).unwrap();
assert_eq!(res.status(), 201);
let mut buf = Vec::new();
block_on(res.into_body().read_to_end(&mut buf)).unwrap();
let res1_value: Value = serde_json::from_slice(&buf).unwrap();
let (res1_value, status_code) = server.create_index(body);
assert_eq!(status_code, 201);
assert_eq!(res1_value.as_object().unwrap().len(), 5); assert_eq!(res1_value.as_object().unwrap().len(), 5);
let r1_name = res1_value["name"].as_str().unwrap(); let r1_name = res1_value["name"].as_str().unwrap();
let r1_uid = res1_value["uid"].as_str().unwrap(); let r1_uid = res1_value["uid"].as_str().unwrap();
@ -105,14 +75,8 @@ fn create_index_with_uid() {
// Must have 1 index with the exact same content that the request 1 // Must have 1 index with the exact same content that the request 1
// GET: /indexes // GET: /indexes
let req = http::Request::get("/indexes").body(Body::empty()).unwrap(); let (res2_value, status_code) = server.list_indexes();
let res = server.simulate(req).unwrap(); assert_eq!(status_code, 200);
assert_eq!(res.status(), 200);
let mut buf = Vec::new();
block_on(res.into_body().read_to_end(&mut buf)).unwrap();
let res2_value: Value = serde_json::from_slice(&buf).unwrap();
assert_eq!(res2_value.as_array().unwrap().len(), 1); assert_eq!(res2_value.as_array().unwrap().len(), 1);
assert_eq!(res2_value[0].as_object().unwrap().len(), 5); assert_eq!(res2_value[0].as_object().unwrap().len(), 5);
let r2_name = res2_value[0]["name"].as_str().unwrap(); let r2_name = res2_value[0]["name"].as_str().unwrap();
@ -128,7 +92,7 @@ fn create_index_with_uid() {
#[test] #[test]
fn create_index_with_name_and_uid() { fn create_index_with_name_and_uid() {
let mut server = common::setup_server().unwrap(); let mut server = common::Server::with_uid("movies");
// 1 - Create a new index // 1 - Create a new index
// Index with a name "Films" and an uid "fn_movies" // Index with a name "Films" and an uid "fn_movies"
@ -137,19 +101,9 @@ fn create_index_with_name_and_uid() {
let body = json!({ let body = json!({
"name": "Films", "name": "Films",
"uid": "fr_movies", "uid": "fr_movies",
}) });
.to_string() let (res1_value, status_code) = server.create_index(body);
.into_bytes(); assert_eq!(status_code, 201);
let req = http::Request::post("/indexes")
.body(Body::from(body))
.unwrap();
let res = server.simulate(req).unwrap();
assert_eq!(res.status(), 201);
let mut buf = Vec::new();
block_on(res.into_body().read_to_end(&mut buf)).unwrap();
let res1_value: Value = serde_json::from_slice(&buf).unwrap();
assert_eq!(res1_value.as_object().unwrap().len(), 5); assert_eq!(res1_value.as_object().unwrap().len(), 5);
let r1_name = res1_value["name"].as_str().unwrap(); let r1_name = res1_value["name"].as_str().unwrap();
@ -166,13 +120,8 @@ fn create_index_with_name_and_uid() {
// Must have 1 index with the exact same content that the request 1 // Must have 1 index with the exact same content that the request 1
// GET: /indexes // GET: /indexes
let req = http::Request::get("/indexes").body(Body::empty()).unwrap(); let (res2_value, status_code) = server.list_indexes();
let res = server.simulate(req).unwrap(); assert_eq!(status_code, 200);
assert_eq!(res.status(), 200);
let mut buf = Vec::new();
block_on(res.into_body().read_to_end(&mut buf)).unwrap();
let res2_value: Value = serde_json::from_slice(&buf).unwrap();
assert_eq!(res2_value.as_array().unwrap().len(), 1); assert_eq!(res2_value.as_array().unwrap().len(), 1);
assert_eq!(res2_value[0].as_object().unwrap().len(), 5); assert_eq!(res2_value[0].as_object().unwrap().len(), 5);
@ -189,26 +138,18 @@ fn create_index_with_name_and_uid() {
#[test] #[test]
fn rename_index() { fn rename_index() {
let mut server = common::setup_server().unwrap(); let mut server = common::Server::with_uid("movies");
// 1 - Create a new index // 1 - Create a new index
// Index with only a name "movies" // Index with only a name "movies"
// POST: /indexes // POST: /indexes
let body = json!({ let body = json!({
"name": "movies", "name": "movies",
}) "uid": "movies",
.to_string() });
.into_bytes();
let req = http::Request::post("/indexes") let (res1_value, status_code) = server.create_index(body);
.body(Body::from(body)) assert_eq!(status_code, 201);
.unwrap();
let res = server.simulate(req).unwrap();
assert_eq!(res.status(), 201);
let mut buf = Vec::new();
block_on(res.into_body().read_to_end(&mut buf)).unwrap();
let res1_value: Value = serde_json::from_slice(&buf).unwrap();
assert_eq!(res1_value.as_object().unwrap().len(), 5); assert_eq!(res1_value.as_object().unwrap().len(), 5);
let r1_name = res1_value["name"].as_str().unwrap(); let r1_name = res1_value["name"].as_str().unwrap();
@ -217,7 +158,7 @@ fn rename_index() {
let r1_updated_at = res1_value["updatedAt"].as_str().unwrap(); let r1_updated_at = res1_value["updatedAt"].as_str().unwrap();
assert_eq!(r1_name, "movies"); assert_eq!(r1_name, "movies");
assert_eq!(r1_uid.len(), 8); assert_eq!(r1_uid.len(), 6);
assert!(r1_created_at.len() > 1); assert!(r1_created_at.len() > 1);
assert!(r1_updated_at.len() > 1); assert!(r1_updated_at.len() > 1);
@ -227,19 +168,10 @@ fn rename_index() {
let body = json!({ let body = json!({
"name": "TV Shows", "name": "TV Shows",
}) });
.to_string()
.into_bytes();
let req = http::Request::put(format!("/indexes/{}", r1_uid)) let (res2_value, status_code) = server.update_index(body);
.body(Body::from(body)) assert_eq!(status_code, 200);
.unwrap();
let res = server.simulate(req).unwrap();
assert_eq!(res.status(), 200);
let mut buf = Vec::new();
block_on(res.into_body().read_to_end(&mut buf)).unwrap();
let res2_value: Value = serde_json::from_slice(&buf).unwrap();
assert_eq!(res2_value.as_object().unwrap().len(), 5); assert_eq!(res2_value.as_object().unwrap().len(), 5);
let r2_name = res2_value["name"].as_str().unwrap(); let r2_name = res2_value["name"].as_str().unwrap();
@ -256,13 +188,8 @@ fn rename_index() {
// Must have 1 index with the exact same content that the request 2 // Must have 1 index with the exact same content that the request 2
// GET: /indexes // GET: /indexes
let req = http::Request::get("/indexes").body(Body::empty()).unwrap(); let (res3_value, status_code) = server.list_indexes();
let res = server.simulate(req).unwrap(); assert_eq!(status_code, 200);
assert_eq!(res.status(), 200);
let mut buf = Vec::new();
block_on(res.into_body().read_to_end(&mut buf)).unwrap();
let res3_value: Value = serde_json::from_slice(&buf).unwrap();
assert_eq!(res3_value.as_array().unwrap().len(), 1); assert_eq!(res3_value.as_array().unwrap().len(), 1);
assert_eq!(res3_value[0].as_object().unwrap().len(), 5); assert_eq!(res3_value[0].as_object().unwrap().len(), 5);
@ -279,7 +206,7 @@ fn rename_index() {
#[test] #[test]
fn delete_index_and_recreate_it() { fn delete_index_and_recreate_it() {
let mut server = common::setup_server().unwrap(); let mut server = common::Server::with_uid("movies");
// 1 - Create a new index // 1 - Create a new index
// Index with only a name "movies" // Index with only a name "movies"
@ -287,19 +214,11 @@ fn delete_index_and_recreate_it() {
let body = json!({ let body = json!({
"name": "movies", "name": "movies",
}) "uid": "movies",
.to_string() });
.into_bytes();
let req = http::Request::post("/indexes") let (res1_value, status_code) = server.create_index(body);
.body(Body::from(body)) assert_eq!(status_code, 201);
.unwrap();
let res = server.simulate(req).unwrap();
assert_eq!(res.status(), 201);
let mut buf = Vec::new();
block_on(res.into_body().read_to_end(&mut buf)).unwrap();
let res1_value: Value = serde_json::from_slice(&buf).unwrap();
assert_eq!(res1_value.as_object().unwrap().len(), 5); assert_eq!(res1_value.as_object().unwrap().len(), 5);
let r1_name = res1_value["name"].as_str().unwrap(); let r1_name = res1_value["name"].as_str().unwrap();
@ -308,7 +227,7 @@ fn delete_index_and_recreate_it() {
let r1_updated_at = res1_value["updatedAt"].as_str().unwrap(); let r1_updated_at = res1_value["updatedAt"].as_str().unwrap();
assert_eq!(r1_name, "movies"); assert_eq!(r1_name, "movies");
assert_eq!(r1_uid.len(), 8); assert_eq!(r1_uid.len(), 6);
assert!(r1_created_at.len() > 1); assert!(r1_created_at.len() > 1);
assert!(r1_updated_at.len() > 1); assert!(r1_updated_at.len() > 1);
@ -316,13 +235,8 @@ fn delete_index_and_recreate_it() {
// Must have 1 index with the exact same content that the request 1 // Must have 1 index with the exact same content that the request 1
// GET: /indexes // GET: /indexes
let req = http::Request::get("/indexes").body(Body::empty()).unwrap(); let (res2_value, status_code) = server.list_indexes();
let res = server.simulate(req).unwrap(); assert_eq!(status_code, 200);
assert_eq!(res.status(), 200);
let mut buf = Vec::new();
block_on(res.into_body().read_to_end(&mut buf)).unwrap();
let res2_value: Value = serde_json::from_slice(&buf).unwrap();
assert_eq!(res2_value.as_array().unwrap().len(), 1); assert_eq!(res2_value.as_array().unwrap().len(), 1);
assert_eq!(res2_value[0].as_object().unwrap().len(), 5); assert_eq!(res2_value[0].as_object().unwrap().len(), 5);
@ -340,27 +254,15 @@ fn delete_index_and_recreate_it() {
// Update "movies" to "TV Shows" // Update "movies" to "TV Shows"
// DELETE: /indexes/:uid // DELETE: /indexes/:uid
let req = http::Request::delete(format!("/indexes/{}", r1_uid)) let (_res2_value, status_code) = server.delete_index();
.body(Body::empty()) assert_eq!(status_code, 204);
.unwrap();
let res = server.simulate(req).unwrap();
assert_eq!(res.status(), 204);
let mut buf = Vec::new();
block_on(res.into_body().read_to_end(&mut buf)).unwrap();
assert_eq!(buf.len(), 0);
// 4 - Check the list of indexes // 4 - Check the list of indexes
// Must have 0 index // Must have 0 index
// GET: /indexes // GET: /indexes
let req = http::Request::get("/indexes").body(Body::empty()).unwrap(); let (res2_value, status_code) = server.list_indexes();
let res = server.simulate(req).unwrap(); assert_eq!(status_code, 200);
assert_eq!(res.status(), 200);
let mut buf = Vec::new();
block_on(res.into_body().read_to_end(&mut buf)).unwrap();
let res2_value: Value = serde_json::from_slice(&buf).unwrap();
assert_eq!(res2_value.as_array().unwrap().len(), 0); assert_eq!(res2_value.as_array().unwrap().len(), 0);
@ -370,19 +272,10 @@ fn delete_index_and_recreate_it() {
let body = json!({ let body = json!({
"name": "movies", "name": "movies",
}) });
.to_string()
.into_bytes();
let req = http::Request::post("/indexes") let (res1_value, status_code) = server.create_index(body);
.body(Body::from(body)) assert_eq!(status_code, 201);
.unwrap();
let res = server.simulate(req).unwrap();
assert_eq!(res.status(), 201);
let mut buf = Vec::new();
block_on(res.into_body().read_to_end(&mut buf)).unwrap();
let res1_value: Value = serde_json::from_slice(&buf).unwrap();
assert_eq!(res1_value.as_object().unwrap().len(), 5); assert_eq!(res1_value.as_object().unwrap().len(), 5);
let r1_name = res1_value["name"].as_str().unwrap(); let r1_name = res1_value["name"].as_str().unwrap();
@ -399,13 +292,8 @@ fn delete_index_and_recreate_it() {
// Must have 1 index with the exact same content that the request 1 // Must have 1 index with the exact same content that the request 1
// GET: /indexes // GET: /indexes
let req = http::Request::get("/indexes").body(Body::empty()).unwrap(); let (res2_value, status_code) = server.list_indexes();
let res = server.simulate(req).unwrap(); assert_eq!(status_code, 200);
assert_eq!(res.status(), 200);
let mut buf = Vec::new();
block_on(res.into_body().read_to_end(&mut buf)).unwrap();
let res2_value: Value = serde_json::from_slice(&buf).unwrap();
assert_eq!(res2_value.as_array().unwrap().len(), 1); assert_eq!(res2_value.as_array().unwrap().len(), 1);
assert_eq!(res2_value[0].as_object().unwrap().len(), 5); assert_eq!(res2_value[0].as_object().unwrap().len(), 5);
@ -422,7 +310,7 @@ fn delete_index_and_recreate_it() {
#[test] #[test]
fn check_multiples_indexes() { fn check_multiples_indexes() {
let mut server = common::setup_server().unwrap(); let mut server = common::Server::with_uid("movies");
// 1 - Create a new index // 1 - Create a new index
// Index with only a name "movies" // Index with only a name "movies"
@ -430,19 +318,10 @@ fn check_multiples_indexes() {
let body = json!({ let body = json!({
"name": "movies", "name": "movies",
}) });
.to_string()
.into_bytes();
let req = http::Request::post("/indexes") let (res1_value, status_code) = server.create_index(body);
.body(Body::from(body)) assert_eq!(status_code, 201);
.unwrap();
let res = server.simulate(req).unwrap();
assert_eq!(res.status(), 201);
let mut buf = Vec::new();
block_on(res.into_body().read_to_end(&mut buf)).unwrap();
let res1_value: Value = serde_json::from_slice(&buf).unwrap();
assert_eq!(res1_value.as_object().unwrap().len(), 5); assert_eq!(res1_value.as_object().unwrap().len(), 5);
let r1_name = res1_value["name"].as_str().unwrap(); let r1_name = res1_value["name"].as_str().unwrap();
@ -459,13 +338,8 @@ fn check_multiples_indexes() {
// Must have 1 index with the exact same content that the request 1 // Must have 1 index with the exact same content that the request 1
// GET: /indexes // GET: /indexes
let req = http::Request::get("/indexes").body(Body::empty()).unwrap(); let (res2_value, status_code) = server.list_indexes();
let res = server.simulate(req).unwrap(); assert_eq!(status_code, 200);
assert_eq!(res.status(), 200);
let mut buf = Vec::new();
block_on(res.into_body().read_to_end(&mut buf)).unwrap();
let res2_value: Value = serde_json::from_slice(&buf).unwrap();
assert_eq!(res2_value.as_array().unwrap().len(), 1); assert_eq!(res2_value.as_array().unwrap().len(), 1);
assert_eq!(res2_value[0].as_object().unwrap().len(), 5); assert_eq!(res2_value[0].as_object().unwrap().len(), 5);
@ -485,19 +359,10 @@ fn check_multiples_indexes() {
let body = json!({ let body = json!({
"name": "films", "name": "films",
}) });
.to_string()
.into_bytes();
let req = http::Request::post("/indexes") let (res3_value, status_code) = server.create_index(body);
.body(Body::from(body)) assert_eq!(status_code, 201);
.unwrap();
let res = server.simulate(req).unwrap();
assert_eq!(res.status(), 201);
let mut buf = Vec::new();
block_on(res.into_body().read_to_end(&mut buf)).unwrap();
let res3_value: Value = serde_json::from_slice(&buf).unwrap();
assert_eq!(res3_value.as_object().unwrap().len(), 5); assert_eq!(res3_value.as_object().unwrap().len(), 5);
let r3_name = res3_value["name"].as_str().unwrap(); let r3_name = res3_value["name"].as_str().unwrap();
@ -514,13 +379,8 @@ fn check_multiples_indexes() {
// Must have 2 index with the exact same content that the request 1 and 3 // Must have 2 index with the exact same content that the request 1 and 3
// GET: /indexes // GET: /indexes
let req = http::Request::get("/indexes").body(Body::empty()).unwrap(); let (res4_value, status_code) = server.list_indexes();
let res = server.simulate(req).unwrap(); assert_eq!(status_code, 200);
assert_eq!(res.status(), 200);
let mut buf = Vec::new();
block_on(res.into_body().read_to_end(&mut buf)).unwrap();
let res4_value: Value = serde_json::from_slice(&buf).unwrap();
assert_eq!(res4_value.as_array().unwrap().len(), 2); assert_eq!(res4_value.as_array().unwrap().len(), 2);
@ -563,37 +423,15 @@ fn check_multiples_indexes() {
#[test] #[test]
fn create_index_failed() { fn create_index_failed() {
let mut server = common::setup_server().unwrap(); let mut server = common::Server::with_uid("movies");
// 1 - Push index creation with empty body
// POST: /indexes
let req = http::Request::post("/indexes").body(Body::empty()).unwrap();
let res = server.simulate(req).unwrap();
assert_eq!(res.status(), 400);
let mut buf = Vec::new();
block_on(res.into_body().read_to_end(&mut buf)).unwrap();
let res_value: Value = serde_json::from_slice(&buf).unwrap();
let message = res_value["message"].as_str().unwrap();
assert_eq!(res_value.as_object().unwrap().len(), 1);
assert_eq!(message, "invalid data");
// 2 - Push index creation with empty json body // 2 - Push index creation with empty json body
// POST: /indexes // POST: /indexes
let body = json!({}).to_string().into_bytes(); let body = json!({});
let req = http::Request::post("/indexes") let (res_value, status_code) = server.create_index(body);
.body(Body::from(body)) assert_eq!(status_code, 400);
.unwrap();
let res = server.simulate(req).unwrap();
assert_eq!(res.status(), 400);
let mut buf = Vec::new();
block_on(res.into_body().read_to_end(&mut buf)).unwrap();
let res_value: Value = serde_json::from_slice(&buf).unwrap();
let message = res_value["message"].as_str().unwrap(); let message = res_value["message"].as_str().unwrap();
assert_eq!(res_value.as_object().unwrap().len(), 1); assert_eq!(res_value.as_object().unwrap().len(), 1);
@ -605,19 +443,10 @@ fn create_index_failed() {
let body = json!({ let body = json!({
"name": "movies", "name": "movies",
"active": true "active": true
}) });
.to_string()
.into_bytes();
let req = http::Request::post("/indexes") let (res_value, status_code) = server.create_index(body);
.body(Body::from(body)) assert_eq!(status_code, 400);
.unwrap();
let res = server.simulate(req).unwrap();
assert_eq!(res.status(), 400);
let mut buf = Vec::new();
block_on(res.into_body().read_to_end(&mut buf)).unwrap();
let res_value: Value = serde_json::from_slice(&buf).unwrap();
let message = res_value["message"].as_str().unwrap(); let message = res_value["message"].as_str().unwrap();
assert_eq!(res_value.as_object().unwrap().len(), 1); assert_eq!(res_value.as_object().unwrap().len(), 1);
@ -629,19 +458,10 @@ fn create_index_failed() {
let body = json!({ let body = json!({
"name": "movies", "name": "movies",
"uid": 0 "uid": 0
}) });
.to_string()
.into_bytes();
let req = http::Request::post("/indexes") let (res_value, status_code) = server.create_index(body);
.body(Body::from(body)) assert_eq!(status_code, 400);
.unwrap();
let res = server.simulate(req).unwrap();
assert_eq!(res.status(), 400);
let mut buf = Vec::new();
block_on(res.into_body().read_to_end(&mut buf)).unwrap();
let res_value: Value = serde_json::from_slice(&buf).unwrap();
let message = res_value["message"].as_str().unwrap(); let message = res_value["message"].as_str().unwrap();
assert_eq!(res_value.as_object().unwrap().len(), 1); assert_eq!(res_value.as_object().unwrap().len(), 1);

View File

@ -1,16 +1,13 @@
use std::convert::Into; use std::convert::Into;
use serde_json::json; use serde_json::json;
use assert_json_diff::assert_json_eq;
mod common; mod common;
#[test] #[test]
fn basic_search() { fn basic_search() {
let mut server = common::setup_server().unwrap(); let mut server = common::Server::with_uid("movies");
server.populate_movies();
common::enrich_server_with_movies_index(&mut server).unwrap();
common::enrich_server_with_movies_settings(&mut server).unwrap();
common::enrich_server_with_movies_documents(&mut server).unwrap();
// 1 - Simple search // 1 - Simple search
// q: Captain // q: Captain
@ -18,7 +15,7 @@ fn basic_search() {
let query = "q=captain&limit=3"; let query = "q=captain&limit=3";
let json = json!([ let expect = json!([
{ {
"id": 299537, "id": 299537,
"popularity": 44.726, "popularity": 44.726,
@ -72,7 +69,8 @@ fn basic_search() {
} }
]); ]);
common::search(&mut server, query, json); let (response, _status_code) = server.search(query);
assert_json_eq!(expect, response["hits"].clone(), ordered: false);
// 2 - Simple search with offset // 2 - Simple search with offset
// q: Captain // q: Captain
@ -81,7 +79,7 @@ fn basic_search() {
let query = "q=captain&limit=3&offset=1"; let query = "q=captain&limit=3&offset=1";
let json = json!([ let expect = json!([
{ {
"id": 271110, "id": 271110,
"popularity": 37.431, "popularity": 37.431,
@ -136,7 +134,8 @@ fn basic_search() {
} }
]); ]);
common::search(&mut server, query, json); let (response, _status_code) = server.search(query);
assert_json_eq!(expect, response["hits"].clone(), ordered: false);
// 3 - Simple search with attribute to highlight all // 3 - Simple search with attribute to highlight all
// q: Captain // q: Captain
@ -145,7 +144,7 @@ fn basic_search() {
let query = "q=captain&limit=1&attributesToHighlight=*"; let query = "q=captain&limit=1&attributesToHighlight=*";
let json = json!([ let expect = json!([
{ {
"id": 299537, "id": 299537,
"popularity": 44.726, "popularity": 44.726,
@ -182,7 +181,8 @@ fn basic_search() {
} }
]); ]);
common::search(&mut server, query, json); let (response, _status_code) = server.search(query);
assert_json_eq!(expect, response["hits"].clone(), ordered: false);
// 4 - Simple search with attribute to highlight title // 4 - Simple search with attribute to highlight title
// q: Captain // q: Captain
@ -191,7 +191,7 @@ fn basic_search() {
let query = "q=captain&limit=1&attributesToHighlight=title"; let query = "q=captain&limit=1&attributesToHighlight=title";
let json = json!([ let expect = json!([
{ {
"id": 299537, "id": 299537,
"popularity": 44.726, "popularity": 44.726,
@ -228,7 +228,8 @@ fn basic_search() {
} }
]); ]);
common::search(&mut server, query, json); let (response, _status_code) = server.search(query);
assert_json_eq!(expect, response["hits"].clone(), ordered: false);
// 1 - Simple search with attribute to highlight title and tagline // 1 - Simple search with attribute to highlight title and tagline
// q: Captain // q: Captain
@ -237,7 +238,7 @@ fn basic_search() {
let query = "q=captain&limit=1&attributesToHighlight=title,tagline"; let query = "q=captain&limit=1&attributesToHighlight=title,tagline";
let json = json!([ let expect = json!([
{ {
"id": 299537, "id": 299537,
"popularity": 44.726, "popularity": 44.726,
@ -274,7 +275,8 @@ fn basic_search() {
} }
]); ]);
common::search(&mut server, query, json); let (response, _status_code) = server.search(query);
assert_json_eq!(expect, response["hits"].clone(), ordered: false);
// 1 - Simple search with attribute to highlight title and overview // 1 - Simple search with attribute to highlight title and overview
// q: Captain // q: Captain
@ -283,7 +285,7 @@ fn basic_search() {
let query = "q=captain&limit=1&attributesToHighlight=title,overview"; let query = "q=captain&limit=1&attributesToHighlight=title,overview";
let json = json!([ let expect = json!([
{ {
"id": 299537, "id": 299537,
"popularity": 44.726, "popularity": 44.726,
@ -320,7 +322,8 @@ fn basic_search() {
} }
]); ]);
common::search(&mut server, query, json); let (response, _status_code) = server.search(query);
assert_json_eq!(expect, response["hits"].clone(), ordered: false);
// 1 - Simple search with matches // 1 - Simple search with matches
// q: Captain // q: Captain
@ -329,7 +332,7 @@ fn basic_search() {
let query = "q=captain&limit=1&matches=true"; let query = "q=captain&limit=1&matches=true";
let json = json!([ let expect = json!([
{ {
"id": 299537, "id": 299537,
"popularity": 44.726, "popularity": 44.726,
@ -363,7 +366,8 @@ fn basic_search() {
} }
]); ]);
common::search(&mut server, query, json); let (response, _status_code) = server.search(query);
assert_json_eq!(expect, response["hits"].clone(), ordered: false);
// 1 - Simple search with crop // 1 - Simple search with crop
// q: Captain // q: Captain
@ -373,7 +377,7 @@ fn basic_search() {
let query = "q=captain&limit=1&attributesToCrop=overview&cropLength=20"; let query = "q=captain&limit=1&attributesToCrop=overview&cropLength=20";
let json = json!([ let expect = json!([
{ {
"id": 299537, "id": 299537,
"popularity": 44.726, "popularity": 44.726,
@ -410,7 +414,8 @@ fn basic_search() {
} }
]); ]);
common::search(&mut server, query, json); let (response, _status_code) = server.search(query);
assert_json_eq!(expect, response["hits"].clone(), ordered: false);
// 1 - Simple search with attributes to retrieve // 1 - Simple search with attributes to retrieve
// q: Captain // q: Captain
@ -419,7 +424,7 @@ fn basic_search() {
let query = "q=captain&limit=1&attributesToRetrieve=title,tagline,overview,poster_path"; let query = "q=captain&limit=1&attributesToRetrieve=title,tagline,overview,poster_path";
let json = json!([ let expect = json!([
{ {
"title": "Captain Marvel", "title": "Captain Marvel",
"tagline": "Higher. Further. Faster.", "tagline": "Higher. Further. Faster.",
@ -428,7 +433,8 @@ fn basic_search() {
} }
]); ]);
common::search(&mut server, query, json); let (response, _status_code) = server.search(query);
assert_json_eq!(expect, response["hits"].clone(), ordered: false);
// 1 - Simple search with filter // 1 - Simple search with filter
// q: Captain // q: Captain
@ -437,7 +443,7 @@ fn basic_search() {
let query = "q=captain&limit=3&filters=director:Anthony%20Russo"; let query = "q=captain&limit=3&filters=director:Anthony%20Russo";
let json = json!([ let expect = json!([
{ {
"id": 271110, "id": 271110,
"popularity": 37.431, "popularity": 37.431,
@ -491,7 +497,8 @@ fn basic_search() {
} }
]); ]);
common::search(&mut server, query, json); let (response, _status_code) = server.search(query);
assert_json_eq!(expect, response["hits"].clone(), ordered: false);
// 1 - Simple search with attributes to highlight and matches // 1 - Simple search with attributes to highlight and matches
// q: Captain // q: Captain
@ -501,7 +508,7 @@ fn basic_search() {
let query = "q=captain&limit=1&attributesToHighlight=title,overview&matches=true"; let query = "q=captain&limit=1&attributesToHighlight=title,overview&matches=true";
let json = json!( [ let expect = json!( [
{ {
"id": 299537, "id": 299537,
"popularity": 44.726, "popularity": 44.726,
@ -552,7 +559,8 @@ fn basic_search() {
} }
]); ]);
common::search(&mut server, query, json); let (response, _status_code) = server.search(query);
assert_json_eq!(expect, response["hits"].clone(), ordered: false);
// 1 - Simple search with attributes to highlight and matches and crop // 1 - Simple search with attributes to highlight and matches and crop
// q: Captain // q: Captain
@ -564,7 +572,7 @@ fn basic_search() {
let query = "q=captain&limit=1&attributesToCrop=overview&cropLength=20&attributesToHighlight=title,overview&matches=true"; let query = "q=captain&limit=1&attributesToCrop=overview&cropLength=20&attributesToHighlight=title,overview&matches=true";
let json = json!([ let expect = json!([
{ {
"id": 299537, "id": 299537,
"popularity": 44.726, "popularity": 44.726,
@ -615,14 +623,14 @@ fn basic_search() {
} }
]); ]);
common::search(&mut server, query, json); let (response, _status_code) = server.search(query);
assert_json_eq!(expect, response["hits"].clone(), ordered: false);
} }
#[test] #[test]
fn search_with_settings_basic() { fn search_with_settings_basic() {
let mut server = common::setup_server().unwrap(); let mut server = common::Server::with_uid("movies");
common::enrich_server_with_movies_index(&mut server).unwrap(); server.populate_movies();
common::enrich_server_with_movies_documents(&mut server).unwrap();
let config = json!({ let config = json!({
"rankingRules": [ "rankingRules": [
@ -665,10 +673,10 @@ fn search_with_settings_basic() {
"acceptNewFields": false, "acceptNewFields": false,
}); });
common::update_config(&mut server, config); server.update_all_settings(config);
let query = "q=the%20avangers&limit=3"; let query = "q=the%20avangers&limit=3";
let response = json!([ let expect = json!([
{ {
"id": 24428, "id": 24428,
"popularity": 44.506, "popularity": 44.506,
@ -722,13 +730,14 @@ fn search_with_settings_basic() {
} }
]); ]);
common::search(&mut server, query, response); let (response, _status_code) = server.search(query);
assert_json_eq!(expect, response["hits"].clone(), ordered: false);
} }
#[test] #[test]
fn search_with_settings_stop_words() { fn search_with_settings_stop_words() {
let mut server = common::setup_server().unwrap(); let mut server = common::Server::with_uid("movies");
common::enrich_server_with_movies_index(&mut server).unwrap(); server.populate_movies();
let config = json!({ let config = json!({
"rankingRules": [ "rankingRules": [
@ -771,11 +780,10 @@ fn search_with_settings_stop_words() {
"acceptNewFields": false, "acceptNewFields": false,
}); });
common::update_config(&mut server, config); server.update_all_settings(config);
common::enrich_server_with_movies_documents(&mut server).unwrap();
let query = "q=the%20avangers&limit=3"; let query = "q=the%20avangers&limit=3";
let response = json!([ let expect = json!([
{ {
"id": 299536, "id": 299536,
"popularity": 65.013, "popularity": 65.013,
@ -829,13 +837,14 @@ fn search_with_settings_stop_words() {
} }
]); ]);
common::search(&mut server, query, response); let (response, _status_code) = server.search(query);
assert_json_eq!(expect, response["hits"].clone(), ordered: false);
} }
#[test] #[test]
fn search_with_settings_synonyms() { fn search_with_settings_synonyms() {
let mut server = common::setup_server().unwrap(); let mut server = common::Server::with_uid("movies");
common::enrich_server_with_movies_index(&mut server).unwrap(); server.populate_movies();
let config = json!({ let config = json!({
"rankingRules": [ "rankingRules": [
@ -883,11 +892,10 @@ fn search_with_settings_synonyms() {
"acceptNewFields": false, "acceptNewFields": false,
}); });
common::update_config(&mut server, config); server.update_all_settings(config);
common::enrich_server_with_movies_documents(&mut server).unwrap();
let query = "q=avangers&limit=3"; let query = "q=avangers&limit=3";
let response = json!([ let expect = json!([
{ {
"id": 299536, "id": 299536,
"popularity": 65.013, "popularity": 65.013,
@ -941,13 +949,14 @@ fn search_with_settings_synonyms() {
} }
]); ]);
common::search(&mut server, query, response); let (response, _status_code) = server.search(query);
assert_json_eq!(expect, response["hits"].clone(), ordered: false);
} }
#[test] #[test]
fn search_with_settings_ranking_rules() { fn search_with_settings_ranking_rules() {
let mut server = common::setup_server().unwrap(); let mut server = common::Server::with_uid("movies");
common::enrich_server_with_movies_index(&mut server).unwrap(); server.populate_movies();
let config = json!({ let config = json!({
"rankingRules": [ "rankingRules": [
@ -990,11 +999,10 @@ fn search_with_settings_ranking_rules() {
"acceptNewFields": false, "acceptNewFields": false,
}); });
common::update_config(&mut server, config); server.update_all_settings(config);
common::enrich_server_with_movies_documents(&mut server).unwrap();
let query = "q=avangers&limit=3"; let query = "q=avangers&limit=3";
let response = json!([ let expect = json!([
{ {
"id": 99861, "id": 99861,
"popularity": 33.938, "popularity": 33.938,
@ -1048,13 +1056,14 @@ fn search_with_settings_ranking_rules() {
} }
]); ]);
common::search(&mut server, query, response); let (response, _status_code) = server.search(query);
assert_json_eq!(expect, response["hits"].clone(), ordered: false);
} }
#[test] #[test]
fn search_with_settings_searchable_attributes() { fn search_with_settings_searchable_attributes() {
let mut server = common::setup_server().unwrap(); let mut server = common::Server::with_uid("movies");
common::enrich_server_with_movies_index(&mut server).unwrap(); server.populate_movies();
let config = json!({ let config = json!({
"rankingRules": [ "rankingRules": [
@ -1096,11 +1105,10 @@ fn search_with_settings_searchable_attributes() {
"acceptNewFields": false, "acceptNewFields": false,
}); });
common::update_config(&mut server, config); server.update_all_settings(config);
common::enrich_server_with_movies_documents(&mut server).unwrap();
let query = "q=avangers&limit=3"; let query = "q=avangers&limit=3";
let response = json!([ let expect = json!([
{ {
"id": 299536, "id": 299536,
"popularity": 65.013, "popularity": 65.013,
@ -1154,13 +1162,14 @@ fn search_with_settings_searchable_attributes() {
} }
]); ]);
common::search(&mut server, query, response); let (response, _status_code) = server.search(query);
assert_json_eq!(expect, response["hits"].clone(), ordered: false);
} }
#[test] #[test]
fn search_with_settings_displayed_attributes() { fn search_with_settings_displayed_attributes() {
let mut server = common::setup_server().unwrap(); let mut server = common::Server::with_uid("movies");
common::enrich_server_with_movies_index(&mut server).unwrap(); server.populate_movies();
let config = json!({ let config = json!({
"rankingRules": [ "rankingRules": [
@ -1197,11 +1206,10 @@ fn search_with_settings_displayed_attributes() {
"acceptNewFields": false, "acceptNewFields": false,
}); });
common::update_config(&mut server, config); server.update_all_settings(config);
common::enrich_server_with_movies_documents(&mut server).unwrap();
let query = "q=avangers&limit=3"; let query = "q=avangers&limit=3";
let response = json!([ let expect = json!([
{ {
"id": 299536, "id": 299536,
"title": "Avengers: Infinity War", "title": "Avengers: Infinity War",
@ -1225,13 +1233,14 @@ fn search_with_settings_displayed_attributes() {
} }
]); ]);
common::search(&mut server, query, response); let (response, _status_code) = server.search(query);
assert_json_eq!(expect, response["hits"].clone(), ordered: false);
} }
#[test] #[test]
fn search_with_settings_searchable_attributes_2() { fn search_with_settings_searchable_attributes_2() {
let mut server = common::setup_server().unwrap(); let mut server = common::Server::with_uid("movies");
common::enrich_server_with_movies_index(&mut server).unwrap(); server.populate_movies();
let config = json!({ let config = json!({
"rankingRules": [ "rankingRules": [
@ -1268,11 +1277,10 @@ fn search_with_settings_searchable_attributes_2() {
"acceptNewFields": false, "acceptNewFields": false,
}); });
common::update_config(&mut server, config); server.update_all_settings(config);
common::enrich_server_with_movies_documents(&mut server).unwrap();
let query = "q=avangers&limit=3"; let query = "q=avangers&limit=3";
let response = json!([ let expect = json!([
{ {
"id": 299536, "id": 299536,
"title": "Avengers: Infinity War", "title": "Avengers: Infinity War",
@ -1296,5 +1304,6 @@ fn search_with_settings_searchable_attributes_2() {
} }
]); ]);
common::search(&mut server, query, response); let (response, _status_code) = server.search(query);
assert_json_eq!(expect, response["hits"].clone(), ordered: false);
} }

View File

@ -1,45 +1,17 @@
use std::convert::Into; use std::convert::Into;
use std::time::Duration;
use assert_json_diff::assert_json_eq; use assert_json_diff::assert_json_eq;
use async_std::io::prelude::*;
use async_std::task::{block_on, sleep};
use http_service::Body;
use serde_json::json; use serde_json::json;
use serde_json::Value;
mod common; mod common;
// Process:
// - Write a full settings update
// - Delete all settings
// Check:
// - Settings are deleted, all fields are null
// - POST success repond Status Code 202
// - Get success repond Status Code 200
// - Delete success repond Status Code 202
#[test] #[test]
fn write_all_and_delete() { fn write_all_and_delete() {
let mut server = common::setup_server().unwrap(); let mut server = common::Server::with_uid("movies");
server.populate_movies();
// 1 - Create the index
let body = json!({
"uid": "movies",
"identifier": "id",
})
.to_string()
.into_bytes();
let req = http::Request::post("/indexes")
.body(Body::from(body))
.unwrap();
let res = server.simulate(req).unwrap();
assert_eq!(res.status(), 201);
// 2 - Send the settings // 2 - Send the settings
let json = json!({ let body = json!({
"rankingRules": [ "rankingRules": [
"typo", "typo",
"words", "words",
@ -79,53 +51,23 @@ fn write_all_and_delete() {
"acceptNewFields": false, "acceptNewFields": false,
}); });
let body = json.to_string().into_bytes(); server.update_all_settings(body.clone());
let req = http::Request::post("/indexes/movies/settings")
.body(Body::from(body))
.unwrap();
let res = server.simulate(req).unwrap();
assert_eq!(res.status(), 202);
block_on(sleep(Duration::from_secs(1)));
// 3 - Get all settings and compare to the previous one // 3 - Get all settings and compare to the previous one
let req = http::Request::get("/indexes/movies/settings") let (response, _status_code) = server.get_all_settings();
.body(Body::empty())
.unwrap();
let res = server.simulate(req).unwrap();
assert_eq!(res.status(), 200);
let mut buf = Vec::new(); assert_json_eq!(body, response, ordered: false);
block_on(res.into_body().read_to_end(&mut buf)).unwrap();
let res_value: Value = serde_json::from_slice(&buf).unwrap();
assert_json_eq!(json, res_value, ordered: false);
// 4 - Delete all settings // 4 - Delete all settings
let req = http::Request::delete("/indexes/movies/settings") server.delete_all_settings();
.body(Body::empty())
.unwrap();
let res = server.simulate(req).unwrap();
assert_eq!(res.status(), 202);
block_on(sleep(Duration::from_secs(2))); // 5 - Get all settings and check if they are set to default values
// 5 - Get all settings and check if they are empty let (response, _status_code) = server.get_all_settings();
let req = http::Request::get("/indexes/movies/settings") let expect = json!({
.body(Body::empty())
.unwrap();
let res = server.simulate(req).unwrap();
assert_eq!(res.status(), 200);
let mut buf = Vec::new();
block_on(res.into_body().read_to_end(&mut buf)).unwrap();
let res_value: Value = serde_json::from_slice(&buf).unwrap();
let json = json!({
"rankingRules": [ "rankingRules": [
"typo", "typo",
"words", "words",
@ -136,61 +78,61 @@ fn write_all_and_delete() {
], ],
"distinctAttribute": null, "distinctAttribute": null,
"searchableAttributes": [ "searchableAttributes": [
"poster_path",
"director",
"id", "id",
"release_date", "production_companies",
"producer",
"poster", "poster",
"description",
"title",
"movie_id", "movie_id",
"rank" "vote_count",
"cast",
"release_date",
"vote_average",
"rank",
"genres",
"overview",
"description",
"tagline",
"popularity",
"title"
], ],
"displayedAttributes": [ "displayedAttributes": [
"movie_id", "poster_path",
"description",
"poster", "poster",
"vote_count",
"id", "id",
"release_date", "movie_id",
"title",
"rank", "rank",
"title" "tagline",
"cast",
"producer",
"production_companies",
"description",
"director",
"genres",
"release_date",
"overview",
"vote_average",
"popularity"
], ],
"stopWords": null, "stopWords": null,
"synonyms": null, "synonyms": null,
"acceptNewFields": true, "acceptNewFields": true,
}); });
assert_json_eq!(json, res_value, ordered: false); assert_json_eq!(expect, response, ordered: false);
} }
// Process:
// - Write a full setting update
// - Rewrite an other settings confirmation
// Check:
// - Settings are overwrited
// - Forgotten attributes are deleted
// - Null attributes are deleted
// - Empty attribute are deleted
#[test] #[test]
fn write_all_and_update() { fn write_all_and_update() {
let mut server = common::setup_server().unwrap(); let mut server = common::Server::with_uid("movies");
server.populate_movies();
// 1 - Create the index
let body = json!({
"uid": "movies",
"identifier": "id",
})
.to_string()
.into_bytes();
let req = http::Request::post("/indexes")
.body(Body::from(body))
.unwrap();
let res = server.simulate(req).unwrap();
assert_eq!(res.status(), 201);
// 2 - Send the settings // 2 - Send the settings
let json = json!({ let body = json!({
"rankingRules": [ "rankingRules": [
"typo", "typo",
"words", "words",
@ -230,33 +172,17 @@ fn write_all_and_update() {
"acceptNewFields": false, "acceptNewFields": false,
}); });
let body = json.to_string().into_bytes(); server.update_all_settings(body.clone());
let req = http::Request::post("/indexes/movies/settings")
.body(Body::from(body))
.unwrap();
let res = server.simulate(req).unwrap();
assert_eq!(res.status(), 202);
block_on(sleep(Duration::from_secs(1)));
// 3 - Get all settings and compare to the previous one // 3 - Get all settings and compare to the previous one
let req = http::Request::get("/indexes/movies/settings") let (response, _status_code) = server.get_all_settings();
.body(Body::empty())
.unwrap();
let res = server.simulate(req).unwrap();
assert_eq!(res.status(), 200);
let mut buf = Vec::new(); assert_json_eq!(body, response, ordered: false);
block_on(res.into_body().read_to_end(&mut buf)).unwrap();
let res_value: Value = serde_json::from_slice(&buf).unwrap();
assert_json_eq!(json, res_value, ordered: false);
// 4 - Update all settings // 4 - Update all settings
let json_update = json!({ let body = json!({
"rankingRules": [ "rankingRules": [
"typo", "typo",
"words", "words",
@ -287,29 +213,13 @@ fn write_all_and_update() {
"acceptNewFields": false, "acceptNewFields": false,
}); });
let body_update = json_update.to_string().into_bytes(); server.update_all_settings(body);
let req = http::Request::post("/indexes/movies/settings")
.body(Body::from(body_update))
.unwrap();
let res = server.simulate(req).unwrap();
assert_eq!(res.status(), 202);
block_on(sleep(Duration::from_secs(1)));
// 5 - Get all settings and check if the content is the same of (4) // 5 - Get all settings and check if the content is the same of (4)
let req = http::Request::get("/indexes/movies/settings") let (response, _status_code) = server.get_all_settings();
.body(Body::empty())
.unwrap();
let res = server.simulate(req).unwrap();
assert_eq!(res.status(), 200);
let mut buf = Vec::new(); let expected = json!({
block_on(res.into_body().read_to_end(&mut buf)).unwrap();
let res_value: Value = serde_json::from_slice(&buf).unwrap();
let res_expected = json!({
"rankingRules": [ "rankingRules": [
"typo", "typo",
"words", "words",
@ -340,5 +250,5 @@ fn write_all_and_update() {
"acceptNewFields": false "acceptNewFields": false
}); });
assert_json_eq!(res_expected, res_value, ordered: false); assert_json_eq!(expected, response, ordered: false);
} }

View File

@ -1,44 +1,16 @@
use std::time::Duration;
use assert_json_diff::assert_json_eq; use assert_json_diff::assert_json_eq;
use async_std::io::prelude::*;
use async_std::task::{block_on, sleep};
use http_service::Body;
use serde_json::json; use serde_json::json;
use serde_json::Value;
mod common; mod common;
// Process:
// - Write a full settings update
// - Delete all settings
// Check:
// - Settings are deleted, all fields are null
// - POST success repond Status Code 202
// - Get success repond Status Code 200
// - Delete success repond Status Code 202
#[test] #[test]
fn write_all_and_delete() { fn write_all_and_delete() {
let mut server = common::setup_server().unwrap(); let mut server = common::Server::with_uid("movies");
server.populate_movies();
// 1 - Create the index
let body = json!({
"uid": "movies",
"identifier": "uid",
})
.to_string()
.into_bytes();
let req = http::Request::post("/indexes")
.body(Body::from(body))
.unwrap();
let res = server.simulate(req).unwrap();
assert_eq!(res.status(), 201);
// 2 - Send the settings // 2 - Send the settings
let json = json!([ let body = json!([
"typo", "typo",
"words", "words",
"proximity", "proximity",
@ -49,51 +21,21 @@ fn write_all_and_delete() {
"desc(rank)", "desc(rank)",
]); ]);
let body = json.to_string().into_bytes(); server.update_ranking_rules(body.clone());
let req = http::Request::post("/indexes/movies/settings/ranking-rules")
.body(Body::from(body))
.unwrap();
let res = server.simulate(req).unwrap();
assert_eq!(res.status(), 202);
block_on(sleep(Duration::from_secs(2)));
// 3 - Get all settings and compare to the previous one // 3 - Get all settings and compare to the previous one
let req = http::Request::get("/indexes/movies/settings/ranking-rules") let (response, _status_code) = server.get_ranking_rules();
.body(Body::empty())
.unwrap();
let res = server.simulate(req).unwrap();
assert_eq!(res.status(), 200);
let mut buf = Vec::new(); assert_json_eq!(body, response, ordered: false);
block_on(res.into_body().read_to_end(&mut buf)).unwrap();
let res_value: Value = serde_json::from_slice(&buf).unwrap();
assert_json_eq!(json, res_value, ordered: false);
// 4 - Delete all settings // 4 - Delete all settings
let req = http::Request::delete("/indexes/movies/settings/ranking-rules") server.delete_ranking_rules();
.body(Body::empty())
.unwrap();
let res = server.simulate(req).unwrap();
assert_eq!(res.status(), 202);
block_on(sleep(Duration::from_secs(2)));
// 5 - Get all settings and check if they are empty // 5 - Get all settings and check if they are empty
let req = http::Request::get("/indexes/movies/settings/ranking-rules") let (response, _status_code) = server.get_ranking_rules();
.body(Body::empty())
.unwrap();
let res = server.simulate(req).unwrap();
assert_eq!(res.status(), 200);
let mut buf = Vec::new();
block_on(res.into_body().read_to_end(&mut buf)).unwrap();
let res_value: Value = serde_json::from_slice(&buf).unwrap();
let json = json!([ let json = json!([
"typo", "typo",
@ -104,39 +46,17 @@ fn write_all_and_delete() {
"exactness" "exactness"
]); ]);
assert_json_eq!(json, res_value, ordered: false); assert_json_eq!(expect, response, ordered: false);
} }
// Process:
// - Write a full setting update
// - Rewrite an other settings confirmation
// Check:
// - Settings are overwrited
// - Forgotten attributes are deleted
// - Null attributes are deleted
// - Empty attribute are deleted
#[test] #[test]
fn write_all_and_update() { fn write_all_and_update() {
let mut server = common::setup_server().unwrap(); let mut server = common::Server::with_uid("movies");
server.populate_movies();
// 1 - Create the index
let body = json!({
"uid": "movies",
"identifier": "uid",
})
.to_string()
.into_bytes();
let req = http::Request::post("/indexes")
.body(Body::from(body))
.unwrap();
let res = server.simulate(req).unwrap();
assert_eq!(res.status(), 201);
// 2 - Send the settings // 2 - Send the settings
let json = json!([ let body = json!([
"typo", "typo",
"words", "words",
"proximity", "proximity",
@ -147,33 +67,17 @@ fn write_all_and_update() {
"desc(rank)", "desc(rank)",
]); ]);
let body = json.to_string().into_bytes(); server.update_ranking_rules(body.clone());
let req = http::Request::post("/indexes/movies/settings/ranking-rules")
.body(Body::from(body))
.unwrap();
let res = server.simulate(req).unwrap();
assert_eq!(res.status(), 202);
block_on(sleep(Duration::from_secs(1)));
// 3 - Get all settings and compare to the previous one // 3 - Get all settings and compare to the previous one
let req = http::Request::get("/indexes/movies/settings/ranking-rules") let (response, _status_code) = server.get_ranking_rules();
.body(Body::empty())
.unwrap();
let res = server.simulate(req).unwrap();
assert_eq!(res.status(), 200);
let mut buf = Vec::new(); assert_json_eq!(body, response, ordered: false);
block_on(res.into_body().read_to_end(&mut buf)).unwrap();
let res_value: Value = serde_json::from_slice(&buf).unwrap();
assert_json_eq!(json, res_value, ordered: false);
// 4 - Update all settings // 4 - Update all settings
let json_update = json!([ let body = json!([
"typo", "typo",
"words", "words",
"proximity", "proximity",
@ -183,29 +87,13 @@ fn write_all_and_update() {
"desc(release_date)", "desc(release_date)",
]); ]);
let body_update = json_update.to_string().into_bytes(); server.update_ranking_rules(body);
let req = http::Request::post("/indexes/movies/settings/ranking-rules")
.body(Body::from(body_update))
.unwrap();
let res = server.simulate(req).unwrap();
assert_eq!(res.status(), 202);
block_on(sleep(Duration::from_secs(1)));
// 5 - Get all settings and check if the content is the same of (4) // 5 - Get all settings and check if the content is the same of (4)
let req = http::Request::get("/indexes/movies/settings/ranking-rules") let (response, _status_code) = server.get_ranking_rules();
.body(Body::empty())
.unwrap();
let res = server.simulate(req).unwrap();
assert_eq!(res.status(), 200);
let mut buf = Vec::new(); let expected = json!([
block_on(res.into_body().read_to_end(&mut buf)).unwrap();
let res_value: Value = serde_json::from_slice(&buf).unwrap();
let res_expected = json!([
"typo", "typo",
"words", "words",
"proximity", "proximity",
@ -215,5 +103,5 @@ fn write_all_and_update() {
"desc(release_date)", "desc(release_date)",
]); ]);
assert_json_eq!(res_expected, res_value, ordered: false); assert_json_eq!(expected, response, ordered: false);
} }