Test delete

This commit is contained in:
Yuriy Dupyn 2023-12-28 12:56:16 +01:00
parent dc3e9b0077
commit 14f4fe4f3b

View file

@ -224,6 +224,7 @@ impl State {
CreateTable(table_name, table_schema) => { CreateTable(table_name, table_schema) => {
let table = Table::new(table_schema); let table = Table::new(table_schema);
self.attach_table(table_name, table); self.attach_table(table_name, table);
// TODO: What about attaching index on the primary column?
Ok(Response::TableCreated) Ok(Response::TableCreated)
}, },
CreateIndex(table_name, column_name) => { CreateIndex(table_name, column_name) => {
@ -389,13 +390,12 @@ impl Table {
fn delete_row_by_id(&mut self, id: UUID) -> usize { fn delete_row_by_id(&mut self, id: UUID) -> usize {
if let Some(row) = self.rows.remove(&id) { if let Some(row) = self.rows.remove(&id) {
let mut something_was_deleted = false;
for (column_position, column_index) in &mut self.indexes { for (column_position, column_index) in &mut self.indexes {
if let DbValue::Indexable(value) = &row[*column_position] { if let DbValue::Indexable(value) = &row[*column_position] {
something_was_deleted = something_was_deleted || column_index.remove(value, id); let _ = column_index.remove(value, id);
}; };
} }
if something_was_deleted { 1 } else { 0 } 1
} else { } else {
0 0
} }
@ -491,6 +491,8 @@ impl Table {
Some(Condition::Eq(eq_column_name, value)) => { Some(Condition::Eq(eq_column_name, value)) => {
let (type_, eq_column_position) = self.schema.get_column(&eq_column_name)?; let (type_, eq_column_position) = self.schema.get_column(&eq_column_name)?;
// TODO: This shouldn't be necessary - we should index the primary column by
// default. The first case shouldn't be a special case!
if self.schema.is_primary(eq_column_position) { if self.schema.is_primary(eq_column_position) {
match value { match value {
DbValue::Indexable(IndexableDbValue::UUID(uuid)) => { DbValue::Indexable(IndexableDbValue::UUID(uuid)) => {
@ -718,11 +720,6 @@ mod tests {
state.interpret(CreateTable(users.clone(), users_schema)).unwrap(); state.interpret(CreateTable(users.clone(), users_schema)).unwrap();
let response0: Response = state.interpret(Select(users.clone(), ColumnSelection::All, None)).unwrap();
assert!(matches!(response0, Response::Selected(_)));
let Response::Selected(rows0) = response0 else { todo!() };
assert!(rows0.len() == 0);
let (id0, name0, age0) = ( let (id0, name0, age0) = (
Indexable(UUID(0)), Indexable(UUID(0)),
Indexable(String("Plato".to_string())), Indexable(String("Plato".to_string())),
@ -790,4 +787,60 @@ mod tests {
assert!(row0[1] == id0); assert!(row0[1] == id0);
} }
} }
#[test]
fn test_delete() {
use DbValue::*;
use IndexableDbValue::*;
use Operation::*;
use ColumnSelection::*;
use Condition::*;
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();
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();
let delete_response: Response = state.interpret(Delete(users.clone(), Some(Eq("id".to_string(), id0.clone())))).unwrap();
// TODO: WTF, why is it 0? Should be 1
assert!(matches!(delete_response, Response::Deleted(1)));
// assert!(matches!(delete_response, Response::Deleted(_)));
let response: Response = state.interpret(Select(users.clone(), All, None)).unwrap();
assert!(matches!(response, Response::Selected(_)));
let Response::Selected(rows) = response else { todo!() };
assert!(rows.len() == 1);
let row = &rows[0];
assert!(row.len() == 3);
assert!(row[0] == id1);
assert!(row[1] == name1);
assert!(row[2] == age1);
}
} }