Skip to content

Commit b044a46

Browse files
committed
more tests
1 parent e31df69 commit b044a46

File tree

5 files changed

+43
-32
lines changed

5 files changed

+43
-32
lines changed

crates/core/src/logic/calendar_logic.rs

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -996,10 +996,24 @@ mod tests {
996996
}
997997

998998
#[test]
999-
fn test_one_half_earlier_second() {
999+
fn ymf_one_half_earlier_month_half_first_half() {
10001000
let ym = YearAndMonth::january(2025);
1001-
let month = YearMonthAndFortnight::year_and_month_with_half(ym, MonthHalf::Second);
1002-
let one_half_earlier = month.one_half_earlier();
1001+
let ymf = YearMonthAndFortnight::year_and_month_with_half(ym, MonthHalf::First);
1002+
let one_half_earlier = ymf.one_half_earlier();
1003+
assert_eq!(
1004+
one_half_earlier,
1005+
YearMonthAndFortnight::year_and_month_with_half(
1006+
YearAndMonth::december(2024),
1007+
MonthHalf::Second
1008+
)
1009+
);
1010+
}
1011+
1012+
#[test]
1013+
fn ymf_one_half_earlier_month_half_second_half() {
1014+
let ym = YearAndMonth::january(2025);
1015+
let ymf = YearMonthAndFortnight::year_and_month_with_half(ym, MonthHalf::Second);
1016+
let one_half_earlier = ymf.one_half_earlier();
10031017
assert_eq!(
10041018
one_half_earlier,
10051019
YearMonthAndFortnight::year_and_month_with_half(ym, MonthHalf::First)

crates/core/src/logic/command/command.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -646,7 +646,7 @@ mod tests {
646646
let result = record_expenses_with_base_path(
647647
&period,
648648
&[Item::sample_expense_breakfast()],
649-
tempdir.path().to_path_buf(),
649+
tempdir.path(),
650650
);
651651
assert!(
652652
result.is_err(),
@@ -670,7 +670,7 @@ mod tests {
670670
let result = record_expenses_with_base_path(
671671
&period,
672672
&[Item::sample_expense_breakfast()],
673-
tempdir.path().to_path_buf(),
673+
tempdir.path(),
674674
);
675675
assert!(
676676
result.is_err(),

crates/core/src/models/data/data.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -59,12 +59,10 @@ impl<Period: IsPeriod> Data<Period> {
5959
cadence: Cadence,
6060
time_off: &Option<TimeOff>,
6161
) -> Result<Quantity> {
62-
let quantity_in_period = quantity_in_period(
63-
target_period,
64-
self.service_fees().rate().granularity(),
65-
cadence,
66-
self.information().record_of_periods_off(),
67-
)?;
62+
let granularity = self.service_fees().rate().granularity();
63+
let periods_off = self.information().record_of_periods_off();
64+
let quantity_in_period =
65+
quantity_in_period(target_period, granularity, cadence, periods_off)?;
6866
let billable_quantity = quantity_in_period - time_off.map(|d| *d).unwrap_or(Quantity::ZERO);
6967
Ok(billable_quantity)
7068
}

crates/core/src/models/data/submodels/cadence.rs

Lines changed: 14 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,13 @@ pub enum Cadence {
1616

1717
impl Cadence {
1818
pub fn validate(&self, granularity: impl Into<Granularity>) -> Result<()> {
19+
use Cadence::*;
20+
use Granularity::*;
1921
let granularity = granularity.into();
2022
match (self, granularity) {
21-
(Self::BiWeekly, Granularity::Month) => {
22-
Err(Error::CannotInvoiceForMonthWhenCadenceIsBiWeekly)
23-
}
24-
(Self::BiWeekly, Granularity::Fortnight | Granularity::Day | Granularity::Hour) => {
25-
Ok(())
26-
}
27-
(
28-
Self::Monthly,
29-
Granularity::Fortnight | Granularity::Day | Granularity::Hour | Granularity::Month,
30-
) => Ok(()),
23+
(BiWeekly, Month) => Err(Error::CannotInvoiceForMonthWhenCadenceIsBiWeekly),
24+
(BiWeekly, Fortnight | Day | Hour) => Ok(()),
25+
(Monthly, Fortnight | Day | Hour | Month) => Ok(()),
3126
}
3227
}
3328
}
@@ -61,15 +56,15 @@ mod tests {
6156
}
6257

6358
#[test]
64-
fn validate() {
65-
assert!(Sut::sample().validate(Granularity::Month).is_ok());
66-
assert!(Sut::sample().validate(Granularity::Fortnight).is_ok());
67-
assert!(Sut::sample().validate(Granularity::Day).is_ok());
68-
assert!(Sut::sample().validate(Granularity::Hour).is_ok());
59+
fn validate_successful() {
60+
assert!(Sut::Monthly.validate(Granularity::Month).is_ok());
61+
assert!(Sut::Monthly.validate(Granularity::Fortnight).is_ok());
62+
assert!(Sut::Monthly.validate(Granularity::Day).is_ok());
63+
assert!(Sut::Monthly.validate(Granularity::Hour).is_ok());
6964

70-
assert!(Sut::sample_other().validate(Granularity::Month).is_err());
71-
assert!(Sut::sample_other().validate(Granularity::Fortnight).is_ok());
72-
assert!(Sut::sample_other().validate(Granularity::Day).is_ok());
73-
assert!(Sut::sample_other().validate(Granularity::Hour).is_ok());
65+
assert!(Sut::BiWeekly.validate(Granularity::Month).is_err());
66+
assert!(Sut::BiWeekly.validate(Granularity::Fortnight).is_ok());
67+
assert!(Sut::BiWeekly.validate(Granularity::Day).is_ok());
68+
assert!(Sut::BiWeekly.validate(Granularity::Hour).is_ok());
7469
}
7570
}

crates/core/src/models/data/submodels/service_fees.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,15 @@ pub struct ServiceFees {
2020
#[bon]
2121
impl ServiceFees {
2222
#[builder]
23-
pub fn new(name: String, rate: impl Into<Rate>, cadence: Cadence) -> Result<Self, Error> {
23+
pub fn new(
24+
name: impl AsRef<str>,
25+
rate: impl Into<Rate>,
26+
cadence: Cadence,
27+
) -> Result<Self, Error> {
2428
let rate = rate.into();
2529
cadence.validate(rate.granularity())?;
2630
Ok(Self {
27-
name,
31+
name: name.as_ref().to_owned(),
2832
rate,
2933
cadence,
3034
})

0 commit comments

Comments
 (0)