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
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
files: lcov.info
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)]
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();
}
}
}