From 18eb7787203b85bd89541c01569a645a38c68859 Mon Sep 17 00:00:00 2001 From: Rushmore Mushambi Date: Fri, 20 Sep 2024 22:51:42 +0100 Subject: [PATCH] Add a test for including an ID in the content body (#4849) --- core/src/syn/parser/test/mod.rs | 3 +- sdk/tests/api.rs | 2 +- sdk/tests/api/mod.rs | 68 +++++++++++++++++++++++++++++++++ 3 files changed, 70 insertions(+), 3 deletions(-) diff --git a/core/src/syn/parser/test/mod.rs b/core/src/syn/parser/test/mod.rs index 73f73735..2d14a033 100644 --- a/core/src/syn/parser/test/mod.rs +++ b/core/src/syn/parser/test/mod.rs @@ -114,6 +114,5 @@ fn escaped_params_backtick() { #[test] fn parse_immediate_insert_subquery() { - let res = - test_parse!(parse_query, r#"LET $insert = INSERT INTO t (SELECT true FROM 1);"#).unwrap(); + test_parse!(parse_query, r#"LET $insert = INSERT INTO t (SELECT true FROM 1);"#).unwrap(); } diff --git a/sdk/tests/api.rs b/sdk/tests/api.rs index b3c1f8a8..441fb069 100644 --- a/sdk/tests/api.rs +++ b/sdk/tests/api.rs @@ -54,7 +54,7 @@ mod api_integration { name: String, } - #[derive(Debug, Clone, Deserialize, PartialEq, PartialOrd)] + #[derive(Debug, Clone, Serialize, Deserialize, PartialEq, PartialOrd)] struct RecordBuf { id: RecordId, name: String, diff --git a/sdk/tests/api/mod.rs b/sdk/tests/api/mod.rs index d6351d1f..c1515b32 100644 --- a/sdk/tests/api/mod.rs +++ b/sdk/tests/api/mod.rs @@ -502,6 +502,74 @@ async fn create_record_with_id_with_content() { ); } +#[test_log::test(tokio::test)] +async fn create_record_with_id_in_content() { + #[derive(Debug, Serialize, Deserialize)] + pub struct Person { + pub id: u32, + pub name: &'static str, + pub job: &'static str, + } + + #[derive(Debug, Serialize, Deserialize)] + pub struct Record { + #[allow(dead_code)] + pub id: RecordId, + } + + let (permit, db) = new_db().await; + db.use_ns(NS).use_db(Ulid::new().to_string()).await.unwrap(); + drop(permit); + + let record: Option = db + .create(("user", "john")) + .content(RecordBuf { + id: RecordId::from_table_key("user", "john"), + name: "John Doe".to_owned(), + }) + .await + .unwrap(); + assert_eq!(record.unwrap().id, "user:john".parse::().unwrap()); + + let error = db + .create::>(("user", "john")) + .content(RecordBuf { + id: RecordId::from_table_key("user", "jane"), + name: "John Doe".to_owned(), + }) + .await + .unwrap_err(); + match error { + surrealdb::Error::Db(DbError::IdMismatch { + .. + }) => {} + surrealdb::Error::Api(ApiError::Query { + .. + }) => {} + error => panic!("unexpected error; {error:?}"), + } + + let _: Option = db + .create("person") + .content(Person { + id: 1010, + name: "Max Mustermann", + job: "chef", + }) + .await + .unwrap(); + + let _: Option = db + .update(("person", 1010)) + .content(Person { + id: 1010, + name: "Max Mustermann", + job: "IT Tech", + }) + .await + .unwrap(); +} + #[test_log::test(tokio::test)] async fn insert_table() { let (permit, db) = new_db().await;