2019-07-16 12:28:55 +02:00
|
|
|
mod helpers;
|
|
|
|
|
2019-08-25 14:59:46 +02:00
|
|
|
use helpers::{in_directory as cwd, Playground, Stub::*};
|
2019-07-16 12:28:55 +02:00
|
|
|
|
2019-07-21 09:08:05 +02:00
|
|
|
#[test]
|
|
|
|
fn can_convert_table_to_csv_text_and_from_csv_text_back_into_table() {
|
2019-08-28 19:01:16 +02:00
|
|
|
let output = nu!(
|
2019-07-21 09:08:05 +02:00
|
|
|
cwd("tests/fixtures/formats"),
|
2019-07-22 04:23:02 +02:00
|
|
|
"open caco3_plastics.csv | to-csv | from-csv | first 1 | get origin | echo $it"
|
|
|
|
);
|
2019-07-21 09:08:05 +02:00
|
|
|
|
|
|
|
assert_eq!(output, "SPAIN");
|
|
|
|
}
|
|
|
|
|
2019-08-25 14:59:46 +02:00
|
|
|
#[test]
|
|
|
|
fn converts_structured_table_to_csv_text() {
|
|
|
|
Playground::setup_for("filter_to_csv_test_1").with_files(vec![FileWithContentToBeTrimmed(
|
2019-08-27 13:05:51 +02:00
|
|
|
"sample.txt",
|
2019-08-25 14:59:46 +02:00
|
|
|
r#"
|
|
|
|
importer,shipper,tariff_item,name,origin
|
|
|
|
Plasticos Rival,Reverte,2509000000,Calcium carbonate,Spain
|
|
|
|
Tigre Ecuador,OMYA Andina,3824909999,Calcium carbonate,Colombia
|
|
|
|
"#,
|
|
|
|
)]);
|
|
|
|
|
2019-08-28 19:01:16 +02:00
|
|
|
let output = nu!(
|
2019-08-25 14:59:46 +02:00
|
|
|
cwd("tests/fixtures/nuplayground/filter_to_csv_test_1"),
|
2019-08-27 13:14:19 +02:00
|
|
|
r#"open sample.txt | lines | split-column "," a b c d origin | last 1 | to-csv | lines | nth 1 | echo "$it""#
|
2019-08-25 14:59:46 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
assert!(output.contains("Tigre Ecuador,OMYA Andina,3824909999,Calcium carbonate,Colombia"));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn converts_structured_table_to_csv_text_skipping_headers_after_conversion() {
|
|
|
|
Playground::setup_for("filter_to_csv_test_2").with_files(vec![FileWithContentToBeTrimmed(
|
2019-08-27 13:05:51 +02:00
|
|
|
"sample.txt",
|
2019-08-25 14:59:46 +02:00
|
|
|
r#"
|
|
|
|
importer,shipper,tariff_item,name,origin
|
|
|
|
Plasticos Rival,Reverte,2509000000,Calcium carbonate,Spain
|
|
|
|
Tigre Ecuador,OMYA Andina,3824909999,Calcium carbonate,Colombia
|
|
|
|
"#,
|
|
|
|
)]);
|
|
|
|
|
2019-08-28 19:01:16 +02:00
|
|
|
let output = nu!(
|
2019-08-25 14:59:46 +02:00
|
|
|
cwd("tests/fixtures/nuplayground/filter_to_csv_test_2"),
|
2019-08-27 13:05:51 +02:00
|
|
|
r#"open sample.txt | lines | split-column "," a b c d origin | last 1 | to-csv --headerless | echo "$it""#
|
2019-08-25 14:59:46 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
assert!(output.contains("Tigre Ecuador,OMYA Andina,3824909999,Calcium carbonate,Colombia"));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn converts_from_csv_text_to_structured_table() {
|
|
|
|
Playground::setup_for("filter_from_csv_test_1").with_files(vec![FileWithContentToBeTrimmed(
|
|
|
|
"los_tres_amigos.txt",
|
|
|
|
r#"
|
|
|
|
first_name,last_name,rusty_luck
|
|
|
|
Andrés,Robalino,1
|
|
|
|
Jonathan,Turner,1
|
|
|
|
Yehuda,Katz,1
|
|
|
|
"#,
|
|
|
|
)]);
|
|
|
|
|
2019-08-28 19:01:16 +02:00
|
|
|
let output = nu!(
|
2019-08-25 14:59:46 +02:00
|
|
|
cwd("tests/fixtures/nuplayground/filter_from_csv_test_1"),
|
|
|
|
"open los_tres_amigos.txt | from-csv | get rusty_luck | str --to-int | sum | echo $it"
|
|
|
|
);
|
|
|
|
|
|
|
|
assert_eq!(output, "3");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn converts_from_csv_text_skipping_headers_to_structured_table() {
|
|
|
|
Playground::setup_for("filter_from_csv_test_2").with_files(vec![FileWithContentToBeTrimmed(
|
|
|
|
"los_tres_amigos.txt",
|
|
|
|
r#"
|
|
|
|
first_name,last_name,rusty_luck
|
|
|
|
Andrés,Robalino,1
|
|
|
|
Jonathan,Turner,1
|
|
|
|
Yehuda,Katz,1
|
|
|
|
"#,
|
|
|
|
)]);
|
|
|
|
|
2019-08-28 19:01:16 +02:00
|
|
|
let output = nu!(
|
2019-08-25 14:59:46 +02:00
|
|
|
cwd("tests/fixtures/nuplayground/filter_from_csv_test_2"),
|
|
|
|
"open los_tres_amigos.txt | from-csv --headerless | get Column3 | str --to-int | sum | echo $it"
|
|
|
|
);
|
|
|
|
|
|
|
|
assert_eq!(output, "3");
|
|
|
|
}
|
|
|
|
|
2019-07-16 12:28:55 +02:00
|
|
|
#[test]
|
|
|
|
fn can_convert_table_to_json_text_and_from_json_text_back_into_table() {
|
2019-08-28 19:01:16 +02:00
|
|
|
let output = nu!(
|
2019-07-16 12:28:55 +02:00
|
|
|
cwd("tests/fixtures/formats"),
|
2019-07-22 04:23:02 +02:00
|
|
|
"open sgml_description.json | to-json | from-json | get glossary.GlossDiv.GlossList.GlossEntry.GlossSee | echo $it"
|
|
|
|
);
|
2019-07-16 12:28:55 +02:00
|
|
|
|
|
|
|
assert_eq!(output, "markup");
|
|
|
|
}
|
|
|
|
|
2019-08-27 13:05:51 +02:00
|
|
|
#[test]
|
|
|
|
fn converts_from_json_text_to_structured_table() {
|
|
|
|
Playground::setup_for("filter_from_json_test_1").with_files(vec![FileWithContentToBeTrimmed(
|
|
|
|
"katz.txt",
|
|
|
|
r#"
|
|
|
|
{
|
|
|
|
"katz": [
|
|
|
|
{"name": "Yehuda", "rusty_luck": 1},
|
|
|
|
{"name": "Jonathan", "rusty_luck": 1},
|
|
|
|
{"name": "Andres", "rusty_luck": 1},
|
|
|
|
{"name":"GorbyPuff", "rusty_luck": 1}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
)]);
|
|
|
|
|
2019-08-28 19:01:16 +02:00
|
|
|
let output = nu!(
|
2019-08-27 13:05:51 +02:00
|
|
|
cwd("tests/fixtures/nuplayground/filter_from_json_test_1"),
|
|
|
|
"open katz.txt | from-json | get katz | get rusty_luck | sum | echo $it"
|
|
|
|
);
|
|
|
|
|
|
|
|
assert_eq!(output, "4");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn converts_from_json_text_recognizing_objects_independendtly_to_structured_table() {
|
|
|
|
Playground::setup_for("filter_from_json_test_2").with_files(vec![FileWithContentToBeTrimmed(
|
|
|
|
"katz.txt",
|
|
|
|
r#"
|
|
|
|
{"name": "Yehuda", "rusty_luck": 1}
|
|
|
|
{"name": "Jonathan", "rusty_luck": 1}
|
|
|
|
{"name": "Andres", "rusty_luck": 1}
|
|
|
|
{"name":"GorbyPuff", "rusty_luck": 3}
|
|
|
|
"#,
|
|
|
|
)]);
|
|
|
|
|
2019-08-28 19:01:16 +02:00
|
|
|
let output = nu!(
|
2019-08-27 13:05:51 +02:00
|
|
|
cwd("tests/fixtures/nuplayground/filter_from_json_test_2"),
|
|
|
|
r#"open katz.txt | from-json --objects | where name == "GorbyPuff" | get rusty_luck | echo $it"#
|
|
|
|
);
|
|
|
|
|
|
|
|
assert_eq!(output, "3");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn converts_structured_table_to_json_text() {
|
|
|
|
Playground::setup_for("filter_to_json_test_1").with_files(vec![FileWithContentToBeTrimmed(
|
|
|
|
"sample.txt",
|
|
|
|
r#"
|
|
|
|
JonAndrehudaTZ,3
|
|
|
|
GorbyPuff,100
|
|
|
|
"#,
|
|
|
|
)]);
|
|
|
|
|
2019-08-28 19:01:16 +02:00
|
|
|
let output = nu!(
|
2019-08-27 13:05:51 +02:00
|
|
|
cwd("tests/fixtures/nuplayground/filter_to_json_test_1"),
|
|
|
|
r#"open sample.txt | lines | split-column "," name luck | pick name | to-json | nth 0 | from-json | get name | echo $it"#
|
|
|
|
);
|
|
|
|
|
|
|
|
assert_eq!(output, "JonAndrehudaTZ");
|
|
|
|
}
|
|
|
|
|
2019-08-26 16:16:34 +02:00
|
|
|
#[test]
|
|
|
|
fn can_convert_json_text_to_bson_and_back_into_table() {
|
2019-08-28 19:01:16 +02:00
|
|
|
let output = nu!(
|
2019-08-26 16:16:34 +02:00
|
|
|
cwd("tests/fixtures/formats"),
|
2019-08-27 03:26:49 +02:00
|
|
|
"open sample.bson | to-bson | from-bson | get root | nth 1 | get b | echo $it"
|
2019-08-26 16:16:34 +02:00
|
|
|
);
|
|
|
|
|
2019-08-27 03:26:49 +02:00
|
|
|
assert_eq!(output, "whel");
|
2019-08-26 16:16:34 +02:00
|
|
|
}
|
|
|
|
|
2019-07-16 12:28:55 +02:00
|
|
|
#[test]
|
|
|
|
fn can_convert_table_to_toml_text_and_from_toml_text_back_into_table() {
|
2019-08-28 19:01:16 +02:00
|
|
|
let output = nu!(
|
2019-07-16 12:28:55 +02:00
|
|
|
cwd("tests/fixtures/formats"),
|
2019-07-17 19:51:53 +02:00
|
|
|
"open cargo_sample.toml | to-toml | from-toml | get package.name | echo $it"
|
|
|
|
);
|
2019-07-16 12:28:55 +02:00
|
|
|
|
|
|
|
assert_eq!(output, "nu");
|
|
|
|
}
|
|
|
|
|
2019-07-17 19:51:53 +02:00
|
|
|
#[test]
|
|
|
|
fn can_convert_table_to_yaml_text_and_from_yaml_text_back_into_table() {
|
2019-08-28 19:01:16 +02:00
|
|
|
let output = nu!(
|
2019-07-17 19:51:53 +02:00
|
|
|
cwd("tests/fixtures/formats"),
|
|
|
|
"open appveyor.yml | to-yaml | from-yaml | get environment.global.PROJECT_NAME | echo $it"
|
|
|
|
);
|
|
|
|
|
|
|
|
assert_eq!(output, "nushell");
|
|
|
|
}
|
|
|
|
|
2019-07-16 12:28:55 +02:00
|
|
|
#[test]
|
|
|
|
fn can_sort_by_column() {
|
2019-08-28 19:01:16 +02:00
|
|
|
let output = nu!(
|
2019-07-16 12:28:55 +02:00
|
|
|
cwd("tests/fixtures/formats"),
|
2019-08-27 13:05:51 +02:00
|
|
|
r#"open cargo_sample.toml --raw | lines | skip 1 | first 4 | split-column "=" | sort-by Column1 | skip 1 | first 1 | get Column1 | trim | echo $it"#
|
2019-07-22 04:23:02 +02:00
|
|
|
);
|
2019-07-16 12:28:55 +02:00
|
|
|
|
|
|
|
assert_eq!(output, "description");
|
|
|
|
}
|
|
|
|
|
2019-08-24 22:31:45 +02:00
|
|
|
#[test]
|
|
|
|
fn can_sort_by_column_reverse() {
|
2019-08-28 19:01:16 +02:00
|
|
|
let output = nu!(
|
2019-08-24 22:31:45 +02:00
|
|
|
cwd("tests/fixtures/formats"),
|
2019-08-27 13:05:51 +02:00
|
|
|
r#"open cargo_sample.toml --raw | lines | skip 1 | first 4 | split-column "=" | sort-by Column1 --reverse | skip 1 | first 1 | get Column1 | trim | echo $it"#
|
2019-08-24 22:31:45 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
assert_eq!(output, "name");
|
|
|
|
}
|
|
|
|
|
2019-07-16 12:28:55 +02:00
|
|
|
#[test]
|
|
|
|
fn can_split_by_column() {
|
2019-08-28 19:01:16 +02:00
|
|
|
let output = nu!(
|
2019-07-16 12:28:55 +02:00
|
|
|
cwd("tests/fixtures/formats"),
|
2019-08-27 13:05:51 +02:00
|
|
|
r#"open cargo_sample.toml --raw | lines | skip 1 | first 1 | split-column "=" | get Column1 | trim | echo $it"#
|
2019-07-22 04:23:02 +02:00
|
|
|
);
|
2019-07-16 12:28:55 +02:00
|
|
|
|
|
|
|
assert_eq!(output, "name");
|
|
|
|
}
|
|
|
|
|
2019-07-26 20:40:00 +02:00
|
|
|
#[test]
|
|
|
|
fn can_sum() {
|
2019-08-28 19:01:16 +02:00
|
|
|
let output = nu!(
|
2019-07-26 20:40:00 +02:00
|
|
|
cwd("tests/fixtures/formats"),
|
|
|
|
"open sgml_description.json | get glossary.GlossDiv.GlossList.GlossEntry.Sections | sum | echo $it"
|
|
|
|
);
|
|
|
|
|
|
|
|
assert_eq!(output, "203")
|
|
|
|
}
|
2019-08-10 11:11:38 +02:00
|
|
|
|
2019-07-16 12:28:55 +02:00
|
|
|
#[test]
|
|
|
|
fn can_filter_by_unit_size_comparison() {
|
2019-08-28 19:01:16 +02:00
|
|
|
let output = nu!(
|
2019-07-16 12:28:55 +02:00
|
|
|
cwd("tests/fixtures/formats"),
|
2019-08-08 19:53:28 +02:00
|
|
|
"ls | where size > 1kb | sort-by size | get name | skip 1 | trim | echo $it"
|
2019-07-17 19:51:53 +02:00
|
|
|
);
|
2019-07-16 12:28:55 +02:00
|
|
|
|
2019-08-08 19:53:28 +02:00
|
|
|
assert_eq!(output, "caco3_plastics.csv");
|
2019-07-16 12:28:55 +02:00
|
|
|
}
|
2019-08-24 23:55:42 +02:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn can_get_last() {
|
2019-08-28 19:01:16 +02:00
|
|
|
let output = nu!(
|
2019-08-24 23:55:42 +02:00
|
|
|
cwd("tests/fixtures/formats"),
|
2019-08-25 02:21:09 +02:00
|
|
|
"ls | sort-by name | last 1 | get name | trim | echo $it"
|
2019-08-24 23:55:42 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
assert_eq!(output, "utf16.ini");
|
2019-08-24 20:32:48 +02:00
|
|
|
}
|
2019-08-25 18:14:17 +02:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn can_get_reverse_first() {
|
2019-08-28 19:01:16 +02:00
|
|
|
let output = nu!(
|
2019-08-25 18:14:17 +02:00
|
|
|
cwd("tests/fixtures/formats"),
|
|
|
|
"ls | sort-by name | reverse | first 1 | get name | trim | echo $it"
|
|
|
|
);
|
|
|
|
|
|
|
|
assert_eq!(output, "utf16.ini");
|
|
|
|
}
|