refactor: testing and coverage

This commit is contained in:
2024-12-16 23:58:34 +01:00
parent 78296750f6
commit 28aa802698
4 changed files with 73 additions and 93 deletions

View File

@@ -0,0 +1,14 @@
#[cfg(test)]
mod tests {
use rustysearch::types::config::Config;
#[test]
fn test_search_engine() {
let config = Config::default();
let config_two = Config::new();
println!("{:?}", config);
println!("{:?}", config_two);
// assert_eq!(config.http, 1);
}
}

View File

@@ -1,6 +1,6 @@
#[cfg(test)]
mod tests {
use rustysearch::search::engine::SearchEngine;
use rustysearch::search::engine::{remove_index_from_disk, SearchEngine};
#[test]
fn test_search_engine() {
@@ -12,6 +12,8 @@ mod tests {
assert_eq!(result.len(), 1);
assert_eq!(search_engine.posts().len(), 1);
assert_eq!(search_engine.number_of_documents(), 1);
remove_index_from_disk();
}
#[test]
@@ -25,5 +27,8 @@ mod tests {
assert_eq!(search_engine.posts().len(), 2);
assert_eq!(search_engine.number_of_documents(), 2);
remove_index_from_disk();
}
}
}

View File

@@ -3,7 +3,11 @@ mod tests {
use std::sync::Mutex;
use actix_web::{test, web, App};
use rustysearch::{handlers::search, search::engine::SearchEngine, types::app_state::AppStateWithSearchEngine};
use rustysearch::{
handlers::search,
search::engine::{remove_index_from_disk, SearchEngine},
types::app_state::AppStateWithSearchEngine,
};
#[actix_web::test]
async fn test_add_document_to_index() {
@@ -13,13 +17,11 @@ mod tests {
search_engine: Mutex::new(search_engine.clone()),
});
let app = test::init_service(App::new()
.app_data(app_state.clone())
.route(
"/search/index/document",
web::post().to(search::add_document_to_index),
)
).await;
let app = test::init_service(App::new().app_data(app_state.clone()).route(
"/search/index/document",
web::post().to(search::add_document_to_index),
))
.await;
let data = search::AddDocumentRequest {
url: "https://example.com".to_string(),
@@ -33,6 +35,8 @@ mod tests {
let resp = test::call_service(&app, req).await;
assert!(resp.status() == 201);
remove_index_from_disk();
}
#[actix_web::test]
@@ -44,13 +48,11 @@ mod tests {
search_engine: Mutex::new(search_engine.clone()),
});
let app = test::init_service(App::new()
.app_data(app_state.clone())
.route(
"/search/index/number_of_documents",
web::get().to(search::get_number_of_documents),
)
).await;
let app = test::init_service(App::new().app_data(app_state.clone()).route(
"/search/index/number_of_documents",
web::get().to(search::get_number_of_documents),
))
.await;
let req = test::TestRequest::get()
.uri("/search/index/number_of_documents")
@@ -58,6 +60,8 @@ mod tests {
let resp = test::call_service(&app, req).await;
assert!(resp.status() == 200);
remove_index_from_disk();
}
#[actix_web::test]
@@ -69,10 +73,12 @@ mod tests {
search_engine: Mutex::new(search_engine.clone()),
});
let app = test::init_service(App::new()
.app_data(app_state.clone())
.route("/search", web::get().to(search::search))
).await;
let app = test::init_service(
App::new()
.app_data(app_state.clone())
.route("/search", web::get().to(search::search)),
)
.await;
let req = test::TestRequest::get()
.uri("/search?query=example")
@@ -80,6 +86,8 @@ mod tests {
let resp = test::call_service(&app, req).await;
assert!(resp.status() == 200);
remove_index_from_disk();
}
#[actix_web::test]
@@ -90,16 +98,19 @@ mod tests {
search_engine: Mutex::new(search_engine.clone()),
});
let app = test::init_service(App::new()
.app_data(app_state.clone())
.route("/search/debug", web::get().to(search::debug_index))
).await;
let app = test::init_service(
App::new()
.app_data(app_state.clone())
.route("/search/debug", web::get().to(search::debug_index)),
)
.await;
let req = test::TestRequest::get()
.uri("/search/debug")
.to_request();
let req = test::TestRequest::get().uri("/search/debug").to_request();
let resp = test::call_service(&app, req).await;
assert!(resp.status() == 200);
remove_index_from_disk();
}
}
}