From e738e6f7d915ad786e6edf46359cc5cc9f2f945b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Riegel?= Date: Sun, 30 Mar 2025 17:47:49 +0200 Subject: [PATCH] add tests --- .../tests/commands/into_datetime.rs | 90 ++++++++++++++++++- 1 file changed, 88 insertions(+), 2 deletions(-) diff --git a/crates/nu-command/tests/commands/into_datetime.rs b/crates/nu-command/tests/commands/into_datetime.rs index 71d3137595..3fa90ade4b 100644 --- a/crates/nu-command/tests/commands/into_datetime.rs +++ b/crates/nu-command/tests/commands/into_datetime.rs @@ -1,18 +1,104 @@ use nu_test_support::nu; +// Tests happy paths + #[test] fn into_datetime_from_record() { let actual = nu!( - r#"{year: 2023, month: 1, day: 1, hour: 0, minute: 0, second: 0, millisecond: 0, microsecond: 0, nanosecond: 0, timezone: "+01:00"} | into datetime"# + r#"{year: 2023, month: 1, day: 2, hour: 3, minute: 4, second: 5, millisecond: 6, microsecond: 7, nanosecond: 8, timezone: '+01:00'} | into datetime | into record"# + ); + let expected = nu!( + r#"{year: 2023, month: 1, day: 2, hour: 3, minute: 4, second: 5, millisecond: 6, microsecond: 7, nanosecond: 8, timezone: '+01:00'}"# ); - let expected = nu!(r#"'01/01/2023' | into datetime"#); assert_eq!(expected.out, actual.out); } +#[test] +fn into_datetime_from_record_defaults() { + let actual = nu!(r#"{year: 2023, month: 1, day: 2} | into datetime | into record"#); + let expected = nu!( + r#"{year: 2023, month: 1, day: 2, hour: 0, minute: 0, second: 0, millisecond: 0, microsecond: 0, nanosecond: 0, timezone: '+00:00'}"# + ); + + assert_eq!(expected.out, actual.out); +} + +#[test] +fn into_datetime_from_record_round_trip() { + let actual = nu!( + r#"(1743348798 | into datetime | into record | into datetime | into int) == 1743348798"# + ); + + assert!(actual.out.contains("true")); +} + #[test] fn into_datetime_table_column() { let actual = nu!(r#"[[date]; ["2022-01-01"] ["2023-01-01"]] | into datetime date"#); assert!(actual.out.contains(" ago")); } + +// Tests error paths + +#[test] +fn into_datetime_from_record_fails_with_wrong_type() { + let actual = nu!(r#"{year: '2023'} | into datetime"#); + + assert!(actual + .err + .contains("nu::shell::only_supports_this_input_type")); +} + +#[test] +fn into_datetime_from_record_fails_with_invalid_date_time_values() { + let actual = nu!(r#"{year: 2023, month: 13} | into datetime"#); + + assert!(actual.err.contains("nu::shell::incorrect_value")); +} + +#[test] +fn into_datetime_from_record_fails_with_invalid_timezone() { + let actual = nu!(r#"{year: 2023, timezone: '+100:00'} | into datetime"#); + + assert!(actual.err.contains("nu::shell::incorrect_value")); +} + +// Tests invalid usage + +// TODO: required key missing + +#[test] +fn into_datetime_from_record_fails_with_unknown_key() { + let actual = nu!(r#"{year: 2023, unknown: 1} | into datetime"#); + + assert!(actual.err.contains("nu::shell::unsupported_input")); +} + +#[test] +fn into_datetime_from_record_incompatible_with_format_flag() { + let actual = nu!( + r#"{year: 2023, month: 1, day: 2, hour: 3, minute: 4, second: 5} | into datetime --format ''"# + ); + + assert!(actual.err.contains("nu::shell::incompatible_parameters")); +} + +#[test] +fn into_datetime_from_record_incompatible_with_timezone_flag() { + let actual = nu!( + r#"{year: 2023, month: 1, day: 2, hour: 3, minute: 4, second: 5} | into datetime --timezone UTC"# + ); + + assert!(actual.err.contains("nu::shell::incompatible_parameters")); +} + +#[test] +fn into_datetime_from_record_incompatible_with_offset_flag() { + let actual = nu!( + r#"{year: 2023, month: 1, day: 2, hour: 3, minute: 4, second: 5} | into datetime --offset 1"# + ); + + assert!(actual.err.contains("nu::shell::incompatible_parameters")); +}