get working
This commit is contained in:
239
server/tests/adresar/get_adresar_test.rs
Normal file
239
server/tests/adresar/get_adresar_test.rs
Normal file
@@ -0,0 +1,239 @@
|
|||||||
|
// tests/adresar/get_adresar_test.rs
|
||||||
|
use rstest::{fixture, rstest};
|
||||||
|
use server::adresar::handlers::get_adresar;
|
||||||
|
use common::proto::multieko2::adresar::{GetAdresarRequest, AdresarResponse};
|
||||||
|
use crate::common::setup_test_db;
|
||||||
|
use sqlx::PgPool;
|
||||||
|
use tonic;
|
||||||
|
|
||||||
|
#[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]
|
||||||
|
async fn existing_record(#[future] pool: PgPool) -> (PgPool, i64) {
|
||||||
|
let pool = pool.await;
|
||||||
|
let record = sqlx::query!(
|
||||||
|
r#"
|
||||||
|
INSERT INTO adresar (
|
||||||
|
firma, kz, drc, ulica, psc, mesto, stat, banka, ucet,
|
||||||
|
skladm, ico, kontakt, telefon, skladu, fax, deleted
|
||||||
|
)
|
||||||
|
VALUES (
|
||||||
|
'Test Company', 'KZ', 'DRC', 'Street', '12345', 'City',
|
||||||
|
'Country', 'Bank', 'Account', 'SkladM', 'ICO', 'Contact',
|
||||||
|
'+421123456789', 'SkladU', 'Fax', false
|
||||||
|
)
|
||||||
|
RETURNING id
|
||||||
|
"#
|
||||||
|
)
|
||||||
|
.fetch_one(&pool)
|
||||||
|
.await
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
(pool, record.id)
|
||||||
|
}
|
||||||
|
|
||||||
|
#[fixture]
|
||||||
|
async fn existing_deleted_record(#[future] pool: PgPool) -> (PgPool, i64) {
|
||||||
|
let pool = pool.await;
|
||||||
|
let record = sqlx::query!(
|
||||||
|
r#"
|
||||||
|
INSERT INTO adresar (firma, deleted)
|
||||||
|
VALUES ('Deleted Company', true)
|
||||||
|
RETURNING id
|
||||||
|
"#
|
||||||
|
)
|
||||||
|
.fetch_one(&pool)
|
||||||
|
.await
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
(pool, record.id)
|
||||||
|
}
|
||||||
|
|
||||||
|
#[fixture]
|
||||||
|
async fn existing_record_with_nulls(#[future] pool: PgPool) -> (PgPool, i64) {
|
||||||
|
let pool = pool.await;
|
||||||
|
let record = sqlx::query!(
|
||||||
|
r#"
|
||||||
|
INSERT INTO adresar (firma)
|
||||||
|
VALUES ('Null Fields Company')
|
||||||
|
RETURNING id
|
||||||
|
"#
|
||||||
|
)
|
||||||
|
.fetch_one(&pool)
|
||||||
|
.await
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
(pool, record.id)
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn assert_response_matches(pool: &PgPool, id: i64, response: &AdresarResponse) {
|
||||||
|
let db_record = sqlx::query!("SELECT * FROM adresar WHERE id = $1", id)
|
||||||
|
.fetch_one(pool)
|
||||||
|
.await
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
assert_eq!(db_record.firma, response.firma);
|
||||||
|
assert_eq!(db_record.kz.unwrap_or_default(), response.kz);
|
||||||
|
assert_eq!(db_record.drc.unwrap_or_default(), response.drc);
|
||||||
|
assert_eq!(db_record.ulica.unwrap_or_default(), response.ulica);
|
||||||
|
assert_eq!(db_record.psc.unwrap_or_default(), response.psc);
|
||||||
|
assert_eq!(db_record.mesto.unwrap_or_default(), response.mesto);
|
||||||
|
assert_eq!(db_record.stat.unwrap_or_default(), response.stat);
|
||||||
|
assert_eq!(db_record.banka.unwrap_or_default(), response.banka);
|
||||||
|
assert_eq!(db_record.ucet.unwrap_or_default(), response.ucet);
|
||||||
|
assert_eq!(db_record.skladm.unwrap_or_default(), response.skladm);
|
||||||
|
assert_eq!(db_record.ico.unwrap_or_default(), response.ico);
|
||||||
|
assert_eq!(db_record.kontakt.unwrap_or_default(), response.kontakt);
|
||||||
|
assert_eq!(db_record.telefon.unwrap_or_default(), response.telefon);
|
||||||
|
assert_eq!(db_record.skladu.unwrap_or_default(), response.skladu);
|
||||||
|
assert_eq!(db_record.fax.unwrap_or_default(), response.fax);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[rstest]
|
||||||
|
#[tokio::test]
|
||||||
|
async fn test_get_adresar_success(
|
||||||
|
#[future] existing_record: (PgPool, i64),
|
||||||
|
) {
|
||||||
|
let (pool, id) = existing_record.await;
|
||||||
|
let request = GetAdresarRequest { id };
|
||||||
|
let response = get_adresar(&pool, request).await.unwrap();
|
||||||
|
|
||||||
|
assert_eq!(response.id, id);
|
||||||
|
assert_response_matches(&pool, id, &response).await;
|
||||||
|
}
|
||||||
|
|
||||||
|
#[rstest]
|
||||||
|
#[tokio::test]
|
||||||
|
async fn test_get_optional_fields_null(
|
||||||
|
#[future] existing_record_with_nulls: (PgPool, i64),
|
||||||
|
) {
|
||||||
|
let (pool, id) = existing_record_with_nulls.await;
|
||||||
|
let request = GetAdresarRequest { id };
|
||||||
|
let response = get_adresar(&pool, request).await.unwrap();
|
||||||
|
|
||||||
|
assert_eq!(response.kz, "");
|
||||||
|
assert_eq!(response.drc, "");
|
||||||
|
assert_eq!(response.ulica, "");
|
||||||
|
assert_eq!(response.psc, "");
|
||||||
|
assert_eq!(response.mesto, "");
|
||||||
|
assert_eq!(response.stat, "");
|
||||||
|
assert_eq!(response.banka, "");
|
||||||
|
assert_eq!(response.ucet, "");
|
||||||
|
assert_eq!(response.skladm, "");
|
||||||
|
assert_eq!(response.ico, "");
|
||||||
|
assert_eq!(response.kontakt, "");
|
||||||
|
assert_eq!(response.telefon, "");
|
||||||
|
assert_eq!(response.skladu, "");
|
||||||
|
assert_eq!(response.fax, "");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[rstest]
|
||||||
|
#[tokio::test]
|
||||||
|
async fn test_get_nonexistent_id(
|
||||||
|
#[future] pool: PgPool,
|
||||||
|
) {
|
||||||
|
let pool = pool.await;
|
||||||
|
let request = GetAdresarRequest { id: 9999 };
|
||||||
|
let result = get_adresar(&pool, request).await;
|
||||||
|
|
||||||
|
assert!(result.is_err());
|
||||||
|
assert_eq!(result.unwrap_err().code(), tonic::Code::NotFound);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[rstest]
|
||||||
|
#[tokio::test]
|
||||||
|
async fn test_get_deleted_record(
|
||||||
|
#[future] existing_deleted_record: (PgPool, i64),
|
||||||
|
) {
|
||||||
|
let (pool, id) = existing_deleted_record.await;
|
||||||
|
let request = GetAdresarRequest { id };
|
||||||
|
let response = get_adresar(&pool, request).await.unwrap();
|
||||||
|
|
||||||
|
assert_eq!(response.firma, "Deleted Company");
|
||||||
|
assert_eq!(response.kz, "");
|
||||||
|
assert_eq!(response.drc, "");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[rstest]
|
||||||
|
#[tokio::test]
|
||||||
|
async fn test_database_error(
|
||||||
|
#[future] closed_pool: PgPool,
|
||||||
|
) {
|
||||||
|
let closed_pool = closed_pool.await;
|
||||||
|
let request = GetAdresarRequest { id: 1 };
|
||||||
|
let result = get_adresar(&closed_pool, request).await;
|
||||||
|
|
||||||
|
assert!(result.is_err());
|
||||||
|
assert_eq!(result.unwrap_err().code(), tonic::Code::Internal);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[rstest]
|
||||||
|
#[tokio::test]
|
||||||
|
async fn test_get_special_characters(
|
||||||
|
#[future] pool: PgPool,
|
||||||
|
) {
|
||||||
|
let pool = pool.await;
|
||||||
|
let firma = "Náměstí ČR";
|
||||||
|
let telefon = "+420 123-456.789";
|
||||||
|
let ulica = "Křižíkova 123";
|
||||||
|
|
||||||
|
let record = sqlx::query!(
|
||||||
|
r#"
|
||||||
|
INSERT INTO adresar (firma, telefon, ulica)
|
||||||
|
VALUES ($1, $2, $3)
|
||||||
|
RETURNING id
|
||||||
|
"#,
|
||||||
|
firma,
|
||||||
|
telefon,
|
||||||
|
ulica
|
||||||
|
)
|
||||||
|
.fetch_one(&pool)
|
||||||
|
.await
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
let request = GetAdresarRequest { id: record.id };
|
||||||
|
let response = get_adresar(&pool, request).await.unwrap();
|
||||||
|
|
||||||
|
assert_eq!(response.firma, firma);
|
||||||
|
assert_eq!(response.telefon, telefon);
|
||||||
|
assert_eq!(response.ulica, ulica);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[rstest]
|
||||||
|
#[tokio::test]
|
||||||
|
async fn test_get_max_length_fields(
|
||||||
|
#[future] pool: PgPool,
|
||||||
|
) {
|
||||||
|
let pool = pool.await;
|
||||||
|
let firma = "a".repeat(255);
|
||||||
|
let telefon = "1".repeat(20);
|
||||||
|
|
||||||
|
let record = sqlx::query!(
|
||||||
|
r#"
|
||||||
|
INSERT INTO adresar (firma, telefon)
|
||||||
|
VALUES ($1, $2)
|
||||||
|
RETURNING id
|
||||||
|
"#,
|
||||||
|
firma,
|
||||||
|
telefon
|
||||||
|
)
|
||||||
|
.fetch_one(&pool)
|
||||||
|
.await
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
let request = GetAdresarRequest { id: record.id };
|
||||||
|
let response = get_adresar(&pool, request).await.unwrap();
|
||||||
|
|
||||||
|
assert_eq!(response.firma.len(), 255);
|
||||||
|
assert_eq!(response.telefon.len(), 20);
|
||||||
|
}
|
||||||
@@ -2,3 +2,4 @@
|
|||||||
|
|
||||||
pub mod post_adresar_test;
|
pub mod post_adresar_test;
|
||||||
pub mod put_adresar_test;
|
pub mod put_adresar_test;
|
||||||
|
pub mod get_adresar_test;
|
||||||
|
|||||||
Reference in New Issue
Block a user