use crate::engine::VertexEditor;
use crate::engine::graph::DependencyGraph;
use crate::reference::{CellRef, Coord};
use formualizer_common::LiteralValue;
use formualizer_parse::parse;
fn lit_num(value: f64) -> LiteralValue {
LiteralValue::Number(value)
}
fn sheet1_cell(graph: &DependencyGraph, row: u32, col: u32) -> CellRef {
let sid = graph.sheet_id("Sheet1").unwrap();
CellRef::new(sid, Coord::from_excel(row, col, true, true))
}
#[test]
fn test_insert_rows() {
let mut graph = super::common::graph_truth_graph();
graph.set_cell_value("Sheet1", 1, 1, lit_num(10.0)).unwrap();
graph.set_cell_value("Sheet1", 2, 1, lit_num(20.0)).unwrap();
graph.set_cell_value("Sheet1", 3, 1, lit_num(30.0)).unwrap();
let sum_result = graph
.set_cell_formula("Sheet1", 4, 1, parse("=SUM(A1:A3)").unwrap())
.unwrap();
let sum_id = sum_result.affected_vertices[0];
let a1_id = *graph
.get_vertex_id_for_address(&sheet1_cell(&graph, 1, 1))
.unwrap();
let a2_id = *graph
.get_vertex_id_for_address(&sheet1_cell(&graph, 2, 1))
.unwrap();
let a3_id = *graph
.get_vertex_id_for_address(&sheet1_cell(&graph, 3, 1))
.unwrap();
let mut editor = VertexEditor::new(&mut graph);
let summary = editor.insert_rows(0, 1, 2).unwrap();
drop(editor);
assert_eq!(
graph.get_vertex_id_for_address(&sheet1_cell(&graph, 1, 1)),
Some(&a1_id)
);
assert_eq!(
graph.get_vertex_id_for_address(&sheet1_cell(&graph, 4, 1)),
Some(&a2_id)
);
assert_eq!(
graph.get_vertex_id_for_address(&sheet1_cell(&graph, 5, 1)),
Some(&a3_id)
);
assert_eq!(
graph.get_vertex_id_for_address(&sheet1_cell(&graph, 6, 1)),
Some(&sum_id)
);
let formula = graph.get_formula(sum_id);
assert!(formula.is_some());
assert_eq!(summary.vertices_moved.len(), 3); assert_eq!(summary.formulas_updated, 1); }
#[test]
fn test_delete_rows() {
let mut graph = super::common::graph_truth_graph();
for i in 1..=5 {
graph
.set_cell_value("Sheet1", i, 1, lit_num(i as f64 * 10.0))
.unwrap();
}
let a1_id = *graph
.get_vertex_id_for_address(&sheet1_cell(&graph, 1, 1))
.unwrap();
let a4_id = *graph
.get_vertex_id_for_address(&sheet1_cell(&graph, 4, 1))
.unwrap();
let a5_id = *graph
.get_vertex_id_for_address(&sheet1_cell(&graph, 5, 1))
.unwrap();
let formula_result = graph
.set_cell_formula("Sheet1", 7, 1, parse("=SUM(A1:A5)").unwrap())
.unwrap();
let mut editor = VertexEditor::new(&mut graph);
let summary = editor.delete_rows(0, 1, 2).unwrap();
drop(editor);
assert_eq!(
graph.get_vertex_id_for_address(&sheet1_cell(&graph, 1, 1)),
Some(&a1_id)
);
assert_eq!(
graph.get_vertex_id_for_address(&sheet1_cell(&graph, 2, 1)),
Some(&a4_id)
);
assert_eq!(
graph.get_vertex_id_for_address(&sheet1_cell(&graph, 3, 1)),
Some(&a5_id)
);
assert!(
graph
.get_vertex_id_for_address(&sheet1_cell(&graph, 4, 1))
.is_none()
);
assert_eq!(summary.vertices_deleted.len(), 2);
assert_eq!(summary.vertices_moved.len(), 3); }
#[test]
fn test_insert_rows_adjusts_formulas() {
let mut graph = super::common::graph_truth_graph();
graph.set_cell_value("Sheet1", 1, 1, lit_num(10.0)).unwrap();
graph.set_cell_value("Sheet1", 3, 1, lit_num(30.0)).unwrap();
graph
.set_cell_formula("Sheet1", 1, 2, parse("=A1*2").unwrap())
.unwrap();
let b3_result = graph
.set_cell_formula("Sheet1", 3, 2, parse("=A3+5").unwrap())
.unwrap();
let b3_id = b3_result.affected_vertices[0];
let mut editor = VertexEditor::new(&mut graph);
editor.insert_rows(0, 1, 1).unwrap();
drop(editor);
let b4_formula = graph.get_formula(b3_id);
assert!(b4_formula.is_some());
}
#[test]
fn test_delete_row_creates_ref_error() {
let mut graph = super::common::graph_truth_graph();
graph.set_cell_value("Sheet1", 1, 1, lit_num(10.0)).unwrap();
graph.set_cell_value("Sheet1", 2, 1, lit_num(20.0)).unwrap();
let b2_result = graph
.set_cell_formula("Sheet1", 2, 2, parse("=A2*2").unwrap())
.unwrap();
let b2_id = b2_result.affected_vertices[0];
let mut editor = VertexEditor::new(&mut graph);
editor.delete_rows(0, 1, 1).unwrap();
drop(editor);
assert!(graph.is_deleted(b2_id));
assert!(
graph
.get_vertex_id_for_address(&sheet1_cell(&graph, 2, 1))
.is_none()
);
}
#[test]
fn test_insert_rows_with_absolute_references() {
let mut graph = super::common::graph_truth_graph();
graph
.set_cell_value("Sheet1", 1, 1, lit_num(100.0))
.unwrap();
graph
.set_cell_value("Sheet1", 5, 1, lit_num(500.0))
.unwrap();
let formula_result = graph
.set_cell_formula("Sheet1", 5, 2, parse("=$A$1+A5").unwrap())
.unwrap();
let formula_id = formula_result.affected_vertices[0];
let mut editor = VertexEditor::new(&mut graph);
editor.insert_rows(0, 2, 2).unwrap();
drop(editor);
let updated_formula = graph.get_formula(formula_id);
assert!(updated_formula.is_some());
}
#[test]
fn test_multiple_row_operations() {
let mut graph = super::common::graph_truth_graph();
for i in 1..=10 {
graph
.set_cell_value("Sheet1", i, 1, lit_num(i as f64))
.unwrap();
}
let a1_id = *graph
.get_vertex_id_for_address(&sheet1_cell(&graph, 1, 1))
.unwrap();
let mut editor = VertexEditor::new(&mut graph);
editor.begin_batch();
editor.insert_rows(0, 2, 2).unwrap();
editor.delete_rows(0, 9, 1).unwrap();
editor.insert_rows(0, 0, 1).unwrap();
editor.commit_batch();
drop(editor);
assert_eq!(
graph.get_vertex_id_for_address(&sheet1_cell(&graph, 2, 1)),
Some(&a1_id)
);
}