working test added
This commit is contained in:
@@ -1,10 +1,10 @@
|
|||||||
// src/adresar/tests/post_adresar_test.rs
|
// src/adresar/tests/post_adresar_test.rs
|
||||||
use super::super::handlers::post_adresar;
|
use super::super::handlers::post_adresar;
|
||||||
use common::proto::multieko2::adresar::PostAdresarRequest;
|
use common::proto::multieko2::adresar::PostAdresarRequest;
|
||||||
use sqlx::{postgres::PgPoolOptions, PgPool, Transaction, Postgres};
|
use sqlx::{postgres::PgPoolOptions, PgPool};
|
||||||
use std::env;
|
use std::env;
|
||||||
|
|
||||||
async fn setup() -> (PgPool, Transaction<'static, Postgres>) {
|
async fn setup_test_db() -> PgPool {
|
||||||
dotenvy::from_filename(".env_test").ok();
|
dotenvy::from_filename(".env_test").ok();
|
||||||
let pool = PgPoolOptions::new()
|
let pool = PgPoolOptions::new()
|
||||||
.max_connections(5)
|
.max_connections(5)
|
||||||
@@ -12,22 +12,37 @@ async fn setup() -> (PgPool, Transaction<'static, Postgres>) {
|
|||||||
.await
|
.await
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
||||||
let transaction = pool.begin().await.unwrap();
|
sqlx::migrate!()
|
||||||
(pool, transaction)
|
.run(&pool)
|
||||||
|
.await
|
||||||
|
.expect("Migrations failed");
|
||||||
|
|
||||||
|
pool
|
||||||
}
|
}
|
||||||
|
|
||||||
#[tokio::test]
|
#[tokio::test]
|
||||||
async fn test_create_adresar_success() {
|
async fn test_create_adresar_success() {
|
||||||
let (pool, mut transaction) = setup().await;
|
let pool = setup_test_db().await;
|
||||||
|
|
||||||
let request = PostAdresarRequest {
|
let request = PostAdresarRequest {
|
||||||
firma: "Test Company".into(),
|
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)
|
let response = post_adresar(&pool, request).await.unwrap();
|
||||||
.await
|
|
||||||
.unwrap();
|
|
||||||
|
|
||||||
// Verify response
|
// Verify response
|
||||||
assert!(response.id > 0);
|
assert!(response.id > 0);
|
||||||
@@ -48,22 +63,18 @@ async fn test_create_adresar_success() {
|
|||||||
assert_eq!(response.fax, "+421123456700");
|
assert_eq!(response.fax, "+421123456700");
|
||||||
|
|
||||||
// Verify database state
|
// Verify database state
|
||||||
let record: (bool,) = sqlx::query_as(
|
let record = sqlx::query!("SELECT deleted FROM adresar WHERE id = $1", response.id)
|
||||||
"SELECT deleted FROM adresar WHERE id = $1"
|
|
||||||
)
|
|
||||||
.bind(response.id)
|
|
||||||
.fetch_one(&pool)
|
.fetch_one(&pool)
|
||||||
.await
|
.await
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
||||||
assert!(!record.0); // Verify deleted is false
|
assert!(!record.deleted); // Verify deleted is false
|
||||||
}
|
}
|
||||||
|
|
||||||
#[tokio::test]
|
#[tokio::test]
|
||||||
async fn test_create_adresar_required_fields() {
|
async fn test_create_adresar_required_fields() {
|
||||||
let pool = setup_test_db().await;
|
let pool = setup_test_db().await;
|
||||||
|
|
||||||
// Test with only required field (firma)
|
|
||||||
let request = PostAdresarRequest {
|
let request = PostAdresarRequest {
|
||||||
firma: "Required Only".into(),
|
firma: "Required Only".into(),
|
||||||
..Default::default()
|
..Default::default()
|
||||||
@@ -93,14 +104,14 @@ async fn test_create_adresar_required_fields() {
|
|||||||
|
|
||||||
#[tokio::test]
|
#[tokio::test]
|
||||||
async fn test_create_adresar_empty_firma() {
|
async fn test_create_adresar_empty_firma() {
|
||||||
let (_, mut transaction) = setup().await;
|
let pool = setup_test_db().await;
|
||||||
|
|
||||||
let request = PostAdresarRequest {
|
let request = PostAdresarRequest {
|
||||||
firma: "".into(),
|
firma: "".into(),
|
||||||
..Default::default()
|
..Default::default()
|
||||||
};
|
};
|
||||||
|
|
||||||
let result = post_adresar(&mut transaction, request).await;
|
let result = post_adresar(&pool, request).await;
|
||||||
|
|
||||||
assert!(result.is_err());
|
assert!(result.is_err());
|
||||||
assert_eq!(result.unwrap_err().code(), tonic::Code::InvalidArgument);
|
assert_eq!(result.unwrap_err().code(), tonic::Code::InvalidArgument);
|
||||||
@@ -108,18 +119,78 @@ async fn test_create_adresar_empty_firma() {
|
|||||||
|
|
||||||
#[tokio::test]
|
#[tokio::test]
|
||||||
async fn test_create_adresar_database_error() {
|
async fn test_create_adresar_database_error() {
|
||||||
let (pool, _) = setup().await;
|
// Create a valid pool but close it immediately to simulate a broken connection
|
||||||
// Simulate closed pool
|
let pool = setup_test_db().await;
|
||||||
let broken_pool = pool.clone();
|
pool.close().await;
|
||||||
broken_pool.close().await;
|
|
||||||
|
|
||||||
let request = PostAdresarRequest {
|
let request = PostAdresarRequest {
|
||||||
firma: "Test".into(),
|
firma: "Test".into(),
|
||||||
..Default::default()
|
..Default::default()
|
||||||
};
|
};
|
||||||
|
|
||||||
let result = post_adresar(&broken_pool, request).await;
|
let result = post_adresar(&pool, request).await;
|
||||||
|
|
||||||
assert!(result.is_err());
|
assert!(result.is_err());
|
||||||
assert_eq!(result.unwrap_err().code(), tonic::Code::Internal);
|
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
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user