Skip to content

Commit

Permalink
Improve date tests and make it clearer how to create and use Date v…
Browse files Browse the repository at this point in the history
…alues
  • Loading branch information
ushkarev committed Jan 26, 2024
1 parent 16a605c commit fae9944
Show file tree
Hide file tree
Showing 9 changed files with 58 additions and 26 deletions.
1 change: 1 addition & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ max_line_length = 120
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true
end_of_line = lf

[*.{json,yaml,yml}]
indent_size = 2
1 change: 1 addition & 0 deletions .github/CODEOWNERS
Validating CODEOWNERS rules …
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* @ministryofjustice/rust
12 changes: 0 additions & 12 deletions .github/workflows/pipeline.yml
Original file line number Diff line number Diff line change
Expand Up @@ -48,18 +48,6 @@ jobs:
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Set version from release tag
run: |
release_tag='${{ github.event.release.tag_name }}'
echo "Release tag $release_tag"
echo $release_tag | grep -E '^[0-9]+\.[0-9]+\.[0-9]+$' || {
echo 'Release tag must be in the form \d+\.\d+\.\d+'
false
}
version=${release_tag}
echo "Will publish version $version"
grep -q 'version = "'$version'"' Cargo.toml
echo "version=$version" >> "$GITHUB_ENV"
- name: Install rust toolchain
uses: dtolnay/rust-toolchain@stable
- name: Publish to crates.io
Expand Down
3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ exclude = [
"Justfile",
]

[package.metadata.docs.rs]
targets = []

[features]
default = ["chrono"]
chrono = ["dep:chrono"]
Expand Down
11 changes: 8 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ division (`Some(Division)`) or only those that are **common** to all divisions f
Using the library
-----------------

![github](https://github.com/ministryofjustice/govuk-bank-holidays-rs/actions/workflows/pipeline.yml/badge.svg?branch=main)
![crates.io](https://img.shields.io/crates/v/govuk-bank-holidays)
![docs.rs](https://img.shields.io/docsrs/govuk-bank-holidays)

Add to your project with:

```shell
Expand All @@ -46,13 +50,14 @@ TODO
----

- Better tests, coverage
- Optionally merge in older known bank holidays into newly-downloaded GOV.UK data? Cached data starts in 2012,
but currently GOV.UK provides nothing before 2018.
- Improve GitHub Actions pipeline
- Publish to docs.rs
- Put test, lint & publish steps into 1 job for better reuse of downloaded toolchain and build caches?
- Use `cargo-semver-checks`
- Performance improvements (particularly around memory and iterators)
- Relax rust version restriction (MSRV)?
- Can `DataSource` be made private, exposing methods on `LoadDataSource` trait or elsewhere?
- Optionally merge in older known bank holidays into newly-downloaded GOV.UK data? Cached data starts in 2012,
but currently GOV.UK provides nothing before 2018.
- Allow for unknown “divisions”? Make enum non-exhaustive?

References
Expand Down
2 changes: 1 addition & 1 deletion src/data_source/cached.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ impl Cached {
pub fn cached_data_source() -> DataSource {
const CACHED_DATA: &[u8] = include_bytes!("bank-holidays.json");
DataSource::try_from_json(CACHED_DATA)
.expect("Cached data is invalid")
.expect("cached data should be valid")
}
}
38 changes: 35 additions & 3 deletions src/dates.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ impl Date {
/// Today’s date.
#[cfg(feature = "time")]
pub fn today() -> Date {
time::OffsetDateTime::now_local().expect("Cannot get now in local timezone").date().into()
time::OffsetDateTime::now_local().expect("cannot get now in local timezone").date().into()
}

/// Create a date from day, month and year integers.
Expand Down Expand Up @@ -216,9 +216,9 @@ mod tests {
#[test]
fn dates() {
let expected = Date::try_from_components(2023, 2, 3)
.expect("Date could not be created");
.expect("date should be valid");
let parsed: Date = serde_json::from_value(serde_json::Value::String("2023-02-03".to_owned()))
.expect("Date could not be parsed");
.expect("date should be valid");
assert_eq!(parsed, expected);

let end_of_january = parsed.previous_day().previous_day().previous_day();
Expand All @@ -228,4 +228,36 @@ mod tests {
assert_eq!(end_of_january.weekday(), Weekday::Tuesday);
assert_eq!(end_of_january.into_components(), (2023, 1, 31));
}

#[cfg(feature = "chrono")]
#[test]
fn chrono() {
let date = DateImpl::from_ymd_opt(2024, 1, 26)
.expect("date should be valid");
let date: Date = date.into();
assert_eq!(date.year(), 2024);
assert_eq!(date.month(), 1);
assert_eq!(date.day(), 26);
let date: DateImpl = date.into();
assert_eq!(date.year(), 2024);
assert_eq!(date.month(), 1);
assert_eq!(date.day(), 26);
}

#[cfg(feature = "time")]
#[test]
fn time() {
use time::Month;

let date = DateImpl::from_calendar_date(2024, Month::January, 26)
.expect("date should be valid");
let date: Date = date.into();
assert_eq!(date.year(), 2024);
assert_eq!(date.month(), 1);
assert_eq!(date.day(), 26);
let date: DateImpl = date.into();
assert_eq!(date.year(), 2024);
assert_eq!(date.month(), Month::January);
assert_eq!(date.day(), 26);
}
}
14 changes: 8 additions & 6 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@
//! ```no_run
//! use govuk_bank_holidays::prelude::*;
//!
//! # async fn demo(date: Date) {
//! # async fn demo() {
//! let date = Date::today();
//!
//! // load bank holidays from GOV.UK
//! let calendar = BankHolidayCalendar::load().await;
//!
Expand Down Expand Up @@ -77,20 +79,20 @@ mod tests {

fn test(calendar: BankHolidayCalendar<MonToFriWorkDays>) {
let date = Date::try_from_components(2023, 1, 10)
.expect("Date should be valid");
.expect("date should be valid");
assert!(calendar.is_work_day(date, None));
assert!(!calendar.is_holiday(date, None));

let date = Date::try_from_components(2022, 1, 1)
.expect("Date should be valid");
.expect("date should be valid");
let holidays = calendar.iter_holidays_after(date, Some(Division::EnglandAndWales));
for bank_holiday in holidays {
// eprintln!("Holidays after 1/1/22: {:?}", bank_holiday);
assert!(bank_holiday.date > date);
}

let date = Date::try_from_components(2022, 1, 7)
.expect("Date should be valid");
.expect("date should be valid");
let mut work_days = calendar.iter_work_days_before(date, Some(Division::EnglandAndWales));
for _ in 0..10 {
let work_day = work_days.next().unwrap();
Expand All @@ -111,7 +113,7 @@ mod tests {
#[ignore]
async fn requested() {
let data_source = Reqwest::default().load_data_source().await
.expect("Bank holidays did not load");
.expect("bank holidays should load");
let calendar = BankHolidayCalendar::new(data_source);
test(calendar);
}
Expand All @@ -130,7 +132,7 @@ mod tests {

// check days of whole month are correctly flagged as work days or not
let mut date = Date::try_from_components(2024, 2, 1)
.expect("Date should be valid");
.expect("date should be valid");
let work_days_february_2024 = std::iter::from_fn(move || {
let is_work_day = calendar.is_work_day(date, Some(Division::Scotland));
date = date.next_day();
Expand Down
2 changes: 1 addition & 1 deletion src/work_days.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ mod tests {
#[test]
fn mon_to_fri() {
let mut date = Date::try_from_components(2024, 1, 1)
.expect("Date should be valid");
.expect("date should be valid");
let weekdays_january_2024 = std::iter::from_fn(move || {
let is_weekday = WorkDays::is_work_day(&MonToFriWorkDays, date);
date = date.next_day();
Expand Down

0 comments on commit fae9944

Please sign in to comment.