Skip to content

Commit e31df69

Browse files
committed
more tests
1 parent bd316e0 commit e31df69

File tree

3 files changed

+163
-0
lines changed

3 files changed

+163
-0
lines changed

crates/core/src/logic/calendar_logic.rs

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -932,4 +932,77 @@ mod tests {
932932
);
933933
assert!(result.is_ok());
934934
}
935+
936+
#[test]
937+
fn try_from_period_anno_for_year_and_month() {
938+
let sut = YearAndMonth::sample();
939+
let period_anno = PeriodAnno::YearAndMonth(sut);
940+
let outcome = YearAndMonth::try_from_period_anno(period_anno).unwrap();
941+
assert_eq!(outcome, sut);
942+
}
943+
944+
#[test]
945+
fn try_from_period_anno_for_year_month_and_fortnight() {
946+
let sut = YearMonthAndFortnight::sample();
947+
let period_anno = PeriodAnno::YearMonthAndFortnight(sut);
948+
let outcome = YearMonthAndFortnight::try_from_period_anno(period_anno).unwrap();
949+
assert_eq!(outcome, sut);
950+
}
951+
952+
#[test]
953+
fn test_quantity_in_period_throws_when_cadence_is_biweekly_and_granularity_is_month() {
954+
let target_period = YearAndMonth::january(2025);
955+
let record_of_periods_off = RecordOfPeriodsOff::new([]);
956+
let result = quantity_in_period(
957+
&target_period,
958+
Granularity::Month,
959+
Cadence::BiWeekly,
960+
&record_of_periods_off,
961+
);
962+
assert!(result.is_err());
963+
if let Err(Error::CannotInvoiceForMonthWhenCadenceIsBiWeekly) = result {
964+
// Expected error
965+
} else {
966+
panic!("Expected CannotInvoiceForMonthWhenCadenceIsBiWeekly error");
967+
}
968+
}
969+
970+
#[test]
971+
fn test_quantity_in_period_granularity_fortnight_cadence_monthly_is_two() {
972+
let target_period = YearAndMonth::january(2025);
973+
let record_of_periods_off = RecordOfPeriodsOff::new([]);
974+
let result = quantity_in_period(
975+
&target_period,
976+
Granularity::Fortnight,
977+
Cadence::Monthly,
978+
&record_of_periods_off,
979+
);
980+
assert!(result.is_ok());
981+
assert_eq!(result.unwrap(), Quantity::TWO);
982+
}
983+
984+
#[test]
985+
fn test_quantity_in_period_granularity_fortnight_cadence_biweekly_is_one() {
986+
let target_period = YearAndMonth::january(2025);
987+
let record_of_periods_off = RecordOfPeriodsOff::new([]);
988+
let result = quantity_in_period(
989+
&target_period,
990+
Granularity::Fortnight,
991+
Cadence::BiWeekly,
992+
&record_of_periods_off,
993+
);
994+
assert!(result.is_ok());
995+
assert_eq!(result.unwrap(), Quantity::ONE);
996+
}
997+
998+
#[test]
999+
fn test_one_half_earlier_second() {
1000+
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();
1003+
assert_eq!(
1004+
one_half_earlier,
1005+
YearMonthAndFortnight::year_and_month_with_half(ym, MonthHalf::First)
1006+
);
1007+
}
9351008
}

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

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -626,4 +626,56 @@ mod tests {
626626
email_settings.smtp_app_password().expose_secret()
627627
);
628628
}
629+
630+
#[test]
631+
fn test_record_expenses_with_base_path_throws_when_cadence_is_monthly_and_period_is_fortnight()
632+
{
633+
let tempdir = tempfile::tempdir().expect("Failed to create temp dir");
634+
let period = YearMonthAndFortnight::builder()
635+
.year(Year::from(2025))
636+
.month(Month::January)
637+
.half(MonthHalf::First)
638+
.build();
639+
let services_fees: ServiceFees = ServiceFees::builder()
640+
.cadence(Cadence::Monthly)
641+
.rate(Rate::daily(UnitPrice::ONE))
642+
.name("Sample Service Fees".to_owned())
643+
.build()
644+
.unwrap();
645+
save_to_disk(&services_fees, service_fees_path(tempdir.path())).unwrap();
646+
let result = record_expenses_with_base_path(
647+
&period,
648+
&[Item::sample_expense_breakfast()],
649+
tempdir.path().to_path_buf(),
650+
);
651+
assert!(
652+
result.is_err(),
653+
"Expected error when recording expenses for fortnight with monthly cadence, got: {:?}",
654+
result
655+
);
656+
}
657+
658+
#[test]
659+
fn test_record_expenses_with_base_path_throws_when_cadence_is_bi_weekly_and_period_is_year_and_month()
660+
{
661+
let tempdir = tempfile::tempdir().expect("Failed to create temp dir");
662+
let period = YearAndMonth::may(2025);
663+
let services_fees: ServiceFees = ServiceFees::builder()
664+
.cadence(Cadence::BiWeekly)
665+
.rate(Rate::daily(UnitPrice::ONE))
666+
.name("Sample Service Fees".to_owned())
667+
.build()
668+
.unwrap();
669+
save_to_disk(&services_fees, service_fees_path(tempdir.path())).unwrap();
670+
let result = record_expenses_with_base_path(
671+
&period,
672+
&[Item::sample_expense_breakfast()],
673+
tempdir.path().to_path_buf(),
674+
);
675+
assert!(
676+
result.is_err(),
677+
"Expected error when recording expenses for year and month with bi-weekly cadence, got: {:?}",
678+
result
679+
);
680+
}
629681
}

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

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -374,4 +374,42 @@ mod tests {
374374
);
375375
}
376376
}
377+
378+
#[test]
379+
fn test_to_partial_when_offset_is_year_month_and_fortnight() {
380+
let offset_period = YearMonthAndFortnight::builder()
381+
.year(2025.into())
382+
.month(Month::May)
383+
.half(MonthHalf::First)
384+
.build();
385+
let sut = Data::<YearMonthAndFortnight>::builder()
386+
.information(
387+
ProtoInvoiceInfo::builder()
388+
.offset(
389+
TimestampedInvoiceNumber::<YearMonthAndFortnight>::builder()
390+
.offset(100.into())
391+
.period(offset_period)
392+
.build(),
393+
)
394+
.build(),
395+
)
396+
.vendor(CompanyInformation::sample_vendor())
397+
.client(CompanyInformation::sample_client())
398+
.payment_info(PaymentInformation::sample())
399+
.service_fees(ServiceFees::sample())
400+
.expensed_periods(ExpensedPeriods::sample())
401+
.build();
402+
let target_period = YearMonthAndFortnight::builder()
403+
.year(2025.into())
404+
.month(Month::May)
405+
.half(MonthHalf::First)
406+
.build();
407+
let input = ValidInput::builder()
408+
.items(InvoicedItems::Service { time_off: None })
409+
.period(target_period)
410+
.build();
411+
let partial = sut.to_partial(input).unwrap();
412+
let invoice_date = partial.information().invoice_date();
413+
assert_eq!(*invoice_date, target_period.to_date_end_of_period());
414+
}
377415
}

0 commit comments

Comments
 (0)