Skip to content

Commit

Permalink
tests: replace comparing hashes to using a proper diff library
Browse files Browse the repository at this point in the history
  • Loading branch information
serprex committed Apr 30, 2024
1 parent 329c121 commit 79f0e1e
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 44 deletions.
21 changes: 7 additions & 14 deletions nexus/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion nexus/server/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,4 @@ cargo-deb = "2.0"

[dev-dependencies]
postgres = "0.19.4"
sha256 = "1.0.3"
similar = "2"
58 changes: 29 additions & 29 deletions nexus/server/tests/server_test.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use postgres::{Client, NoTls, SimpleQueryMessage};
use std::{
fs::{read_dir, File},
io::{prelude::*, BufReader, Write},
Expand All @@ -7,7 +6,12 @@ use std::{
thread,
time::Duration,
};

use postgres::{Client, NoTls, SimpleQueryMessage};
use similar::{ChangeTag, TextDiff};

mod create_peers;

fn input_files() -> Vec<String> {
let sql_directory = read_dir("tests/sql").unwrap();
sql_directory
Expand Down Expand Up @@ -112,8 +116,8 @@ fn server_test() {
let expected_output_path = ["tests/results/expected/", file, ".out"].concat();
let mut output_file = File::create(["tests/results/actual/", file, ".out"].concat())
.expect("Unable to create result file");

for query in queries {
let mut output = Vec::new();
dbg!(query.as_str());

// filter out comments and empty lines
Expand All @@ -132,14 +136,13 @@ fn server_test() {
match res[0] {
// Fetch column names for the output
SimpleQueryMessage::Row(ref simplerow) => {
for column in simplerow.columns() {
column_names.push(column.name());
}
column_names.extend(simplerow.columns().iter().map(|column| column.name()));
}
SimpleQueryMessage::CommandComplete(_x) => (),
_ => (),
};

let mut output = String::new();
res.iter().for_each(|row| {
for column_head in &column_names {
let row_parse = match row {
Expand All @@ -148,43 +151,40 @@ fn server_test() {
_ => None,
};

let row_value = match row_parse {
None => {
continue;
}
Some(x) => x,
};
output.push(row_value.to_owned());
if let Some(row_value) = row_parse {
output.push_str(row_value);
}
}
});

for i in &output {
let output_line = (*i).as_bytes();
output_file
.write_all(output_line)
.expect("Unable to write query output");
output_file
.write_all("\n".as_bytes())
.expect("Output file write failure");
}
output.push('\n');
output_file
.write_all(output.as_bytes())
.expect("Unable to write query output");

// flush the output file
output_file.flush().expect("Unable to flush output file");
}

// Compare hash of expected and obtained files
let obtained_file = std::fs::read(&actual_output_path).unwrap();
let expected_file = std::fs::read(&expected_output_path).unwrap();
let obtained_hash = sha256::digest(obtained_file.as_slice());
let expected_hash = sha256::digest(expected_file.as_slice());

let obtained_file = std::fs::read_to_string(&actual_output_path).unwrap();
let expected_file = std::fs::read_to_string(&expected_output_path).unwrap();
// if there is a mismatch, print the diff, along with the path.
if obtained_hash != expected_hash {
if obtained_file != expected_file {
let diff = TextDiff::from_lines(&obtained_file, &expected_file);
for change in diff.iter_all_changes() {
let sign = match change.tag() {
ChangeTag::Delete => "-",
ChangeTag::Insert => "+",
ChangeTag::Equal => " ",
};
print!("{}{}", sign, change);
}

tracing::info!("expected: {expected_output_path}");
tracing::info!("obtained: {actual_output_path}");
panic!("result didn't match expected output");
}

assert_eq!(obtained_hash, expected_hash);
});
}

Expand Down

0 comments on commit 79f0e1e

Please sign in to comment.