Add booking dao implementations

This commit is contained in:
Simon Goller 2024-05-07 18:19:00 +02:00
parent 4bca60a23c
commit 71c1432fd1
10 changed files with 225 additions and 13 deletions

View file

@ -1,7 +1,8 @@
use async_trait::async_trait;
use dao::booking;
use service::{
booking::{Booking, BookingService},
ServiceError,
ServiceError, ValidationFailureItem,
};
use std::sync::Arc;
use uuid::Uuid;
@ -95,16 +96,40 @@ where
return Err(ServiceError::VersionSetOnCreate);
}
let mut validation = Vec::with_capacity(8);
if booking.created.is_some() {
validation.push(ValidationFailureItem::InvalidValue("created".into()));
}
if booking.sales_person_id == Uuid::nil() {
validation.push(ValidationFailureItem::InvalidValue(
"sales_person_id".into(),
));
}
if booking.slot_id == Uuid::nil() {
validation.push(ValidationFailureItem::InvalidValue("slot_id".into()));
}
if booking.calendar_week <= 0 {
validation.push(ValidationFailureItem::InvalidValue("calendar_week".into()));
}
if booking.calendar_week > 53 {
validation.push(ValidationFailureItem::InvalidValue("calendar_week".into()));
}
if !validation.is_empty() {
return Err(ServiceError::ValidationError(validation.into()));
}
let new_id = self.uuid_service.new_uuid("booking-id");
let new_version = self.uuid_service.new_uuid("booking-version");
let new_booking = Booking {
id: new_id,
version: new_version,
created: Some(self.clock_service.date_time_now()),
..booking.clone()
};
self.booking_dao
.create(&(&new_booking).into(), BOOKING_SERVICE_PROCESS)
.create(&(&new_booking).try_into()?, BOOKING_SERVICE_PROCESS)
.await?;
Ok(new_booking)

View file

@ -2,7 +2,8 @@ use crate::test::error_test::*;
use dao::booking::{BookingEntity, MockBookingDao};
use mockall::predicate::eq;
use service::{
booking::Booking, clock::MockClockService, uuid_service::MockUuidService, MockPermissionService,
booking::Booking, clock::MockClockService, uuid_service::MockUuidService,
MockPermissionService, ValidationFailureItem,
};
use time::{Date, Month, PrimitiveDateTime, Time};
use uuid::{uuid, Uuid};
@ -36,6 +37,10 @@ pub fn default_booking() -> Booking {
slot_id: default_slot_id(),
calendar_week: 3,
year: 2024,
created: Some(PrimitiveDateTime::new(
Date::from_calendar_date(2024, Month::January, 1).unwrap(),
Time::from_hms(0, 0, 0).unwrap(),
)),
deleted: None,
version: default_version(),
}
@ -48,6 +53,10 @@ pub fn default_booking_entity() -> BookingEntity {
slot_id: default_slot_id(),
calendar_week: 3,
year: 2024,
created: PrimitiveDateTime::new(
Date::from_calendar_date(2024, Month::January, 1).unwrap(),
Time::from_hms(0, 0, 0).unwrap(),
),
deleted: None,
version: default_version(),
}
@ -186,7 +195,13 @@ async fn test_create() {
let mut deps = build_dependencies(true, "hr");
deps.booking_dao
.expect_create()
.with(eq(default_booking_entity()), eq("booking-service"))
.with(
eq(BookingEntity {
created: generate_default_datetime(),
..default_booking_entity()
}),
eq("booking-service"),
)
.returning(|_, _| Ok(()));
deps.uuid_service
.expect_new_uuid()
@ -202,13 +217,20 @@ async fn test_create() {
&Booking {
id: Uuid::nil(),
version: Uuid::nil(),
created: None,
..default_booking()
},
(),
)
.await;
assert!(result.is_ok());
assert_eq!(result.unwrap(), default_booking());
assert_eq!(
result.unwrap(),
Booking {
created: Some(generate_default_datetime()),
..default_booking()
}
);
}
#[tokio::test]
@ -260,6 +282,27 @@ async fn test_create_with_version() {
test_zero_version_error(&result);
}
#[tokio::test]
async fn test_create_with_created_fail() {
let deps = build_dependencies(true, "hr");
let service = deps.build_service();
let result = service
.create(
&Booking {
id: Uuid::nil(),
version: Uuid::nil(),
..default_booking()
},
(),
)
.await;
test_validation_error(
&result,
&ValidationFailureItem::InvalidValue("created".into()),
1,
);
}
#[tokio::test]
async fn test_delete_no_permission() {
let deps = build_dependencies(false, "hr");
@ -291,10 +334,7 @@ async fn test_delete() {
.expect_update()
.with(
eq(BookingEntity {
deleted: Some(PrimitiveDateTime::new(
Date::from_calendar_date(2063, Month::April, 5).unwrap(),
Time::from_hms(23, 42, 0).unwrap(),
)),
deleted: Some(generate_default_datetime()),
version: alternate_version(),
..default_booking_entity()
}),

View file

@ -1,4 +1,5 @@
use service::ValidationFailureItem;
use time::{Date, Month, PrimitiveDateTime, Time};
use uuid::Uuid;
pub fn test_forbidden<T>(result: &Result<T, service::ServiceError>) {
@ -106,3 +107,10 @@ pub fn test_validation_error<T>(
panic!("Expected validation error");
}
}
pub fn generate_default_datetime() -> PrimitiveDateTime {
PrimitiveDateTime::new(
Date::from_calendar_date(2063, Month::April, 5).unwrap(),
Time::from_hms(23, 42, 0).unwrap(),
)
}