nushell/crates/nu-command/tests/commands/headers.rs
Ian Manske f38657e6f3
Fix headers command handling of missing values (#9603)
# Description
This fixes the `headers` command handling of missing values (issue
#9602). Previously, each row in the table would have its columns set to
be exactly equal to the first row even if it had less columns than the
first row. This would cause to values magically change their column or
cause panics in other commands if rows ended up having more columns than
values.

# Tests
Added a missing values  test for the `headers` command
2023-07-06 19:54:59 +02:00

129 lines
2.8 KiB
Rust

use nu_test_support::{nu, pipeline};
#[test]
fn headers_uses_first_row_as_header() {
let actual = nu!(
cwd: "tests/fixtures/formats", pipeline(
r#"
open sample_headers.xlsx
| get Sheet1
| headers
| get header0
| to json --raw"#
));
assert_eq!(actual.out, r#"["r1c0","r2c0"]"#)
}
#[test]
fn headers_adds_missing_column_name() {
let actual = nu!(
cwd: "tests/fixtures/formats", pipeline(
r#"
open sample_headers.xlsx
| get Sheet1
| headers
| get column1
| to json --raw"#
));
assert_eq!(actual.out, r#"["r1c1","r2c1"]"#)
}
#[test]
fn headers_handles_missing_values() {
let actual = nu!(pipeline(
r#"
[{x: a, y: b}, {x: 1, y: 2}, {x: 1, z: 3}]
| headers
| to nuon --raw
"#
));
assert_eq!(actual.out, "[{a: 1, b: 2}, {a: 1}]")
}
#[test]
fn headers_invalid_column_type_empty_record() {
let actual = nu!(
cwd: "tests/fixtures/formats", pipeline(
r#"
[[a b]; [{}, 2], [3,4] ]
| headers"#
));
assert!(actual
.err
.contains("needs compatible type: Null, String, Bool, Float, Int"));
}
#[test]
fn headers_invalid_column_type_record() {
let actual = nu!(
cwd: "tests/fixtures/formats", pipeline(
r#"
[[a b]; [1 (scope aliases)] [2 2]]
| headers"#
));
assert!(actual
.err
.contains("needs compatible type: Null, String, Bool, Float, Int"));
}
#[test]
fn headers_invalid_column_type_array() {
let actual = nu!(
cwd: "tests/fixtures/formats", pipeline(
r#"
[[a b]; [[f,g], 2], [3,4] ]
| headers"#
));
assert!(actual
.err
.contains("needs compatible type: Null, String, Bool, Float, Int"));
}
#[test]
fn headers_invalid_column_type_range() {
let actual = nu!(
cwd: "tests/fixtures/formats", pipeline(
r#"
[[a b]; [(1..5), 2], [3,4] ]
| headers"#
));
assert!(actual
.err
.contains("needs compatible type: Null, String, Bool, Float, Int"));
}
#[test]
fn headers_invalid_column_type_duration() {
let actual = nu!(
cwd: "tests/fixtures/formats", pipeline(
r#"
[[a b]; [((date now) - (date now)), 2], [3,4] ]
| headers"#
));
assert!(actual
.err
.contains("needs compatible type: Null, String, Bool, Float, Int"));
}
#[test]
fn headers_invalid_column_type_binary() {
let actual = nu!(
cwd: "tests/fixtures/formats", pipeline(
r#"
[[a b]; [("aa" | into binary), 2], [3,4] ]
| headers"#
));
assert!(actual
.err
.contains("needs compatible type: Null, String, Bool, Float, Int"));
}