Move the error type test functions into its own module
This commit is contained in:
parent
8f378472ea
commit
9a367c9260
4 changed files with 112 additions and 107 deletions
108
service_impl/src/test/error_test.rs
Normal file
108
service_impl/src/test/error_test.rs
Normal file
|
|
@ -0,0 +1,108 @@
|
||||||
|
use service::ValidationFailureItem;
|
||||||
|
use uuid::Uuid;
|
||||||
|
|
||||||
|
pub fn test_forbidden<T>(result: &Result<T, service::ServiceError>) {
|
||||||
|
if let Err(service::ServiceError::Forbidden) = result {
|
||||||
|
// All good
|
||||||
|
} else {
|
||||||
|
panic!("Expected forbidden error");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn test_not_found<T>(result: &Result<T, service::ServiceError>, target_id: &Uuid) {
|
||||||
|
if let Err(service::ServiceError::EntityNotFound(id)) = result {
|
||||||
|
assert_eq!(
|
||||||
|
id, target_id,
|
||||||
|
"Expected entity {} not found but got {}",
|
||||||
|
target_id, id
|
||||||
|
);
|
||||||
|
} else {
|
||||||
|
panic!("Expected entity {} not found error", target_id);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn test_zero_id_error<T>(result: &Result<T, service::ServiceError>) {
|
||||||
|
if let Err(service::ServiceError::IdSetOnCreate) = result {
|
||||||
|
} else {
|
||||||
|
panic!("Expected id set on create error");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn test_zero_version_error<T>(result: &Result<T, service::ServiceError>) {
|
||||||
|
if let Err(service::ServiceError::VersionSetOnCreate) = result {
|
||||||
|
} else {
|
||||||
|
panic!("Expected version set on create error");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn test_overlapping_time_range_error<T>(result: &Result<T, service::ServiceError>) {
|
||||||
|
if let Err(service::ServiceError::OverlappingTimeRange) = result {
|
||||||
|
} else {
|
||||||
|
panic!("Expected overlapping time range error");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn test_time_order_wrong<T>(result: &Result<T, service::ServiceError>) {
|
||||||
|
if let Err(service::ServiceError::TimeOrderWrong(_from, _to)) = result {
|
||||||
|
} else {
|
||||||
|
panic!("Expected time order failure");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn test_date_order_wrong<T>(result: &Result<T, service::ServiceError>) {
|
||||||
|
if let Err(service::ServiceError::DateOrderWrong(_from, _to)) = result {
|
||||||
|
} else {
|
||||||
|
panic!("Expected date order failure");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn test_conflicts<T>(
|
||||||
|
result: &Result<T, service::ServiceError>,
|
||||||
|
target_id: &Uuid,
|
||||||
|
expected_version: &Uuid,
|
||||||
|
actual_version: &Uuid,
|
||||||
|
) {
|
||||||
|
if let Err(service::ServiceError::EntityConflicts(
|
||||||
|
err_id,
|
||||||
|
err_expected_version,
|
||||||
|
err_actual_version,
|
||||||
|
)) = result
|
||||||
|
{
|
||||||
|
assert_eq!(
|
||||||
|
err_id, target_id,
|
||||||
|
"Expected entity {} conflicts but got {}",
|
||||||
|
target_id, err_id
|
||||||
|
);
|
||||||
|
|
||||||
|
assert_eq!(
|
||||||
|
expected_version, err_expected_version,
|
||||||
|
"Expected expected version {} but got {}",
|
||||||
|
expected_version, err_expected_version
|
||||||
|
);
|
||||||
|
assert_eq!(
|
||||||
|
actual_version, err_actual_version,
|
||||||
|
"Expected actual version {} but got {}",
|
||||||
|
actual_version, err_actual_version
|
||||||
|
);
|
||||||
|
} else {
|
||||||
|
panic!("Expected entity {} conflicts error", target_id);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn test_validation_error(
|
||||||
|
result: &Result<(), service::ServiceError>,
|
||||||
|
validation_failure: &ValidationFailureItem,
|
||||||
|
fail_count: usize,
|
||||||
|
) {
|
||||||
|
if let Err(service::ServiceError::ValidationError(validation_failure_items)) = result {
|
||||||
|
if !validation_failure_items.contains(validation_failure) {
|
||||||
|
panic!(
|
||||||
|
"Validation failure not found: {:?} in {:?}",
|
||||||
|
validation_failure, validation_failure_items
|
||||||
|
);
|
||||||
|
}
|
||||||
|
assert_eq!(fail_count, validation_failure_items.len());
|
||||||
|
} else {
|
||||||
|
panic!("Expected validation error");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -1,4 +1,6 @@
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
|
pub mod error_test;
|
||||||
|
#[cfg(test)]
|
||||||
mod permission_test;
|
mod permission_test;
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
pub mod slot;
|
pub mod slot;
|
||||||
|
|
|
||||||
|
|
@ -1,3 +1,4 @@
|
||||||
|
use crate::test::error_test::*;
|
||||||
use crate::*;
|
use crate::*;
|
||||||
use mockall::predicate::eq;
|
use mockall::predicate::eq;
|
||||||
use service::PermissionService;
|
use service::PermissionService;
|
||||||
|
|
@ -20,14 +21,6 @@ fn generate_dependencies_mocks_permission(
|
||||||
(permission_dao, user_service)
|
(permission_dao, user_service)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn test_forbidden<T>(result: &Result<T, service::ServiceError>) {
|
|
||||||
if let Err(service::ServiceError::Forbidden) = result {
|
|
||||||
// All good
|
|
||||||
} else {
|
|
||||||
panic!("Expected forbidden error");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[tokio::test]
|
#[tokio::test]
|
||||||
async fn test_check_permission() {
|
async fn test_check_permission() {
|
||||||
let (permission_dao, user_service) = generate_dependencies_mocks_permission(true, "hello");
|
let (permission_dao, user_service) = generate_dependencies_mocks_permission(true, "hello");
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,7 @@
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
|
|
||||||
use crate::slot::*;
|
use crate::slot::*;
|
||||||
use crate::test::permission_test::test_forbidden;
|
use crate::test::error_test::*;
|
||||||
use dao::slot::{MockSlotDao, SlotEntity};
|
use dao::slot::{MockSlotDao, SlotEntity};
|
||||||
use mockall::predicate::eq;
|
use mockall::predicate::eq;
|
||||||
use service::{
|
use service::{
|
||||||
|
|
@ -47,104 +47,6 @@ pub fn generate_default_slot_entity() -> SlotEntity {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn test_not_found<T>(result: &Result<T, service::ServiceError>, target_id: &Uuid) {
|
|
||||||
if let Err(service::ServiceError::EntityNotFound(id)) = result {
|
|
||||||
assert_eq!(
|
|
||||||
id, target_id,
|
|
||||||
"Expected entity {} not found but got {}",
|
|
||||||
target_id, id
|
|
||||||
);
|
|
||||||
} else {
|
|
||||||
panic!("Expected entity {} not found error", target_id);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn test_zero_id_error<T>(result: &Result<T, service::ServiceError>) {
|
|
||||||
if let Err(service::ServiceError::IdSetOnCreate) = result {
|
|
||||||
} else {
|
|
||||||
panic!("Expected id set on create error");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn test_zero_version_error<T>(result: &Result<T, service::ServiceError>) {
|
|
||||||
if let Err(service::ServiceError::VersionSetOnCreate) = result {
|
|
||||||
} else {
|
|
||||||
panic!("Expected version set on create error");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn test_overlapping_time_range_error<T>(result: &Result<T, service::ServiceError>) {
|
|
||||||
if let Err(service::ServiceError::OverlappingTimeRange) = result {
|
|
||||||
} else {
|
|
||||||
panic!("Expected overlapping time range error");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn test_time_order_wrong<T>(result: &Result<T, service::ServiceError>) {
|
|
||||||
if let Err(service::ServiceError::TimeOrderWrong(_from, _to)) = result {
|
|
||||||
} else {
|
|
||||||
panic!("Expected time order failure");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn test_date_order_wrong<T>(result: &Result<T, service::ServiceError>) {
|
|
||||||
if let Err(service::ServiceError::DateOrderWrong(_from, _to)) = result {
|
|
||||||
} else {
|
|
||||||
panic!("Expected date order failure");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn test_conflicts<T>(
|
|
||||||
result: &Result<T, service::ServiceError>,
|
|
||||||
target_id: &Uuid,
|
|
||||||
expected_version: &Uuid,
|
|
||||||
actual_version: &Uuid,
|
|
||||||
) {
|
|
||||||
if let Err(service::ServiceError::EntityConflicts(
|
|
||||||
err_id,
|
|
||||||
err_expected_version,
|
|
||||||
err_actual_version,
|
|
||||||
)) = result
|
|
||||||
{
|
|
||||||
assert_eq!(
|
|
||||||
err_id, target_id,
|
|
||||||
"Expected entity {} conflicts but got {}",
|
|
||||||
target_id, err_id
|
|
||||||
);
|
|
||||||
|
|
||||||
assert_eq!(
|
|
||||||
expected_version, err_expected_version,
|
|
||||||
"Expected expected version {} but got {}",
|
|
||||||
expected_version, err_expected_version
|
|
||||||
);
|
|
||||||
assert_eq!(
|
|
||||||
actual_version, err_actual_version,
|
|
||||||
"Expected actual version {} but got {}",
|
|
||||||
actual_version, err_actual_version
|
|
||||||
);
|
|
||||||
} else {
|
|
||||||
panic!("Expected entity {} conflicts error", target_id);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn test_validation_error(
|
|
||||||
result: &Result<(), service::ServiceError>,
|
|
||||||
validation_failure: &ValidationFailureItem,
|
|
||||||
fail_count: usize,
|
|
||||||
) {
|
|
||||||
if let Err(service::ServiceError::ValidationError(validation_failure_items)) = result {
|
|
||||||
if !validation_failure_items.contains(validation_failure) {
|
|
||||||
panic!(
|
|
||||||
"Validation failure not found: {:?} in {:?}",
|
|
||||||
validation_failure, validation_failure_items
|
|
||||||
);
|
|
||||||
}
|
|
||||||
assert_eq!(fail_count, validation_failure_items.len());
|
|
||||||
} else {
|
|
||||||
panic!("Expected validation error");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub struct SlotServiceDependencies {
|
pub struct SlotServiceDependencies {
|
||||||
pub slot_dao: MockSlotDao,
|
pub slot_dao: MockSlotDao,
|
||||||
pub permission_service: MockPermissionService,
|
pub permission_service: MockPermissionService,
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue