From 28aa802698addb8841f5230d57c24b5879dc00b8 Mon Sep 17 00:00:00 2001 From: Alex Wellnitz Date: Mon, 16 Dec 2024 23:58:34 +0100 Subject: [PATCH] refactor: testing and coverage --- .github/workflows/coverage.yml | 78 ++++++---------------------------- tests/config_type_tests.rs | 14 ++++++ tests/search_engine_tests.rs | 9 +++- tests/search_handler_tests.rs | 65 ++++++++++++++++------------ 4 files changed, 73 insertions(+), 93 deletions(-) create mode 100644 tests/config_type_tests.rs diff --git a/.github/workflows/coverage.yml b/.github/workflows/coverage.yml index a205a54..33f1437 100644 --- a/.github/workflows/coverage.yml +++ b/.github/workflows/coverage.yml @@ -1,73 +1,23 @@ name: Coverage -on: - push: -# branches: -# - develop -# pull_request_target: -# branches: -# - develop - -env: - CARGO_TERM_COLOR: always +on: [pull_request, push] jobs: - report: - name: Report - environment: coverage + coverage: runs-on: ubuntu-latest env: - CARGO_INCREMENTAL: "0" - RUSTFLAGS: "-Z profile -C codegen-units=1 -C inline-threshold=0 -C link-dead-code -C overflow-checks=off -C panic=abort -Z panic_abort_tests" - RUSTDOCFLAGS: "-Z profile -C codegen-units=1 -C inline-threshold=0 -C link-dead-code -C overflow-checks=off -C panic=abort -Z panic_abort_tests" - + CARGO_TERM_COLOR: always steps: - - id: checkout_push - if: github.event_name == 'push' - name: Checkout Repository (Push) - uses: actions/checkout@v4 - - - id: checkout_pull_request_target - if: github.event_name == 'pull_request_target' - name: Checkout Repository (Pull Request Target) - uses: actions/checkout@v4 - with: - ref: "refs/pull/${{ github.event.pull_request.number }}/head" - - - id: setup - name: Setup Toolchain - uses: dtolnay/rust-toolchain@stable - with: - toolchain: nightly - components: llvm-tools-preview - - - id: cache - name: Enable Workflow Cache - uses: Swatinem/rust-cache@v2 - - - id: tools - name: Install Tools - uses: taiki-e/install-action@v2 - with: - tool: grcov - - - id: check - name: Run Build Checks - run: cargo check --tests --benches --examples --workspace --all-targets --all-features - - - id: test - name: Run Unit Tests - run: cargo test --tests --benches --examples --workspace --all-targets --all-features - - - id: coverage - name: Generate Coverage Report - uses: actions-rs/grcov@v0.1.5 - - - id: upload - name: Upload Coverage Report + - uses: actions/checkout@v4 + - name: Install Rust + run: rustup update stable + - name: Install cargo-llvm-cov + uses: taiki-e/install-action@cargo-llvm-cov + - name: Generate code coverage + run: cargo llvm-cov --all-features --workspace --lcov --output-path lcov.info + - name: Upload coverage to Codecov uses: codecov/codecov-action@v3 with: - token: ${{ secrets.CODECOV_TOKEN }} - files: ${{ steps.coverage.outputs.report }} - verbose: true - fail_ci_if_error: true \ No newline at end of file + files: lcov.info + fail_ci_if_error: true + diff --git a/tests/config_type_tests.rs b/tests/config_type_tests.rs new file mode 100644 index 0000000..a1ebea4 --- /dev/null +++ b/tests/config_type_tests.rs @@ -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); + } +} diff --git a/tests/search_engine_tests.rs b/tests/search_engine_tests.rs index bb71ad3..72e66c3 100644 --- a/tests/search_engine_tests.rs +++ b/tests/search_engine_tests.rs @@ -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(); } -} \ No newline at end of file +} + diff --git a/tests/search_handler_tests.rs b/tests/search_handler_tests.rs index 75f3f43..82285c4 100644 --- a/tests/search_handler_tests.rs +++ b/tests/search_handler_tests.rs @@ -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(); } -} \ No newline at end of file +} +