From e25ddfc4082dd300ea45548fae4f84a8cf4c39d0 Mon Sep 17 00:00:00 2001 From: Dmitrii Blaginin Date: Thu, 1 Aug 2024 12:23:11 +0100 Subject: [PATCH] Fix validation on schemafull `RELATE` (#4226) --- core/src/doc/relate.rs | 4 ++-- lib/tests/relate.rs | 50 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+), 2 deletions(-) diff --git a/core/src/doc/relate.rs b/core/src/doc/relate.rs index cefad442..ec66e24c 100644 --- a/core/src/doc/relate.rs +++ b/core/src/doc/relate.rs @@ -37,10 +37,10 @@ impl<'a> Document<'a> { opt: &Options, stm: &Statement<'_>, ) -> Result { - // Store record edges - self.edges(ctx, opt, stm).await?; // Alter record data self.alter(stk, ctx, opt, stm).await?; + // Store record edges + self.edges(ctx, opt, stm).await?; // Merge fields data self.field(stk, ctx, opt, stm).await?; // Reset fields data diff --git a/lib/tests/relate.rs b/lib/tests/relate.rs index 3e4fa1d9..f5adeb6d 100644 --- a/lib/tests/relate.rs +++ b/lib/tests/relate.rs @@ -1,6 +1,9 @@ mod parse; + use parse::Parse; + mod helpers; +use crate::helpers::Test; use helpers::new_ds; use surrealdb::dbs::Session; @@ -207,3 +210,50 @@ async fn relate_with_complex_table() -> Result<(), Error> { // Ok(()) } + +#[tokio::test] +async fn schemafull_relate() -> Result<(), Error> { + let sql = r#" + INSERT INTO person [ + { id: 1 }, + { id: 2 } + ]; + + DEFINE TABLE likes TYPE RELATION FROM person TO person; + DEFINE FIELD reason ON likes TYPE string; + + RELATE person:1 -> likes -> person:2 CONTENT {id: 1, reason: "nice smile"}; + RELATE person:2 -> likes -> person:1 CONTENT {id: 2, reason: true}; + RELATE dog:1 -> likes -> person:2 CONTENT {id: 3, reason: "nice smell"}; + "#; + + let mut t = Test::new(sql).await?; + + t.expect_val( + "[ + {id: person:1}, + {id: person:2} + ]", + )?; + + t.skip_ok(2)?; + + t.expect_val( + "[ + { + id: likes:1, + in: person:1, + out: person:2, + reason: 'nice smile' + } + ]", + )?; + + // reason is bool not string + t.expect_error_func(|e| matches!(e, Error::FieldCheck { .. }))?; + + // dog:1 is not a person + t.expect_error_func(|e| matches!(e, Error::FieldCheck { .. }))?; + + Ok(()) +}