From 2acbdb9f37e7eedfcf328ef3dcaae6493ba42a4c Mon Sep 17 00:00:00 2001 From: Alex Wellnitz Date: Mon, 12 Feb 2024 23:48:30 +0100 Subject: [PATCH] feat: add tests for hello and search handlers --- tests/hello_handler_tests.rs | 23 ++++++++++++++++++++++ tests/search_handler_tests.rs | 37 +++++++++++++++++++++++++++++++++++ 2 files changed, 60 insertions(+) create mode 100644 tests/hello_handler_tests.rs create mode 100644 tests/search_handler_tests.rs diff --git a/tests/hello_handler_tests.rs b/tests/hello_handler_tests.rs new file mode 100644 index 0000000..294a4a8 --- /dev/null +++ b/tests/hello_handler_tests.rs @@ -0,0 +1,23 @@ +#[cfg(test)] +mod tests { + use actix_web::{http::header::ContentType, test, App}; + use rustysearch::handlers::hello; + + #[actix_web::test] + async fn test_index_get() { + let app = test::init_service(App::new().service(hello::say_hello)).await; + let req = test::TestRequest::default() + .insert_header(ContentType::plaintext()) + .to_request(); + let resp = test::call_service(&app, req).await; + assert!(resp.status().is_success()); + } + + #[actix_web::test] + async fn test_index_post() { + let app = test::init_service(App::new().service(hello::say_hello)).await; + let req = test::TestRequest::post().uri("/").to_request(); + let resp = test::call_service(&app, req).await; + assert!(resp.status().is_client_error()); + } +} \ No newline at end of file diff --git a/tests/search_handler_tests.rs b/tests/search_handler_tests.rs new file mode 100644 index 0000000..381b538 --- /dev/null +++ b/tests/search_handler_tests.rs @@ -0,0 +1,37 @@ +#[cfg(test)] +mod tests { + use std::sync::Mutex; + + use actix_web::{test, web, App}; + use rustysearch::{handlers::search, search::engine::SearchEngine, types::app_state::AppStateWithSearchEngine}; + + #[actix_web::test] + async fn test_add_document_to_index() { + let search_engine = SearchEngine::new(1.5, 0.75); + + let app_state = web::Data::new(AppStateWithSearchEngine { + 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 data = search::AddDocumentRequest { + url: "https://example.com".to_string(), + content: "This is an example document".to_string(), + }; + + let req = test::TestRequest::post() + .uri("/search/index/document") + .set_json(data) + .to_request(); + + let resp = test::call_service(&app, req).await; + assert!(resp.status() == 201); + } +} \ No newline at end of file