From 0d5e8f07b82cf53383284dfbf5deb494de2e508c Mon Sep 17 00:00:00 2001 From: Jim Crossley Date: Fri, 15 Mar 2024 14:14:42 -0400 Subject: [PATCH] Use test-log to handle logger init stuff Signed-off-by: Jim Crossley --- modules/importer/Cargo.toml | 2 +- modules/importer/src/test.rs | 99 +++++++++++++++++------------------- 2 files changed, 49 insertions(+), 52 deletions(-) diff --git a/modules/importer/Cargo.toml b/modules/importer/Cargo.toml index 633eccac8..05125398c 100644 --- a/modules/importer/Cargo.toml +++ b/modules/importer/Cargo.toml @@ -20,4 +20,4 @@ utoipa = { version = "4", features = ["actix_extras"] } uuid = { version = "1.7.0", features = ["v4"] } [dev-dependencies] -env_logger = "0.11" +test-log = { version = "0.2.15", features = ["env_logger", "trace"] } diff --git a/modules/importer/src/test.rs b/modules/importer/src/test.rs index c4f4d1169..358e7d851 100644 --- a/modules/importer/src/test.rs +++ b/modules/importer/src/test.rs @@ -3,39 +3,38 @@ use super::model::ImportConfiguration; use actix_web::{ http::{header, StatusCode}, - test, App, + test as actix, App, }; use serde_json::json; +use test_log::test; use trustify_common::db::Database; -#[actix_web::test] +#[test(actix_web::test)] async fn test_default() { - let _ = env_logger::try_init(); - let db = Database::for_test("test_default").await.unwrap(); let app = - test::init_service(App::new().configure(|svc| super::endpoints::configure(svc, db))).await; + actix::init_service(App::new().configure(|svc| super::endpoints::configure(svc, db))).await; // create one - let req = test::TestRequest::post() + let req = actix::TestRequest::post() .uri("/api/v1/importer/foo") .set_json(json!({"foo":"bar"})) .to_request(); - let resp = test::call_service(&app, req).await; + let resp = actix::call_service(&app, req).await; assert_eq!(resp.status(), StatusCode::CREATED); // now list all - let req = test::TestRequest::get() + let req = actix::TestRequest::get() .uri("/api/v1/importer") .to_request(); - let resp = test::call_service(&app, req).await; + let resp = actix::call_service(&app, req).await; assert_eq!(resp.status(), StatusCode::OK); - let result: Vec = test::read_body_json(resp).await; + let result: Vec = actix::read_body_json(resp).await; assert_eq!( result, vec![ImportConfiguration { @@ -46,24 +45,24 @@ async fn test_default() { // update it - let req = test::TestRequest::put() + let req = actix::TestRequest::put() .uri("/api/v1/importer/foo") .set_json(json!({"foo":"baz"})) .to_request(); - let resp = test::call_service(&app, req).await; + let resp = actix::call_service(&app, req).await; assert_eq!(resp.status(), StatusCode::NO_CONTENT); // get it - let req = test::TestRequest::get() + let req = actix::TestRequest::get() .uri("/api/v1/importer/foo") .to_request(); - let resp = test::call_service(&app, req).await; + let resp = actix::call_service(&app, req).await; assert_eq!(resp.status(), StatusCode::OK); - let result: ImportConfiguration = test::read_body_json(resp).await; + let result: ImportConfiguration = actix::read_body_json(resp).await; assert_eq!( result, ImportConfiguration { @@ -74,65 +73,63 @@ async fn test_default() { // delete it - let req = test::TestRequest::delete() + let req = actix::TestRequest::delete() .uri("/api/v1/importer/foo") .to_request(); - let resp = test::call_service(&app, req).await; + let resp = actix::call_service(&app, req).await; assert_eq!(resp.status(), StatusCode::NO_CONTENT); // get none - let req = test::TestRequest::get() + let req = actix::TestRequest::get() .uri("/api/v1/importer/foo") .to_request(); - let resp = test::call_service(&app, req).await; + let resp = actix::call_service(&app, req).await; assert_eq!(resp.status(), StatusCode::NOT_FOUND); } -#[actix_web::test] +#[test(actix_web::test)] async fn test_oplock() { - let _ = env_logger::try_init(); - let db = Database::for_test("test_oplock").await.unwrap(); let app = - test::init_service(App::new().configure(|svc| super::endpoints::configure(svc, db))).await; + actix::init_service(App::new().configure(|svc| super::endpoints::configure(svc, db))).await; // create one - let req = test::TestRequest::post() + let req = actix::TestRequest::post() .uri("/api/v1/importer/foo") .set_json(json!({"foo":"bar"})) .to_request(); - let resp = test::call_service(&app, req).await; + let resp = actix::call_service(&app, req).await; assert_eq!(resp.status(), StatusCode::CREATED); // update it (no lock) - let req = test::TestRequest::put() + let req = actix::TestRequest::put() .uri("/api/v1/importer/foo") .set_json(json!({"foo":"baz"})) .to_request(); - let resp = test::call_service(&app, req).await; + let resp = actix::call_service(&app, req).await; assert_eq!(resp.status(), StatusCode::NO_CONTENT); // get it - let req = test::TestRequest::get() + let req = actix::TestRequest::get() .uri("/api/v1/importer/foo") .to_request(); - let resp = test::call_service(&app, req).await; + let resp = actix::call_service(&app, req).await; assert_eq!(resp.status(), StatusCode::OK); let etag = resp.headers().get(header::ETAG); assert!(etag.is_some()); let etag = etag.cloned().unwrap(); - let result: ImportConfiguration = test::read_body_json(resp).await; + let result: ImportConfiguration = actix::read_body_json(resp).await; assert_eq!( result, ImportConfiguration { @@ -143,25 +140,25 @@ async fn test_oplock() { // update it (with lock) - let req = test::TestRequest::put() + let req = actix::TestRequest::put() .uri("/api/v1/importer/foo") .set_json(json!({"foo":"buz"})) .append_header((header::IF_MATCH, etag.clone())) .to_request(); - let resp = test::call_service(&app, req).await; + let resp = actix::call_service(&app, req).await; assert_eq!(resp.status(), StatusCode::NO_CONTENT); // get it - let req = test::TestRequest::get() + let req = actix::TestRequest::get() .uri("/api/v1/importer/foo") .to_request(); - let resp = test::call_service(&app, req).await; + let resp = actix::call_service(&app, req).await; assert_eq!(resp.status(), StatusCode::OK); - let result: ImportConfiguration = test::read_body_json(resp).await; + let result: ImportConfiguration = actix::read_body_json(resp).await; assert_eq!( result, ImportConfiguration { @@ -172,33 +169,33 @@ async fn test_oplock() { // update it (with broken lock) - let req = test::TestRequest::put() + let req = actix::TestRequest::put() .uri("/api/v1/importer/foo") .set_json(json!({"foo":"boz"})) .append_header((header::IF_MATCH, etag.clone())) .to_request(); - let resp = test::call_service(&app, req).await; + let resp = actix::call_service(&app, req).await; assert_eq!(resp.status(), StatusCode::PRECONDITION_FAILED); // update it (with wrong name) - let req = test::TestRequest::put() + let req = actix::TestRequest::put() .uri("/api/v1/importer/foo2") .set_json(json!({"foo":"boz"})) .append_header((header::IF_MATCH, etag.clone())) .to_request(); - let resp = test::call_service(&app, req).await; + let resp = actix::call_service(&app, req).await; assert_eq!(resp.status(), StatusCode::NOT_FOUND); // get it (must not change) - let req = test::TestRequest::get() + let req = actix::TestRequest::get() .uri("/api/v1/importer/foo") .to_request(); - let resp = test::call_service(&app, req).await; + let resp = actix::call_service(&app, req).await; assert_eq!(resp.status(), StatusCode::OK); let old_etag = etag; @@ -207,7 +204,7 @@ async fn test_oplock() { let etag = etag.cloned().unwrap(); assert_ne!(old_etag, etag); - let result: ImportConfiguration = test::read_body_json(resp).await; + let result: ImportConfiguration = actix::read_body_json(resp).await; assert_eq!( result, ImportConfiguration { @@ -218,24 +215,24 @@ async fn test_oplock() { // delete it (wrong lock) - let req = test::TestRequest::delete() + let req = actix::TestRequest::delete() .uri("/api/v1/importer/foo") .append_header((header::IF_MATCH, old_etag.clone())) .to_request(); - let resp = test::call_service(&app, req).await; + let resp = actix::call_service(&app, req).await; assert_eq!(resp.status(), StatusCode::NO_CONTENT); // get it (must still be there) - let req = test::TestRequest::get() + let req = actix::TestRequest::get() .uri("/api/v1/importer/foo") .to_request(); - let resp = test::call_service(&app, req).await; + let resp = actix::call_service(&app, req).await; assert_eq!(resp.status(), StatusCode::OK); - let result: ImportConfiguration = test::read_body_json(resp).await; + let result: ImportConfiguration = actix::read_body_json(resp).await; assert_eq!( result, ImportConfiguration { @@ -246,20 +243,20 @@ async fn test_oplock() { // delete it (correct lock) - let req = test::TestRequest::delete() + let req = actix::TestRequest::delete() .uri("/api/v1/importer/foo") .append_header((header::IF_MATCH, etag.clone())) .to_request(); - let resp = test::call_service(&app, req).await; + let resp = actix::call_service(&app, req).await; assert_eq!(resp.status(), StatusCode::NO_CONTENT); // get none - let req = test::TestRequest::get() + let req = actix::TestRequest::get() .uri("/api/v1/importer/foo") .to_request(); - let resp = test::call_service(&app, req).await; + let resp = actix::call_service(&app, req).await; assert_eq!(resp.status(), StatusCode::NOT_FOUND); }