Skip to content

Commit

Permalink
Remove file I/O from tests that don't need it (nushell#11182)
Browse files Browse the repository at this point in the history
# Description

This PR implements modifications to command tests that write unnecessary
json and csv to disk then load it with open, by using nuon literals
instead.

- Fixes nushell#7189



# User-Facing Changes
None

# Tests + Formatting
This only affects existing tests, which still pass.
  • Loading branch information
cosineblast authored and hardfau1t committed Dec 14, 2023
1 parent 0110af6 commit abac8ff
Show file tree
Hide file tree
Showing 20 changed files with 421 additions and 767 deletions.
33 changes: 11 additions & 22 deletions crates/nu-command/tests/commands/compact.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,8 @@
use nu_test_support::fs::Stub::FileWithContentToBeTrimmed;
use nu_test_support::playground::Playground;
use nu_test_support::{nu, pipeline};

#[test]
fn discards_rows_where_given_column_is_empty() {
Playground::setup("compact_test_1", |dirs, sandbox| {
sandbox.with_files(vec![FileWithContentToBeTrimmed(
"los_tres_amigos.json",
r#"
let sample_json = r#"
{
"amigos": [
{"name": "Yehuda", "rusty_luck": 1},
Expand All @@ -16,35 +11,29 @@ fn discards_rows_where_given_column_is_empty() {
{"name":"GorbyPuff"}
]
}
"#,
)]);
"#;

let actual = nu!(
cwd: dirs.test(), pipeline(
"
open los_tres_amigos.json
let actual = nu!(pipeline(&format!(
"
{sample_json}
| get amigos
| compact rusty_luck
| length
"
));
)));

assert_eq!(actual.out, "3");
});
assert_eq!(actual.out, "3");
}
#[test]
fn discards_empty_rows_by_default() {
Playground::setup("compact_test_2", |dirs, _| {
let actual = nu!(
cwd: dirs.test(), pipeline(
r#"
let actual = nu!(pipeline(
r#"
echo "[1,2,3,14,null]"
| from json
| compact
| length
"#
));
));

assert_eq!(actual.out, "4");
});
assert_eq!(actual.out, "4");
}
22 changes: 7 additions & 15 deletions crates/nu-command/tests/commands/default.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,8 @@
use nu_test_support::fs::Stub::FileWithContentToBeTrimmed;
use nu_test_support::playground::Playground;
use nu_test_support::{nu, pipeline};

#[test]
fn adds_row_data_if_column_missing() {
Playground::setup("default_test_1", |dirs, sandbox| {
sandbox.with_files(vec![FileWithContentToBeTrimmed(
"los_tres_amigos.json",
r#"
let sample = r#"
{
"amigos": [
{"name": "Yehuda"},
Expand All @@ -16,22 +11,19 @@ fn adds_row_data_if_column_missing() {
{"name":"GorbyPuff"}
]
}
"#,
)]);
"#;

let actual = nu!(
cwd: dirs.test(), pipeline(
"
open los_tres_amigos.json
let actual = nu!(pipeline(&format!(
"
{sample}
| get amigos
| default 1 rusty_luck
| where rusty_luck == 1
| length
"
));
)));

assert_eq!(actual.out, "2");
});
assert_eq!(actual.out, "2");
}

#[test]
Expand Down
74 changes: 23 additions & 51 deletions crates/nu-command/tests/commands/flatten.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use nu_test_support::fs::Stub::FileWithContentToBeTrimmed;
use nu_test_support::playground::Playground;
use nu_test_support::{nu, pipeline};

#[test]
Expand Down Expand Up @@ -43,10 +41,7 @@ fn flatten_nested_tables() {

#[test]
fn flatten_row_column_explicitly() {
Playground::setup("flatten_test_1", |dirs, sandbox| {
sandbox.with_files(vec![FileWithContentToBeTrimmed(
"katz.json",
r#"
let sample = r#"
[
{
"people": {
Expand All @@ -61,24 +56,18 @@ fn flatten_row_column_explicitly() {
}
}
]
"#,
)]);
"#;

let actual = nu!(
cwd: dirs.test(),
"open katz.json | flatten people --all | where name == Andres | length"
);
let actual = nu!(pipeline(&format!(
"{sample} | flatten people --all | where name == Andres | length"
)));

assert_eq!(actual.out, "1");
})
assert_eq!(actual.out, "1");
}

#[test]
fn flatten_row_columns_having_same_column_names_flats_separately() {
Playground::setup("flatten_test_2", |dirs, sandbox| {
sandbox.with_files(vec![FileWithContentToBeTrimmed(
"katz.json",
r#"
let sample = r#"
[
{
"people": {
Expand All @@ -95,24 +84,18 @@ fn flatten_row_columns_having_same_column_names_flats_separately() {
"city": [{"name": "Oregon"}, {"name": "Brooklin"}]
}
]
"#,
)]);
"#;

let actual = nu!(
cwd: dirs.test(),
"open katz.json | flatten --all | flatten people city | get city_name | length"
);
let actual = nu!(pipeline(&format!(
"{sample} | flatten --all | flatten people city | get city_name | length"
)));

assert_eq!(actual.out, "4");
})
assert_eq!(actual.out, "4");
}

#[test]
fn flatten_table_columns_explicitly() {
Playground::setup("flatten_test_3", |dirs, sandbox| {
sandbox.with_files(vec![FileWithContentToBeTrimmed(
"katz.json",
r#"
let sample = r#"
[
{
"people": {
Expand All @@ -129,24 +112,18 @@ fn flatten_table_columns_explicitly() {
"city": ["Oregon", "Brooklin"]
}
]
"#,
)]);
"#;

let actual = nu!(
cwd: dirs.test(),
"open katz.json | flatten city --all | where people.name == Katz | length"
);
let actual = nu!(pipeline(&format!(
"{sample} | flatten city --all | where people.name == Katz | length",
)));

assert_eq!(actual.out, "2");
})
assert_eq!(actual.out, "2");
}

#[test]
fn flatten_more_than_one_column_that_are_subtables_not_supported() {
Playground::setup("flatten_test_4", |dirs, sandbox| {
sandbox.with_files(vec![FileWithContentToBeTrimmed(
"katz.json",
r#"
let sample = r#"
[
{
"people": {
Expand All @@ -165,15 +142,10 @@ fn flatten_more_than_one_column_that_are_subtables_not_supported() {
"city": ["Oregon", "Brooklin"]
}
]
"#,
)]);
"#;

let actual = nu!(
cwd: dirs.test(),
"open katz.json | flatten tags city --all"
);
let actual = nu!(pipeline(&format!("{sample} | flatten tags city --all")));

assert!(actual.err.contains("tried flattening"));
assert!(actual.err.contains("but is flattened already"));
})
assert!(actual.err.contains("tried flattening"));
assert!(actual.err.contains("but is flattened already"));
}
84 changes: 30 additions & 54 deletions crates/nu-command/tests/commands/group_by.rs
Original file line number Diff line number Diff line change
@@ -1,40 +1,29 @@
use nu_test_support::fs::Stub::FileWithContentToBeTrimmed;
use nu_test_support::playground::Playground;
use nu_test_support::{nu, pipeline};

#[test]
fn groups() {
Playground::setup("group_by_test_1", |dirs, sandbox| {
sandbox.with_files(vec![FileWithContentToBeTrimmed(
"los_tres_caballeros.csv",
r#"
first_name,last_name,rusty_at,type
Andrés,Robalino,10/11/2013,A
JT,Turner,10/12/2013,B
Yehuda,Katz,10/11/2013,A
"#,
)]);
let sample = r#"
[[first_name, last_name, rusty_at, type];
[Andrés, Robalino, "10/11/2013", A],
[JT, Turner, "10/12/2013", B],
[Yehuda, Katz, "10/11/2013", A]]
"#;

let actual = nu!(
cwd: dirs.test(), pipeline(
r#"
open los_tres_caballeros.csv
let actual = nu!(pipeline(&format!(
r#"
{sample}
| group-by rusty_at
| get "10/11/2013"
| length
"#
));
)));

assert_eq!(actual.out, "2");
})
assert_eq!(actual.out, "2");
}

#[test]
fn errors_if_given_unknown_column_name() {
Playground::setup("group_by_test_2", |dirs, sandbox| {
sandbox.with_files(vec![FileWithContentToBeTrimmed(
"los_tres_caballeros.json",
r#"
let sample = r#"
{
"nu": {
"committers": [
Expand All @@ -54,46 +43,33 @@ fn errors_if_given_unknown_column_name() {
]
}
}
"#,
)]);
"#;

let actual = nu!(
cwd: dirs.test(), pipeline(
r#"
open los_tres_caballeros.json
| group-by {|| get nu.releases.version }
let actual = nu!(pipeline(&format!(
r#"
'{sample}'
| from json
| group-by {{|| get nu.releases.version }}
"#
));
)));

assert!(actual
.err
.contains("requires a table with one value for grouping"));
})
assert!(actual
.err
.contains("requires a table with one value for grouping"));
}

#[test]
fn errors_if_column_not_found() {
Playground::setup("group_by_test_3", |dirs, sandbox| {
sandbox.with_files(vec![FileWithContentToBeTrimmed(
"los_tres_caballeros.csv",
r#"
first_name,last_name,rusty_at,type
Andrés,Robalino,10/11/2013,A
JT,Turner,10/12/2013,B
Yehuda,Katz,10/11/2013,A
"#,
)]);
let sample = r#"
[[first_name, last_name, rusty_at, type];
[Andrés, Robalino, "10/11/2013", A],
[JT, Turner, "10/12/2013", B],
[Yehuda, Katz, "10/11/2013", A]]
"#;

let actual = nu!(
cwd: dirs.test(), pipeline(
"
open los_tres_caballeros.csv
| group-by ttype
"
));
let actual = nu!(pipeline(&format!("{sample} | group-by ttype")));

assert!(actual.err.contains("did you mean 'type'"),);
})
assert!(actual.err.contains("did you mean 'type'"),);
}

#[test]
Expand Down
Loading

0 comments on commit abac8ff

Please sign in to comment.