put working now doing post general test
This commit is contained in:
3
server/tests/tables_data/handlers/mod.rs
Normal file
3
server/tests/tables_data/handlers/mod.rs
Normal file
@@ -0,0 +1,3 @@
|
||||
// tests/tables_data/mod.rs
|
||||
pub mod post_table_data_test;
|
||||
|
||||
241
server/tests/tables_data/handlers/post_table_data_test.rs
Normal file
241
server/tests/tables_data/handlers/post_table_data_test.rs
Normal file
@@ -0,0 +1,241 @@
|
||||
// tests/tables_data/handlers/post_table_data_test.rs
|
||||
use rstest::{fixture, rstest};
|
||||
use sqlx::PgPool;
|
||||
use std::collections::HashMap;
|
||||
use common::proto::multieko2::tables_data::{PostTableDataRequest, PostTableDataResponse};
|
||||
use server::tables_data::handlers::post_table_data;
|
||||
use crate::common::setup_test_db;
|
||||
use tonic;
|
||||
use chrono::Utc;
|
||||
|
||||
// Fixtures
|
||||
#[fixture]
|
||||
async fn pool() -> PgPool {
|
||||
setup_test_db().await
|
||||
}
|
||||
|
||||
#[fixture]
|
||||
async fn closed_pool(#[future] pool: PgPool) -> PgPool {
|
||||
let pool = pool.await;
|
||||
pool.close().await;
|
||||
pool
|
||||
}
|
||||
|
||||
#[fixture]
|
||||
fn valid_request() -> HashMap<String, String> {
|
||||
let mut map = HashMap::new();
|
||||
map.insert("firma".into(), "Test Company".into());
|
||||
map.insert("kz".into(), "KZ123".into());
|
||||
map.insert("drc".into(), "DRC456".into());
|
||||
map.insert("ulica".into(), "Test Street".into());
|
||||
map.insert("psc".into(), "12345".into());
|
||||
map.insert("mesto".into(), "Test City".into());
|
||||
map.insert("stat".into(), "Test Country".into());
|
||||
map.insert("banka".into(), "Test Bank".into());
|
||||
map.insert("ucet".into(), "123456789".into());
|
||||
map.insert("skladm".into(), "Warehouse M".into());
|
||||
map.insert("ico".into(), "12345678".into());
|
||||
map.insert("kontakt".into(), "John Doe".into());
|
||||
map.insert("telefon".into(), "+421123456789".into());
|
||||
map.insert("skladu".into(), "Warehouse U".into());
|
||||
map.insert("fax".into(), "+421123456700".into());
|
||||
map
|
||||
}
|
||||
|
||||
#[fixture]
|
||||
fn minimal_request() -> HashMap<String, String> {
|
||||
let mut map = HashMap::new();
|
||||
map.insert("firma".into(), "Required Only".into());
|
||||
map
|
||||
}
|
||||
|
||||
fn create_table_request(data: HashMap<String, String>) -> PostTableDataRequest {
|
||||
PostTableDataRequest {
|
||||
profile_name: "default".into(),
|
||||
table_name: "2025_adresar".into(),
|
||||
data,
|
||||
}
|
||||
}
|
||||
|
||||
async fn assert_table_response(pool: &PgPool, response: &PostTableDataResponse, expected: &HashMap<String, String>) {
|
||||
let row = sqlx::query!(r#"SELECT * FROM "2025_adresar" WHERE id = $1"#, response.inserted_id)
|
||||
.fetch_one(pool)
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
assert_eq!(row.firma, expected["firma"]);
|
||||
assert!(!row.deleted);
|
||||
|
||||
// Check optional fields using direct struct access
|
||||
let check_field = |field: &str, value: &str| {
|
||||
let db_value = match field {
|
||||
"kz" => row.kz.as_deref().unwrap_or_default(),
|
||||
"drc" => row.drc.as_deref().unwrap_or_default(),
|
||||
"ulica" => row.ulica.as_deref().unwrap_or_default(),
|
||||
"psc" => row.psc.as_deref().unwrap_or_default(),
|
||||
"mesto" => row.mesto.as_deref().unwrap_or_default(),
|
||||
"stat" => row.stat.as_deref().unwrap_or_default(),
|
||||
"banka" => row.banka.as_deref().unwrap_or_default(),
|
||||
"ucet" => row.ucet.as_deref().unwrap_or_default(),
|
||||
"skladm" => row.skladm.as_deref().unwrap_or_default(),
|
||||
"ico" => row.ico.as_deref().unwrap_or_default(),
|
||||
"kontakt" => row.kontakt.as_deref().unwrap_or_default(),
|
||||
"telefon" => row.telefon.as_deref().unwrap_or_default(),
|
||||
"skladu" => row.skladu.as_deref().unwrap_or_default(),
|
||||
"fax" => row.fax.as_deref().unwrap_or_default(),
|
||||
_ => panic!("Unexpected field: {}", field),
|
||||
};
|
||||
assert_eq!(db_value, value);
|
||||
};
|
||||
|
||||
check_field("kz", expected.get("kz").unwrap_or(&String::new()));
|
||||
check_field("drc", expected.get("drc").unwrap_or(&String::new()));
|
||||
check_field("ulica", expected.get("ulica").unwrap_or(&String::new()));
|
||||
check_field("psc", expected.get("psc").unwrap_or(&String::new()));
|
||||
check_field("mesto", expected.get("mesto").unwrap_or(&String::new()));
|
||||
check_field("stat", expected.get("stat").unwrap_or(&String::new()));
|
||||
check_field("banka", expected.get("banka").unwrap_or(&String::new()));
|
||||
check_field("ucet", expected.get("ucet").unwrap_or(&String::new()));
|
||||
check_field("skladm", expected.get("skladm").unwrap_or(&String::new()));
|
||||
check_field("ico", expected.get("ico").unwrap_or(&String::new()));
|
||||
check_field("kontakt", expected.get("kontakt").unwrap_or(&String::new()));
|
||||
check_field("telefon", expected.get("telefon").unwrap_or(&String::new()));
|
||||
check_field("skladu", expected.get("skladu").unwrap_or(&String::new()));
|
||||
check_field("fax", expected.get("fax").unwrap_or(&String::new()));
|
||||
|
||||
// Handle timestamp conversion
|
||||
let created_at: chrono::DateTime<Utc> = row.created_at.unwrap().into();
|
||||
assert!(created_at <= Utc::now());
|
||||
}
|
||||
|
||||
#[rstest]
|
||||
#[tokio::test]
|
||||
async fn test_create_table_data_success(
|
||||
#[future] pool: PgPool,
|
||||
valid_request: HashMap<String, String>,
|
||||
) {
|
||||
let pool = pool.await;
|
||||
let request = create_table_request(valid_request.clone());
|
||||
let response = post_table_data(&pool, request).await.unwrap();
|
||||
|
||||
assert!(response.inserted_id > 0);
|
||||
assert!(response.success);
|
||||
assert_eq!(response.message, "Data inserted successfully");
|
||||
assert_table_response(&pool, &response, &valid_request).await;
|
||||
}
|
||||
|
||||
// Remaining tests follow the same pattern with fixed parameter declarations
|
||||
#[rstest]
|
||||
#[tokio::test]
|
||||
async fn test_create_table_data_whitespace_trimming(
|
||||
#[future] pool: PgPool,
|
||||
valid_request: HashMap<String, String>,
|
||||
) {
|
||||
let pool = pool.await;
|
||||
let mut request = valid_request;
|
||||
request.insert("firma".into(), " Test Company ".into());
|
||||
request.insert("telefon".into(), " +421123456789 ".into());
|
||||
|
||||
let response = post_table_data(&pool, create_table_request(request)).await.unwrap();
|
||||
|
||||
let row = sqlx::query!(r#"SELECT firma, telefon FROM "2025_adresar" WHERE id = $1"#, response.inserted_id)
|
||||
.fetch_one(&pool)
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
assert_eq!(row.firma, "Test Company");
|
||||
assert_eq!(row.telefon.unwrap(), "+421123456789");
|
||||
}
|
||||
|
||||
#[rstest]
|
||||
#[tokio::test]
|
||||
async fn test_create_table_data_empty_optional_fields(
|
||||
#[future] pool: PgPool,
|
||||
valid_request: HashMap<String, String>,
|
||||
) {
|
||||
let pool = pool.await;
|
||||
let mut request = valid_request;
|
||||
request.insert("telefon".into(), " ".into());
|
||||
|
||||
let response = post_table_data(&pool, create_table_request(request)).await.unwrap();
|
||||
let telefon: Option<String> = sqlx::query_scalar!(r#"SELECT telefon FROM "2025_adresar" WHERE id = $1"#, response.inserted_id)
|
||||
.fetch_one(&pool)
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
assert!(telefon.is_none());
|
||||
}
|
||||
|
||||
// Fixed parameter declarations for remaining tests
|
||||
#[rstest]
|
||||
#[tokio::test]
|
||||
async fn test_create_table_data_invalid_firma(
|
||||
#[future] pool: PgPool,
|
||||
valid_request: HashMap<String, String>,
|
||||
) {
|
||||
let pool = pool.await;
|
||||
let mut request = valid_request;
|
||||
request.insert("firma".into(), " ".into());
|
||||
|
||||
let result = post_table_data(&pool, create_table_request(request)).await;
|
||||
assert!(result.is_err());
|
||||
assert_eq!(result.unwrap_err().code(), tonic::Code::InvalidArgument);
|
||||
}
|
||||
|
||||
#[rstest]
|
||||
#[tokio::test]
|
||||
async fn test_create_table_data_minimal_request(
|
||||
#[future] pool: PgPool,
|
||||
minimal_request: HashMap<String, String>,
|
||||
) {
|
||||
let pool = pool.await;
|
||||
let response = post_table_data(&pool, create_table_request(minimal_request.clone())).await.unwrap();
|
||||
assert!(response.inserted_id > 0);
|
||||
assert_table_response(&pool, &response, &minimal_request).await;
|
||||
}
|
||||
|
||||
#[rstest]
|
||||
#[tokio::test]
|
||||
async fn test_create_table_data_telefon_length_limit(
|
||||
#[future] pool: PgPool,
|
||||
valid_request: HashMap<String, String>,
|
||||
) {
|
||||
let pool = pool.await;
|
||||
let mut request = valid_request;
|
||||
request.insert("telefon".into(), "1".repeat(16));
|
||||
|
||||
let result = post_table_data(&pool, create_table_request(request)).await;
|
||||
assert!(result.is_err());
|
||||
assert_eq!(result.unwrap_err().code(), tonic::Code::Internal);
|
||||
}
|
||||
|
||||
#[rstest]
|
||||
#[tokio::test]
|
||||
async fn test_create_table_data_special_characters(
|
||||
#[future] pool: PgPool,
|
||||
valid_request: HashMap<String, String>,
|
||||
) {
|
||||
let pool = pool.await;
|
||||
let mut request = valid_request;
|
||||
request.insert("ulica".into(), "Náměstí 28. října 123/456".into());
|
||||
|
||||
let response = post_table_data(&pool, create_table_request(request)).await.unwrap();
|
||||
let row = sqlx::query!(r#"SELECT ulica FROM "2025_adresar" WHERE id = $1"#, response.inserted_id)
|
||||
.fetch_one(&pool)
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
assert_eq!(row.ulica.unwrap(), "Náměstí 28. října 123/456");
|
||||
}
|
||||
|
||||
#[rstest]
|
||||
#[tokio::test]
|
||||
async fn test_create_table_data_database_error(
|
||||
#[future] closed_pool: PgPool,
|
||||
minimal_request: HashMap<String, String>,
|
||||
) {
|
||||
let closed_pool = closed_pool.await;
|
||||
let result = post_table_data(&closed_pool, create_table_request(minimal_request)).await;
|
||||
assert!(result.is_err());
|
||||
assert_eq!(result.unwrap_err().code(), tonic::Code::Internal);
|
||||
}
|
||||
2
server/tests/tables_data/mod.rs
Normal file
2
server/tests/tables_data/mod.rs
Normal file
@@ -0,0 +1,2 @@
|
||||
// tests/tables_data/mod.rs
|
||||
pub mod handlers;
|
||||
Reference in New Issue
Block a user