Skip to content

Commit

Permalink
test: protocol check handles versions
Browse files Browse the repository at this point in the history
  • Loading branch information
roeap committed Nov 5, 2023
1 parent 4663c64 commit db8b5e2
Showing 1 changed file with 142 additions and 0 deletions.
142 changes: 142 additions & 0 deletions crates/deltalake-core/src/operations/transaction/protocol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,3 +136,145 @@ pub static INSTANCE: Lazy<ProtocolChecker> = Lazy::new(|| {

ProtocolChecker::new(reader_features, writer_features)
});

#[cfg(test)]
mod tests {
use super::*;
use crate::kernel::{Action, Protocol};

#[test]
fn test_versions() {
let checker_1 = ProtocolChecker::new(HashSet::new(), HashSet::new());
let actions = vec![Action::Protocol(Protocol {
min_reader_version: 1,
min_writer_version: 1,
..Default::default()
})];
let snapshot_1 = DeltaTableState::from_actions(actions, 1).unwrap();
assert!(checker_1.can_read_from(&snapshot_1).is_ok());
assert!(checker_1.can_write_to(&snapshot_1).is_ok());

let checker_2 = ProtocolChecker::new(READER_V2.clone(), HashSet::new());
let actions = vec![Action::Protocol(Protocol {
min_reader_version: 2,
min_writer_version: 1,
..Default::default()
})];
let snapshot_2 = DeltaTableState::from_actions(actions, 1).unwrap();
assert!(checker_1.can_read_from(&snapshot_2).is_err());
assert!(checker_1.can_write_to(&snapshot_2).is_err());
assert!(checker_2.can_read_from(&snapshot_1).is_ok());
assert!(checker_2.can_read_from(&snapshot_2).is_ok());
assert!(checker_2.can_write_to(&snapshot_2).is_ok());

let checker_3 = ProtocolChecker::new(READER_V2.clone(), WRITER_V2.clone());
let actions = vec![Action::Protocol(Protocol {
min_reader_version: 2,
min_writer_version: 2,
..Default::default()
})];
let snapshot_3 = DeltaTableState::from_actions(actions, 1).unwrap();
assert!(checker_1.can_read_from(&snapshot_3).is_err());
assert!(checker_1.can_write_to(&snapshot_3).is_err());
assert!(checker_2.can_read_from(&snapshot_3).is_ok());
assert!(checker_2.can_write_to(&snapshot_3).is_err());
assert!(checker_3.can_read_from(&snapshot_1).is_ok());
assert!(checker_3.can_read_from(&snapshot_2).is_ok());
assert!(checker_3.can_read_from(&snapshot_3).is_ok());
assert!(checker_3.can_write_to(&snapshot_3).is_ok());

let checker_4 = ProtocolChecker::new(READER_V2.clone(), WRITER_V3.clone());
let actions = vec![Action::Protocol(Protocol {
min_reader_version: 2,
min_writer_version: 3,
..Default::default()
})];
let snapshot_4 = DeltaTableState::from_actions(actions, 1).unwrap();
assert!(checker_1.can_read_from(&snapshot_4).is_err());
assert!(checker_1.can_write_to(&snapshot_4).is_err());
assert!(checker_2.can_read_from(&snapshot_4).is_ok());
assert!(checker_2.can_write_to(&snapshot_4).is_err());
assert!(checker_3.can_read_from(&snapshot_4).is_ok());
assert!(checker_3.can_write_to(&snapshot_4).is_err());
assert!(checker_4.can_read_from(&snapshot_1).is_ok());
assert!(checker_4.can_read_from(&snapshot_2).is_ok());
assert!(checker_4.can_read_from(&snapshot_3).is_ok());
assert!(checker_4.can_read_from(&snapshot_4).is_ok());
assert!(checker_4.can_write_to(&snapshot_4).is_ok());

let checker_5 = ProtocolChecker::new(READER_V2.clone(), WRITER_V4.clone());
let actions = vec![Action::Protocol(Protocol {
min_reader_version: 2,
min_writer_version: 4,
..Default::default()
})];
let snapshot_5 = DeltaTableState::from_actions(actions, 1).unwrap();
assert!(checker_1.can_read_from(&snapshot_5).is_err());
assert!(checker_1.can_write_to(&snapshot_5).is_err());
assert!(checker_2.can_read_from(&snapshot_5).is_ok());
assert!(checker_2.can_write_to(&snapshot_5).is_err());
assert!(checker_3.can_read_from(&snapshot_5).is_ok());
assert!(checker_3.can_write_to(&snapshot_5).is_err());
assert!(checker_4.can_read_from(&snapshot_5).is_ok());
assert!(checker_4.can_write_to(&snapshot_5).is_err());
assert!(checker_5.can_read_from(&snapshot_1).is_ok());
assert!(checker_5.can_read_from(&snapshot_2).is_ok());
assert!(checker_5.can_read_from(&snapshot_3).is_ok());
assert!(checker_5.can_read_from(&snapshot_4).is_ok());
assert!(checker_5.can_read_from(&snapshot_5).is_ok());
assert!(checker_5.can_write_to(&snapshot_5).is_ok());

let checker_6 = ProtocolChecker::new(READER_V2.clone(), WRITER_V5.clone());
let actions = vec![Action::Protocol(Protocol {
min_reader_version: 2,
min_writer_version: 5,
..Default::default()
})];
let snapshot_6 = DeltaTableState::from_actions(actions, 1).unwrap();
assert!(checker_1.can_read_from(&snapshot_6).is_err());
assert!(checker_1.can_write_to(&snapshot_6).is_err());
assert!(checker_2.can_read_from(&snapshot_6).is_ok());
assert!(checker_2.can_write_to(&snapshot_6).is_err());
assert!(checker_3.can_read_from(&snapshot_6).is_ok());
assert!(checker_3.can_write_to(&snapshot_6).is_err());
assert!(checker_4.can_read_from(&snapshot_6).is_ok());
assert!(checker_4.can_write_to(&snapshot_6).is_err());
assert!(checker_5.can_read_from(&snapshot_6).is_ok());
assert!(checker_5.can_write_to(&snapshot_6).is_err());
assert!(checker_6.can_read_from(&snapshot_1).is_ok());
assert!(checker_6.can_read_from(&snapshot_2).is_ok());
assert!(checker_6.can_read_from(&snapshot_3).is_ok());
assert!(checker_6.can_read_from(&snapshot_4).is_ok());
assert!(checker_6.can_read_from(&snapshot_5).is_ok());
assert!(checker_6.can_read_from(&snapshot_6).is_ok());
assert!(checker_6.can_write_to(&snapshot_6).is_ok());

let checker_7 = ProtocolChecker::new(READER_V2.clone(), WRITER_V6.clone());
let actions = vec![Action::Protocol(Protocol {
min_reader_version: 2,
min_writer_version: 6,
..Default::default()
})];
let snapshot_7 = DeltaTableState::from_actions(actions, 1).unwrap();
assert!(checker_1.can_read_from(&snapshot_7).is_err());
assert!(checker_1.can_write_to(&snapshot_7).is_err());
assert!(checker_2.can_read_from(&snapshot_7).is_ok());
assert!(checker_2.can_write_to(&snapshot_7).is_err());
assert!(checker_3.can_read_from(&snapshot_7).is_ok());
assert!(checker_3.can_write_to(&snapshot_7).is_err());
assert!(checker_4.can_read_from(&snapshot_7).is_ok());
assert!(checker_4.can_write_to(&snapshot_7).is_err());
assert!(checker_5.can_read_from(&snapshot_7).is_ok());
assert!(checker_5.can_write_to(&snapshot_7).is_err());
assert!(checker_6.can_read_from(&snapshot_7).is_ok());
assert!(checker_6.can_write_to(&snapshot_7).is_err());
assert!(checker_7.can_read_from(&snapshot_1).is_ok());
assert!(checker_7.can_read_from(&snapshot_2).is_ok());
assert!(checker_7.can_read_from(&snapshot_3).is_ok());
assert!(checker_7.can_read_from(&snapshot_4).is_ok());
assert!(checker_7.can_read_from(&snapshot_5).is_ok());
assert!(checker_7.can_read_from(&snapshot_6).is_ok());
assert!(checker_7.can_read_from(&snapshot_7).is_ok());
assert!(checker_7.can_write_to(&snapshot_7).is_ok());
}
}

0 comments on commit db8b5e2

Please sign in to comment.