User-Integration Tests and better infrastructure #28

Merged
mixel merged 8 commits from integration-test-addition into main 2025-06-20 17:03:40 +02:00
2 changed files with 21 additions and 13 deletions
Showing only changes of commit 7033ad7013 - Show all commits

View file

@ -9,26 +9,32 @@ use super::setup;
struct TestState {
_postgres: ContainerAsync<Postgres>,
_redis: ContainerAsync<Redis>,
database: Database,
}
lazy_static! {
static ref TEST_STATE: tokio::sync::OnceCell<TestState> = tokio::sync::OnceCell::new();
}
pub async fn get_database() -> &'static Database {
let state = TEST_STATE
pub async fn get_database() -> Database {
let _state = TEST_STATE
.get_or_init(|| async {
let (postgres, redis, database) = setup().await;
let (postgres, redis, _database) = setup().await;
TestState {
_postgres: postgres,
_redis: redis,
database,
}
})
.await;
&state.database
// Create a new database connection for each test
let database_url = backend::build_database_url();
let mut opts = sea_orm::ConnectOptions::new(database_url);
opts.max_connections(5)
.min_connections(1)
.connect_timeout(std::time::Duration::from_secs(10))
.acquire_timeout(std::time::Duration::from_secs(10));
Database::new(opts).await.unwrap()
}
static TEST_COUNTER: AtomicU64 = AtomicU64::new(1);
@ -71,7 +77,7 @@ macro_rules! create_test_app {
actix_web::test::init_service(
actix_web::App::new()
.app_data(actix_web::web::Data::new(db.clone()))
.app_data(actix_web::web::Data::new(db))
.service(
actix_web::web::scope("/api/v1")
.configure(backend::controller::register_controllers),

View file

@ -17,7 +17,7 @@ mod tests {
#[actix_web::test]
async fn test_create_user() {
let ctx: TestContext = TestContext::new();
let db = crate::common::test_helpers::get_database().await;
let db = &crate::common::test_helpers::get_database().await;
let app = create_test_app!();
@ -61,7 +61,7 @@ mod tests {
#[actix_web::test]
async fn test_delete_user() {
let ctx = TestContext::new();
let db = crate::common::test_helpers::get_database().await;
let db = &crate::common::test_helpers::get_database().await;
let app = create_test_app!();
@ -97,7 +97,7 @@ mod tests {
#[actix_web::test]
async fn test_get_users() {
let ctx = TestContext::new();
let db = crate::common::test_helpers::get_database().await;
let db = &crate::common::test_helpers::get_database().await;
let app = create_test_app!();
@ -127,8 +127,10 @@ mod tests {
assert!(found, "User {} not found in API response", user.username);
}
// Verify database consistency
assert!(ctx.assert_user_count(db, 3).await);
// Verify our created users exist in database
for user in &users {
assert!(ctx.assert_user_exists(db, user.id).await);
}
// Cleanup
ctx.cleanup_all(db).await;
@ -137,7 +139,7 @@ mod tests {
#[actix_web::test]
async fn test_delete_nonexistent_user() {
let ctx = TestContext::new();
let db = crate::common::test_helpers::get_database().await;
let db = &crate::common::test_helpers::get_database().await;
let app = create_test_app!();
let fake_id = "00000000-0000-0000-0000-000000000000";