refactor: replace println with debug logging in tests for better log management
This commit is contained in:
parent
e383615150
commit
5094b3cb46
2 changed files with 22 additions and 27 deletions
|
@ -1,6 +1,10 @@
|
|||
use backend::{Database, build_database_url};
|
||||
use log::{debug, info};
|
||||
use migration::{Migrator, MigratorTrait};
|
||||
use testcontainers::{ContainerAsync, ImageExt, runners::AsyncRunner};
|
||||
use testcontainers::{
|
||||
ContainerAsync, ImageExt, core::logs::consumer::logging_consumer::LoggingConsumer,
|
||||
runners::AsyncRunner,
|
||||
};
|
||||
use testcontainers_modules::{postgres::Postgres, redis::Redis};
|
||||
|
||||
pub mod test_helpers;
|
||||
|
@ -22,8 +26,8 @@ pub async fn setup() -> (ContainerAsync<Postgres>, ContainerAsync<Redis>, Databa
|
|||
let postgres_port = postgres.get_host_port_ipv4(5432).await.unwrap();
|
||||
let redis_port = redis.get_host_port_ipv4(6379).await.unwrap();
|
||||
|
||||
println!("PostgreSQL container started on port: {}", postgres_port);
|
||||
println!("Redis container started on port: {}", redis_port);
|
||||
debug!("PostgreSQL container started on port: {}", postgres_port);
|
||||
debug!("Redis container started on port: {}", redis_port);
|
||||
|
||||
// Wait for PostgreSQL to be ready
|
||||
wait_for_postgres_ready(&postgres).await;
|
||||
|
@ -39,7 +43,7 @@ pub async fn setup() -> (ContainerAsync<Postgres>, ContainerAsync<Redis>, Databa
|
|||
}
|
||||
|
||||
let database_url = build_database_url();
|
||||
println!("Database URL: {}", database_url);
|
||||
info!("Database URL: {}", database_url);
|
||||
|
||||
let database = Database::new(database_url.into()).await.unwrap();
|
||||
|
||||
|
@ -49,32 +53,22 @@ pub async fn setup() -> (ContainerAsync<Postgres>, ContainerAsync<Redis>, Databa
|
|||
}
|
||||
|
||||
async fn wait_for_postgres_ready(container: &ContainerAsync<Postgres>) {
|
||||
use sea_orm::{Database as SeaOrmDatabase, DbErr};
|
||||
|
||||
println!("Waiting for PostgreSQL to be ready...");
|
||||
|
||||
let postgres_port = container.get_host_port_ipv4(5432).await.unwrap();
|
||||
let connection_string = format!(
|
||||
"postgresql://postgres:postgres@127.0.0.1:{}/test_db",
|
||||
postgres_port
|
||||
);
|
||||
info!("Waiting for PostgreSQL to be ready...");
|
||||
|
||||
for attempt in 1..=30 {
|
||||
match SeaOrmDatabase::connect(&connection_string).await {
|
||||
Ok(conn) => match conn.ping().await {
|
||||
Ok(_) => {
|
||||
println!("PostgreSQL is ready after {} attempts", attempt);
|
||||
match container.stdout_to_vec().await {
|
||||
Ok(logs) => {
|
||||
let log_string = String::from_utf8_lossy(&logs);
|
||||
|
||||
if log_string.contains("database system is ready to accept connections") {
|
||||
info!("PostgreSQL is ready after {} attempts", attempt);
|
||||
return;
|
||||
}
|
||||
Err(_) => {
|
||||
println!("Attempt {}: PostgreSQL connection failed ping", attempt);
|
||||
|
||||
debug!("Attempt {}: PostgreSQL not ready yet", attempt);
|
||||
}
|
||||
},
|
||||
Err(DbErr::Conn(_)) => {
|
||||
println!("Attempt {}: PostgreSQL connection refused", attempt);
|
||||
}
|
||||
Err(_) => {
|
||||
println!("Attempt {}: PostgreSQL other error", attempt);
|
||||
Err(e) => {
|
||||
debug!("Attempt {}: Failed to read logs: {}", attempt, e);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -6,6 +6,7 @@ use crate::common::test_helpers::get_database;
|
|||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use log::debug;
|
||||
use serde_json::json;
|
||||
|
||||
use super::*;
|
||||
|
@ -46,8 +47,8 @@ mod tests {
|
|||
let body = test::read_body(req).await;
|
||||
let body_str = String::from_utf8_lossy(&body);
|
||||
|
||||
println!("Response status: {}", status);
|
||||
println!("Response body: {}", body_str);
|
||||
debug!("Response status: {}", status);
|
||||
debug!("Response body: {}", body_str);
|
||||
|
||||
assert!(status.is_success() || status.is_client_error());
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue