refactor: testing and coverage

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

View File

@ -1,73 +1,23 @@
name: Coverage name: Coverage
on: on: [pull_request, push]
push:
# branches:
# - develop
# pull_request_target:
# branches:
# - develop
env:
CARGO_TERM_COLOR: always
jobs: jobs:
report: coverage:
name: Report
environment: coverage
runs-on: ubuntu-latest runs-on: ubuntu-latest
env: env:
CARGO_INCREMENTAL: "0" CARGO_TERM_COLOR: always
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"
steps: steps:
- id: checkout_push - uses: actions/checkout@v4
if: github.event_name == 'push' - name: Install Rust
name: Checkout Repository (Push) run: rustup update stable
uses: actions/checkout@v4 - name: Install cargo-llvm-cov
uses: taiki-e/install-action@cargo-llvm-cov
- id: checkout_pull_request_target - name: Generate code coverage
if: github.event_name == 'pull_request_target' run: cargo llvm-cov --all-features --workspace --lcov --output-path lcov.info
name: Checkout Repository (Pull Request Target) - name: Upload coverage to Codecov
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: codecov/codecov-action@v3 uses: codecov/codecov-action@v3
with: with:
token: ${{ secrets.CODECOV_TOKEN }} files: lcov.info
files: ${{ steps.coverage.outputs.report }}
verbose: true
fail_ci_if_error: true fail_ci_if_error: true

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