From 5e990d569b04e679537f14dc9f59aa17716242e8 Mon Sep 17 00:00:00 2001 From: Micha de Vries Date: Thu, 19 Sep 2024 10:56:50 +0100 Subject: [PATCH] fix: remove `single` check for rpc insert (#4812) --- core/src/rpc/rpc_context.rs | 15 ++------------- tests/common/tests.rs | 33 ++++++++++++++++++++++++++++++++- 2 files changed, 34 insertions(+), 14 deletions(-) diff --git a/core/src/rpc/rpc_context.rs b/core/src/rpc/rpc_context.rs index 13a3f44a..ce962224 100644 --- a/core/src/rpc/rpc_context.rs +++ b/core/src/rpc/rpc_context.rs @@ -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) } diff --git a/tests/common/tests.rs b/tests/common/tests.rs index e96196cb..7d602557 100644 --- a/tests/common/tests.rs +++ b/tests/common/tests.rs @@ -348,13 +348,44 @@ async fn insert() -> Result<(), Box> { 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(())