changing the structure of the unit testing for good
This commit is contained in:
77
server/tests/common/mod.rs
Normal file
77
server/tests/common/mod.rs
Normal file
@@ -0,0 +1,77 @@
|
||||
// tests/common/mod.rs
|
||||
use sqlx::{postgres::PgPoolOptions, PgPool};
|
||||
use std::env;
|
||||
use tokio::sync::OnceCell;
|
||||
|
||||
static DB_POOL: OnceCell<PgPool> = 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());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user