Making tests more independent
Some checks failed
Rust Build / Check (pull_request) Successful in 46s
Rust Build / Test Suite (pull_request) Failing after 55s
Rust Build / Rustfmt (pull_request) Successful in 29s
Rust Build / Clippy (pull_request) Successful in 52s
Rust Build / build (pull_request) Successful in 1m10s
Some checks failed
Rust Build / Check (pull_request) Successful in 46s
Rust Build / Test Suite (pull_request) Failing after 55s
Rust Build / Rustfmt (pull_request) Successful in 29s
Rust Build / Clippy (pull_request) Successful in 52s
Rust Build / build (pull_request) Successful in 1m10s
This commit is contained in:
@@ -20,5 +20,6 @@ icarus_models = { git = "ssh://git@git.kundeng.us/phoenix/icarus_models.git", ta
|
|||||||
|
|
||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
http-body-util = { version = "0.1.3" }
|
http-body-util = { version = "0.1.3" }
|
||||||
|
url = { version = "2.5" }
|
||||||
reqwest = { version = "0.12.5", features = ["json"] } # For making HTTP requests in tests
|
reqwest = { version = "0.12.5", features = ["json"] } # For making HTTP requests in tests
|
||||||
once_cell = { version = "1.19" } # Useful for lazy initialization in tests/app setup
|
once_cell = { version = "1.19" } # Useful for lazy initialization in tests/app setup
|
||||||
|
81
src/main.rs
81
src/main.rs
@@ -59,6 +59,9 @@ mod init {
|
|||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
|
use std::{env, str::FromStr};
|
||||||
|
use uuid::Uuid;
|
||||||
|
|
||||||
use axum::{
|
use axum::{
|
||||||
body::Body,
|
body::Body,
|
||||||
http::{Request, StatusCode},
|
http::{Request, StatusCode},
|
||||||
@@ -74,6 +77,58 @@ mod tests {
|
|||||||
pub data: icarus_auth::models::common::User,
|
pub data: icarus_auth::models::common::User,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async fn connect_to_db(db_name: &str) -> Result<sqlx::PgPool, sqlx::Error> {
|
||||||
|
let db_url =
|
||||||
|
env::var("TEST_DATABASE_URL").expect("TEST_DATABASE_URL must be set for tests");
|
||||||
|
let options = sqlx::postgres::PgConnectOptions::from_str(&db_url)?.database(db_name);
|
||||||
|
sqlx::PgPool::connect_with(options).await
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn create_database(
|
||||||
|
template_pool: &sqlx::PgPool,
|
||||||
|
db_name: &str,
|
||||||
|
) -> Result<(), sqlx::Error> {
|
||||||
|
let create_query = format!("CREATE DATABASE {}", db_name);
|
||||||
|
match sqlx::query(&create_query).execute(template_pool).await {
|
||||||
|
Ok(_) => Ok(()),
|
||||||
|
Err(e) => Err(e),
|
||||||
|
}
|
||||||
|
// Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
// Function to drop a database
|
||||||
|
async fn drop_database(template_pool: &sqlx::PgPool, db_name: &str) -> Result<(), sqlx::Error> {
|
||||||
|
let drop_query = format!("DROP DATABASE IF EXISTS {} WITH (FORCE)", db_name);
|
||||||
|
sqlx::query(&drop_query).execute(template_pool).await?;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn get_database_name() -> Result<String, Box<dyn std::error::Error>> {
|
||||||
|
dotenvy::dotenv().ok(); // Load .env file if it exists
|
||||||
|
|
||||||
|
match std::env::var("TEST_DATABASE_URL") {
|
||||||
|
Ok(database_url) => {
|
||||||
|
let parsed_url = url::Url::parse(&database_url)?;
|
||||||
|
if parsed_url.scheme() == "postgres" || parsed_url.scheme() == "postgresql" {
|
||||||
|
match parsed_url
|
||||||
|
.path_segments()
|
||||||
|
.and_then(|segments| segments.last().map(|s| s.to_string()))
|
||||||
|
{
|
||||||
|
Some(sss) => Ok(sss),
|
||||||
|
None => Err("Error parsing".into()),
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// Handle other database types if needed
|
||||||
|
Err("Error parsing".into())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Err(_) => {
|
||||||
|
// TEST_DATABASE_URL environment variable not found
|
||||||
|
Err("Error parsing".into())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[tokio::test]
|
#[tokio::test]
|
||||||
async fn test_hello_world() {
|
async fn test_hello_world() {
|
||||||
let app = init::app().await;
|
let app = init::app().await;
|
||||||
@@ -98,12 +153,32 @@ mod tests {
|
|||||||
|
|
||||||
#[tokio::test]
|
#[tokio::test]
|
||||||
async fn test_register_user() {
|
async fn test_register_user() {
|
||||||
|
let tm_db_url = env::var("DATABASE_URL").expect("DATABASE_URL must be present");
|
||||||
|
let tm_options = sqlx::postgres::PgConnectOptions::from_str(&tm_db_url).unwrap();
|
||||||
|
let tm_pool = sqlx::PgPool::connect_with(tm_options).await.unwrap();
|
||||||
|
|
||||||
|
let db_name = get_database_name().unwrap() + &"_" + &uuid::Uuid::new_v4().to_string()[..5];
|
||||||
|
// assert_eq!(true, db_name.is_empty(), "{:?} {:?}", db_name, tm_db_url);
|
||||||
|
// assert!(db_name.is_empty(), "eee {:?}", tm_db_url);
|
||||||
|
match create_database(&tm_pool, &db_name).await {
|
||||||
|
Ok(_) => {
|
||||||
|
println!("Success");
|
||||||
|
}
|
||||||
|
Err(e) => {
|
||||||
|
assert!(false, "Error: {:?}", e.to_string());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
let pool = connect_to_db(&db_name).await.unwrap();
|
||||||
|
|
||||||
|
/*
|
||||||
let pool = icarus_auth::db_pool::create_pool()
|
let pool = icarus_auth::db_pool::create_pool()
|
||||||
.await
|
.await
|
||||||
.expect("Failed to create pool");
|
.expect("Failed to create pool");
|
||||||
|
*/
|
||||||
db::migrations(&pool).await;
|
db::migrations(&pool).await;
|
||||||
|
|
||||||
let tx = pool.begin().await.unwrap();
|
// let mut tx = pool.begin().await.unwrap();
|
||||||
let app = init::routes().await.layer(axum::Extension(pool));
|
let app = init::routes().await.layer(axum::Extension(pool));
|
||||||
|
|
||||||
let usr = icarus_auth::models::common::CreateUser {
|
let usr = icarus_auth::models::common::CreateUser {
|
||||||
@@ -145,6 +220,8 @@ mod tests {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
tx.rollback().await.unwrap();
|
drop_database(&tm_pool, &db_name).await;
|
||||||
|
|
||||||
|
// tx.rollback().await.unwrap();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user