use crate::report::flow::Header;
use crate::report::model::ReportResult;
pub struct DryRunReport {
pub rows: usize,
pub result: ReportResult,
pub header: Header,
pub errors: Vec<String>,
pub var_warnings: Vec<String>,
}
impl DryRunReport {
pub fn from_result(result: ReportResult, header: Header, var_warnings: Vec<String>) -> Self {
let mut seen = std::collections::HashSet::new();
let errors: Vec<String> = result
.errors
.iter()
.filter(|e| seen.insert((*e).clone()))
.cloned()
.collect();
let rows = result.rows.len();
Self {
rows,
result,
header,
errors,
var_warnings,
}
}
}
#[cfg(test)]
mod tests {
use super::DryRunReport;
use crate::report::model::{ReportResult, ReportRow};
fn result_with(errors: Vec<&str>, rows: usize) -> ReportResult {
ReportResult {
rows: (0..rows).map(|_| ReportRow::default()).collect(),
errors: errors.into_iter().map(String::from).collect(),
..Default::default()
}
}
#[test]
fn a_preview_counts_the_rows_the_run_would_emit() {
let preview =
DryRunReport::from_result(result_with(Vec::new(), 6), Default::default(), Vec::new());
assert_eq!(
preview.rows, 6,
"the projected row count is the whole point of a dry run"
);
}
#[test]
fn a_problem_repeated_by_every_iteration_is_only_reported_once() {
let preview = DryRunReport::from_result(
result_with(
vec![
"no such request: login",
"no such request: login",
"empty glob",
],
3,
),
Default::default(),
Vec::new(),
);
assert_eq!(
preview.errors,
vec![
"no such request: login".to_string(),
"empty glob".to_string()
],
"duplicates are collapsed, and first-seen order is kept so the causes read in flow order"
);
}
#[test]
fn variable_warnings_are_carried_through_untouched() {
let warnings = vec!["{{TOKEN}} may not be set".to_string()];
let preview = DryRunReport::from_result(
result_with(Vec::new(), 1),
Default::default(),
warnings.clone(),
);
assert_eq!(preview.var_warnings, warnings);
}
}