Saving changes
Some checks failed
Rust Build / Check (pull_request) Successful in 48s
Rust Build / Test Suite (pull_request) Failing after 1m28s
Rust Build / Rustfmt (pull_request) Successful in 30s
Rust Build / Clippy (pull_request) Successful in 1m15s
Rust Build / build (pull_request) Successful in 2m6s
Some checks failed
Rust Build / Check (pull_request) Successful in 48s
Rust Build / Test Suite (pull_request) Failing after 1m28s
Rust Build / Rustfmt (pull_request) Successful in 30s
Rust Build / Clippy (pull_request) Successful in 1m15s
Rust Build / build (pull_request) Successful in 2m6s
This commit is contained in:
59
src/main.rs
59
src/main.rs
@@ -59,8 +59,6 @@ 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,
|
||||||
@@ -77,14 +75,31 @@ 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> {
|
mod db_mgr {
|
||||||
let db_url =
|
use std::str::FromStr;
|
||||||
env::var("TEST_DATABASE_URL").expect("TEST_DATABASE_URL must be set for tests");
|
|
||||||
|
pub const LIMIT: usize = 6;
|
||||||
|
|
||||||
|
pub async fn eeee() -> Result<sqlx::PgPool, sqlx::Error> {
|
||||||
|
let tm_db_url = std::env::var("DATABASE_URL").expect("DATABASE_URL must be present");
|
||||||
|
let tm_options = sqlx::postgres::PgConnectOptions::from_str(&tm_db_url).unwrap();
|
||||||
|
sqlx::PgPool::connect_with(tm_options).await
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn generate_db_name() -> String {
|
||||||
|
let db_name =
|
||||||
|
get_database_name().unwrap() + &"_" + &uuid::Uuid::new_v4().to_string()[..LIMIT];
|
||||||
|
db_name
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn connect_to_db(db_name: &str) -> Result<sqlx::PgPool, sqlx::Error> {
|
||||||
|
let db_url = std::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);
|
let options = sqlx::postgres::PgConnectOptions::from_str(&db_url)?.database(db_name);
|
||||||
sqlx::PgPool::connect_with(options).await
|
sqlx::PgPool::connect_with(options).await
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn create_database(
|
pub async fn create_database(
|
||||||
template_pool: &sqlx::PgPool,
|
template_pool: &sqlx::PgPool,
|
||||||
db_name: &str,
|
db_name: &str,
|
||||||
) -> Result<(), sqlx::Error> {
|
) -> Result<(), sqlx::Error> {
|
||||||
@@ -93,17 +108,19 @@ mod tests {
|
|||||||
Ok(_) => Ok(()),
|
Ok(_) => Ok(()),
|
||||||
Err(e) => Err(e),
|
Err(e) => Err(e),
|
||||||
}
|
}
|
||||||
// Ok(())
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Function to drop a database
|
// Function to drop a database
|
||||||
async fn drop_database(template_pool: &sqlx::PgPool, db_name: &str) -> Result<(), sqlx::Error> {
|
pub 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);
|
let drop_query = format!("DROP DATABASE IF EXISTS {} WITH (FORCE)", db_name);
|
||||||
sqlx::query(&drop_query).execute(template_pool).await?;
|
sqlx::query(&drop_query).execute(template_pool).await?;
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_database_name() -> Result<String, Box<dyn std::error::Error>> {
|
pub fn get_database_name() -> Result<String, Box<dyn std::error::Error>> {
|
||||||
dotenvy::dotenv().ok(); // Load .env file if it exists
|
dotenvy::dotenv().ok(); // Load .env file if it exists
|
||||||
|
|
||||||
match std::env::var("TEST_DATABASE_URL") {
|
match std::env::var("TEST_DATABASE_URL") {
|
||||||
@@ -128,6 +145,7 @@ mod tests {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[tokio::test]
|
#[tokio::test]
|
||||||
async fn test_hello_world() {
|
async fn test_hello_world() {
|
||||||
@@ -153,14 +171,11 @@ 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_pool = db_mgr::eeee().await.unwrap();
|
||||||
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];
|
let db_name = db_mgr::generate_db_name().await;
|
||||||
// assert_eq!(true, db_name.is_empty(), "{:?} {:?}", db_name, tm_db_url);
|
|
||||||
// assert!(db_name.is_empty(), "eee {:?}", tm_db_url);
|
match db_mgr::create_database(&tm_pool, &db_name).await {
|
||||||
match create_database(&tm_pool, &db_name).await {
|
|
||||||
Ok(_) => {
|
Ok(_) => {
|
||||||
println!("Success");
|
println!("Success");
|
||||||
}
|
}
|
||||||
@@ -169,16 +184,10 @@ mod tests {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
let pool = connect_to_db(&db_name).await.unwrap();
|
let pool = db_mgr::connect_to_db(&db_name).await.unwrap();
|
||||||
|
|
||||||
/*
|
|
||||||
let pool = icarus_auth::db_pool::create_pool()
|
|
||||||
.await
|
|
||||||
.expect("Failed to create pool");
|
|
||||||
*/
|
|
||||||
db::migrations(&pool).await;
|
db::migrations(&pool).await;
|
||||||
|
|
||||||
// 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 {
|
||||||
@@ -220,8 +229,6 @@ mod tests {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
drop_database(&tm_pool, &db_name).await;
|
let _ = db_mgr::drop_database(&tm_pool, &db_name).await;
|
||||||
|
|
||||||
// tx.rollback().await.unwrap();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user