Skip to content

Commit

Permalink
more unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
giuseppe-g-gelardi committed Nov 21, 2024
1 parent d1b2285 commit 4b1cd7a
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 126 deletions.
42 changes: 0 additions & 42 deletions cargobase-test.json

This file was deleted.

88 changes: 37 additions & 51 deletions src/cargobase/database.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ impl Database {
table.set_file_name(self.file_name.clone());
if self.tables.iter().any(|t| t.name == table.name) {
println!("Table {} already exists, Skipping creation.", table.name);
return Ok(())
return Ok(());
}

self.tables.push(table.clone());
Expand Down Expand Up @@ -204,39 +204,44 @@ mod tests {
use crate::{Column, Columns, Table};

use super::*;
use serde_json::json;
use std::fs;

#[derive(Serialize, Deserialize, Debug, PartialEq, Clone, Default)]
struct TestData {
id: String,
name: String,
}

fn setup_test_db() -> Database {
// Cleanup the test database file for each test
std::fs::remove_file("test_db.json").ok();
let mut db = Database::new("test_db");

let test_columns = Columns::from_struct::<TestData>(true);

let mut table = Table::new("TestTable".to_string(), test_columns);
db.add_table(&mut table).unwrap();

db
}

#[test]
fn test_database_new() {
let file_name = "test.json";
if std::path::Path::new(file_name).exists() {
fs::remove_file(file_name).expect("Failed to remove test file");
}

let db = Database::new("test");
assert_eq!(db.name, "test");
assert_eq!(db.file_name, "test.json");
std::fs::remove_file("test_db.json").ok();
let db = setup_test_db();
assert_eq!(db.name, "test_db");
assert_eq!(db.file_name, "test_db.json");
assert_eq!(db.tables.len(), 0);

if std::path::Path::new(file_name).exists() {
fs::remove_file(file_name).expect("Failed to remove test file");
}
std::fs::remove_file("test_db.json").ok();
}

#[test]
fn test_add_table_success() {
// if it exists, remove the test_db.json file before testing
std::fs::remove_file("test_db.json").ok();
let mut db = Database::new("test_db");

#[derive(Debug, PartialEq, Serialize, Deserialize, Clone, Default)]
struct TestStruct {
id: i32,
name: String,
email: String,
}

let test_columns = Columns::from_struct::<TestStruct>(true);
let test_columns = Columns::from_struct::<TestData>(true);
let mut test_table = Table::new("TestTable".to_string(), test_columns);

let result = db.add_table(&mut test_table);
Expand All @@ -252,21 +257,10 @@ mod tests {

#[test]
fn test_add_table_already_exists() {
let mut db = Database::new("test_db");
std::fs::remove_file("test_db.json").ok();
let mut db = setup_test_db();

// works with both methods
// #[derive(Debug, PartialEq, Serialize, Deserialize, Clone, Default)]
// struct TestStruct {
// id: i32,
// name: String,
// email: String,
// }
//
// let test_columns = Columns::from_struct::<TestStruct>(true);
// let mut test_table = Table::new("TestTable".to_string(), test_columns);

// Define columns and create a table
let columns = Columns::new(vec![Column::new("id", true), Column::new("name", true)]);
let columns = Columns::from_struct::<TestData>(true);
let mut test_table = Table::new("TestTable".to_string(), columns);

// Add the table for the first time
Expand All @@ -277,27 +271,19 @@ mod tests {
let result_second_add = db.add_table(&mut test_table);

// Verify behavior when the table already exists
assert!(result_second_add.is_err()); // Second addition should return an error
assert!(result_second_add.is_ok()); // Second addition should succeed
assert_eq!(
result_second_add.unwrap_err(),
result_second_add,
// spelling and grammar have to match!!
"Table TestTable already exists, Skipping creation."
// "Table TestTable already exists, Skipping creation.",
Ok(())
);

// Verify that the database still contains only one instance of the table
assert_eq!(db.tables.len(), 1);
assert_eq!(db.tables[0].name, "TestTable");
}

// #[test] // WARN: this fails randomly. Need to fix.
// fn test_add_table_sets_file_name() {
// let mut db = Database::new("test_db");
// let columns = Columns(vec![Column::new("id", true), Column::new("name", true)]);
// let mut table = Table::new("TestTable".to_string(), columns);
//
// let _ = db.add_table(&mut table);
//
// // Ensure the table's file_name is set correctly
// assert_eq!(table.file_name, Some("test_db.json".to_string()));
// }
// remove the test_db.json file after testing
std::fs::remove_file("test_db.json").ok();
}
}
55 changes: 22 additions & 33 deletions src/cargobase/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ mod tests {
use super::*;
use serde_json::json;

use crate::{Column, Columns};
use crate::Columns;

#[derive(Serialize, Deserialize, Debug, PartialEq, Clone, Default)]
struct TestData {
Expand All @@ -236,6 +236,8 @@ mod tests {
}

fn setup_test_db() -> Database {
// Cleanup the test database file for each test
std::fs::remove_file("test_db.json").ok();
let mut db = Database::new("test_db");
// let columns = Columns(vec![Column::new("id", true), Column::new("name", true)]);

Expand Down Expand Up @@ -407,13 +409,11 @@ mod tests {
fn test_query_all() {
std::fs::remove_file("test_db.json").ok();

// let mut db = setup_test_db();
let mut db = Database::new("test_db");
let test_columns = Columns::from_struct::<TestData>(true);
let mut test_table = Table::new("TestTable".to_string(), test_columns);
let _ = db.add_table(&mut test_table);

// println!("Database initialized: {:?}", db);
// let mut db = Database::new("test_db");
// let test_columns = Columns::from_struct::<TestData>(true);
// let mut test_table = Table::new("TestTable".to_string(), test_columns);
// let _ = db.add_table(&mut test_table);
let mut db = setup_test_db();

let test_data1 = TestData {
id: "1".to_string(),
Expand All @@ -424,32 +424,21 @@ mod tests {
name: "Bob".to_string(),
};

db.add_row().to("TestTable").data_from_struct(test_data1.clone()).execute_add().expect("Failed to add row 1");
println!("Database initialized: {:?}", db);

// let rows: Vec<TestData> = db.get_rows().from("TestTable").all();
// println!("All rows: {:?}", rows);
db.add_row()
.to("TestTable")
.data_from_struct(test_data1.clone())
.execute_add()
.expect("Failed to add row 1");
db.add_row()
.to("TestTable")
.data_from_struct(test_data2.clone())
.execute_add()
.expect("Failed to add row 2");

std::fs::remove_file("test_db.json").ok();
let rows: Vec<TestData> = db.get_rows().from("TestTable").all();

// db.add_row()
// .to("TestTable")
// .data_from_struct(test_data1.clone())
// .execute_add()
// .expect("Failed to add row 1");
//
// db.add_row()
// .to("TestTable")
// .data_from_struct(test_data2.clone())
// .execute_add()
// .expect("Failed to add row 2");

// let rows: Vec<TestData> = db.get_rows().from("TestTable").all();
//
// println!("All rows: {:?}", rows);

// assert_eq!(rows.len(), 2);
// assert!(rows.contains(&test_data1));
// assert!(rows.contains(&test_data2));
assert_eq!(rows.len(), 2);
assert!(rows.contains(&test_data1));
assert!(rows.contains(&test_data2));
}
}
21 changes: 21 additions & 0 deletions test_db.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"name": "test_db",
"file_name": "test_db.json",
"tables": [
{
"name": "TestTable",
"rows": [],
"columns": [
{
"name": "id",
"required": true
},
{
"name": "name",
"required": true
}
],
"file_name": "test_db.json"
}
]
}

0 comments on commit 4b1cd7a

Please sign in to comment.