use tempfile::TempDir;
use uuid::Uuid;
use super::super::SqliteStore;
use super::make_rt;
use kanban_domain::{Board, Card, Column, DataStore};
#[test]
fn test_apply_snapshot_failure_leaves_existing_data_untouched() {
let dir = TempDir::new().unwrap();
let path = dir.path().join("atomic.sqlite3");
let rt = make_rt();
rt.block_on(async {
let store = SqliteStore::open(&path).await.unwrap();
let board = Board::new("Existing", None::<String>);
let board_id = board.id;
let column = Column::new(board.id, "Todo", 0);
let existing_column_id = column.id;
let card = Card::new(board.id, column.id, "Existing card", 0);
let existing_card_id = card.id;
store.upsert_board(board).unwrap();
store.upsert_column(column).unwrap();
store.upsert_card(card).unwrap();
let mut bad_snapshot = store.snapshot().unwrap();
let mut broken_card = bad_snapshot.cards[0].clone();
broken_card.sprint_id = Some(Uuid::new_v4());
bad_snapshot.cards = vec![broken_card];
let result = store.apply_snapshot(bad_snapshot);
assert!(
result.is_err(),
"a dangling sprint_id must fail at commit, not silently succeed"
);
assert!(
store.get_board(board_id).unwrap().is_some(),
"a failed apply_snapshot must roll back the wipe too -- the \
pre-existing board must still be there, not left deleted"
);
assert!(
store.get_column(existing_column_id).unwrap().is_some(),
"the pre-existing column must survive a rolled-back apply_snapshot"
);
assert!(
store.get_card(existing_card_id).unwrap().is_some(),
"the pre-existing card must survive a rolled-back apply_snapshot"
);
});
}