fix: remove single check for rpc insert (#4812)

This commit is contained in:
Micha de Vries 2024-09-19 10:56:50 +01:00 committed by GitHub
parent 127b443f30
commit 5e990d569b
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 34 additions and 14 deletions

View file

@ -299,8 +299,6 @@ pub trait RpcContext {
let Ok((what, data)) = params.needs_two() else {
return Err(RpcError::InvalidParams);
};
// Return a single result?
let one = what.is_thing_single();
// Specify the SQL query string
let mut res = match what {
@ -323,11 +321,7 @@ pub trait RpcContext {
}
};
// Extract the first query result
let res = match one {
true => res.remove(0).result?.first(),
false => res.remove(0).result?,
};
let res = res.remove(0).result?;
// Return the result to the client
Ok(res.into())
}
@ -337,8 +331,6 @@ pub trait RpcContext {
return Err(RpcError::InvalidParams);
};
let one = what.is_thing_single();
let mut res = match what {
Value::None | Value::Null => {
let sql = "INSERT RELATION $data RETURN AFTER";
@ -360,10 +352,7 @@ pub trait RpcContext {
_ => return Err(RpcError::InvalidParams),
};
let res = match one {
true => res.remove(0).result?.first(),
false => res.remove(0).result?,
};
let res = res.remove(0).result?;
Ok(res)
}

View file

@ -348,13 +348,44 @@ async fn insert() -> Result<(), Box<dyn std::error::Error>> {
assert_eq!(res.len(), 1, "result: {res:?}");
assert_eq!(res[0]["name"], "foo", "result: {res:?}");
assert_eq!(res[0]["value"], "bar", "result: {res:?}");
// Send INSERT command trying to create multiple records
let res = socket
.send_request(
"insert",
json!([
"tester",
[
{
"name": "foo",
"value": "bar",
},
{
"name": "foo",
"value": "bar",
}
]
]),
)
.await?;
assert!(res.is_object(), "result: {res:?}");
assert!(res["result"].is_array(), "result: {res:?}");
let res = res["result"].as_array().unwrap();
assert_eq!(res.len(), 2, "result: {res:?}");
assert_eq!(res[0]["name"], "foo", "result: {res:?}");
assert_eq!(res[0]["value"], "bar", "result: {res:?}");
assert_eq!(res[1]["name"], "foo", "result: {res:?}");
assert_eq!(res[1]["value"], "bar", "result: {res:?}");
// Verify the data was inserted and can be queried
let res = socket.send_message_query("SELECT * FROM tester").await?;
assert!(res[0]["result"].is_array(), "result: {res:?}");
let res = res[0]["result"].as_array().unwrap();
assert_eq!(res.len(), 1, "result: {res:?}");
assert_eq!(res.len(), 3, "result: {res:?}");
assert_eq!(res[0]["name"], "foo", "result: {res:?}");
assert_eq!(res[0]["value"], "bar", "result: {res:?}");
assert_eq!(res[1]["name"], "foo", "result: {res:?}");
assert_eq!(res[1]["value"], "bar", "result: {res:?}");
assert_eq!(res[2]["name"], "foo", "result: {res:?}");
assert_eq!(res[2]["value"], "bar", "result: {res:?}");
// Test passed
server.finish().unwrap();
Ok(())