use serde_json::{json, Value};
mod query_test_utils;
use query_test_utils::shared::setup_client_with_master_key;
#[tokio::test]
async fn test_call_simple_cloud_function() {
let client = setup_client_with_master_key();
let result: Result<String, parse_rs::ParseError> = client
.cloud()
.run("hello", &json!({})) .await;
match result {
Ok(message) => {
assert_eq!(message, "Hello from Cloud Code!"); }
Err(e) => {
panic!("Cloud function call failed: {:?}", e);
}
}
}
#[tokio::test]
async fn test_call_cloud_function_with_params() {
let client = setup_client_with_master_key();
let params = json!({ "message": "Test message" });
#[derive(serde::Deserialize, Debug, PartialEq)]
struct EchoResponse {
#[serde(rename = "echoedMessage")] echoed_message: String,
}
let result: Result<EchoResponse, parse_rs::ParseError> =
client.cloud().run("echo", ¶ms).await;
match result {
Ok(response) => {
assert_eq!(response.echoed_message, "Test message");
}
Err(e) => {
panic!("Cloud function call with params failed: {:?}", e);
}
}
}
#[tokio::test]
async fn test_call_non_existent_cloud_function() {
let client = setup_client_with_master_key();
let params = json!({});
let result: Result<Value, parse_rs::ParseError> =
client.cloud().run("nonExistentFunction", ¶ms).await;
match result {
Ok(_) => panic!("Calling a non-existent function should fail"),
Err(parse_rs::ParseError::OtherParseError { code, message }) => {
assert_eq!(
code, 141,
"Expected error code 141 for non-existent function, got {}",
code
);
assert!(
message.contains("Invalid function: \"nonExistentFunction\""),
"Error message should indicate invalid function. Got: {}",
message
);
}
Err(e) => {
panic!(
"Unexpected error type when calling non-existent function: {:?}",
e
);
}
}
}