Skip to content

Commit

Permalink
Add tests. Minor refactors.
Browse files Browse the repository at this point in the history
  • Loading branch information
Kakapio committed Apr 9, 2024
1 parent b21bacd commit d641c38
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 7 deletions.
93 changes: 88 additions & 5 deletions src/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,13 +94,14 @@ fn execute_statement(statement: Statement, tb: &mut Table) -> ExecuteResult {
}

fn execute_insert(statement: Statement, table: &mut Table) -> ExecuteResult {
table.data.push(statement.row_instance.expect("Insert is missing row data."));
table
.data
.push(statement.row_instance.expect("Insert is missing row data."));

ExecuteResult::Success(None)
}

fn execute_select(statement: Statement, table: &mut Table) -> ExecuteResult {

// Select didn't specify an instance. Return all data in table.
if statement.row_instance == None {
for row in table.data.iter() {
Expand All @@ -109,8 +110,6 @@ fn execute_select(statement: Statement, table: &mut Table) -> ExecuteResult {
return ExecuteResult::Success(Some(table.data.iter().cloned().collect()));
}



// Select cmd specified an instance of data.
for row in table.data.iter() {
/* I use 'as_ref()' here because otherwise the ownership of row_instance would go to unwrap().
Expand All @@ -121,5 +120,89 @@ fn execute_select(statement: Statement, table: &mut Table) -> ExecuteResult {
}
}

ExecuteResult::Success(Some(table.data.iter().filter(|row| row.id == statement.row_instance.as_ref().unwrap().id).cloned().collect()))
ExecuteResult::Success(Some(
table
.data
.iter()
.filter(|row| row.id == statement.row_instance.as_ref().unwrap().id)
.cloned()
.collect(),
))
}

#[cfg(test)]
mod tests {
use super::*;

/// Helper method to quickly run SQL commands and mutate a table.
fn do_sql_cmd(tb: &mut Table, cmd: &str) {
let mut statement = Statement::default();
prepare_statement(&String::from(cmd), &mut statement);
execute_statement(statement, tb);
}

// Testing whether insert command errors.
#[test]
fn execute_statement_insert() {
let mut table = Table::new();
do_sql_cmd(&mut table, "insert 13 rosh [email protected]");

assert_eq!(
table.data,
vec![Row {
id: 13,
username: String::from("rosh"),
email: String::from("[email protected]")
}]
);
}

// Making sure our test doesn't allow everything to pass.
#[test]
fn execute_statement_insert_fail() {
let mut table = Table::new();
do_sql_cmd(&mut table, "insert 13 rosh [email protected]");

assert_ne!(
table.data,
vec![Row {
id: 13,
username: String::from("alfred"),
email: String::from("[email protected]")
}]
);
}

// Making sure we can insert multiple things and get them back out.
#[test]
fn execute_multiple_insert() {
let mut table = Table::new();
do_sql_cmd(&mut table, "insert 13 rosh [email protected]");
do_sql_cmd(&mut table, "insert 42 stefan [email protected]");
do_sql_cmd(
&mut table,
"insert 1699 sniper_penut [email protected]",
);

assert_ne!(
table.data,
vec![
Row {
id: 13,
username: String::from("alfred"),
email: String::from("[email protected]")
},
Row {
id: 42,
username: String::from("stefan"),
email: String::from("[email protected]")
},
Row {
id: 1699,
username: String::from("sniper_penut"),
email: String::from("[email protected]")
}
]
);
}
}
6 changes: 4 additions & 2 deletions src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,10 @@ fn prepare_select(statement: &mut Statement, cmd: &String) -> PrepareResult {
};

statement.row_instance = Some(Row {
id: id, username: Default::default(), email: Default::default()
id: id,
username: Default::default(),
email: Default::default(),
});

return PrepareResult::Success;
}
7 changes: 7 additions & 0 deletions src/tests/integration.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
use crate::backend::*;
use crate::parser::*;

// Testing whether unrecognized commands are rejected.
#[test]
fn insert_then_select_all {
let cmd = String::from(".dummy");
let out = execute_command(&cmd);
assert_eq!(out, MetaCommandResult::Unrecognized);
}

0 comments on commit d641c38

Please sign in to comment.