2020-01-22 14:30:09 +01:00
|
|
|
#![allow(dead_code)]
|
2020-01-27 08:52:36 +01:00
|
|
|
use serde_json::Value;
|
2020-01-15 17:11:27 +01:00
|
|
|
use std::error::Error;
|
2020-01-22 14:30:09 +01:00
|
|
|
use std::time::Duration;
|
2020-01-15 17:11:27 +01:00
|
|
|
|
2020-01-23 11:30:18 +01:00
|
|
|
use async_std::task::{block_on, sleep};
|
2020-01-27 08:52:36 +01:00
|
|
|
use async_std::io::prelude::*;
|
|
|
|
use assert_json_diff::assert_json_eq;
|
2020-01-22 14:30:09 +01:00
|
|
|
use http_service::Body;
|
|
|
|
use http_service_mock::{make_server, TestBackend};
|
2020-01-15 17:11:27 +01:00
|
|
|
use meilisearch_http::data::Data;
|
|
|
|
use meilisearch_http::option::Opt;
|
|
|
|
use meilisearch_http::routes;
|
2020-01-22 14:30:09 +01:00
|
|
|
use serde_json::json;
|
|
|
|
use tempdir::TempDir;
|
|
|
|
use tide::server::Service;
|
2020-01-15 17:11:27 +01:00
|
|
|
|
2020-01-22 14:30:09 +01:00
|
|
|
pub fn setup_server() -> Result<TestBackend<Service<Data>>, Box<dyn Error>> {
|
2020-01-15 17:11:27 +01: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(),
|
|
|
|
api_key: None,
|
|
|
|
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 14:30:09 +01:00
|
|
|
|
2020-01-23 11:30:18 +01:00
|
|
|
pub fn enrich_server_with_movies_index(
|
|
|
|
server: &mut TestBackend<Service<Data>>,
|
|
|
|
) -> Result<(), Box<dyn Error>> {
|
2020-01-22 14:30:09 +01:00
|
|
|
let body = json!({
|
|
|
|
"uid": "movies",
|
2020-01-23 11:30:18 +01:00
|
|
|
})
|
|
|
|
.to_string()
|
|
|
|
.into_bytes();
|
2020-01-22 14:30:09 +01:00
|
|
|
|
2020-01-23 11:30:18 +01:00
|
|
|
let req = http::Request::post("/indexes")
|
|
|
|
.body(Body::from(body))
|
|
|
|
.unwrap();
|
2020-01-27 08:52:36 +01:00
|
|
|
let _res = server.simulate(req).unwrap();
|
2020-01-22 14:30:09 +01:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2020-01-23 11:30:18 +01:00
|
|
|
pub fn enrich_server_with_movies_settings(
|
|
|
|
server: &mut TestBackend<Service<Data>>,
|
|
|
|
) -> Result<(), Box<dyn Error>> {
|
2020-01-22 14:30:09 +01:00
|
|
|
let json = json!({
|
|
|
|
"rankingRules": [
|
|
|
|
"_typo",
|
|
|
|
"_words",
|
|
|
|
"_proximity",
|
|
|
|
"_attribute",
|
|
|
|
"_words_position",
|
|
|
|
"dsc(popularity)",
|
|
|
|
"_exact",
|
|
|
|
"dsc(vote_average)",
|
|
|
|
],
|
|
|
|
"rankingDistinct": null,
|
|
|
|
"attributeIdentifier": "id",
|
|
|
|
"attributesSearchable": [
|
|
|
|
"title",
|
|
|
|
"tagline",
|
|
|
|
"overview",
|
|
|
|
"cast",
|
|
|
|
"director",
|
|
|
|
"producer",
|
|
|
|
"production_companies",
|
|
|
|
"genres",
|
|
|
|
],
|
|
|
|
"attributesDisplayed": [
|
|
|
|
"title",
|
|
|
|
"director",
|
|
|
|
"producer",
|
|
|
|
"tagline",
|
|
|
|
"genres",
|
|
|
|
"id",
|
|
|
|
"overview",
|
|
|
|
"vote_count",
|
|
|
|
"vote_average",
|
|
|
|
"poster_path",
|
|
|
|
"popularity",
|
|
|
|
],
|
|
|
|
"stopWords": null,
|
|
|
|
"synonyms": null
|
|
|
|
});
|
|
|
|
|
|
|
|
let body = json.to_string().into_bytes();
|
|
|
|
|
2020-01-23 11:30:18 +01:00
|
|
|
let req = http::Request::post("/indexes/movies/settings")
|
|
|
|
.body(Body::from(body))
|
|
|
|
.unwrap();
|
2020-01-27 08:52:36 +01:00
|
|
|
let _res = server.simulate(req).unwrap();
|
2020-01-22 14:30:09 +01:00
|
|
|
|
|
|
|
block_on(sleep(Duration::from_secs(5)));
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2020-01-23 11:30:18 +01:00
|
|
|
pub fn enrich_server_with_movies_documents(
|
|
|
|
server: &mut TestBackend<Service<Data>>,
|
|
|
|
) -> Result<(), Box<dyn Error>> {
|
2020-01-22 14:30:09 +01:00
|
|
|
let body = include_bytes!("assets/movies.json").to_vec();
|
|
|
|
|
2020-01-23 11:30:18 +01:00
|
|
|
let req = http::Request::post("/indexes/movies/documents")
|
|
|
|
.body(Body::from(body))
|
|
|
|
.unwrap();
|
2020-01-27 08:52:36 +01:00
|
|
|
let _res = server.simulate(req).unwrap();
|
2020-01-22 14:30:09 +01:00
|
|
|
|
|
|
|
block_on(sleep(Duration::from_secs(5)));
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
2020-01-23 18:33:23 +01: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 08:52:36 +01:00
|
|
|
assert_json_eq!(expect, response["hits"].clone(), ordered: false)
|
2020-01-23 18:33:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn update_config(server: &mut TestBackend<Service<Data>>, config: Value) {
|
|
|
|
let body = config.to_string().into_bytes();
|
|
|
|
|
2020-01-27 08:52:36 +01:00
|
|
|
let req = http::Request::post("/indexes/movies/settings")
|
2020-01-23 18:33:23 +01:00
|
|
|
.body(Body::from(body))
|
|
|
|
.unwrap();
|
|
|
|
let res = server.simulate(req).unwrap();
|
2020-01-27 08:52:36 +01:00
|
|
|
assert_eq!(res.status(), 202);
|
2020-01-23 18:33:23 +01:00
|
|
|
|
|
|
|
block_on(sleep(Duration::from_secs(5)));
|
|
|
|
}
|