Skip to content

Commit

Permalink
Fix test cleanup function
Browse files Browse the repository at this point in the history
  • Loading branch information
JakenHerman committed Oct 15, 2024
1 parent 34a08c6 commit c0765f1
Show file tree
Hide file tree
Showing 8 changed files with 29 additions and 27 deletions.
22 changes: 11 additions & 11 deletions src/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};

Expand Down Expand Up @@ -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(())
}
6 changes: 3 additions & 3 deletions tests/add_todo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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();
Expand All @@ -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();
Expand Down
2 changes: 2 additions & 0 deletions tests/cleanup.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
DROP TABLE users;
DROP TABLE todos;
2 changes: 1 addition & 1 deletion tests/complete_todo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
2 changes: 1 addition & 1 deletion tests/delete_todo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
2 changes: 1 addition & 1 deletion tests/get_todos.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
8 changes: 4 additions & 4 deletions tests/update_todo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand Down
12 changes: 6 additions & 6 deletions tests/user.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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();
Expand All @@ -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();
Expand All @@ -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();
Expand Down Expand Up @@ -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();
Expand Down

0 comments on commit c0765f1

Please sign in to comment.