Support Jiff's "friendly" format as input for jiff::Duration GraphQL scalar

This commit is contained in:
tyranron 2025-01-02 15:45:25 +02:00
parent 497c839f30
commit b75e1aa55a
No known key found for this signature in database
GPG key ID: 762E144FB230A4F0
2 changed files with 77 additions and 3 deletions

View file

@ -53,7 +53,7 @@ fnv = "1.0.5"
futures = { version = "0.3.22", features = ["alloc"], default-features = false }
graphql-parser = { version = "0.4", optional = true }
indexmap = { version = "2.0", features = ["serde"] }
jiff = { version = "0.1.5", features = ["std"], default-features = false, optional = true }
jiff = { version = "0.1.16", features = ["std"], default-features = false, optional = true }
juniper_codegen = { version = "0.16.0", path = "../juniper_codegen" }
rust_decimal = { version = "1.20", default-features = false, optional = true }
ryu = { version = "1.0", optional = true }
@ -78,7 +78,7 @@ void = { version = "1.0.2", optional = true }
[dev-dependencies]
bencher = "0.1.2"
chrono = { version = "0.4.30", features = ["alloc"], default-features = false }
jiff = { version = "0.1.5", features = ["tzdb-bundle-always"], default-features = false }
jiff = { version = "0.1.16", features = ["tzdb-bundle-always"], default-features = false }
pretty_assertions = "1.0.0"
serde_json = "1.0.18"
serial_test = "3.0"

View file

@ -1170,6 +1170,81 @@ mod duration_test {
}
}
#[test]
fn parses_jiff_friendly_input() {
for (raw, expected) in [
("40d", 40.days()),
("40 days", 40.days()),
("1y1d", 1.year().days(1)),
("1yr 1d", 1.year().days(1)),
("3d4h59m", 3.days().hours(4).minutes(59)),
("3 days, 4 hours, 59 minutes", 3.days().hours(4).minutes(59)),
("3d 4h 59m", 3.days().hours(4).minutes(59)),
("2h30m", 2.hours().minutes(30)),
("2h 30m", 2.hours().minutes(30)),
("1mo", 1.month()),
("1w", 1.week()),
("1 week", 1.week()),
("1w4d", 1.week().days(4)),
("1 wk 4 days", 1.week().days(4)),
("1m", 1.minute()),
("0.0021s", 2.milliseconds().microseconds(100)),
("0s", 0.seconds()),
("0d", 0.seconds()),
("0 days", 0.seconds()),
(
"1y1mo1d1h1m1.1s",
1.year()
.months(1)
.days(1)
.hours(1)
.minutes(1)
.seconds(1)
.milliseconds(100),
),
(
"1yr 1mo 1day 1hr 1min 1.1sec",
1.year()
.months(1)
.days(1)
.hours(1)
.minutes(1)
.seconds(1)
.milliseconds(100),
),
(
"1 year, 1 month, 1 day, 1 hour, 1 minute 1.1 seconds",
1.year()
.months(1)
.days(1)
.hours(1)
.minutes(1)
.seconds(1)
.milliseconds(100),
),
(
"1 year, 1 month, 1 day, 01:01:01.1",
1.year()
.months(1)
.days(1)
.hours(1)
.minutes(1)
.seconds(1)
.milliseconds(100),
),
] {
let input: InputValue = graphql_input_value!((raw));
let parsed = Duration::from_input_value(&input);
assert!(
parsed.is_ok(),
"failed to parse `{raw}`: {:?}",
parsed.unwrap_err(),
);
assert_eq!(parsed.unwrap(), expected, "input: {raw}");
}
}
#[test]
fn fails_on_invalid_input() {
for input in [
@ -1178,7 +1253,6 @@ mod duration_test {
graphql_input_value!("P0"),
graphql_input_value!("PT"),
graphql_input_value!("PTS"),
graphql_input_value!("56:34:22"),
graphql_input_value!("1996-12-19"),
graphql_input_value!("1996-12-19T14:23:43"),
graphql_input_value!("1996-12-19T14:23:43Z"),