csv-validation 0.0.2__tar.gz → 0.0.3__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -83,7 +83,7 @@ dependencies = [
83
83
 
84
84
  [[package]]
85
85
  name = "csv_validation"
86
- version = "0.0.2"
86
+ version = "0.0.3"
87
87
  dependencies = [
88
88
  "csv",
89
89
  "flate2",
@@ -1,6 +1,6 @@
1
1
  [package]
2
2
  name = "csv_validation"
3
- version = "0.0.2"
3
+ version = "0.0.3"
4
4
  edition = "2021"
5
5
 
6
6
  # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: csv_validation
3
- Version: 0.0.2
3
+ Version: 0.0.3
4
4
  Classifier: Programming Language :: Rust
5
5
  Classifier: Programming Language :: Python :: Implementation :: CPython
6
6
  Classifier: Programming Language :: Python :: Implementation :: PyPy
@@ -1,3 +1,4 @@
1
+ use std::collections::HashSet;
1
2
  use std::fs;
2
3
  use std::fs::File;
3
4
  use std::io::{BufReader, Read};
@@ -140,15 +141,31 @@ fn get_validations(definition_path: &str) -> Vec<ColumnValidations> {
140
141
  }
141
142
 
142
143
  fn validate_column_names(reader: &mut Reader<Box<dyn Read>>, validations: &Vec<ColumnValidations>) -> bool {
143
- let column_names = validations.iter()
144
+ let expected_column_names = validations.iter()
144
145
  .map(|v| v.column_name.clone())
145
146
  .collect::<Vec<String>>();
146
- debug!("Expected Column Names: {:?}", column_names);
147
+ debug!("Expected Column Names: {:?}", expected_column_names);
147
148
 
148
- let headers: Vec<&str> = reader.headers().unwrap().iter().collect();
149
+ let headers: Vec<String> = reader.headers().unwrap().iter().map(|s| String::from(s) ).collect();
149
150
  debug!("Actual Column Names: {:?}", headers);
150
151
 
151
- column_names == headers
152
+ if expected_column_names != headers {
153
+ if expected_column_names.len() != headers.len() {
154
+ let expected_columns_set: HashSet<String> = expected_column_names.iter().cloned().collect();
155
+ let headers_set: HashSet<String> = headers.iter().cloned().collect();
156
+ debug!("File headers not in expected columns: {:?}", headers_set.difference(&expected_columns_set));
157
+ debug!("Columns in expected columns not in file headers: {:?}", expected_columns_set.difference(&headers_set));
158
+ }
159
+ else {
160
+ for (expected_column, header) in zip(expected_column_names, headers) {
161
+ if expected_column != header {
162
+ debug!("{:?} != {:?}", expected_column, header);
163
+ }
164
+ }
165
+ }
166
+ return false
167
+ }
168
+ true
152
169
  }
153
170
 
154
171
  /// A Python module implemented in Rust.
@@ -178,4 +195,9 @@ mod tests {
178
195
  fn test_validate_csv_gz() {
179
196
  assert!(validate("test/test_file.csv.gz", "test/test_validations.yml").unwrap());
180
197
  }
198
+
199
+ #[test]
200
+ fn test_wrong_headers() {
201
+ assert!(!validate("test/test_file.csv", "test/test_validations_wrong_headers.yml").unwrap());
202
+ }
181
203
  }
@@ -0,0 +1,5 @@
1
+ columns:
2
+ - name: First Column
3
+ - name: Second Column
4
+ - name: Wrong Column
5
+ - name: Expected Column Not In File
File without changes