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)]
use http::StatusCode;
use serde_json::Value;
use std::error::Error;
use std::time::Duration;
use assert_json_diff::assert_json_eq;
use async_std::io::prelude::*;
use async_std::task::{block_on, sleep};
use http_service::Body;
@ -16,171 +15,427 @@ use serde_json::json;
use tempdir::TempDir;
use tide::server::Service;
pub fn setup_server() -> Result<TestBackend<Service<Data>>, Box<dyn Error>> {
let tmp_dir = TempDir::new("meilisearch")?;
let opt = Opt {
db_path: tmp_dir.path().to_str().unwrap().to_string(),
http_addr: "127.0.0.1:7700".to_owned(),
master_key: None,
env: "development".to_owned(),
no_analytics: true,
};
let data = Data::new(opt.clone());
let mut app = tide::with_state(data);
routes::load_routes(&mut app);
let http_server = app.into_http_service();
Ok(make_server(http_server)?)
pub struct Server {
uid: String,
mock: TestBackend<Service<Data>>,
}
pub fn enrich_server_with_movies_index(
server: &mut TestBackend<Service<Data>>,
) -> Result<(), Box<dyn Error>> {
let body = json!({
"uid": "movies",
"identifier": "id",
})
.to_string()
.into_bytes();
impl Server {
pub fn with_uid(uid: &str) -> Server {
let tmp_dir = TempDir::new("meilisearch").unwrap();
let req = http::Request::post("/indexes")
.body(Body::from(body))
.unwrap();
let _res = server.simulate(req).unwrap();
let opt = Opt {
db_path: tmp_dir.path().to_str().unwrap().to_string(),
http_addr: "127.0.0.1:7700".to_owned(),
master_key: None,
env: "development".to_owned(),
no_analytics: true,
};
Ok(())
}
let data = Data::new(opt.clone());
let mut app = tide::with_state(data);
routes::load_routes(&mut app);
let http_server = app.into_http_service();
let mock = make_server(http_server).unwrap();
pub fn enrich_server_with_movies_settings(
server: &mut TestBackend<Service<Data>>,
) -> Result<(), Box<dyn Error>> {
let json = json!({
"rankingRules": [
"typo",
"words",
"proximity",
"attribute",
"wordsPosition",
"desc(popularity)",
"exactness",
"desc(vote_average)",
],
"distinctAttribute": null,
"searchableAttributes": [
"title",
"tagline",
"overview",
"cast",
"director",
"producer",
"production_companies",
"genres",
],
"displayedAttributes": [
"title",
"director",
"producer",
"tagline",
"genres",
"id",
"overview",
"vote_count",
"vote_average",
"poster_path",
"popularity",
],
"stopWords": null,
"synonyms": null,
"acceptNewFields": false,
});
Server {
uid: uid.to_string(),
mock,
}
}
let body = json.to_string().into_bytes();
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 req = http::Request::post("/indexes/movies/settings")
.body(Body::from(body))
.unwrap();
let res = server.simulate(req).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();
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());
if response["status"] == "processed" {
return;
}
block_on(sleep(Duration::from_secs(1)));
}
}
wait_update_id(server, response["updateId"].as_u64().unwrap());
// // Global Http request GET/POST/DELETE async or sync
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))
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 = server.simulate(req).unwrap();
assert_eq!(res.status(), 200);
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: Value = serde_json::from_slice(&buf).unwrap();
if response["status"] == "processed" {
return;
}
block_on(sleep(Duration::from_secs(1)));
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!({
"uid": "movies",
"identifier": "id",
});
self.create_index(body);
let body = json!({
"rankingRules": [
"typo",
"words",
"proximity",
"attribute",
"wordsPosition",
"desc(popularity)",
"exactness",
"desc(vote_average)",
],
"searchableAttributes": [
"title",
"tagline",
"overview",
"cast",
"director",
"producer",
"production_companies",
"genres",
],
"displayedAttributes": [
"title",
"director",
"producer",
"tagline",
"genres",
"id",
"overview",
"vote_count",
"vote_average",
"poster_path",
"popularity",
],
"acceptNewFields": false,
});
self.update_all_settings(body);
let dataset = include_bytes!("assets/movies.json");
let body: Value = serde_json::from_slice(dataset).unwrap();
self.add_or_replace_multiple_documents(body);
}
}

View File

@ -1,4 +1,3 @@
use http_service::Body;
use serde_json::json;
use std::convert::Into;
@ -6,51 +5,34 @@ mod common;
#[test]
fn test_healthyness() {
let mut server = common::setup_server().unwrap();
let mut server = common::Server::with_uid("movies");
// Check that the server is healthy
let req = http::Request::get("/health").body(Body::empty()).unwrap();
let res = server.simulate(req).unwrap();
assert_eq!(res.status(), 200);
let (_response, status_code) = server.get_health();
assert_eq!(status_code, 200);
// Set the serve Unhealthy
let body = json!({
"health": false,
})
.to_string()
.into_bytes();
let req = http::Request::put("/health")
.body(Body::from(body))
.unwrap();
let res = server.simulate(req).unwrap();
assert_eq!(res.status(), 200);
});
let (_response, status_code) = server.update_health(body);
assert_eq!(status_code, 200);
// Check that the server is unhealthy
let req = http::Request::get("/health").body(Body::empty()).unwrap();
let res = server.simulate(req).unwrap();
assert_eq!(res.status(), 503);
let (_response, status_code) = server.get_health();
assert_eq!(status_code, 503);
// Set the server healthy
let body = json!({
"health": true,
})
.to_string()
.into_bytes();
let req = http::Request::put("/health")
.body(Body::from(body))
.unwrap();
let res = server.simulate(req).unwrap();
assert_eq!(res.status(), 200);
});
let (_response, status_code) = server.update_health(body);
assert_eq!(status_code, 200);
// Check if the server is healthy
let req = http::Request::get("/health").body(Body::empty()).unwrap();
let res = server.simulate(req).unwrap();
assert_eq!(res.status(), 200);
let (_response, status_code) = server.get_health();
assert_eq!(status_code, 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::Value;
mod common;
#[test]
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
// Index with only a name "movies"
@ -16,20 +12,10 @@ fn create_index_with_name() {
let body = json!({
"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);
let r1_name = res1_value["name"].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
// GET: /indexes
let req = http::Request::get("/indexes").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 res2_value: Value = serde_json::from_slice(&buf).unwrap();
let (res2_value, status_code) = server.list_indexes();
assert_eq!(status_code, 200);
assert_eq!(res2_value.as_array().unwrap().len(), 1);
assert_eq!(res2_value[0].as_object().unwrap().len(), 5);
let r2_name = res2_value[0]["name"].as_str().unwrap();
@ -68,7 +48,7 @@ fn create_index_with_name() {
#[test]
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
// Index with only an uid "movies"
@ -76,20 +56,10 @@ fn create_index_with_uid() {
let body = json!({
"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);
let r1_name = res1_value["name"].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
// GET: /indexes
let req = http::Request::get("/indexes").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 res2_value: Value = serde_json::from_slice(&buf).unwrap();
let (res2_value, status_code) = server.list_indexes();
assert_eq!(status_code, 200);
assert_eq!(res2_value.as_array().unwrap().len(), 1);
assert_eq!(res2_value[0].as_object().unwrap().len(), 5);
let r2_name = res2_value[0]["name"].as_str().unwrap();
@ -128,7 +92,7 @@ fn create_index_with_uid() {
#[test]
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
// Index with a name "Films" and an uid "fn_movies"
@ -137,19 +101,9 @@ fn create_index_with_name_and_uid() {
let body = json!({
"name": "Films",
"uid": "fr_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);
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
// GET: /indexes
let req = http::Request::get("/indexes").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 res2_value: Value = serde_json::from_slice(&buf).unwrap();
let (res2_value, status_code) = server.list_indexes();
assert_eq!(status_code, 200);
assert_eq!(res2_value.as_array().unwrap().len(), 1);
assert_eq!(res2_value[0].as_object().unwrap().len(), 5);
@ -189,26 +138,18 @@ fn create_index_with_name_and_uid() {
#[test]
fn rename_index() {
let mut server = common::setup_server().unwrap();
let mut server = common::Server::with_uid("movies");
// 1 - Create a new index
// Index with only a name "movies"
// POST: /indexes
let body = json!({
"name": "movies",
})
.to_string()
.into_bytes();
"uid": "movies",
});
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);
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();
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_updated_at.len() > 1);
@ -227,19 +168,10 @@ fn rename_index() {
let body = json!({
"name": "TV Shows",
})
.to_string()
.into_bytes();
});
let req = http::Request::put(format!("/indexes/{}", r1_uid))
.body(Body::from(body))
.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();
let (res2_value, status_code) = server.update_index(body);
assert_eq!(status_code, 200);
assert_eq!(res2_value.as_object().unwrap().len(), 5);
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
// GET: /indexes
let req = http::Request::get("/indexes").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 res3_value: Value = serde_json::from_slice(&buf).unwrap();
let (res3_value, status_code) = server.list_indexes();
assert_eq!(status_code, 200);
assert_eq!(res3_value.as_array().unwrap().len(), 1);
assert_eq!(res3_value[0].as_object().unwrap().len(), 5);
@ -279,7 +206,7 @@ fn rename_index() {
#[test]
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
// Index with only a name "movies"
@ -287,19 +214,11 @@ fn delete_index_and_recreate_it() {
let body = json!({
"name": "movies",
})
.to_string()
.into_bytes();
"uid": "movies",
});
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);
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();
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_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
// GET: /indexes
let req = http::Request::get("/indexes").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 res2_value: Value = serde_json::from_slice(&buf).unwrap();
let (res2_value, status_code) = server.list_indexes();
assert_eq!(status_code, 200);
assert_eq!(res2_value.as_array().unwrap().len(), 1);
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"
// DELETE: /indexes/:uid
let req = http::Request::delete(format!("/indexes/{}", r1_uid))
.body(Body::empty())
.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);
let (_res2_value, status_code) = server.delete_index();
assert_eq!(status_code, 204);
// 4 - Check the list of indexes
// Must have 0 index
// GET: /indexes
let req = http::Request::get("/indexes").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 res2_value: Value = serde_json::from_slice(&buf).unwrap();
let (res2_value, status_code) = server.list_indexes();
assert_eq!(status_code, 200);
assert_eq!(res2_value.as_array().unwrap().len(), 0);
@ -370,19 +272,10 @@ fn delete_index_and_recreate_it() {
let body = json!({
"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);
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
// GET: /indexes
let req = http::Request::get("/indexes").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 res2_value: Value = serde_json::from_slice(&buf).unwrap();
let (res2_value, status_code) = server.list_indexes();
assert_eq!(status_code, 200);
assert_eq!(res2_value.as_array().unwrap().len(), 1);
assert_eq!(res2_value[0].as_object().unwrap().len(), 5);
@ -422,7 +310,7 @@ fn delete_index_and_recreate_it() {
#[test]
fn check_multiples_indexes() {
let mut server = common::setup_server().unwrap();
let mut server = common::Server::with_uid("movies");
// 1 - Create a new index
// Index with only a name "movies"
@ -430,19 +318,10 @@ fn check_multiples_indexes() {
let body = json!({
"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);
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
// GET: /indexes
let req = http::Request::get("/indexes").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 res2_value: Value = serde_json::from_slice(&buf).unwrap();
let (res2_value, status_code) = server.list_indexes();
assert_eq!(status_code, 200);
assert_eq!(res2_value.as_array().unwrap().len(), 1);
assert_eq!(res2_value[0].as_object().unwrap().len(), 5);
@ -485,19 +359,10 @@ fn check_multiples_indexes() {
let body = json!({
"name": "films",
})
.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 res3_value: Value = serde_json::from_slice(&buf).unwrap();
let (res3_value, status_code) = server.create_index(body);
assert_eq!(status_code, 201);
assert_eq!(res3_value.as_object().unwrap().len(), 5);
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
// GET: /indexes
let req = http::Request::get("/indexes").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 res4_value: Value = serde_json::from_slice(&buf).unwrap();
let (res4_value, status_code) = server.list_indexes();
assert_eq!(status_code, 200);
assert_eq!(res4_value.as_array().unwrap().len(), 2);
@ -563,37 +423,15 @@ fn check_multiples_indexes() {
#[test]
fn create_index_failed() {
let mut server = common::setup_server().unwrap();
// 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");
let mut server = common::Server::with_uid("movies");
// 2 - Push index creation with empty json body
// POST: /indexes
let body = json!({}).to_string().into_bytes();
let body = json!({});
let req = http::Request::post("/indexes")
.body(Body::from(body))
.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 (res_value, status_code) = server.create_index(body);
assert_eq!(status_code, 400);
let message = res_value["message"].as_str().unwrap();
assert_eq!(res_value.as_object().unwrap().len(), 1);
@ -605,19 +443,10 @@ fn create_index_failed() {
let body = json!({
"name": "movies",
"active": true
})
.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(), 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 (res_value, status_code) = server.create_index(body);
assert_eq!(status_code, 400);
let message = res_value["message"].as_str().unwrap();
assert_eq!(res_value.as_object().unwrap().len(), 1);
@ -629,19 +458,10 @@ fn create_index_failed() {
let body = json!({
"name": "movies",
"uid": 0
})
.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(), 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 (res_value, status_code) = server.create_index(body);
assert_eq!(status_code, 400);
let message = res_value["message"].as_str().unwrap();
assert_eq!(res_value.as_object().unwrap().len(), 1);

View File

@ -1,16 +1,13 @@
use std::convert::Into;
use serde_json::json;
use assert_json_diff::assert_json_eq;
mod common;
#[test]
fn basic_search() {
let mut server = common::setup_server().unwrap();
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();
let mut server = common::Server::with_uid("movies");
server.populate_movies();
// 1 - Simple search
// q: Captain
@ -18,7 +15,7 @@ fn basic_search() {
let query = "q=captain&limit=3";
let json = json!([
let expect = json!([
{
"id": 299537,
"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
// q: Captain
@ -81,7 +79,7 @@ fn basic_search() {
let query = "q=captain&limit=3&offset=1";
let json = json!([
let expect = json!([
{
"id": 271110,
"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
// q: Captain
@ -145,7 +144,7 @@ fn basic_search() {
let query = "q=captain&limit=1&attributesToHighlight=*";
let json = json!([
let expect = json!([
{
"id": 299537,
"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
// q: Captain
@ -191,7 +191,7 @@ fn basic_search() {
let query = "q=captain&limit=1&attributesToHighlight=title";
let json = json!([
let expect = json!([
{
"id": 299537,
"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
// q: Captain
@ -237,7 +238,7 @@ fn basic_search() {
let query = "q=captain&limit=1&attributesToHighlight=title,tagline";
let json = json!([
let expect = json!([
{
"id": 299537,
"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
// q: Captain
@ -283,7 +285,7 @@ fn basic_search() {
let query = "q=captain&limit=1&attributesToHighlight=title,overview";
let json = json!([
let expect = json!([
{
"id": 299537,
"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
// q: Captain
@ -329,7 +332,7 @@ fn basic_search() {
let query = "q=captain&limit=1&matches=true";
let json = json!([
let expect = json!([
{
"id": 299537,
"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
// q: Captain
@ -373,7 +377,7 @@ fn basic_search() {
let query = "q=captain&limit=1&attributesToCrop=overview&cropLength=20";
let json = json!([
let expect = json!([
{
"id": 299537,
"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
// q: Captain
@ -419,7 +424,7 @@ fn basic_search() {
let query = "q=captain&limit=1&attributesToRetrieve=title,tagline,overview,poster_path";
let json = json!([
let expect = json!([
{
"title": "Captain Marvel",
"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
// q: Captain
@ -437,7 +443,7 @@ fn basic_search() {
let query = "q=captain&limit=3&filters=director:Anthony%20Russo";
let json = json!([
let expect = json!([
{
"id": 271110,
"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
// q: Captain
@ -501,7 +508,7 @@ fn basic_search() {
let query = "q=captain&limit=1&attributesToHighlight=title,overview&matches=true";
let json = json!( [
let expect = json!( [
{
"id": 299537,
"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
// 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 json = json!([
let expect = json!([
{
"id": 299537,
"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]
fn search_with_settings_basic() {
let mut server = common::setup_server().unwrap();
common::enrich_server_with_movies_index(&mut server).unwrap();
common::enrich_server_with_movies_documents(&mut server).unwrap();
let mut server = common::Server::with_uid("movies");
server.populate_movies();
let config = json!({
"rankingRules": [
@ -665,10 +673,10 @@ fn search_with_settings_basic() {
"acceptNewFields": false,
});
common::update_config(&mut server, config);
server.update_all_settings(config);
let query = "q=the%20avangers&limit=3";
let response = json!([
let expect = json!([
{
"id": 24428,
"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]
fn search_with_settings_stop_words() {
let mut server = common::setup_server().unwrap();
common::enrich_server_with_movies_index(&mut server).unwrap();
let mut server = common::Server::with_uid("movies");
server.populate_movies();
let config = json!({
"rankingRules": [
@ -771,11 +780,10 @@ fn search_with_settings_stop_words() {
"acceptNewFields": false,
});
common::update_config(&mut server, config);
common::enrich_server_with_movies_documents(&mut server).unwrap();
server.update_all_settings(config);
let query = "q=the%20avangers&limit=3";
let response = json!([
let expect = json!([
{
"id": 299536,
"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]
fn search_with_settings_synonyms() {
let mut server = common::setup_server().unwrap();
common::enrich_server_with_movies_index(&mut server).unwrap();
let mut server = common::Server::with_uid("movies");
server.populate_movies();
let config = json!({
"rankingRules": [
@ -883,11 +892,10 @@ fn search_with_settings_synonyms() {
"acceptNewFields": false,
});
common::update_config(&mut server, config);
common::enrich_server_with_movies_documents(&mut server).unwrap();
server.update_all_settings(config);
let query = "q=avangers&limit=3";
let response = json!([
let expect = json!([
{
"id": 299536,
"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]
fn search_with_settings_ranking_rules() {
let mut server = common::setup_server().unwrap();
common::enrich_server_with_movies_index(&mut server).unwrap();
let mut server = common::Server::with_uid("movies");
server.populate_movies();
let config = json!({
"rankingRules": [
@ -990,11 +999,10 @@ fn search_with_settings_ranking_rules() {
"acceptNewFields": false,
});
common::update_config(&mut server, config);
common::enrich_server_with_movies_documents(&mut server).unwrap();
server.update_all_settings(config);
let query = "q=avangers&limit=3";
let response = json!([
let expect = json!([
{
"id": 99861,
"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]
fn search_with_settings_searchable_attributes() {
let mut server = common::setup_server().unwrap();
common::enrich_server_with_movies_index(&mut server).unwrap();
let mut server = common::Server::with_uid("movies");
server.populate_movies();
let config = json!({
"rankingRules": [
@ -1096,11 +1105,10 @@ fn search_with_settings_searchable_attributes() {
"acceptNewFields": false,
});
common::update_config(&mut server, config);
common::enrich_server_with_movies_documents(&mut server).unwrap();
server.update_all_settings(config);
let query = "q=avangers&limit=3";
let response = json!([
let expect = json!([
{
"id": 299536,
"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]
fn search_with_settings_displayed_attributes() {
let mut server = common::setup_server().unwrap();
common::enrich_server_with_movies_index(&mut server).unwrap();
let mut server = common::Server::with_uid("movies");
server.populate_movies();
let config = json!({
"rankingRules": [
@ -1197,11 +1206,10 @@ fn search_with_settings_displayed_attributes() {
"acceptNewFields": false,
});
common::update_config(&mut server, config);
common::enrich_server_with_movies_documents(&mut server).unwrap();
server.update_all_settings(config);
let query = "q=avangers&limit=3";
let response = json!([
let expect = json!([
{
"id": 299536,
"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]
fn search_with_settings_searchable_attributes_2() {
let mut server = common::setup_server().unwrap();
common::enrich_server_with_movies_index(&mut server).unwrap();
let mut server = common::Server::with_uid("movies");
server.populate_movies();
let config = json!({
"rankingRules": [
@ -1268,11 +1277,10 @@ fn search_with_settings_searchable_attributes_2() {
"acceptNewFields": false,
});
common::update_config(&mut server, config);
common::enrich_server_with_movies_documents(&mut server).unwrap();
server.update_all_settings(config);
let query = "q=avangers&limit=3";
let response = json!([
let expect = json!([
{
"id": 299536,
"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::time::Duration;
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::Value;
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]
fn write_all_and_delete() {
let mut server = common::setup_server().unwrap();
// 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);
let mut server = common::Server::with_uid("movies");
server.populate_movies();
// 2 - Send the settings
let json = json!({
let body = json!({
"rankingRules": [
"typo",
"words",
@ -79,53 +51,23 @@ fn write_all_and_delete() {
"acceptNewFields": false,
});
let body = json.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);
block_on(sleep(Duration::from_secs(1)));
server.update_all_settings(body.clone());
// 3 - Get all settings and compare to the previous one
let req = http::Request::get("/indexes/movies/settings")
.body(Body::empty())
.unwrap();
let res = server.simulate(req).unwrap();
assert_eq!(res.status(), 200);
let (response, _status_code) = server.get_all_settings();
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();
assert_json_eq!(json, res_value, ordered: false);
assert_json_eq!(body, response, ordered: false);
// 4 - Delete all settings
let req = http::Request::delete("/indexes/movies/settings")
.body(Body::empty())
.unwrap();
let res = server.simulate(req).unwrap();
assert_eq!(res.status(), 202);
server.delete_all_settings();
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")
.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 expect = json!({
"rankingRules": [
"typo",
"words",
@ -136,61 +78,61 @@ fn write_all_and_delete() {
],
"distinctAttribute": null,
"searchableAttributes": [
"poster_path",
"director",
"id",
"release_date",
"production_companies",
"producer",
"poster",
"description",
"title",
"movie_id",
"rank"
"vote_count",
"cast",
"release_date",
"vote_average",
"rank",
"genres",
"overview",
"description",
"tagline",
"popularity",
"title"
],
"displayedAttributes": [
"movie_id",
"description",
"poster_path",
"poster",
"vote_count",
"id",
"release_date",
"movie_id",
"title",
"rank",
"title"
"tagline",
"cast",
"producer",
"production_companies",
"description",
"director",
"genres",
"release_date",
"overview",
"vote_average",
"popularity"
],
"stopWords": null,
"synonyms": null,
"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]
fn write_all_and_update() {
let mut server = common::setup_server().unwrap();
// 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);
let mut server = common::Server::with_uid("movies");
server.populate_movies();
// 2 - Send the settings
let json = json!({
let body = json!({
"rankingRules": [
"typo",
"words",
@ -230,33 +172,17 @@ fn write_all_and_update() {
"acceptNewFields": false,
});
let body = json.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);
block_on(sleep(Duration::from_secs(1)));
server.update_all_settings(body.clone());
// 3 - Get all settings and compare to the previous one
let req = http::Request::get("/indexes/movies/settings")
.body(Body::empty())
.unwrap();
let res = server.simulate(req).unwrap();
assert_eq!(res.status(), 200);
let (response, _status_code) = server.get_all_settings();
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();
assert_json_eq!(json, res_value, ordered: false);
assert_json_eq!(body, response, ordered: false);
// 4 - Update all settings
let json_update = json!({
let body = json!({
"rankingRules": [
"typo",
"words",
@ -287,29 +213,13 @@ fn write_all_and_update() {
"acceptNewFields": false,
});
let body_update = json_update.to_string().into_bytes();
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)));
server.update_all_settings(body);
// 5 - Get all settings and check if the content is the same of (4)
let req = http::Request::get("/indexes/movies/settings")
.body(Body::empty())
.unwrap();
let res = server.simulate(req).unwrap();
assert_eq!(res.status(), 200);
let (response, _status_code) = server.get_all_settings();
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 res_expected = json!({
let expected = json!({
"rankingRules": [
"typo",
"words",
@ -340,5 +250,5 @@ fn write_all_and_update() {
"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 async_std::io::prelude::*;
use async_std::task::{block_on, sleep};
use http_service::Body;
use serde_json::json;
use serde_json::Value;
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]
fn write_all_and_delete() {
let mut server = common::setup_server().unwrap();
// 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);
let mut server = common::Server::with_uid("movies");
server.populate_movies();
// 2 - Send the settings
let json = json!([
let body = json!([
"typo",
"words",
"proximity",
@ -49,51 +21,21 @@ fn write_all_and_delete() {
"desc(rank)",
]);
let body = json.to_string().into_bytes();
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)));
server.update_ranking_rules(body.clone());
// 3 - Get all settings and compare to the previous one
let req = http::Request::get("/indexes/movies/settings/ranking-rules")
.body(Body::empty())
.unwrap();
let res = server.simulate(req).unwrap();
assert_eq!(res.status(), 200);
let (response, _status_code) = server.get_ranking_rules();
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();
assert_json_eq!(json, res_value, ordered: false);
assert_json_eq!(body, response, ordered: false);
// 4 - Delete all settings
let req = http::Request::delete("/indexes/movies/settings/ranking-rules")
.body(Body::empty())
.unwrap();
let res = server.simulate(req).unwrap();
assert_eq!(res.status(), 202);
block_on(sleep(Duration::from_secs(2)));
server.delete_ranking_rules();
// 5 - Get all settings and check if they are empty
let req = http::Request::get("/indexes/movies/settings/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 (response, _status_code) = server.get_ranking_rules();
let json = json!([
"typo",
@ -104,39 +46,17 @@ fn write_all_and_delete() {
"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]
fn write_all_and_update() {
let mut server = common::setup_server().unwrap();
// 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);
let mut server = common::Server::with_uid("movies");
server.populate_movies();
// 2 - Send the settings
let json = json!([
let body = json!([
"typo",
"words",
"proximity",
@ -147,33 +67,17 @@ fn write_all_and_update() {
"desc(rank)",
]);
let body = json.to_string().into_bytes();
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)));
server.update_ranking_rules(body.clone());
// 3 - Get all settings and compare to the previous one
let req = http::Request::get("/indexes/movies/settings/ranking-rules")
.body(Body::empty())
.unwrap();
let res = server.simulate(req).unwrap();
assert_eq!(res.status(), 200);
let (response, _status_code) = server.get_ranking_rules();
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();
assert_json_eq!(json, res_value, ordered: false);
assert_json_eq!(body, response, ordered: false);
// 4 - Update all settings
let json_update = json!([
let body = json!([
"typo",
"words",
"proximity",
@ -183,29 +87,13 @@ fn write_all_and_update() {
"desc(release_date)",
]);
let body_update = json_update.to_string().into_bytes();
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)));
server.update_ranking_rules(body);
// 5 - Get all settings and check if the content is the same of (4)
let req = http::Request::get("/indexes/movies/settings/ranking-rules")
.body(Body::empty())
.unwrap();
let res = server.simulate(req).unwrap();
assert_eq!(res.status(), 200);
let (response, _status_code) = server.get_ranking_rules();
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 res_expected = json!([
let expected = json!([
"typo",
"words",
"proximity",
@ -215,5 +103,5 @@ fn write_all_and_update() {
"desc(release_date)",
]);
assert_json_eq!(res_expected, res_value, ordered: false);
assert_json_eq!(expected, response, ordered: false);
}