-
Notifications
You must be signed in to change notification settings - Fork 70
/
test_jsonl.rs
122 lines (110 loc) · 4.49 KB
/
test_jsonl.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
use crate::workdir::Workdir;
#[test]
fn jsonl_simple() {
let wrk = Workdir::new("jsonl_simple");
wrk.create_from_string(
"data.jsonl",
r#"{"id":1,"father":"Mark","mother":"Charlotte","oldest_child":"Tom","boy":true}
{"id":2,"father":"John","mother":"Ann","oldest_child":"Jessika","boy":false}
{"id":3,"father":"Bob","mother":"Monika","oldest_child":"Jerry","boy":true}"#,
);
let mut cmd = wrk.command("jsonl");
cmd.arg("data.jsonl");
let got: Vec<Vec<String>> = wrk.read_stdout(&mut cmd);
let expected = vec![
svec!["id", "father", "mother", "oldest_child", "boy"],
svec!["1", "Mark", "Charlotte", "Tom", "true"],
svec!["2", "John", "Ann", "Jessika", "false"],
svec!["3", "Bob", "Monika", "Jerry", "true"],
];
assert_eq!(got, expected);
}
#[test]
fn jsonl_simple_delimiter() {
let wrk = Workdir::new("jsonl_simple_delimiter");
wrk.create_from_string(
"data.jsonl",
r#"{"id":1,"father":"Mark","mother":"Charlotte","oldest_child":"Tom","boy":true}
{"id":2,"father":"John","mother":"Ann","oldest_child":"Jessika","boy":false}
{"id":3,"father":"Bob","mother":"Monika","oldest_child":"Jerry","boy":true}"#,
);
let mut cmd = wrk.command("jsonl");
cmd.args(["--delimiter", ";"]).arg("data.jsonl");
let got: Vec<Vec<String>> = wrk.read_stdout(&mut cmd);
let expected = vec![
svec!["id;father;mother;oldest_child;boy"],
svec!["1;Mark;Charlotte;Tom;true"],
svec!["2;John;Ann;Jessika;false"],
svec!["3;Bob;Monika;Jerry;true"],
];
assert_eq!(got, expected);
}
#[test]
fn jsonl_simple_error() {
let wrk = Workdir::new("jsonl");
wrk.create_from_string(
"data.jsonl",
r#"{"id":1,"father":"Mark","mother":"Charlotte","oldest_child":"Tom","boy":true}
{"id":2,"father":"John","mother":"Ann","oldest_child":"Jessika","boy":false}
{"id":3,"father":"Bob","mother":"Monika","oldest_child":"Jerry","boy":true}
{"id":4,"father":"Gad","mother":"Maria","oldest_child":"Hesus"Espiritu","boy":true}
{"id":5,"father":"Donald","mother":"Melania","oldest_child":"Ivanka","boy":false}"#,
);
let mut cmd = wrk.command("jsonl");
cmd.arg("data.jsonl");
let got: Vec<Vec<String>> = wrk.read_stdout(&mut cmd);
// 4 and 5 are not displayed as jsonl encounters an error and just stops
let expected = vec![
svec!["id", "father", "mother", "oldest_child", "boy"],
svec!["1", "Mark", "Charlotte", "Tom", "true"],
svec!["2", "John", "Ann", "Jessika", "false"],
svec!["3", "Bob", "Monika", "Jerry", "true"],
];
assert_eq!(got, expected);
wrk.assert_err(&mut cmd);
}
#[test]
fn jsonl_simple_ignore_error() {
let wrk = Workdir::new("jsonl");
wrk.create_from_string(
"data.jsonl",
r#"{"id":1,"father":"Mark","mother":"Charlotte","oldest_child":"Tom","boy":true}
{"id":2,"father":"John","mother":"Ann","oldest_child":"Jessika","boy":false}
{"id":3,"father":"Bob","mother":"Monika","oldest_child":"Jerry","boy":true}
{"id":4,"father":"Gad","mother":"Maria","oldest_child":"Hesus"Espiritu","boy":true}
{"id":5,"father":"Donald","mother":"Melania","oldest_child":"Ivanka","boy":false}"#,
);
let mut cmd = wrk.command("jsonl");
cmd.arg("--ignore-errors").arg("data.jsonl");
let got: Vec<Vec<String>> = wrk.read_stdout(&mut cmd);
// 4 is ignored as its invalid jsonl
let expected = vec![
svec!["id", "father", "mother", "oldest_child", "boy"],
svec!["1", "Mark", "Charlotte", "Tom", "true"],
svec!["2", "John", "Ann", "Jessika", "false"],
svec!["3", "Bob", "Monika", "Jerry", "true"],
svec!["5", "Donald", "Melania", "Ivanka", "false"],
];
assert_eq!(got, expected);
wrk.assert_success(&mut cmd);
}
#[test]
fn jsonl_nested() {
let wrk = Workdir::new("jsonl");
wrk.create_from_string(
"data.jsonl",
r#"{"id":1,"father":"Mark","mother":"Charlotte","children":["Tom"]}
{"id":2,"father":"John","mother":"Ann","children":["Jessika","Antony","Jack"]}
{"id":3,"father":"Bob","mother":"Monika","children":["Jerry","Karol"]}"#,
);
let mut cmd = wrk.command("jsonl");
cmd.arg("data.jsonl");
let got: Vec<Vec<String>> = wrk.read_stdout(&mut cmd);
let expected = vec![
svec!["id", "father", "mother", "children"],
svec!["1", "Mark", "Charlotte", "\"Tom\""],
svec!["2", "John", "Ann", "\"Jessika\",\"Antony\",\"Jack\""],
svec!["3", "Bob", "Monika", "\"Jerry\",\"Karol\""],
];
assert_eq!(got, expected);
}