2020-02-13 17:25:37 +08:00
|
|
|
#![allow(dead_code)]
|
|
|
|
|
2020-01-27 15:52:36 +08:00
|
|
|
use serde_json::Value;
|
2020-01-16 00:11:27 +08:00
|
|
|
use std::error::Error;
|
2020-01-22 21:30:09 +08:00
|
|
|
use std::time::Duration;
|
2020-01-16 00:11:27 +08:00
|
|
|
|
2020-01-27 15:52:36 +08:00
|
|
|
use assert_json_diff::assert_json_eq;
|
2020-01-28 01:27:42 +08:00
|
|
|
use async_std::io::prelude::*;
|
|
|
|
use async_std::task::{block_on, sleep};
|
2020-01-22 21:30:09 +08:00
|
|
|
use http_service::Body;
|
|
|
|
use http_service_mock::{make_server, TestBackend};
|
2020-01-16 00:11:27 +08:00
|
|
|
use meilisearch_http::data::Data;
|
|
|
|
use meilisearch_http::option::Opt;
|
|
|
|
use meilisearch_http::routes;
|
2020-01-22 21:30:09 +08:00
|
|
|
use serde_json::json;
|
|
|
|
use tempdir::TempDir;
|
|
|
|
use tide::server::Service;
|
2020-01-16 00:11:27 +08:00
|
|
|
|
2020-01-22 21:30:09 +08:00
|
|
|
pub fn setup_server() -> Result<TestBackend<Service<Data>>, Box<dyn Error>> {
|
2020-01-16 00:11:27 +08:00
|
|
|
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(),
|
2020-02-06 22:41:11 +08:00
|
|
|
master_key: None,
|
2020-02-06 22:42:35 +08:00
|
|
|
env: "development".to_owned(),
|
2020-01-16 00:11:27 +08:00
|
|
|
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)?)
|
|
|
|
}
|
2020-01-22 21:30:09 +08:00
|
|
|
|
2020-01-23 18:30:18 +08:00
|
|
|
pub fn enrich_server_with_movies_index(
|
|
|
|
server: &mut TestBackend<Service<Data>>,
|
|
|
|
) -> Result<(), Box<dyn Error>> {
|
2020-01-22 21:30:09 +08:00
|
|
|
let body = json!({
|
|
|
|
"uid": "movies",
|
2020-02-03 05:59:19 +08:00
|
|
|
"identifier": "id",
|
2020-01-23 18:30:18 +08:00
|
|
|
})
|
|
|
|
.to_string()
|
|
|
|
.into_bytes();
|
2020-01-22 21:30:09 +08:00
|
|
|
|
2020-01-23 18:30:18 +08:00
|
|
|
let req = http::Request::post("/indexes")
|
|
|
|
.body(Body::from(body))
|
|
|
|
.unwrap();
|
2020-01-27 15:52:36 +08:00
|
|
|
let _res = server.simulate(req).unwrap();
|
2020-01-22 21:30:09 +08:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2020-01-23 18:30:18 +08:00
|
|
|
pub fn enrich_server_with_movies_settings(
|
|
|
|
server: &mut TestBackend<Service<Data>>,
|
|
|
|
) -> Result<(), Box<dyn Error>> {
|
2020-01-22 21:30:09 +08:00
|
|
|
let json = json!({
|
|
|
|
"rankingRules": [
|
2020-02-27 01:47:03 +08:00
|
|
|
"typo",
|
|
|
|
"words",
|
|
|
|
"proximity",
|
|
|
|
"attribute",
|
2020-02-27 21:31:08 +08:00
|
|
|
"wordsPosition",
|
2020-03-03 00:13:23 +08:00
|
|
|
"desc(popularity)",
|
2020-02-27 01:47:03 +08:00
|
|
|
"exactness",
|
2020-03-03 00:13:23 +08:00
|
|
|
"desc(vote_average)",
|
2020-01-22 21:30:09 +08:00
|
|
|
],
|
2020-02-26 23:05:02 +08:00
|
|
|
"distinctAttribute": null,
|
2020-01-30 01:30:21 +08:00
|
|
|
"searchableAttributes": [
|
2020-01-22 21:30:09 +08:00
|
|
|
"title",
|
|
|
|
"tagline",
|
|
|
|
"overview",
|
|
|
|
"cast",
|
|
|
|
"director",
|
|
|
|
"producer",
|
|
|
|
"production_companies",
|
|
|
|
"genres",
|
|
|
|
],
|
2020-01-30 01:30:21 +08:00
|
|
|
"displayedAttributes": [
|
2020-01-22 21:30:09 +08:00
|
|
|
"title",
|
|
|
|
"director",
|
|
|
|
"producer",
|
|
|
|
"tagline",
|
|
|
|
"genres",
|
|
|
|
"id",
|
|
|
|
"overview",
|
|
|
|
"vote_count",
|
|
|
|
"vote_average",
|
|
|
|
"poster_path",
|
|
|
|
"popularity",
|
|
|
|
],
|
|
|
|
"stopWords": null,
|
2020-01-28 01:27:42 +08:00
|
|
|
"synonyms": null,
|
2020-02-26 23:05:02 +08:00
|
|
|
"acceptNewFields": false,
|
2020-01-22 21:30:09 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
let body = json.to_string().into_bytes();
|
|
|
|
|
2020-01-23 18:30:18 +08:00
|
|
|
let req = http::Request::post("/indexes/movies/settings")
|
|
|
|
.body(Body::from(body))
|
|
|
|
.unwrap();
|
2020-02-13 17:25:37 +08:00
|
|
|
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();
|
2020-01-22 21:30:09 +08:00
|
|
|
|
2020-02-13 17:25:37 +08:00
|
|
|
assert!(response["updateId"].as_u64().is_some());
|
|
|
|
|
|
|
|
wait_update_id(server, response["updateId"].as_u64().unwrap());
|
2020-01-22 21:30:09 +08:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2020-01-23 18:30:18 +08:00
|
|
|
pub fn enrich_server_with_movies_documents(
|
|
|
|
server: &mut TestBackend<Service<Data>>,
|
|
|
|
) -> Result<(), Box<dyn Error>> {
|
2020-01-22 21:30:09 +08:00
|
|
|
let body = include_bytes!("assets/movies.json").to_vec();
|
|
|
|
|
2020-01-23 18:30:18 +08:00
|
|
|
let req = http::Request::post("/indexes/movies/documents")
|
|
|
|
.body(Body::from(body))
|
|
|
|
.unwrap();
|
2020-02-13 17:25:37 +08:00
|
|
|
let res = server.simulate(req).unwrap();
|
2020-01-22 21:30:09 +08:00
|
|
|
|
2020-02-13 17:25:37 +08:00
|
|
|
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());
|
2020-01-22 21:30:09 +08:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
2020-01-24 01:33:23 +08:00
|
|
|
|
|
|
|
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();
|
|
|
|
|
2020-01-27 15:52:36 +08:00
|
|
|
assert_json_eq!(expect, response["hits"].clone(), ordered: false)
|
2020-01-24 01:33:23 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn update_config(server: &mut TestBackend<Service<Data>>, config: Value) {
|
|
|
|
let body = config.to_string().into_bytes();
|
|
|
|
|
2020-01-27 15:52:36 +08:00
|
|
|
let req = http::Request::post("/indexes/movies/settings")
|
2020-01-24 01:33:23 +08:00
|
|
|
.body(Body::from(body))
|
|
|
|
.unwrap();
|
|
|
|
let res = server.simulate(req).unwrap();
|
2020-01-27 15:52:36 +08:00
|
|
|
assert_eq!(res.status(), 202);
|
2020-01-24 01:33:23 +08:00
|
|
|
|
2020-02-13 17:25:37 +08:00
|
|
|
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" {
|
2020-02-27 01:49:17 +08:00
|
|
|
return;
|
2020-02-13 17:25:37 +08:00
|
|
|
}
|
|
|
|
block_on(sleep(Duration::from_secs(1)));
|
|
|
|
}
|
2020-01-24 01:33:23 +08:00
|
|
|
}
|