Test for column index

This commit is contained in:
Yuriy Dupyn 2023-12-29 08:14:29 +01:00
parent 25b26acde0
commit 8b9249be36

View file

@ -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() {