Skip to content

Commit

Permalink
Merge pull request #32 from VolumeGraphics/compare_csv_header
Browse files Browse the repository at this point in the history
Compare csv header
  • Loading branch information
ChrisRega committed Mar 30, 2023
2 parents 09319bf + eacbeed commit b56788b
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 5 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,7 @@ rules:
- Add file metadata comparison
- Add external checking
- Add and adjust reports for file metadata comparison and external checking
- Add csv header comparison. Previously, the report only marks the differences but not returning any error.
### 0.2.4
- add check for row lines of both compared csv files, and throw error if they are unequal
Expand Down
63 changes: 62 additions & 1 deletion src/csv/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,10 @@ pub(crate) enum DiffType {
actual: Value,
position: Position,
},
UnequalHeader {
nominal: String,
actual: String,
},
}

impl Display for DiffType {
Expand Down Expand Up @@ -125,6 +129,14 @@ impl Display for DiffType {
)
.unwrap_or_default();
}
DiffType::UnequalHeader { nominal, actual } => {
write!(
f,
"Different header strings -- Expected {}, Found {}",
nominal, actual
)
.unwrap_or_default();
}
};
Ok(())
}
Expand Down Expand Up @@ -361,6 +373,15 @@ pub(crate) fn compare_tables(
.zip(actual.columns.iter())
.enumerate()
{
if let (Some(nom_header), Some(act_header)) = (&col_nom.header, &col_act.header) {
if nom_header != act_header {
diffs.extend(vec![DiffType::UnequalHeader {
nominal: nom_header.to_owned(),
actual: act_header.to_owned(),
}]);
}
}

for (row, (val_nom, val_act)) in col_nom.rows.iter().zip(col_act.rows.iter()).enumerate() {
let position = Position { row, col };
let diffs_field = compare_values(val_nom, val_act, config, position)?;
Expand Down Expand Up @@ -490,7 +511,10 @@ pub(crate) fn compare_paths(
#[cfg(test)]
mod tests {
use super::*;
use crate::csv::DiffType::{DifferentValueTypes, OutOfTolerance, UnequalStrings};
use crate::csv::DiffType::{
DifferentValueTypes, OutOfTolerance, UnequalHeader, UnequalStrings,
};
use crate::csv::Preprocessor::ExtractHeaders;
use std::io::Cursor;

const NOMINAL: &str = "nominal";
Expand Down Expand Up @@ -630,6 +654,43 @@ mod tests {
}
}

#[test]
fn header_diffs_on_table_level() {
let config = CSVCompareConfig {
preprocessing: Some(vec![ExtractHeaders]),
exclude_field_regex: None,
comparison_modes: vec![],
delimiters: Delimiters::default(),
};

let mut actual = Table::from_reader(
File::open("tests/csv/data/Annotations.csv").unwrap(),
&config.delimiters,
)
.unwrap();

ExtractHeaders.process(&mut actual).unwrap();

let mut nominal = Table::from_reader(
File::open("tests/csv/data/Annotations_diff.csv").unwrap(),
&config.delimiters,
)
.unwrap();

ExtractHeaders.process(&mut nominal).unwrap();

let diff = compare_tables(&nominal, &actual, &config).unwrap();
assert_eq!(diff.len(), 3);

let first_diff = diff.first().unwrap();
if let UnequalHeader { nominal, actual } = first_diff {
assert_eq!(nominal, "Position x [mm]");
assert_eq!(actual, "Pos. x [mm]");
} else {
unreachable!();
}
}

#[test]
fn different_type_search_only() {
let config = CSVCompareConfig {
Expand Down
4 changes: 4 additions & 0 deletions src/report/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,9 @@ pub(crate) fn write_csv_detail(
DiffType::UnequalStrings { position, .. } => position,
DiffType::OutOfTolerance { position, .. } => position,
DiffType::DifferentValueTypes { position, .. } => position,
_ => {
return false;
}
};

position.row == current_pos.row && position.col == current_pos.col
Expand All @@ -222,6 +225,7 @@ pub(crate) fn write_csv_detail(
DiffType::DifferentValueTypes { .. } => {
"Different value types".to_owned()
}
_ => "Unknown difference".to_owned(),
})
.collect(),
};
Expand Down
8 changes: 4 additions & 4 deletions src/report/template.rs
Original file line number Diff line number Diff line change
Expand Up @@ -337,14 +337,14 @@ pub const PLAIN_CSV_DETAIL_TEMPLATE: &str = r###"
<table id="report" class="cell-border">
<thead>
{% if headers.columns|length > 0 %}
<tr>
<th>{% if headers.has_diff %}<br>&nbsp;{% endif %}</th>
<th>{% if headers.has_diff %}&nbsp;<br>{% endif %}</th>
<tr {% if headers.has_diff %} class="error" {% endif %}>
<th>{% if headers.has_diff %}x<br>&nbsp;{% endif %}</th>
<th>{% if headers.has_diff %}&nbsp;<br>x{% endif %}</th>
{% for col in headers.columns %}
<th>
{{ col.nominal_value }}
{% if headers.has_diff %}
<div class="{% if col.nominal_value != col.actual_value %} actual {% endif %}">{{ col.actual_value }}</div>
<div class="{% if col.nominal_value != col.actual_value %} diffs {% endif %}">{{ col.actual_value }}</div>
{% endif %}
</th>
{% endfor %}
Expand Down
7 changes: 7 additions & 0 deletions tests/csv/data/Annotations_diff.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Name,Position x [mm],Position y [mm],Position z [mm],Deviation [mm],Min. Deviation [mm],Max. Deviation [mm],Description,Image,Focused image,Captures,Tolerance status,Type
Deviation 5,-22.663068771,39.907196045,-49.257804871,>= 0.600000024,n/a,n/a,,Off,Off,0,No tolerance,Rule based
Deviation 4,-22.826931000,39.911468506,-20.156715393,>= 0.600000024,n/a,n/a,,Off,Off,0,No tolerance,Rule based
Deviation 3,-22.591037750,39.901805878,-33.957965851,>= 0.600000024,n/a,n/a,,Off,Off,0,No tolerance,Rule based
Deviation 2,-22.739957809,39.908485413,-65.457649231,>= 0.600000024,n/a,n/a,,Off,Off,0,No tolerance,Rule based
Deviation 1,-22.650812149,41.070060730,-80.756362915,>= 0.600000024,n/a,n/a,,Off,Off,0,No tolerance,Rule based

0 comments on commit b56788b

Please sign in to comment.