Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use test-log to handle logger init stuff #85

Merged
merged 1 commit into from
Mar 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion modules/importer/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"] }
99 changes: 48 additions & 51 deletions modules/importer/src/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<ImportConfiguration> = test::read_body_json(resp).await;
let result: Vec<ImportConfiguration> = actix::read_body_json(resp).await;
assert_eq!(
result,
vec![ImportConfiguration {
Expand All @@ -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 {
Expand All @@ -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 {
Expand All @@ -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 {
Expand All @@ -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;
Expand All @@ -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 {
Expand All @@ -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 {
Expand All @@ -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);
}
Loading