From 5df7311e41b11ae82bbbdeafb258031279635202 Mon Sep 17 00:00:00 2001 From: filipriec Date: Mon, 24 Feb 2025 09:53:50 +0100 Subject: [PATCH] changing the structure of the unit testing for good --- server/Cargo.toml | 4 + server/src/adresar/tests/mod.rs | 4 - .../adresar}/post_adresar_test.rs | 0 .../adresar}/put_adresar_test.rs | 0 server/tests/common/mod.rs | 77 +++++++++++++++++++ 5 files changed, 81 insertions(+), 4 deletions(-) delete mode 100644 server/src/adresar/tests/mod.rs rename server/{src/adresar/tests => tests/adresar}/post_adresar_test.rs (100%) rename server/{src/adresar/tests => tests/adresar}/put_adresar_test.rs (100%) create mode 100644 server/tests/common/mod.rs diff --git a/server/Cargo.toml b/server/Cargo.toml index ba7330d..4914391 100644 --- a/server/Cargo.toml +++ b/server/Cargo.toml @@ -21,3 +21,7 @@ tracing = "0.1.41" [lib] name = "server" path = "src/lib.rs" + +[dev-dependencies] +tokio = { version = "1.0", features = ["full", "test-util"] } +dotenv = "0.15" diff --git a/server/src/adresar/tests/mod.rs b/server/src/adresar/tests/mod.rs deleted file mode 100644 index a6e60d6..0000000 --- a/server/src/adresar/tests/mod.rs +++ /dev/null @@ -1,4 +0,0 @@ -// src/adresar/tests/mod.rs - -pub mod post_adresar_test; -pub mod put_adresar_test; diff --git a/server/src/adresar/tests/post_adresar_test.rs b/server/tests/adresar/post_adresar_test.rs similarity index 100% rename from server/src/adresar/tests/post_adresar_test.rs rename to server/tests/adresar/post_adresar_test.rs diff --git a/server/src/adresar/tests/put_adresar_test.rs b/server/tests/adresar/put_adresar_test.rs similarity index 100% rename from server/src/adresar/tests/put_adresar_test.rs rename to server/tests/adresar/put_adresar_test.rs diff --git a/server/tests/common/mod.rs b/server/tests/common/mod.rs new file mode 100644 index 0000000..7240a19 --- /dev/null +++ b/server/tests/common/mod.rs @@ -0,0 +1,77 @@ +// tests/common/mod.rs +use sqlx::{postgres::PgPoolOptions, PgPool}; +use std::env; +use tokio::sync::OnceCell; + +static DB_POOL: OnceCell = OnceCell::const_new(); + +pub async fn get_db_pool() -> PgPool { + DB_POOL.get_or_init(|| async { + dotenvy::from_filename(".env_test").ok(); + let pool = PgPoolOptions::new() + .max_connections(5) + .connect(&env::var("TEST_DATABASE_URL").unwrap()) + .await + .unwrap(); + + // Run migrations + sqlx::migrate!() + .run(&pool) + .await + .expect("Migrations failed"); + + pool + }).await.clone() +} + +pub async fn cleanup_db(pool: &PgPool) { + sqlx::query!("TRUNCATE TABLE adresar, uctovnictvo RESTART IDENTITY CASCADE") + .execute(pool) + .await + .expect("Failed to cleanup test database"); +} + +// Test helpers +pub mod adresar_helpers { + use common::proto::multieko2::adresar::{PostAdresarRequest, AdresarResponse}; + use sqlx::PgPool; + + pub fn valid_request() -> PostAdresarRequest { + PostAdresarRequest { + firma: "Test Company".into(), + kz: "KZ123".into(), + drc: "DRC456".into(), + ulica: "Test Street".into(), + psc: "12345".into(), + mesto: "Test City".into(), + stat: "Test Country".into(), + banka: "Test Bank".into(), + ucet: "123456789".into(), + skladm: "Warehouse M".into(), + ico: "12345678".into(), + kontakt: "John Doe".into(), + telefon: "+421123456789".into(), + skladu: "Warehouse U".into(), + fax: "+421123456700".into(), + } + } + + pub fn minimal_request() -> PostAdresarRequest { + PostAdresarRequest { + firma: "Required Only".into(), + ..Default::default() + } + } + + pub async fn assert_response_matches(pool: &PgPool, response: &AdresarResponse) { + let db_record = sqlx::query!("SELECT * FROM adresar WHERE id = $1", response.id) + .fetch_one(pool) + .await + .unwrap(); + + assert_eq!(db_record.firma, response.firma); + assert_eq!(db_record.telefon, Some(response.telefon.clone())); + assert!(!db_record.deleted); + assert!(db_record.created_at.is_some()); + } +}