Skip to content

Commit

Permalink
add list tables method and rename table
Browse files Browse the repository at this point in the history
  • Loading branch information
giuseppe-g-gelardi committed Dec 14, 2024
1 parent ec6c19b commit edc0c4d
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 2 deletions.
1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ documentation = "TODO: https://docs.rs/cargobase"
readme = "README.md"
keywords = ["key-value", "store", "database", "in-memory", "file-based", "utility", "local", "rust"]
categories = ["data-management", "development-tools", "utilities", "database"]
edition = "2021"

[dependencies]
serde = { version = "1.0.215", features = ["derive"] }
Expand Down
72 changes: 71 additions & 1 deletion src/database.rs
Original file line number Diff line number Diff line change
Expand Up @@ -176,14 +176,48 @@ impl Database {
let view = View::new(self);
view.single_table(table_name);
}

pub fn list_tables(&self) -> Vec<String> {
self.tables.keys().cloned().collect()
}

pub async fn rename_table(
&mut self,
old_name: &str,
new_name: &str,
) -> Result<(), DatabaseError> {
if old_name == new_name {
return Err(DatabaseError::InvalidData(
"old name and new name are the same".to_string(),
));
}

let table = self.tables.remove(old_name).ok_or_else(|| {
DatabaseError::TableNotFound(format!("Table {} not found", old_name.to_string()))
});

if self.tables.contains_key(new_name) {
return Err(DatabaseError::TableAlreadyExists(new_name.to_string()));
}

let mut table = table?;
table.name = new_name.to_string();
self.tables.insert(new_name.to_string(), table);

self.save_to_file()
.await
.map_err(DatabaseError::SaveError)?;

Ok(())
}
}

#[cfg(test)]
mod tests {
use tracing_test::traced_test;

use super::*;
use crate::{setup_temp_db, Columns, Table};
use crate::{setup_temp_db, Column, Columns, Table};

#[derive(Serialize, Deserialize, Debug, PartialEq, Clone, Default)]
struct TestData {
Expand Down Expand Up @@ -320,4 +354,40 @@ mod tests {

assert_eq!(db, loaded_db);
}

#[tokio::test]
async fn test_rename_table_success() {
let mut db = setup_temp_db().await;

db.rename_table("TestTable", "RenamedTable")
.await
.expect("Failed to rename table");

assert!(db.tables.contains_key("RenamedTable"));
assert!(!db.tables.contains_key("TestTable"));
}

#[tokio::test]
async fn test_rename_table_already_exists() {
let mut db = setup_temp_db().await;

let mut another_table = Table::new(
"AnotherTable".to_string(),
Columns::new(vec![Column::new("id", true)]),
);
db.add_table(&mut another_table).await.unwrap();

let result = db.rename_table("TestTable", "AnotherTable").await;

assert!(matches!(result, Err(DatabaseError::TableAlreadyExists(_))));
}

#[tokio::test]
async fn test_rename_table_not_found() {
let mut db = setup_temp_db().await;

let result = db.rename_table("NonExistentTable", "NewTable").await;

assert!(matches!(result, Err(DatabaseError::TableNotFound(_))));
}
}

0 comments on commit edc0c4d

Please sign in to comment.