working test added

This commit is contained in:
filipriec
2025-02-23 20:35:45 +01:00
parent 7157ab8e43
commit d1be9c74ed

View File

@@ -1,33 +1,48 @@
// src/adresar/tests/post_adresar_test.rs
use super::super::handlers::post_adresar;
use common::proto::multieko2::adresar::PostAdresarRequest;
use sqlx::{postgres::PgPoolOptions, PgPool, Transaction, Postgres};
use sqlx::{postgres::PgPoolOptions, PgPool};
use std::env;
async fn setup() -> (PgPool, Transaction<'static, Postgres>) {
async fn setup_test_db() -> PgPool {
dotenvy::from_filename(".env_test").ok();
let pool = PgPoolOptions::new()
.max_connections(5)
.connect(&env::var("TEST_DATABASE_URL").unwrap())
.await
.unwrap();
let transaction = pool.begin().await.unwrap();
(pool, transaction)
sqlx::migrate!()
.run(&pool)
.await
.expect("Migrations failed");
pool
}
#[tokio::test]
async fn test_create_adresar_success() {
let (pool, mut transaction) = setup().await;
let pool = setup_test_db().await;
let request = PostAdresarRequest {
firma: "Test Company".into(),
..Default::default()
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(),
};
let response = post_adresar(&mut transaction, request)
.await
.unwrap();
let response = post_adresar(&pool, request).await.unwrap();
// Verify response
assert!(response.id > 0);
@@ -48,22 +63,18 @@ async fn test_create_adresar_success() {
assert_eq!(response.fax, "+421123456700");
// Verify database state
let record: (bool,) = sqlx::query_as(
"SELECT deleted FROM adresar WHERE id = $1"
)
.bind(response.id)
.fetch_one(&pool)
.await
.unwrap();
let record = sqlx::query!("SELECT deleted FROM adresar WHERE id = $1", response.id)
.fetch_one(&pool)
.await
.unwrap();
assert!(!record.0); // Verify deleted is false
assert!(!record.deleted); // Verify deleted is false
}
#[tokio::test]
async fn test_create_adresar_required_fields() {
let pool = setup_test_db().await;
// Test with only required field (firma)
let request = PostAdresarRequest {
firma: "Required Only".into(),
..Default::default()
@@ -93,14 +104,14 @@ async fn test_create_adresar_required_fields() {
#[tokio::test]
async fn test_create_adresar_empty_firma() {
let (_, mut transaction) = setup().await;
let pool = setup_test_db().await;
let request = PostAdresarRequest {
firma: "".into(),
..Default::default()
};
let result = post_adresar(&mut transaction, request).await;
let result = post_adresar(&pool, request).await;
assert!(result.is_err());
assert_eq!(result.unwrap_err().code(), tonic::Code::InvalidArgument);
@@ -108,18 +119,78 @@ async fn test_create_adresar_empty_firma() {
#[tokio::test]
async fn test_create_adresar_database_error() {
let (pool, _) = setup().await;
// Simulate closed pool
let broken_pool = pool.clone();
broken_pool.close().await;
// Create a valid pool but close it immediately to simulate a broken connection
let pool = setup_test_db().await;
pool.close().await;
let request = PostAdresarRequest {
firma: "Test".into(),
..Default::default()
};
let result = post_adresar(&broken_pool, request).await;
let result = post_adresar(&pool, request).await;
assert!(result.is_err());
assert_eq!(result.unwrap_err().code(), tonic::Code::Internal);
}
#[tokio::test]
async fn test_create_adresar_max_length_fields() {
let pool = setup_test_db().await;
let request = PostAdresarRequest {
firma: "a".repeat(255), // Assuming 255 is max length
kz: "a".repeat(100),
drc: "a".repeat(100),
ulica: "a".repeat(100),
psc: "a".repeat(20),
mesto: "a".repeat(100),
stat: "a".repeat(100),
banka: "a".repeat(100),
ucet: "a".repeat(100),
skladm: "a".repeat(100),
ico: "a".repeat(20),
kontakt: "a".repeat(100),
telefon: "a".repeat(20),
skladu: "a".repeat(100),
fax: "a".repeat(20),
};
let response = post_adresar(&pool, request).await.unwrap();
assert!(response.id > 0);
// Verify all fields were stored correctly
assert_eq!(response.firma.len(), 255);
assert_eq!(response.kz.len(), 100);
// ... add similar assertions for other fields
}
#[tokio::test]
async fn test_create_adresar_special_characters() {
let pool = setup_test_db().await;
let request = PostAdresarRequest {
firma: "Test & Company ©".into(),
kz: "KZ-123/456".into(),
drc: "Dr. Černý".into(),
ulica: "Náměstí 28. října".into(),
psc: "123 45".into(),
mesto: "Praha 1".into(),
stat: "Česká republika".into(),
banka: "Banka & Spol.".into(),
ucet: "123456-789/0100".into(),
skladm: "Sklad #1".into(),
ico: "12345678".into(),
kontakt: "Jan Novák <jnovak@test.com>".into(),
telefon: "+420 123 456 789".into(),
skladu: "Sklad Ústí".into(),
fax: "+420 123 456 700".into(),
};
let response = post_adresar(&pool, request).await.unwrap();
assert!(response.id > 0);
assert_eq!(response.firma, "Test & Company ©");
assert_eq!(response.kz, "KZ-123/456");
// ... verify other special character fields
}