better test

This commit is contained in:
filipriec
2025-03-04 14:53:39 +01:00
parent 72c6076b1e
commit b2e29eb8c8

View File

@@ -241,3 +241,59 @@ async fn test_create_table_data_database_error(
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);
} }
#[rstest]
#[tokio::test]
async fn test_create_table_data_empty_firma(
#[future] pool: PgPool,
minimal_request: HashMap<String, String>,
) {
let pool = pool.await;
let mut request = minimal_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_optional_fields_null_vs_empty(
#[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());
}
#[rstest]
#[tokio::test]
async fn test_create_table_data_field_length_limits(
#[future] pool: PgPool,
valid_request: HashMap<String, String>,
) {
let pool = pool.await;
let mut request = valid_request;
request.insert("firma".into(), "a".repeat(255));
request.insert("telefon".into(), "1".repeat(15)); // Within limits
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.len(), 255);
assert_eq!(row.telefon.unwrap().len(), 15);
}