From 2c1f2ab4211c20a5ff1c418db89ed1e8ae88ef5f Mon Sep 17 00:00:00 2001 From: giuseppe-g-gelardi Date: Thu, 12 Dec 2024 23:46:01 -0500 Subject: [PATCH] update naming and imports --- src/database.rs | 80 +++++------ src/lib.rs | 4 +- src/query.rs | 356 ++++++++++++++++++++++++------------------------ src/table.rs | 38 +++--- src/util.rs | 18 ++- src/view.rs | 6 +- 6 files changed, 250 insertions(+), 252 deletions(-) diff --git a/src/database.rs b/src/database.rs index 3dd921b..095c192 100644 --- a/src/database.rs +++ b/src/database.rs @@ -4,14 +4,14 @@ use tracing; use crate::{query::Operation, DatabaseError, Query, Table, View}; #[derive(Debug, PartialEq, Serialize, Deserialize, Clone)] -pub struct DatabaseAsync { +pub struct Database { pub(crate) name: String, pub(crate) file_name: String, pub(crate) tables: Vec, } -impl DatabaseAsync { - pub async fn new_async(name: &str) -> Self { +impl Database { + pub async fn new(name: &str) -> Self { let name = name.to_string(); let file_name = format!("{name}.json"); @@ -19,7 +19,7 @@ impl DatabaseAsync { tracing::info!("Database already exists: {name}, loading database"); // Load the database from the file - match DatabaseAsync::load_from_file_async(&file_name).await { + match Database::load_from_file(&file_name).await { Ok(db) => return db, Err(e) => { tracing::error!("Failed to load database from file: {file_name}, error: {e}"); @@ -33,14 +33,14 @@ impl DatabaseAsync { } } - DatabaseAsync { + Database { name, file_name, tables: Vec::new(), } } - pub async fn drop_database_async(&self) -> Result<(), DatabaseError> { + pub async fn drop_database(&self) -> Result<(), DatabaseError> { if tokio::fs::remove_file(&self.file_name).await.is_err() { tracing::error!( "{}", @@ -52,7 +52,7 @@ impl DatabaseAsync { Ok(()) } - pub async fn add_table_async(&mut self, table: &mut Table) -> Result<(), DatabaseError> { + pub async fn add_table(&mut self, table: &mut Table) -> Result<(), DatabaseError> { if self.tables.iter().any(|t| t.name == table.name) { tracing::warn!( "{}", @@ -62,21 +62,21 @@ impl DatabaseAsync { } self.tables.push(table.clone()); - self.save_to_file_async() + self.save_to_file() .await .map_err(|e| DatabaseError::SaveError(e))?; Ok(()) } - pub async fn drop_table_async(&mut self, table_name: &str) -> Result<(), DatabaseError> { - let mut db = DatabaseAsync::load_from_file_async(&self.file_name) + pub async fn drop_table(&mut self, table_name: &str) -> Result<(), DatabaseError> { + let mut db = Database::load_from_file(&self.file_name) .await .map_err(|e| DatabaseError::LoadError(e))?; if let Some(index) = db.tables.iter().position(|t| t.name == table_name) { let removed_table = db.tables.remove(index); tracing::info!("Table `{}` dropped successfully", removed_table.name); - db.save_to_file_async() + db.save_to_file() .await .map_err(|e| DatabaseError::SaveError(e))?; @@ -88,16 +88,16 @@ impl DatabaseAsync { } } - pub(crate) async fn save_to_file_async(&self) -> Result<(), tokio::io::Error> { + pub(crate) async fn save_to_file(&self) -> Result<(), tokio::io::Error> { let json_data = serde_json::to_string_pretty(&self)?; tokio::fs::write(&self.file_name, json_data).await?; tracing::info!("Database saved to file: {}", self.file_name); Ok(()) } - pub(crate) async fn load_from_file_async(file_name: &str) -> Result { + pub(crate) async fn load_from_file(file_name: &str) -> Result { let json_data = tokio::fs::read_to_string(file_name).await?; - let db: DatabaseAsync = serde_json::from_str(&json_data)?; + let db: Database = serde_json::from_str(&json_data)?; tracing::info!("Database loaded from file: {}", file_name); Ok(db) } @@ -172,7 +172,7 @@ mod tests { use tracing_test::traced_test; use super::*; - use crate::{setup_temp_db_async, Columns, Table}; + use crate::{setup_temp_db, Columns, Table}; #[derive(Serialize, Deserialize, Debug, PartialEq, Clone, Default)] struct TestData { @@ -181,8 +181,8 @@ mod tests { } #[tokio::test] - async fn test_database_new_async() { - let db = setup_temp_db_async().await; + async fn test_database_new() { + let db = setup_temp_db().await; let db_name = &db.name.to_string(); let fnn = format!("{db_name}.json"); @@ -193,24 +193,24 @@ mod tests { } #[tokio::test] - async fn test_drop_database_async() { - let db = setup_temp_db_async().await; - let result = db.drop_database_async().await; + async fn test_drop_database() { + let db = setup_temp_db().await; + let result = db.drop_database().await; assert!(result.is_ok()); assert!(!std::path::Path::new(&db.file_name).exists()); } #[tokio::test] - async fn test_add_table_success_async() { + async fn test_add_table_success() { // this test does not use the setup_temp_db function // because it needs to test the creation of a new database and table tokio::fs::remove_file("test_db.json").await.ok(); - let mut db = DatabaseAsync::new_async("test_db").await; + let mut db = Database::new("test_db").await; let test_columns = Columns::from_struct::(true); let mut test_table = Table::new("TestTable".to_string(), test_columns); - let result = db.add_table_async(&mut test_table).await; + let result = db.add_table(&mut test_table).await; assert!(result.is_ok()); assert_eq!(db.tables.len(), 1); @@ -221,13 +221,13 @@ mod tests { #[traced_test] #[tokio::test] - async fn test_add_table_already_exists_async() { - let mut db = setup_temp_db_async().await; + async fn test_add_table_already_exists() { + let mut db = setup_temp_db().await; // Create a duplicate table let columns = Columns::from_struct::(true); let mut duplicate_table = Table::new("TestTable".to_string(), columns); - let result = db.add_table_async(&mut duplicate_table).await; + let result = db.add_table(&mut duplicate_table).await; // Assert that the result is Ok(()) even when the table already exists assert!(result.is_ok()); @@ -241,9 +241,9 @@ mod tests { } #[tokio::test] - async fn test_drop_table_success_async() { - let mut db = setup_temp_db_async().await; - let result = db.drop_table_async("TestTable").await; + async fn test_drop_table_success() { + let mut db = setup_temp_db().await; + let result = db.drop_table("TestTable").await; assert!(result.is_ok()); assert_eq!(db.tables.len(), 0); @@ -251,9 +251,9 @@ mod tests { #[traced_test] #[tokio::test] - async fn test_drop_table_not_found_async() { - let mut db = setup_temp_db_async().await; - let result = db.drop_table_async("NonExistentTable").await; + async fn test_drop_table_not_found() { + let mut db = setup_temp_db().await; + let result = db.drop_table("NonExistentTable").await; assert!(result.is_ok()); @@ -267,45 +267,45 @@ mod tests { } #[tokio::test] - async fn test_save_to_file_async() { + async fn test_save_to_file() { use tempfile::NamedTempFile; let temp_file = NamedTempFile::new().expect("Failed to create a temporary file"); let db_path = temp_file.path().to_str().unwrap().to_string(); - let db = DatabaseAsync { + let db = Database { name: "test_db".to_string(), file_name: db_path.clone(), tables: vec![], }; - db.save_to_file_async() + db.save_to_file() .await .expect("Failed to save database"); - let loaded_db = DatabaseAsync::load_from_file_async(&db_path) + let loaded_db = Database::load_from_file(&db_path) .await .expect("Failed to load database"); assert_eq!(db, loaded_db); } #[tokio::test] - async fn test_load_from_file_async() { + async fn test_load_from_file() { use tempfile::NamedTempFile; let temp_file = NamedTempFile::new().expect("Failed to create a temporary file"); let db_path = temp_file.path().to_str().unwrap().to_string(); - let db = DatabaseAsync { + let db = Database { name: "test_db".to_string(), file_name: db_path.to_string(), tables: vec![], }; - db.save_to_file_async() + db.save_to_file() .await .expect("Failed to save database"); - let loaded_db = DatabaseAsync::load_from_file_async(&db_path) + let loaded_db = Database::load_from_file(&db_path) .await .expect("Failed to load database"); diff --git a/src/lib.rs b/src/lib.rs index 7a1360d..efa0f46 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -12,8 +12,8 @@ pub use columns::{Column, Columns}; pub use errors::DatabaseError; pub use row::Row; -pub use database::DatabaseAsync; +pub use database::Database; pub use query::Query; pub use table::Table; -pub use util::setup_temp_db_async; +pub use util::setup_temp_db; pub use view::View; diff --git a/src/query.rs b/src/query.rs index faf7d23..b9893b7 100644 --- a/src/query.rs +++ b/src/query.rs @@ -2,7 +2,7 @@ use serde::de::DeserializeOwned; use serde::{Deserialize, Serialize}; use serde_json::Value; -use crate::{DatabaseAsync, DatabaseError, Row, Table}; +use crate::{Database, DatabaseError, Row, Table}; #[derive(Debug, PartialEq, Serialize, Deserialize, Clone, Copy)] pub enum Operation { @@ -37,12 +37,12 @@ impl Query { self } - pub async fn where_eq_async( + pub async fn where_eq( self, key: &str, value: &str, ) -> Result, DatabaseError> { - let mut db = DatabaseAsync::load_from_file_async(&self.db_file_name) + let mut db = Database::load_from_file(&self.db_file_name) .await .map_err(DatabaseError::LoadError)?; self.handle_where_eq(&mut db, key, value).await // Shared logic @@ -51,7 +51,7 @@ impl Query { // Shared logic for where_eq async fn handle_where_eq( &self, - db: &mut DatabaseAsync, + db: &mut Database, key: &str, value: &str, ) -> Result, DatabaseError> { @@ -74,14 +74,14 @@ impl Query { Operation::Read => self.execute_select(table, key, value), Operation::Update => { let result = self.execute_update(table, key, value); - db.save_to_file_async() + db.save_to_file() .await .map_err(DatabaseError::SaveError)?; result } Operation::Delete => { let result = self.execute_delete(table, key, value); - db.save_to_file_async() + db.save_to_file() .await .map_err(DatabaseError::SaveError)?; result @@ -92,14 +92,14 @@ impl Query { // #[cfg(feature = "async")] // pub async fn execute_add(self) -> Result<(), DatabaseError> { - pub async fn execute_add_async(self) -> Result<(), DatabaseError> { - let mut db = DatabaseAsync::load_from_file_async(&self.db_file_name) + pub async fn execute_add(self) -> Result<(), DatabaseError> { + let mut db = Database::load_from_file(&self.db_file_name) .await .map_err(DatabaseError::LoadError)?; self.handle_execute_add_sync(&mut db).await // Shared logic } - async fn handle_execute_add_sync(&self, db: &mut DatabaseAsync) -> Result<(), DatabaseError> { + async fn handle_execute_add_sync(&self, db: &mut Database) -> Result<(), DatabaseError> { let table_name = self .table_name .clone() @@ -114,7 +114,7 @@ impl Query { if let Some(row_data) = self.row_data.clone() { table.columns.validate(row_data.clone())?; table.rows.push(Row::new(row_data)); - db.save_to_file_async() + db.save_to_file() .await .map_err(DatabaseError::SaveError)?; Ok(()) @@ -241,12 +241,12 @@ impl Query { Ok(None) // No matching record found } - pub async fn all_async(&self) -> Vec { - let db = DatabaseAsync::load_from_file_async(&self.db_file_name) + pub async fn all(&self) -> Vec { + let db = Database::load_from_file(&self.db_file_name) .await .unwrap_or_else(|e| { tracing::error!("Failed to load database from file: {}", e); - DatabaseAsync { + Database { name: String::new(), file_name: self.db_file_name.clone(), tables: Vec::new(), @@ -255,7 +255,7 @@ impl Query { self.handle_all(&db) // Shared logic } - fn handle_all(&self, db: &DatabaseAsync) -> Vec { + fn handle_all(&self, db: &Database) -> Vec { if let Some(table_name) = &self.table_name { if let Some(table) = db.tables.iter().find(|t| t.name == *table_name) { table @@ -282,7 +282,7 @@ mod tests { use super::*; use serde_json::json; - use crate::setup_temp_db_async; + use crate::setup_temp_db; #[derive(Serialize, Deserialize, Debug, PartialEq, Clone, Default)] struct TestData { @@ -344,7 +344,7 @@ mod tests { #[tokio::test] async fn test_query_all() { - let mut db = setup_temp_db_async().await; + let mut db = setup_temp_db().await; let test_data1 = TestData { id: "1".to_string(), @@ -358,18 +358,18 @@ mod tests { db.add_row() .from("TestTable") .data_from_struct(test_data1.clone()) - .execute_add_async() + .execute_add() .await .expect("Failed to add row 1"); db.add_row() .from("TestTable") .data_from_struct(test_data2.clone()) - .execute_add_async() + .execute_add() .await .expect("Failed to add row 2"); - let rows: Vec = db.get_rows().from("TestTable").all_async().await; + let rows: Vec = db.get_rows().from("TestTable").all().await; assert_eq!(rows.len(), 2); assert!(rows.contains(&test_data1)); @@ -395,7 +395,7 @@ mod tests { #[tokio::test] async fn test_query_where_eq_no_match() { - let mut db = setup_temp_db_async().await; + let mut db = setup_temp_db().await; let test_data = TestData { id: "1".to_string(), @@ -405,14 +405,14 @@ mod tests { db.add_row() .from("TestTable") .data_from_struct(test_data) - .execute_add_async() + .execute_add() .await .expect("Failed to add row"); let result: Option = db .get_rows() .from("TestTable") - .where_eq_async("id", "999") + .where_eq("id", "999") .await .unwrap(); assert!(result.is_none(), "Expected no matching record"); @@ -420,7 +420,7 @@ mod tests { #[tokio::test] async fn test_query_where_eq_match() { - let mut db = setup_temp_db_async().await; + let mut db = setup_temp_db().await; let test_data = TestData { id: "1".to_string(), @@ -430,14 +430,14 @@ mod tests { db.add_row() .from("TestTable") .data_from_struct(test_data.clone()) - .execute_add_async() + .execute_add() .await .expect("Failed to add row"); let result: Option = db .get_rows() .from("TestTable") - .where_eq_async("id", "1") + .where_eq("id", "1") .await .unwrap(); assert_eq!(result, Some(test_data), "Expected matching record"); @@ -445,7 +445,7 @@ mod tests { #[tokio::test] async fn test_query_execute_update() { - let mut db = setup_temp_db_async().await; + let mut db = setup_temp_db().await; let test_data = TestData { id: "1".to_string(), @@ -455,7 +455,7 @@ mod tests { db.add_row() .from("TestTable") .data_from_struct(test_data.clone()) - .execute_add_async() + .execute_add() .await .expect("Failed to add row"); @@ -465,7 +465,7 @@ mod tests { .update_row() .from("TestTable") .data(update_data) - .where_eq_async::("id", "1") + .where_eq::("id", "1") .await .unwrap(); @@ -482,7 +482,7 @@ mod tests { #[tokio::test] async fn test_query_execute_delete() { - let mut db = setup_temp_db_async().await; + let mut db = setup_temp_db().await; let test_data = TestData { id: "1".to_string(), @@ -492,14 +492,14 @@ mod tests { db.add_row() .from("TestTable") .data_from_struct(test_data.clone()) - .execute_add_async() + .execute_add() .await .expect("Failed to add row"); let result = db .get_single() .from("TestTable") - .where_eq_async::("id", "1") + .where_eq::("id", "1") .await .unwrap(); @@ -508,15 +508,15 @@ mod tests { let _ = db .delete_single() .from("TestTable") - .where_eq_async::("id", "1") + .where_eq::("id", "1") .await .unwrap(); - let rows: Vec = db.get_rows().from("TestTable").all_async().await; + let rows: Vec = db.get_rows().from("TestTable").all().await; let deleted_record = db .get_single() .from("TestTable") - .where_eq_async::("id", "1") + .where_eq::("id", "1") .await .unwrap(); @@ -524,150 +524,150 @@ mod tests { assert!(rows.is_empty(), "Expected all records to be deleted"); } - // #[cfg(feature = "async")] - #[tokio::test] - async fn test_query_where_eq_match_async() { - let mut db = setup_temp_db_async().await; - - let test_data = TestData { - id: "1".to_string(), - name: "Alice".to_string(), - }; - - db.add_row() - .from("TestTable") - .data_from_struct(test_data.clone()) - .execute_add_async() - .await - .expect("Failed to add row"); - - let result: Option = db - .get_rows() - .from("TestTable") - .where_eq_async("id", "1") - .await - .unwrap(); - - assert_eq!(result, Some(test_data), "Expected matching record"); - } - - // #[cfg(feature = "async")] - #[tokio::test] - async fn test_query_execute_update_async() { - let mut db = setup_temp_db_async().await; - - let test_data = TestData { - id: "1".to_string(), - name: "Alice".to_string(), - }; - - db.add_row() - .from("TestTable") - .data_from_struct(test_data.clone()) - .execute_add_async() - .await - .expect("Failed to add row"); - - let update_data = json!({ "name": "Updated Alice" }); - - let result = db - .update_row() - .from("TestTable") - .data(update_data) - .where_eq_async::("id", "1") - .await - .unwrap(); - - assert!( - result.is_some(), - "Expected update to return the updated record" - ); - assert_eq!( - result.unwrap().name, - "Updated Alice", - "Name was not updated" - ); - } - - // #[cfg(feature = "async")] - #[tokio::test] - async fn test_query_all_async() { - let mut db = setup_temp_db_async().await; - - let test_data1 = TestData { - id: "1".to_string(), - name: "Alice".to_string(), - }; - let test_data2 = TestData { - id: "2".to_string(), - name: "Bob".to_string(), - }; - - db.add_row() - .from("TestTable") - .data_from_struct(test_data1.clone()) - .execute_add_async() - .await - .expect("Failed to add row 1"); - - db.add_row() - .from("TestTable") - .data_from_struct(test_data2.clone()) - .execute_add_async() - .await - .expect("Failed to add row 2"); - - let rows: Vec = db.get_rows().from("TestTable").all_async().await; - - assert_eq!(rows.len(), 2); - assert!(rows.contains(&test_data1)); - assert!(rows.contains(&test_data2)); - } - - // #[cfg(feature = "async")] - #[tokio::test] - async fn test_query_execute_delete_async() { - let mut db = setup_temp_db_async().await; - - let test_data = TestData { - id: "1".to_string(), - name: "Alice".to_string(), - }; - - db.add_row() - .from("TestTable") - .data_from_struct(test_data.clone()) - .execute_add_async() - .await - .expect("Failed to add row"); - - let result = db - .get_single() - .from("TestTable") - .where_eq_async::("id", "1") - .await - .unwrap(); - - assert!(result.is_some(), "Expected record to exist before deletion"); - - let _ = db - .delete_single() - .from("TestTable") - .where_eq_async::("id", "1") - .await - .unwrap(); - - let rows: Vec = db.get_rows().from("TestTable").all_async().await; - let deleted_record = db - .get_single() - .from("TestTable") - .where_eq_async::("id", "1") - .await - .unwrap(); - - assert!(deleted_record.is_none(), "Expected record to be deleted"); - assert!(rows.is_empty(), "Expected all records to be deleted"); - } + // // #[cfg(feature = "async")] + // #[tokio::test] + // async fn test_query_where_eq_match() { + // let mut db = setup_temp_db().await; + // + // let test_data = TestData { + // id: "1".to_string(), + // name: "Alice".to_string(), + // }; + // + // db.add_row() + // .from("TestTable") + // .data_from_struct(test_data.clone()) + // .execute_add() + // .await + // .expect("Failed to add row"); + // + // let result: Option = db + // .get_rows() + // .from("TestTable") + // .where_eq("id", "1") + // .await + // .unwrap(); + // + // assert_eq!(result, Some(test_data), "Expected matching record"); + // } + // + // // #[cfg(feature = "async")] + // #[tokio::test] + // async fn test_query_execute_update() { + // let mut db = setup_temp_db().await; + // + // let test_data = TestData { + // id: "1".to_string(), + // name: "Alice".to_string(), + // }; + // + // db.add_row() + // .from("TestTable") + // .data_from_struct(test_data.clone()) + // .execute_add() + // .await + // .expect("Failed to add row"); + // + // let update_data = json!({ "name": "Updated Alice" }); + // + // let result = db + // .update_row() + // .from("TestTable") + // .data(update_data) + // .where_eq::("id", "1") + // .await + // .unwrap(); + // + // assert!( + // result.is_some(), + // "Expected update to return the updated record" + // ); + // assert_eq!( + // result.unwrap().name, + // "Updated Alice", + // "Name was not updated" + // ); + // } + // + // // #[cfg(feature = "async")] + // #[tokio::test] + // async fn test_query_all() { + // let mut db = setup_temp_db().await; + // + // let test_data1 = TestData { + // id: "1".to_string(), + // name: "Alice".to_string(), + // }; + // let test_data2 = TestData { + // id: "2".to_string(), + // name: "Bob".to_string(), + // }; + // + // db.add_row() + // .from("TestTable") + // .data_from_struct(test_data1.clone()) + // .execute_add() + // .await + // .expect("Failed to add row 1"); + // + // db.add_row() + // .from("TestTable") + // .data_from_struct(test_data2.clone()) + // .execute_add() + // .await + // .expect("Failed to add row 2"); + // + // let rows: Vec = db.get_rows().from("TestTable").all().await; + // + // assert_eq!(rows.len(), 2); + // assert!(rows.contains(&test_data1)); + // assert!(rows.contains(&test_data2)); + // } + // + // // #[cfg(feature = "async")] + // #[tokio::test] + // async fn test_query_execute_delete() { + // let mut db = setup_temp_db().await; + // + // let test_data = TestData { + // id: "1".to_string(), + // name: "Alice".to_string(), + // }; + // + // db.add_row() + // .from("TestTable") + // .data_from_struct(test_data.clone()) + // .execute_add() + // .await + // .expect("Failed to add row"); + // + // let result = db + // .get_single() + // .from("TestTable") + // .where_eq::("id", "1") + // .await + // .unwrap(); + // + // assert!(result.is_some(), "Expected record to exist before deletion"); + // + // let _ = db + // .delete_single() + // .from("TestTable") + // .where_eq::("id", "1") + // .await + // .unwrap(); + // + // let rows: Vec = db.get_rows().from("TestTable").all().await; + // let deleted_record = db + // .get_single() + // .from("TestTable") + // .where_eq::("id", "1") + // .await + // .unwrap(); + // + // assert!(deleted_record.is_none(), "Expected record to be deleted"); + // assert!(rows.is_empty(), "Expected all records to be deleted"); + // } } // pub fn where_eq( diff --git a/src/table.rs b/src/table.rs index 738f60f..8857f95 100644 --- a/src/table.rs +++ b/src/table.rs @@ -2,7 +2,7 @@ use serde::{Deserialize, Serialize}; use serde_json::Value; use tracing; -use crate::{Columns, DatabaseAsync, Row}; +use crate::{Columns, Database, Row}; #[derive(Debug, PartialEq, Serialize, Deserialize, Clone)] pub struct Table { @@ -20,7 +20,7 @@ impl Table { } } - pub async fn add_row_async(&mut self, db: &mut DatabaseAsync, data: Value) { + pub async fn add_row(&mut self, db: &mut Database, data: Value) { if let Some(table) = db.get_table_mut(&self.name) { if data.is_array() { if let Some(array) = data.as_array() { @@ -31,7 +31,7 @@ impl Table { } else { table.rows.push(Row::new(data)) } - match db.save_to_file_async().await { + match db.save_to_file().await { Ok(_) => {} Err(e) => { tracing::error!("Failed to save to file: {}", e); @@ -45,7 +45,7 @@ impl Table { #[cfg(test)] mod tests { - use crate::{setup_temp_db_async, Column, Columns}; + use crate::{setup_temp_db, Column, Columns}; use super::*; @@ -61,16 +61,16 @@ mod tests { } #[tokio::test] - async fn test_table_add_row_single_async() { - let mut db = setup_temp_db_async().await; + async fn test_table_add_row_single() { + let mut db = setup_temp_db().await; let mut table = Table::new( "TestTable".to_string(), Columns::new(vec![Column::new("id", true), Column::new("name", true)]), ); - db.add_table_async(&mut table).await.unwrap(); + db.add_table(&mut table).await.unwrap(); let row_data = json!({"id": "1", "name": "John Doe"}); - table.add_row_async(&mut db, row_data).await; + table.add_row(&mut db, row_data).await; assert_eq!(db.tables[0].rows.len(), 1); assert_eq!( @@ -80,19 +80,19 @@ mod tests { } #[tokio::test] - async fn test_table_add_row_array_async() { - let mut db = setup_temp_db_async().await; + async fn test_table_add_row_array() { + let mut db = setup_temp_db().await; let mut table = Table::new( "TestTable".to_string(), Columns::new(vec![Column::new("id", true), Column::new("name", true)]), ); - db.add_table_async(&mut table).await.unwrap(); + db.add_table(&mut table).await.unwrap(); let row_data = json!([ {"id": "1", "name": "John Doe"}, {"id": "2", "name": "Jane Doe"} ]); - table.add_row_async(&mut db, row_data).await; + table.add_row(&mut db, row_data).await; assert_eq!(db.tables[0].rows.len(), 2); assert_eq!( @@ -107,15 +107,15 @@ mod tests { #[traced_test] #[tokio::test] - async fn test_table_add_row_table_now_found_async() { - let mut db = setup_temp_db_async().await; + async fn test_table_add_row_table_now_found() { + let mut db = setup_temp_db().await; let mut table = Table::new( "NonExistentTable".to_string(), Columns::new(vec![Column::new("id", true), Column::new("name", true)]), ); let row_data = json!({"id": "1", "name": "John Doe"}); - table.add_row_async(&mut db, row_data).await; + table.add_row(&mut db, row_data).await; assert!(logs_contain("Table NonExistentTable not found")); assert_eq!(db.tables.len(), 1); // Original table remains unchanged @@ -123,18 +123,18 @@ mod tests { #[traced_test] #[tokio::test] - async fn test_table_add_row_save_failure_async() { - let mut db = setup_temp_db_async().await; + async fn test_table_add_row_save_failure() { + let mut db = setup_temp_db().await; let mut table = Table::new( "TestTable".to_string(), Columns::new(vec![Column::new("id", true), Column::new("name", true)]), ); - db.add_table_async(&mut table).await.unwrap(); + db.add_table(&mut table).await.unwrap(); // Simulate failure in saving db.file_name = "/invalid/path.json".to_string(); let row_data = json!({"id": "1", "name": "John Doe"}); - table.add_row_async(&mut db, row_data).await; + table.add_row(&mut db, row_data).await; assert!(logs_contain("Failed to save to file")); } diff --git a/src/util.rs b/src/util.rs index a162d4c..9b52ee1 100644 --- a/src/util.rs +++ b/src/util.rs @@ -1,7 +1,7 @@ use serde::{Deserialize, Serialize}; use tempfile::NamedTempFile; -use crate::{Columns, DatabaseAsync, Table}; +use crate::{Columns, Database, Table}; #[derive(Serialize, Deserialize, Debug, PartialEq, Clone, Default)] struct TestData { @@ -9,20 +9,18 @@ struct TestData { name: String, } -pub async fn setup_temp_db_async() -> DatabaseAsync { +pub async fn setup_temp_db() -> Database { let temp_file = NamedTempFile::new().expect("Failed to create a temporary file"); let db_path = temp_file.path().to_str().unwrap().to_string(); // Initialize the test database - let mut db = DatabaseAsync::new_async(&db_path).await; + let mut db = Database::new(&db_path).await; let test_columns = Columns::from_struct::(true); let mut table = Table::new("TestTable".to_string(), test_columns); - db.add_table_async(&mut table).await.unwrap(); + db.add_table(&mut table).await.unwrap(); - db.save_to_file_async() - .await - .expect("Failed to save database"); + db.save_to_file().await.expect("Failed to save database"); db } @@ -32,14 +30,14 @@ mod tests { use super::*; #[tokio::test] - async fn test_setup_temp_db_async() { - let db = setup_temp_db_async().await; + async fn test_setup_temp_db() { + let db = setup_temp_db().await; assert_eq!(db.tables.len(), 1); assert_eq!(db.tables[0].name, "TestTable"); } #[tokio::test] - async fn test_temp_file_cleanup_async() { + async fn test_temp_file_cleanup() { // Create a temporary database let temp_file = tempfile::Builder::new() .prefix("test_db") diff --git a/src/view.rs b/src/view.rs index f0b1a06..301726d 100644 --- a/src/view.rs +++ b/src/view.rs @@ -1,12 +1,12 @@ -use crate::{DatabaseAsync, Table}; +use crate::{Database, Table}; pub struct View<'a> { - database: &'a DatabaseAsync, + database: &'a Database, } impl<'a> View<'a> { /// Create a new `View` instance - pub fn new(database: &'a DatabaseAsync) -> Self { + pub fn new(database: &'a Database) -> Self { View { database } }