tests for general data pushing

This commit is contained in:
filipriec
2025-03-04 14:22:55 +01:00
parent c8bd4ab6ad
commit 83c62148e9
2 changed files with 29 additions and 3 deletions

View File

@@ -8,7 +8,7 @@ use common::proto::multieko2::tables_data::{PostTableDataRequest, PostTableDataR
pub async fn post_table_data(
db_pool: &PgPool,
request: PostTableDataRequest,
) -> Result<PostTableDataResponse, Status> { // Return PostTableDataResponse directly
) -> Result<PostTableDataResponse, Status> {
let profile_name = request.profile_name;
let table_name = request.table_name;
let data = request.data;
@@ -107,7 +107,7 @@ pub async fn post_table_data(
"TEXT" | "VARCHAR(15)" | "VARCHAR(255)" => {
if let Some(max_len) = sql_type.strip_prefix("VARCHAR(")
.and_then(|s| s.strip_suffix(')'))
.and_then(|s| s.parse::<usize>().ok())
.and_then(|s| s.parse::<usize>().ok())
{
if value.len() > max_len {
return Err(Status::invalid_argument(format!("Value too long for {}", col)));
@@ -148,7 +148,7 @@ pub async fn post_table_data(
.await
.map_err(|e| Status::internal(format!("Insert failed: {}", e)))?;
Ok(PostTableDataResponse { // Return PostTableDataResponse directly
Ok(PostTableDataResponse {
success: true,
message: "Data inserted successfully".into(),
inserted_id,

View File

@@ -26,5 +26,31 @@ pub async fn setup_test_db() -> PgPool {
.await
.expect("Migrations failed");
// Insert default profile if it doesn't exist
let profile = sqlx::query!(
r#"
INSERT INTO profiles (name)
VALUES ('default')
ON CONFLICT (name) DO NOTHING
RETURNING id
"#
)
.fetch_optional(&pool)
.await
.expect("Failed to insert test profile");
let profile_id = if let Some(profile) = profile {
profile.id
} else {
// If the profile already exists, fetch its ID
sqlx::query!(
"SELECT id FROM profiles WHERE name = 'default'"
)
.fetch_one(&pool)
.await
.expect("Failed to fetch default profile ID")
.id
};
pool
}