meilisearch/meilisearch-http/tests/common.rs

147 lines
3.9 KiB
Rust
Raw Normal View History

2020-01-27 15:52:36 +08:00
use serde_json::Value;
2020-01-16 00:11:27 +08:00
use std::error::Error;
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};
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;
use serde_json::json;
use tempdir::TempDir;
use tide::server::Service;
2020-01-16 00:11:27 +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(),
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-23 18:30:18 +08:00
pub fn enrich_server_with_movies_index(
server: &mut TestBackend<Service<Data>>,
) -> Result<(), Box<dyn Error>> {
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-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();
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>> {
let json = json!({
"rankingRules": [
"_typo",
"_words",
"_proximity",
"_attribute",
"_words_position",
"dsc(popularity)",
"_exact",
"dsc(vote_average)",
],
"rankingDistinct": null,
2020-01-30 01:30:21 +08:00
"identifier": "id",
"searchableAttributes": [
"title",
"tagline",
"overview",
"cast",
"director",
"producer",
"production_companies",
"genres",
],
2020-01-30 01:30:21 +08:00
"displayedAttributes": [
"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,
"indexNewFields": false,
});
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-01-27 15:52:36 +08:00
let _res = server.simulate(req).unwrap();
block_on(sleep(Duration::from_secs(5)));
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>> {
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-01-27 15:52:36 +08:00
let _res = server.simulate(req).unwrap();
2020-02-03 05:59:19 +08:00
block_on(sleep(Duration::from_secs(10)));
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
block_on(sleep(Duration::from_secs(5)));
}