extract create_test_app to a macro

This commit is contained in:
Mika 2025-06-17 14:17:20 +02:00
parent 5094b3cb46
commit cc58c15c71
3 changed files with 21 additions and 15 deletions

View file

@ -1,10 +1,7 @@
use backend::{Database, build_database_url}; use backend::{Database, build_database_url};
use log::{debug, info}; use log::{debug, info};
use migration::{Migrator, MigratorTrait}; use migration::{Migrator, MigratorTrait};
use testcontainers::{ use testcontainers::{ContainerAsync, ImageExt, runners::AsyncRunner};
ContainerAsync, ImageExt, core::logs::consumer::logging_consumer::LoggingConsumer,
runners::AsyncRunner,
};
use testcontainers_modules::{postgres::Postgres, redis::Redis}; use testcontainers_modules::{postgres::Postgres, redis::Redis};
pub mod test_helpers; pub mod test_helpers;

View file

@ -29,3 +29,20 @@ pub async fn get_database() -> &'static Database {
&state.database &state.database
} }
#[macro_export]
macro_rules! create_test_app {
() => {{
let db = $crate::common::test_helpers::get_database().await;
actix_web::test::init_service(
actix_web::App::new()
.app_data(actix_web::web::Data::new(db.clone()))
.service(
actix_web::web::scope("/api/v1")
.configure(backend::controller::register_controllers),
),
)
.await
}};
}

View file

@ -1,8 +1,7 @@
use actix_web::{App, http::header, test, web}; use actix_web::{http::header, test};
use backend::controller;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use crate::common::test_helpers::get_database; use crate::create_test_app;
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
@ -20,14 +19,7 @@ mod tests {
#[actix_web::test] #[actix_web::test]
async fn test_login() { async fn test_login() {
let db = get_database().await; let app = create_test_app!();
let app = test::init_service(
App::new()
.app_data(web::Data::new(db.clone()))
.service(web::scope("/api/v1").configure(controller::register_controllers)),
)
.await;
let req = test::TestRequest::post() let req = test::TestRequest::post()
.uri("/api/v1/user") .uri("/api/v1/user")