-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add countrows method update cargo toml and move main.rs, hopefully gi…
…thub doesnt complain
- Loading branch information
1 parent
edc0c4d
commit dfb0a78
Showing
3 changed files
with
62 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,7 +24,7 @@ cargobase.json | |
cargobase-async.json | ||
cargobase-testing.json | ||
|
||
main.rs | ||
src/main.rs | ||
|
||
.env | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -210,10 +210,22 @@ impl Database { | |
|
||
Ok(()) | ||
} | ||
|
||
pub fn count_rows(&self, table_name: &str) -> Result<usize, DatabaseError> { | ||
if let Some(table) = self.tables.get(table_name) { | ||
Ok(table.rows.len()) | ||
} else { | ||
Err(DatabaseError::TableNotFound(format!( | ||
"Table {} not found", | ||
table_name.to_string() | ||
))) | ||
} | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use serde_json::json; | ||
use tracing_test::traced_test; | ||
|
||
use super::*; | ||
|
@@ -390,4 +402,50 @@ mod tests { | |
|
||
assert!(matches!(result, Err(DatabaseError::TableNotFound(_)))); | ||
} | ||
|
||
#[tokio::test] | ||
async fn test_count_rows() { | ||
#[derive(Serialize, Deserialize, Debug, PartialEq, Clone, Default)] | ||
pub struct User { | ||
id: String, | ||
name: String, | ||
email: String, | ||
} | ||
let mut db = setup_temp_db().await; | ||
|
||
let user_columns = Columns::from_struct::<User>(true); | ||
// | ||
let mut users_table = Table::new("users".to_string(), user_columns.clone()); | ||
db.add_table(&mut users_table) | ||
.await | ||
.expect("failed to add users table"); | ||
|
||
let user1 = json!({ | ||
"id": "1", | ||
"name": "John Doe", | ||
"email": "[email protected]" | ||
}); | ||
let user2 = json!({ | ||
"id": "2", | ||
"name": "Jane Smith", | ||
"email": "[email protected]" | ||
}); | ||
let user3 = json!({ | ||
"id": "3", | ||
"name": "Alice Johnson", | ||
"email": "[email protected]" | ||
}); | ||
|
||
users_table.add_row(&mut db, user1).await; | ||
users_table.add_row(&mut db, user2).await; | ||
users_table.add_row(&mut db, user3).await; | ||
|
||
// Count rows in the table | ||
let row_count = db.count_rows("users").unwrap(); | ||
assert_eq!(row_count, 3); | ||
|
||
// Attempt to count rows for a non-existent table | ||
let result = db.count_rows("NonExistentTable"); | ||
assert!(matches!(result, Err(DatabaseError::TableNotFound(_)))); | ||
} | ||
} |