test: added testing for rw and pk insert and read

This commit is contained in:
Elias Renman
2023-04-02 14:28:59 +02:00
parent c4d0de51e6
commit 72b3bd1558
4 changed files with 68 additions and 29 deletions

View File

@@ -18,16 +18,16 @@ impl Table {
Table { indexes: vec![], rows: HashMap::new(), name: name.to_string(), pk_key: pk_key.to_string() }
}
pub fn from_file(file_path: String) -> Table {
pub fn from_file(file_path: String) -> Result<Table, &'static str> {
let serialized = fs::read_to_string(file_path);
if serialized.is_err() {
panic!("Failed loading from file");
return Err("Failed loading from file");
}
let table: Table = serde_json::from_str(&serialized.unwrap()).unwrap();
return table;
return Ok(table);
}
pub fn to_file(&self, file_path: String) {
pub fn to_file(&self, file_path: &'static str) {
let serialized = serde_json::to_string(self).unwrap();
println!("serialized = {}", serialized);
@@ -39,15 +39,16 @@ impl Table {
pub fn create_index(&mut self, index: Index) {
// TODO: Add row indexes to the index itself before pushing to the table
self.indexes.push(index)
}
pub fn find_by_pk(&self, value: &u64) -> &Row {
pub fn find_by_pk(&self, value: &u64) -> Result<&Row, &'static str> {
let row = self.rows.get(value);
if row.is_none() {
panic!("Row not found!");
return Err("Row not found!");
}
return row.unwrap().clone();
Ok(row.unwrap().clone())
}
pub fn insert_row(&mut self, row: Row) {

View File

@@ -1,34 +1,16 @@
use serde_json::Value;
macro_rules! hashmap {
($( $key: expr => $val: expr ),*) => {{
let mut map: ::std::collections::HashMap<String, Value> = ::std::collections::HashMap::new();
let mut map: ::std::collections::HashMap<String, ::serde_json::Value> = ::std::collections::HashMap::new();
$( map.insert($key.to_string(), serde_json::to_value($val).unwrap()); )*
map
}}
}
use database::{table::Table, row::Row};
mod database;
fn main() {
let mut table: Table = Table::new("Cats", "id");
table.insert_row(Row::new(hashmap!["id" => 1, "name" => "Ozzy"]));
table.insert_row(Row::new(hashmap!["id" => 2, "name" => "Simon"]));
let row = table.find_by_pk(&1u64);
let pretty_print = serde_json::to_string_pretty(row);
println!("Found Row: {}", pretty_print.unwrap());
let row = table.find_by_pk(&2u64);
let pretty_print = serde_json::to_string_pretty(row);
println!("Found Row: {}", pretty_print.unwrap());
let row = table.find_by_pk(&3u64);
let pretty_print = serde_json::to_string_pretty(row);
println!("Failed to Find Row: {}", pretty_print.unwrap());
}
#[cfg(test)]
mod tests;

1
src/tests/mod.rs Normal file
View File

@@ -0,0 +1 @@
mod table_test;

55
src/tests/table_test.rs Normal file
View File

@@ -0,0 +1,55 @@
#[cfg(test)]
mod tests {
use std::{fs, path::Path};
use crate::database::{table::Table, row::Row};
#[test]
fn should_find_two_rows() {
let mut table: Table = Table::new("Cats", "id");
table.insert_row(Row::new(hashmap!["id" => 1, "name" => "Ozzy"]));
table.insert_row(Row::new(hashmap!["id" => 2, "name" => "Simon"]));
let row = table.find_by_pk(&1u64);
assert_eq!(row.is_err(), false);
let pretty_print = serde_json::to_string_pretty(row.unwrap());
println!("Found Row: {}", pretty_print.unwrap());
let row = table.find_by_pk(&2u64);
let pretty_print = serde_json::to_string_pretty(row.unwrap());
println!("Found Row: {}", pretty_print.unwrap());
}
#[test]
fn should_fail_to_find_row() {
let mut table: Table = Table::new("Cats", "id");
table.insert_row(Row::new(hashmap!["id" => 1, "name" => "Ozzy"]));
table.insert_row(Row::new(hashmap!["id" => 2, "name" => "Simon"]));
let row = table.find_by_pk(&3u64);
assert_eq!(row.is_err(), true);
}
#[test]
fn should_write_and_read_to_file() {
let mut table: Table = Table::new("Cats", "id");
table.insert_row(Row::new(hashmap!["id" => 1, "name" => "Ozzy"]));
table.insert_row(Row::new(hashmap!["id" => 2, "name" => "Simon"]));
table.to_file("./db.json");
let exists = Path::try_exists(Path::new("./db.json"));
assert_eq!(exists.unwrap(), true);
let table = Table::from_file("./db.json".to_owned());
assert_eq!(table.is_ok(), true);
let binding = table.unwrap();
let row = binding.find_by_pk(&1u64);
assert_eq!(row.is_ok(), true);
// Cleanup file
fs::remove_file("./db.json");
}
}