diff --git a/src/helpers.rs b/src/helpers.rs index 377c9ff..17cb82c 100644 --- a/src/helpers.rs +++ b/src/helpers.rs @@ -4,8 +4,6 @@ use rocket::local::blocking::Client; use rocket::{self, routes}; use crate::todos::{get_todos, add_todo, delete_todo, update_todo, complete_todo}; use crate::user::{create_user, get_user_by_id}; -use std::fs; -use std::path::Path; use diesel::sql_query; use diesel::r2d2::{self, ConnectionManager}; @@ -58,17 +56,19 @@ pub fn run_seed_script(pool: &DbPool) -> Result<(), diesel::result::Error> { Ok(()) } -pub fn cleanup_database() { +pub fn cleanup_database(pool: &DbPool) -> Result<(), diesel::result::Error> { info!("Cleaning up database"); - let path = "test.sqlite"; - if Path::new(path).exists() { - if let Err(e) = fs::remove_file(path) { - error!("Failed to remove test.sqlite: {:?}", e); - } else { - info!("Successfully removed test.sqlite"); + // Get a connection from the pool + let mut connection = pool.get().expect("Failed to get connection from pool."); + + let sql = include_str!("../tests/cleanup.sql"); + for statement in sql.split(';') { + let trimmed = statement.trim(); + if !trimmed.is_empty() { + sql_query(trimmed) + .execute(&mut connection)?; // Now using pooled connection } - } else { - info!("No test.sqlite file found to remove."); } + Ok(()) } \ No newline at end of file diff --git a/tests/add_todo.rs b/tests/add_todo.rs index 742817a..ddc1cea 100644 --- a/tests/add_todo.rs +++ b/tests/add_todo.rs @@ -5,8 +5,8 @@ use rocket::http::ContentType; #[test] fn test_add_valid_todo() { - cleanup_database(); // Clean up the database before starting the test let mut pool = establish_test_connection(); + cleanup_database(&mut pool).unwrap(); // Clean up the database before starting the test run_seed_script(&mut pool).unwrap(); // Seed the database with initial data let client = setup_rocket(); @@ -29,8 +29,8 @@ fn test_add_valid_todo() { #[test] fn test_add_todo_empty_title() { - cleanup_database(); // Clean up the database before starting the test let mut pool = establish_test_connection(); + cleanup_database(&mut pool).unwrap(); // Clean up the database before starting the test run_seed_script(&mut pool).unwrap(); // Seed the database with initial data let client = setup_rocket(); @@ -53,8 +53,8 @@ fn test_add_todo_empty_title() { #[test] fn test_add_todo_marked_completed() { - cleanup_database(); // Clean up the database before starting the test let mut pool = establish_test_connection(); + cleanup_database(&mut pool).unwrap(); // Clean up the database before starting the test run_seed_script(&mut pool).unwrap(); // Seed the database with initial data let client = setup_rocket(); diff --git a/tests/cleanup.sql b/tests/cleanup.sql new file mode 100644 index 0000000..a8fa9cc --- /dev/null +++ b/tests/cleanup.sql @@ -0,0 +1,2 @@ +DROP TABLE users; +DROP TABLE todos; \ No newline at end of file diff --git a/tests/complete_todo.rs b/tests/complete_todo.rs index 50a38a3..e3e0ed7 100644 --- a/tests/complete_todo.rs +++ b/tests/complete_todo.rs @@ -5,8 +5,8 @@ use rocket::http::ContentType; #[test] fn test_complete_todo() { - cleanup_database(); // Clean up the database before starting the test let mut pool = establish_test_connection(); + cleanup_database(&mut pool).unwrap(); // Clean up the database before starting the test run_seed_script(&mut pool).unwrap(); // Seed the database with initial data let client = setup_rocket(); diff --git a/tests/delete_todo.rs b/tests/delete_todo.rs index 42db265..a7bc015 100644 --- a/tests/delete_todo.rs +++ b/tests/delete_todo.rs @@ -3,8 +3,8 @@ use dooly::helpers::{establish_test_connection, setup_rocket, run_seed_script, c #[test] fn test_delete_todo() { - cleanup_database(); // Clean up the database before starting the test let mut pool = establish_test_connection(); + cleanup_database(&mut pool).unwrap(); // Clean up the database before starting the test run_seed_script(&mut pool).unwrap(); // Seed the database with initial data let client = setup_rocket(); diff --git a/tests/get_todos.rs b/tests/get_todos.rs index a78c6c7..72bfc53 100644 --- a/tests/get_todos.rs +++ b/tests/get_todos.rs @@ -3,8 +3,8 @@ use dooly::helpers::{establish_test_connection, setup_rocket, run_seed_script, c #[test] fn test_get_todos() { - cleanup_database(); // Clean up the database before starting the test let mut pool = establish_test_connection(); + cleanup_database(&mut pool).unwrap(); // Clean up the database before starting the test run_seed_script(&mut pool).unwrap(); // Seed the database with initial data let client = setup_rocket(); diff --git a/tests/update_todo.rs b/tests/update_todo.rs index 6f25ca4..5797f26 100644 --- a/tests/update_todo.rs +++ b/tests/update_todo.rs @@ -5,8 +5,8 @@ use rocket::http::ContentType; #[test] fn test_valid_update_todo() { - cleanup_database(); // Clean up the database before starting the test - let pool = establish_test_connection(); + let mut pool = establish_test_connection(); + cleanup_database(&mut pool).unwrap(); // Clean up the database before starting the test run_seed_script(&pool).unwrap(); // Seed the database with initial data let client = setup_rocket(); @@ -53,8 +53,8 @@ fn test_valid_update_todo() { #[test] fn test_invalid_update_todo_empty_title() { - cleanup_database(); // Clean up the database before starting the test - let pool = establish_test_connection(); // Use pool now + let mut pool = establish_test_connection(); // Use pool now + cleanup_database(&mut pool).unwrap(); // Clean up the database before starting the test run_seed_script(&pool).unwrap(); // Seed the database with initial data let client = setup_rocket(); diff --git a/tests/user.rs b/tests/user.rs index 33d8d58..c055af1 100644 --- a/tests/user.rs +++ b/tests/user.rs @@ -5,8 +5,8 @@ use rocket::http::ContentType; #[test] fn test_create_user_valid() { - cleanup_database(); // Clean up the database before starting the test let pool = establish_test_connection(); + cleanup_database(&pool).unwrap(); // Clean up the database before starting the test run_seed_script(&pool).unwrap(); // Seed the database with initial data let client = setup_rocket(); @@ -28,8 +28,8 @@ fn test_create_user_valid() { #[test] fn test_create_user_empty_username() { - cleanup_database(); // Clean up the database before starting the test let pool = establish_test_connection(); + cleanup_database(&pool).unwrap(); // Clean up the database before starting the test run_seed_script(&pool).unwrap(); // Seed the database with initial data let client = setup_rocket(); @@ -51,8 +51,8 @@ fn test_create_user_empty_username() { #[test] fn test_create_user_empty_password() { - cleanup_database(); // Clean up the database before starting the test let pool = establish_test_connection(); + cleanup_database(&pool).unwrap(); // Clean up the database before starting the test run_seed_script(&pool).unwrap(); // Seed the database with initial data let client = setup_rocket(); @@ -74,8 +74,8 @@ fn test_create_user_empty_password() { #[test] fn test_get_user_by_id() { - cleanup_database(); // Clean up the database before starting the test let pool = establish_test_connection(); + cleanup_database(&pool).unwrap(); // Clean up the database before starting the test run_seed_script(&pool).unwrap(); // Seed the database with initial data let client = setup_rocket(); @@ -109,8 +109,8 @@ fn test_get_user_by_id() { #[test] fn test_get_user_by_id_not_found() { - cleanup_database(); // Clean up the database before starting the test - let pool = establish_test_connection(); + let mut pool = establish_test_connection(); + cleanup_database(&mut pool).unwrap(); // Clean up the database before starting the test run_seed_script(&pool).unwrap(); // Seed the database with initial data let client = setup_rocket();