-
Notifications
You must be signed in to change notification settings - Fork 66
/
test_schema.rs
259 lines (218 loc) · 10.4 KB
/
test_schema.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
#![cfg(target_family = "unix")]
use std::path::Path;
use assert_json_diff::assert_json_eq;
use serde_json::Value;
use serial_test::file_serial;
use crate::workdir::Workdir;
#[test]
#[file_serial]
fn generate_schema_with_defaults_and_validate_trim_with_no_errors() {
// create workspace and invoke schema command with value constraints flag
let wrk = Workdir::new("fn generate_schema_with_defaults_and_validate_trim_with_no_errors")
.flexible(true);
wrk.clear_contents().unwrap();
// copy csv file to workdir
let csv = wrk.load_test_resource("adur-public-toilets.csv");
wrk.create_from_string("adur-public-toilets.csv", &csv);
// run schema command with value constraints option
let mut cmd = wrk.command("schema");
cmd.arg("adur-public-toilets.csv");
wrk.output(&mut cmd);
// load output schema file
let output_schema_string: String =
wrk.from_str(&wrk.path("adur-public-toilets.csv.schema.json"));
let output_schema_json =
serde_json::from_str(&output_schema_string).expect("parse schema json");
// make sure it's a valid JSON Schema by compiling with jsonschema library
jsonschema::JSONSchema::options()
.compile(&output_schema_json)
.expect("valid JSON Schema");
// diff output json with expected json
let expected_schema: String =
wrk.load_test_resource("adur-public-toilets.csv.schema-default.expected.json");
let expected_schema_json: Value = serde_json::from_str(&expected_schema).unwrap();
assert_json_eq!(expected_schema_json, output_schema_json);
// invoke validate command from schema created above
let mut cmd2 = wrk.command("validate");
cmd2.arg("adur-public-toilets.csv");
cmd2.arg("--trim");
cmd2.arg("adur-public-toilets.csv.schema.json");
wrk.output(&mut cmd2);
// not expecting any invalid rows, so confirm there are NO output files generated
let validation_error_path = &wrk.path("adur-public-toilets.csv.validation-errors.tsv");
println!("not expecting validation error file at: {validation_error_path:?}");
assert!(!Path::new(validation_error_path).exists());
assert!(!Path::new(&wrk.path("adur-public-toilets.csv.valid")).exists());
assert!(!Path::new(&wrk.path("adur-public-toilets.csv.invalid")).exists());
wrk.assert_success(&mut cmd);
}
#[test]
#[file_serial]
fn generate_schema_with_optional_flags_notrim_and_validate_with_errors() {
// create workspace and invoke schema command with value constraints flag
let wrk = Workdir::new("generate_schema_with_optional_flags_notrim_and_validate_with_errors")
.flexible(true);
wrk.clear_contents().unwrap();
// copy csv file to workdir
let csv = wrk.load_test_resource("adur-public-toilets.csv");
wrk.create_from_string("adur-public-toilets.csv", &csv);
// run schema command with value constraints option
let mut cmd = wrk.command("schema");
cmd.arg("adur-public-toilets.csv");
cmd.arg("--enum-threshold");
cmd.arg("13");
cmd.arg("--pattern-columns");
cmd.arg("ReportEmail,OpeningHours");
cmd.arg("--strict-dates");
wrk.output(&mut cmd);
// load output schema file
let output_schema_string: String =
wrk.from_str(&wrk.path("adur-public-toilets.csv.schema.json"));
let output_schema_json =
serde_json::from_str(&output_schema_string).expect("parse schema json");
// make sure it's a valid JSON Schema by compiling with jsonschema library
jsonschema::JSONSchema::options()
.compile(&output_schema_json)
.expect("valid JSON Schema");
// diff output json with expected json
let expected_schema: String =
wrk.load_test_resource("adur-public-toilets.csv.schema-strict.expected.json");
let expected_schema_json: Value = serde_json::from_str(&expected_schema).unwrap();
assert_json_eq!(expected_schema_json, output_schema_json);
// invoke validate command from schema created above
let mut cmd2 = wrk.command("validate");
cmd2.arg("adur-public-toilets.csv");
cmd2.arg("adur-public-toilets.csv.schema.json");
wrk.output(&mut cmd2);
// validation report
let validation_errors_expected = r#"row_number field error
1 OpeningHours "S = 09:00 - 21:00 W = 09:00 - 17:00 " is not one of ["09.00 - 17.00","S = 08:00 - 21:00 W = 08:00 - 17:00","S = 09:00 - 15:00 W = 09:00 - 15:00","S = 09:00 - 21:00 W = 09:00 - 17:00",null]
2 ExtractDate "07/07/2014 00:00" is not a "date"
3 ExtractDate "2014-07-07 00:00" is not a "date"
4 ExtractDate "07/07/2014 00:00" is not a "date"
5 ExtractDate "07/07/2014 00:00" is not a "date"
6 ExtractDate "07/07/2014 00:00" is not a "date"
7 ExtractDate "07/07/2014 00:00" is not a "date"
8 ExtractDate "07/07/2014 00:00" is not a "date"
9 ExtractDate "07/07/2014 00:00" is not a "date"
10 ExtractDate "07/07/2014 00:00" is not a "date"
11 ExtractDate "07/07/2014 00:00" is not a "date"
12 ExtractDate "07/07/2014 00:00" is not a "date"
13 ExtractDate "07/07/2014 00:00" is not a "date"
14 ExtractDate "07/07/2014 00:00" is not a "date"
15 ExtractDate "07/07/2014 00:00" is not a "date"
"#;
// expecting invalid rows, so confirm there ARE output files generated
let validation_error_path = &wrk.path("adur-public-toilets.csv.validation-errors.tsv");
println!("expecting validation error file at: {validation_error_path:?}");
assert!(Path::new(validation_error_path).exists());
assert!(Path::new(&wrk.path("adur-public-toilets.csv.valid")).exists());
assert!(Path::new(&wrk.path("adur-public-toilets.csv.invalid")).exists());
// check validation error output
let validation_error_output: String =
wrk.from_str(&wrk.path("adur-public-toilets.csv.validation-errors.tsv"));
assert!(!validation_error_output.is_empty());
assert_eq!(
validation_errors_expected.to_string(),
validation_error_output
);
wrk.assert_err(&mut cmd2);
}
#[test]
#[file_serial]
fn generate_schema_with_optional_flags_trim_and_validate_with_errors() {
// create workspace and invoke schema command with value constraints flag
let wrk = Workdir::new("generate_schema_with_optional_flags_trim_and_validate_with_errors")
.flexible(true);
wrk.clear_contents().unwrap();
// copy csv file to workdir
let csv = wrk.load_test_resource("adur-public-toilets.csv");
wrk.create_from_string("adur-public-toilets.csv", &csv);
// run schema command with value constraints option
let mut cmd = wrk.command("schema");
cmd.arg("adur-public-toilets.csv");
cmd.arg("--enum-threshold");
cmd.arg("13");
cmd.arg("--pattern-columns");
cmd.arg("ReportEmail,OpeningHours");
cmd.arg("--strict-dates");
wrk.output(&mut cmd);
// load output schema file
let output_schema_string: String =
wrk.from_str(&wrk.path("adur-public-toilets.csv.schema.json"));
let output_schema_json =
serde_json::from_str(&output_schema_string).expect("parse schema json");
// make sure it's a valid JSON Schema by compiling with jsonschema library
jsonschema::JSONSchema::options()
.compile(&output_schema_json)
.expect("valid JSON Schema");
// diff output json with expected json
let expected_schema: String =
wrk.load_test_resource("adur-public-toilets.csv.schema-strict.expected.json");
let expected_schema_json: Value = serde_json::from_str(&expected_schema).unwrap();
assert_json_eq!(expected_schema_json, output_schema_json);
// invoke validate command from schema created above
let mut cmd2 = wrk.command("validate");
cmd2.arg("adur-public-toilets.csv");
cmd2.arg("--trim");
cmd2.arg("adur-public-toilets.csv.schema.json");
wrk.output(&mut cmd2);
// validation report
let validation_errors_expected = r#"row_number field error
2 ExtractDate "07/07/2014 00:00" is not a "date"
3 ExtractDate "2014-07-07 00:00" is not a "date"
4 ExtractDate "07/07/2014 00:00" is not a "date"
5 ExtractDate "07/07/2014 00:00" is not a "date"
6 ExtractDate "07/07/2014 00:00" is not a "date"
7 ExtractDate "07/07/2014 00:00" is not a "date"
8 ExtractDate "07/07/2014 00:00" is not a "date"
9 ExtractDate "07/07/2014 00:00" is not a "date"
10 ExtractDate "07/07/2014 00:00" is not a "date"
11 ExtractDate "07/07/2014 00:00" is not a "date"
12 ExtractDate "07/07/2014 00:00" is not a "date"
13 ExtractDate "07/07/2014 00:00" is not a "date"
14 ExtractDate "07/07/2014 00:00" is not a "date"
15 ExtractDate "07/07/2014 00:00" is not a "date"
"#;
// expecting invalid rows, so confirm there ARE output files generated
let validation_error_path = &wrk.path("adur-public-toilets.csv.validation-errors.tsv");
println!("expecting validation error file at: {validation_error_path:?}");
assert!(Path::new(validation_error_path).exists());
assert!(Path::new(&wrk.path("adur-public-toilets.csv.valid")).exists());
assert!(Path::new(&wrk.path("adur-public-toilets.csv.invalid")).exists());
// check validation error output
let validation_error_output: String =
wrk.from_str(&wrk.path("adur-public-toilets.csv.validation-errors.tsv"));
assert!(!validation_error_output.is_empty());
assert_eq!(
validation_errors_expected.to_string(),
validation_error_output
);
wrk.assert_err(&mut cmd2);
}
#[test]
#[file_serial]
fn generate_schema_with_defaults_to_stdout() {
// create workspace and invoke schema command with value constraints flag
let wrk = Workdir::new("generate_schema_with_defaults_to_stdout").flexible(true);
wrk.clear_contents().unwrap();
// copy csv file to workdir
let csv = wrk.load_test_resource("adur-public-toilets.csv");
wrk.create_from_string("adur-public-toilets.csv", &csv);
// run schema command
let mut cmd = wrk.command("schema");
cmd.arg("adur-public-toilets.csv");
let _stdout = wrk.stdout::<String>(&mut cmd);
let schema_json_file = wrk
.path("adur-public-toilets.csv.schema.json")
.to_string_lossy()
.into_owned();
// load schema_json_file into a string
let schema_json = std::fs::read_to_string(&schema_json_file).unwrap();
let output_schema_json: Value = serde_json::from_str(&schema_json).unwrap();
// diff output json with expected json
let expected_schema: String =
wrk.load_test_resource("adur-public-toilets.csv.schema-default.expected.json");
let expected_schema_json: Value = serde_json::from_str(&expected_schema).unwrap();
assert_json_eq!(expected_schema_json, output_schema_json);
}