From 8b9249be36853f402251303b82614fda9bbacc64 Mon Sep 17 00:00:00 2001 From: Yuriy Dupyn <2153100+omedusyo@users.noreply.github.com> Date: Fri, 29 Dec 2023 08:14:29 +0100 Subject: [PATCH] Test for column index --- minisql/src/interpreter.rs | 68 +++++++++++++++++++++++++++++++++++++- 1 file changed, 67 insertions(+), 1 deletion(-) diff --git a/minisql/src/interpreter.rs b/minisql/src/interpreter.rs index b5a98b5..9b9f818 100644 --- a/minisql/src/interpreter.rs +++ b/minisql/src/interpreter.rs @@ -480,7 +480,73 @@ mod tests { assert!(row[2] == age1); } - // TODO: Test CreateIndex + #[test] + fn test_index() { + use ColumnSelection::*; + use Condition::*; + use IndexableValue::*; + use Operation::*; + use Value::*; + + let mut state = State::new(); + let users_schema = users_schema(); + let users = users_schema.table_name.clone(); + + state + .interpret(CreateTable(users.clone(), users_schema)) + .unwrap(); + + state.interpret(CreateIndex(users.clone(), "name".to_string())).unwrap(); + + let (id0, name0, age0) = ( + Indexable(Uuid(0)), + Indexable(String("Plato".to_string())), + Indexable(Int(64)), + ); + state + .interpret(Insert( + users.clone(), + vec![ + ("id".to_string(), id0.clone()), + ("name".to_string(), name0.clone()), + ("age".to_string(), age0.clone()), + ], + )) + .unwrap(); + + let (id1, name1, age1) = ( + Indexable(Uuid(1)), + Indexable(String("Aristotle".to_string())), + Indexable(Int(20)), + ); + state + .interpret(Insert( + users.clone(), + vec![ + ("id".to_string(), id1.clone()), + ("name".to_string(), name1.clone()), + ("age".to_string(), age1.clone()), + ], + )) + .unwrap(); + + assert!(state.tables.len() == 1); + let table = &state.tables[0]; + assert!(table.rows.len() == 2); + + let user: ColumnPosition = 1; + assert!(table.indexes.contains_key(&user)); + + let index = table.indexes.get(&user).unwrap(); + + let plato_id = 0; + let aristotle_id = 1; + + let plato_ids = index.get(&String("Plato".to_string())); + assert!(plato_ids.contains(&plato_id)); + assert!(!plato_ids.contains(&aristotle_id)); + assert!(plato_ids.len() == 1); + } } pub fn example() {