working perfectly well
This commit is contained in:
@@ -1,77 +1,30 @@
|
||||
// tests/common/mod.rs
|
||||
use dotenvy;
|
||||
use sqlx::{postgres::PgPoolOptions, PgPool};
|
||||
use std::env;
|
||||
use tokio::sync::OnceCell;
|
||||
use std::path::Path;
|
||||
|
||||
static DB_POOL: OnceCell<PgPool> = OnceCell::const_new();
|
||||
pub async fn setup_test_db() -> PgPool {
|
||||
// Get path to server directory
|
||||
let manifest_dir = env::var("CARGO_MANIFEST_DIR").expect("CARGO_MANIFEST_DIR must be set");
|
||||
let env_path = Path::new(&manifest_dir).join(".env_test");
|
||||
|
||||
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();
|
||||
// Load environment variables
|
||||
dotenvy::from_path(env_path).ok();
|
||||
|
||||
// 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)
|
||||
// Create connection pool
|
||||
let database_url = env::var("TEST_DATABASE_URL").expect("TEST_DATABASE_URL must be set");
|
||||
let pool = PgPoolOptions::new()
|
||||
.max_connections(5)
|
||||
.connect(&database_url)
|
||||
.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());
|
||||
}
|
||||
.expect("Failed to create pool");
|
||||
|
||||
// Run migrations
|
||||
sqlx::migrate!()
|
||||
.run(&pool)
|
||||
.await
|
||||
.expect("Migrations failed");
|
||||
|
||||
pool
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user