2022-07-28 09:00:06 +00:00
|
|
|
mod parse;
|
|
|
|
use parse::Parse;
|
2023-08-30 18:01:30 +00:00
|
|
|
mod helpers;
|
|
|
|
use helpers::new_ds;
|
2024-01-09 16:57:52 +00:00
|
|
|
use helpers::with_enough_stack;
|
2022-12-30 08:23:19 +00:00
|
|
|
use surrealdb::dbs::Session;
|
|
|
|
use surrealdb::err::Error;
|
2023-08-19 22:19:16 +00:00
|
|
|
use surrealdb::sql::Thing;
|
2022-07-28 09:00:06 +00:00
|
|
|
use surrealdb::sql::Value;
|
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
async fn field_definition_value_assert_failure() -> Result<(), Error> {
|
|
|
|
let sql = "
|
|
|
|
DEFINE TABLE person SCHEMAFULL;
|
|
|
|
DEFINE FIELD age ON person TYPE number ASSERT $value > 0;
|
2023-09-04 16:10:18 +00:00
|
|
|
DEFINE FIELD email ON person TYPE string ASSERT string::is::email($value);
|
2023-04-25 10:13:04 +00:00
|
|
|
DEFINE FIELD name ON person TYPE option<string> VALUE $value OR 'No name';
|
2022-07-28 09:00:06 +00:00
|
|
|
CREATE person:test SET email = 'info@surrealdb.com', other = 'ignore';
|
2022-09-01 00:29:51 +00:00
|
|
|
CREATE person:test SET email = 'info@surrealdb.com', other = 'ignore', age = NONE;
|
|
|
|
CREATE person:test SET email = 'info@surrealdb.com', other = 'ignore', age = NULL;
|
2023-04-25 10:13:04 +00:00
|
|
|
CREATE person:test SET email = 'info@surrealdb.com', other = 'ignore', age = 0;
|
|
|
|
CREATE person:test SET email = 'info@surrealdb.com', other = 'ignore', age = 13;
|
2022-07-28 09:00:06 +00:00
|
|
|
";
|
2023-08-30 18:01:30 +00:00
|
|
|
let dbs = new_ds().await?;
|
2023-07-29 18:47:25 +00:00
|
|
|
let ses = Session::owner().with_ns("test").with_db("test");
|
2023-07-05 21:26:13 +00:00
|
|
|
let res = &mut dbs.execute(sql, &ses, None).await?;
|
2023-04-25 10:13:04 +00:00
|
|
|
assert_eq!(res.len(), 9);
|
2022-07-28 09:00:06 +00:00
|
|
|
//
|
|
|
|
let tmp = res.remove(0).result;
|
|
|
|
assert!(tmp.is_ok());
|
|
|
|
//
|
|
|
|
let tmp = res.remove(0).result;
|
|
|
|
assert!(tmp.is_ok());
|
|
|
|
//
|
|
|
|
let tmp = res.remove(0).result;
|
|
|
|
assert!(tmp.is_ok());
|
|
|
|
//
|
|
|
|
let tmp = res.remove(0).result;
|
|
|
|
assert!(tmp.is_ok());
|
|
|
|
//
|
|
|
|
let tmp = res.remove(0).result;
|
2023-06-06 06:12:59 +00:00
|
|
|
assert!(
|
|
|
|
matches!(
|
|
|
|
&tmp,
|
|
|
|
Err(e) if e.to_string() == "Found NONE for field `age`, with record `person:test`, but expected a number",
|
|
|
|
|
|
|
|
),
|
|
|
|
"{}",
|
|
|
|
tmp.unwrap_err().to_string()
|
|
|
|
);
|
2022-09-01 00:29:51 +00:00
|
|
|
//
|
|
|
|
let tmp = res.remove(0).result;
|
2023-06-06 06:12:59 +00:00
|
|
|
assert!(
|
|
|
|
matches!(
|
|
|
|
&tmp,
|
|
|
|
Err(e) if e.to_string() == "Found NONE for field `age`, with record `person:test`, but expected a number"
|
|
|
|
),
|
|
|
|
"{}",
|
|
|
|
tmp.unwrap_err().to_string()
|
|
|
|
);
|
2022-09-01 00:29:51 +00:00
|
|
|
//
|
|
|
|
let tmp = res.remove(0).result;
|
2023-06-06 06:12:59 +00:00
|
|
|
assert!(
|
|
|
|
matches!(
|
|
|
|
&tmp,
|
|
|
|
Err(e) if e.to_string() == "Found NULL for field `age`, with record `person:test`, but expected a number"
|
|
|
|
),
|
|
|
|
"{}",
|
|
|
|
tmp.unwrap_err().to_string()
|
|
|
|
);
|
2022-07-28 09:00:06 +00:00
|
|
|
//
|
2023-04-25 10:13:04 +00:00
|
|
|
let tmp = res.remove(0).result;
|
2023-06-06 06:12:59 +00:00
|
|
|
assert!(
|
|
|
|
matches!(
|
|
|
|
&tmp,
|
|
|
|
Err(e) if e.to_string() == "Found 0 for field `age`, with record `person:test`, but field must conform to: $value > 0"
|
|
|
|
),
|
|
|
|
"{}",
|
|
|
|
tmp.unwrap_err().to_string()
|
|
|
|
);
|
2023-04-25 10:13:04 +00:00
|
|
|
//
|
|
|
|
let tmp = res.remove(0).result?;
|
|
|
|
let val = Value::parse(
|
|
|
|
"[
|
|
|
|
{
|
|
|
|
age: 13,
|
|
|
|
email: 'info@surrealdb.com',
|
|
|
|
id: person:test,
|
|
|
|
name: 'No name',
|
|
|
|
}
|
|
|
|
]",
|
|
|
|
);
|
|
|
|
assert_eq!(tmp, val);
|
|
|
|
//
|
2022-07-28 09:00:06 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
async fn field_definition_value_assert_success() -> Result<(), Error> {
|
|
|
|
let sql = "
|
|
|
|
DEFINE TABLE person SCHEMAFULL;
|
|
|
|
DEFINE FIELD age ON person TYPE number ASSERT $value > 0;
|
2023-09-04 16:10:18 +00:00
|
|
|
DEFINE FIELD email ON person TYPE string ASSERT string::is::email($value);
|
2023-04-25 10:13:04 +00:00
|
|
|
DEFINE FIELD name ON person TYPE option<string> VALUE $value OR 'No name';
|
2022-07-28 09:00:06 +00:00
|
|
|
CREATE person:test SET email = 'info@surrealdb.com', other = 'ignore', age = 22;
|
|
|
|
";
|
2023-08-30 18:01:30 +00:00
|
|
|
let dbs = new_ds().await?;
|
2023-07-29 18:47:25 +00:00
|
|
|
let ses = Session::owner().with_ns("test").with_db("test");
|
2023-07-05 21:26:13 +00:00
|
|
|
let res = &mut dbs.execute(sql, &ses, None).await?;
|
2022-07-28 09:00:06 +00:00
|
|
|
assert_eq!(res.len(), 5);
|
|
|
|
//
|
|
|
|
let tmp = res.remove(0).result;
|
|
|
|
assert!(tmp.is_ok());
|
|
|
|
//
|
|
|
|
let tmp = res.remove(0).result;
|
|
|
|
assert!(tmp.is_ok());
|
|
|
|
//
|
|
|
|
let tmp = res.remove(0).result;
|
|
|
|
assert!(tmp.is_ok());
|
|
|
|
//
|
|
|
|
let tmp = res.remove(0).result;
|
|
|
|
assert!(tmp.is_ok());
|
|
|
|
//
|
|
|
|
let tmp = res.remove(0).result?;
|
|
|
|
let val = Value::parse(
|
|
|
|
"[
|
|
|
|
{
|
|
|
|
id: person:test,
|
|
|
|
email: 'info@surrealdb.com',
|
|
|
|
age: 22,
|
|
|
|
name: 'No name',
|
|
|
|
}
|
|
|
|
]",
|
|
|
|
);
|
|
|
|
assert_eq!(tmp, val);
|
|
|
|
//
|
|
|
|
Ok(())
|
|
|
|
}
|
2022-10-20 14:32:25 +00:00
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
async fn field_definition_empty_nested_objects() -> Result<(), Error> {
|
|
|
|
let sql = "
|
|
|
|
DEFINE TABLE person SCHEMAFULL;
|
|
|
|
DEFINE FIELD settings on person TYPE object;
|
|
|
|
UPDATE person:test CONTENT {
|
|
|
|
settings: {
|
|
|
|
nested: {
|
|
|
|
object: {
|
|
|
|
thing: 'test'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
SELECT * FROM person;
|
|
|
|
";
|
2023-08-30 18:01:30 +00:00
|
|
|
let dbs = new_ds().await?;
|
2023-07-29 18:47:25 +00:00
|
|
|
let ses = Session::owner().with_ns("test").with_db("test");
|
2023-07-05 21:26:13 +00:00
|
|
|
let res = &mut dbs.execute(sql, &ses, None).await?;
|
2022-10-20 14:32:25 +00:00
|
|
|
assert_eq!(res.len(), 4);
|
|
|
|
//
|
|
|
|
let tmp = res.remove(0).result;
|
|
|
|
assert!(tmp.is_ok());
|
|
|
|
//
|
|
|
|
let tmp = res.remove(0).result;
|
|
|
|
assert!(tmp.is_ok());
|
|
|
|
//
|
|
|
|
let tmp = res.remove(0).result?;
|
|
|
|
let val = Value::parse(
|
|
|
|
"[
|
|
|
|
{
|
|
|
|
id: person:test,
|
|
|
|
settings: {},
|
|
|
|
}
|
|
|
|
]",
|
|
|
|
);
|
|
|
|
assert_eq!(tmp, val);
|
|
|
|
//
|
|
|
|
let tmp = res.remove(0).result?;
|
|
|
|
let val = Value::parse(
|
|
|
|
"[
|
|
|
|
{
|
|
|
|
id: person:test,
|
|
|
|
settings: {},
|
|
|
|
}
|
|
|
|
]",
|
|
|
|
);
|
|
|
|
assert_eq!(tmp, val);
|
|
|
|
//
|
|
|
|
Ok(())
|
|
|
|
}
|
2023-01-04 09:24:14 +00:00
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
async fn field_definition_empty_nested_arrays() -> Result<(), Error> {
|
|
|
|
let sql = "
|
|
|
|
DEFINE TABLE person SCHEMAFULL;
|
|
|
|
DEFINE FIELD settings on person TYPE object;
|
|
|
|
UPDATE person:test CONTENT {
|
|
|
|
settings: {
|
|
|
|
nested: [
|
|
|
|
1,
|
|
|
|
2,
|
|
|
|
3,
|
|
|
|
4,
|
|
|
|
5
|
|
|
|
]
|
|
|
|
}
|
|
|
|
};
|
|
|
|
SELECT * FROM person;
|
|
|
|
";
|
2023-08-30 18:01:30 +00:00
|
|
|
let dbs = new_ds().await?;
|
2023-07-29 18:47:25 +00:00
|
|
|
let ses = Session::owner().with_ns("test").with_db("test");
|
2023-07-05 21:26:13 +00:00
|
|
|
let res = &mut dbs.execute(sql, &ses, None).await?;
|
2023-01-04 09:24:14 +00:00
|
|
|
assert_eq!(res.len(), 4);
|
|
|
|
//
|
|
|
|
let tmp = res.remove(0).result;
|
|
|
|
assert!(tmp.is_ok());
|
|
|
|
//
|
|
|
|
let tmp = res.remove(0).result;
|
|
|
|
assert!(tmp.is_ok());
|
|
|
|
//
|
|
|
|
let tmp = res.remove(0).result?;
|
|
|
|
let val = Value::parse(
|
|
|
|
"[
|
|
|
|
{
|
|
|
|
id: person:test,
|
|
|
|
settings: {},
|
|
|
|
}
|
|
|
|
]",
|
|
|
|
);
|
|
|
|
assert_eq!(tmp, val);
|
|
|
|
//
|
|
|
|
let tmp = res.remove(0).result?;
|
|
|
|
let val = Value::parse(
|
|
|
|
"[
|
|
|
|
{
|
|
|
|
id: person:test,
|
|
|
|
settings: {},
|
|
|
|
}
|
|
|
|
]",
|
|
|
|
);
|
|
|
|
assert_eq!(tmp, val);
|
|
|
|
//
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
async fn field_definition_empty_nested_flexible() -> Result<(), Error> {
|
|
|
|
let sql = "
|
|
|
|
DEFINE TABLE person SCHEMAFULL;
|
|
|
|
DEFINE FIELD settings on person FLEXIBLE TYPE object;
|
|
|
|
UPDATE person:test CONTENT {
|
|
|
|
settings: {
|
|
|
|
nested: {
|
|
|
|
object: {
|
|
|
|
thing: 'test'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
SELECT * FROM person;
|
|
|
|
";
|
2023-08-30 18:01:30 +00:00
|
|
|
let dbs = new_ds().await?;
|
2023-07-29 18:47:25 +00:00
|
|
|
let ses = Session::owner().with_ns("test").with_db("test");
|
2023-07-05 21:26:13 +00:00
|
|
|
let res = &mut dbs.execute(sql, &ses, None).await?;
|
2023-01-04 09:24:14 +00:00
|
|
|
assert_eq!(res.len(), 4);
|
|
|
|
//
|
|
|
|
let tmp = res.remove(0).result;
|
|
|
|
assert!(tmp.is_ok());
|
|
|
|
//
|
|
|
|
let tmp = res.remove(0).result;
|
|
|
|
assert!(tmp.is_ok());
|
|
|
|
//
|
|
|
|
let tmp = res.remove(0).result?;
|
|
|
|
let val = Value::parse(
|
|
|
|
"[
|
|
|
|
{
|
|
|
|
id: person:test,
|
|
|
|
settings: {
|
|
|
|
nested: {
|
|
|
|
object: {
|
|
|
|
thing: 'test'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
}
|
|
|
|
]",
|
|
|
|
);
|
|
|
|
assert_eq!(tmp, val);
|
|
|
|
//
|
|
|
|
let tmp = res.remove(0).result?;
|
|
|
|
let val = Value::parse(
|
|
|
|
"[
|
|
|
|
{
|
|
|
|
id: person:test,
|
|
|
|
settings: {
|
|
|
|
nested: {
|
|
|
|
object: {
|
|
|
|
thing: 'test'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
}
|
|
|
|
]",
|
|
|
|
);
|
|
|
|
assert_eq!(tmp, val);
|
|
|
|
//
|
|
|
|
Ok(())
|
|
|
|
}
|
2023-08-19 19:22:15 +00:00
|
|
|
|
2023-08-31 12:06:10 +00:00
|
|
|
#[tokio::test]
|
|
|
|
async fn field_selection_variable_field_projection() -> Result<(), Error> {
|
|
|
|
let sql = "
|
|
|
|
CREATE person:test SET title = 'Mr', name.first = 'Tobie', name.last = 'Morgan Hitchcock';
|
|
|
|
LET $param = 'name.first';
|
|
|
|
SELECT type::field($param), type::field('name.last') FROM person;
|
|
|
|
SELECT VALUE { 'firstname': type::field($param), lastname: type::field('name.last') } FROM person;
|
|
|
|
SELECT VALUE [type::field($param), type::field('name.last')] FROM person;
|
2023-12-15 21:52:09 +00:00
|
|
|
SELECT type::field($param) AS first_name FROM person;
|
2023-08-31 12:06:10 +00:00
|
|
|
";
|
|
|
|
let dbs = new_ds().await?;
|
|
|
|
let ses = Session::owner().with_ns("test").with_db("test");
|
|
|
|
let res = &mut dbs.execute(sql, &ses, None).await?;
|
2023-12-15 21:52:09 +00:00
|
|
|
assert_eq!(res.len(), 6);
|
2023-08-31 12:06:10 +00:00
|
|
|
//
|
|
|
|
let tmp = res.remove(0).result?;
|
|
|
|
let val = Value::parse(
|
|
|
|
"[
|
|
|
|
{
|
|
|
|
id: person:test,
|
|
|
|
title: 'Mr',
|
|
|
|
name: {
|
|
|
|
first: 'Tobie',
|
|
|
|
last: 'Morgan Hitchcock',
|
|
|
|
}
|
|
|
|
}
|
|
|
|
]",
|
|
|
|
);
|
|
|
|
assert_eq!(tmp, val);
|
|
|
|
//
|
|
|
|
let tmp = res.remove(0).result;
|
|
|
|
assert!(tmp.is_ok());
|
|
|
|
//
|
|
|
|
let tmp = res.remove(0).result?;
|
|
|
|
let val = Value::parse(
|
|
|
|
"[
|
|
|
|
{
|
|
|
|
name: {
|
|
|
|
first: 'Tobie',
|
|
|
|
last: 'Morgan Hitchcock',
|
|
|
|
}
|
|
|
|
}
|
|
|
|
]",
|
|
|
|
);
|
|
|
|
assert_eq!(tmp, val);
|
|
|
|
//
|
|
|
|
let tmp = res.remove(0).result?;
|
|
|
|
let val = Value::parse(
|
|
|
|
"[
|
|
|
|
{
|
|
|
|
firstname: 'Tobie',
|
|
|
|
lastname: 'Morgan Hitchcock',
|
|
|
|
}
|
|
|
|
]",
|
|
|
|
);
|
|
|
|
assert_eq!(tmp, val);
|
|
|
|
//
|
|
|
|
let tmp = res.remove(0).result?;
|
|
|
|
let val = Value::parse(
|
|
|
|
"[
|
|
|
|
['Tobie', 'Morgan Hitchcock']
|
|
|
|
]",
|
|
|
|
);
|
|
|
|
assert_eq!(tmp, val);
|
2023-12-15 21:52:09 +00:00
|
|
|
|
|
|
|
let tmp = res.remove(0).result?;
|
|
|
|
let val = Value::parse(
|
|
|
|
"[
|
|
|
|
{ first_name: 'Tobie' }
|
|
|
|
]",
|
|
|
|
);
|
|
|
|
assert_eq!(tmp, val);
|
2023-08-31 12:06:10 +00:00
|
|
|
//
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
async fn field_selection_variable_fields_projection() -> Result<(), Error> {
|
|
|
|
let sql = "
|
|
|
|
CREATE person:test SET title = 'Mr', name.first = 'Tobie', name.last = 'Morgan Hitchcock';
|
|
|
|
LET $param = ['name.first', 'name.last'];
|
|
|
|
SELECT type::fields($param), type::fields(['title']) FROM person;
|
|
|
|
SELECT VALUE { 'names': type::fields($param) } FROM person;
|
|
|
|
SELECT VALUE type::fields($param) FROM person;
|
|
|
|
";
|
|
|
|
let dbs = new_ds().await?;
|
|
|
|
let ses = Session::owner().with_ns("test").with_db("test");
|
|
|
|
let res = &mut dbs.execute(sql, &ses, None).await?;
|
|
|
|
assert_eq!(res.len(), 5);
|
|
|
|
//
|
|
|
|
let tmp = res.remove(0).result?;
|
|
|
|
let val = Value::parse(
|
|
|
|
"[
|
|
|
|
{
|
|
|
|
id: person:test,
|
|
|
|
title: 'Mr',
|
|
|
|
name: {
|
|
|
|
first: 'Tobie',
|
|
|
|
last: 'Morgan Hitchcock',
|
|
|
|
}
|
|
|
|
}
|
|
|
|
]",
|
|
|
|
);
|
|
|
|
assert_eq!(tmp, val);
|
|
|
|
//
|
|
|
|
let tmp = res.remove(0).result;
|
|
|
|
assert!(tmp.is_ok());
|
|
|
|
//
|
|
|
|
let tmp = res.remove(0).result?;
|
|
|
|
let val = Value::parse(
|
|
|
|
"[
|
|
|
|
{
|
|
|
|
title: 'Mr',
|
|
|
|
name: {
|
|
|
|
first: 'Tobie',
|
|
|
|
last: 'Morgan Hitchcock',
|
|
|
|
}
|
|
|
|
}
|
|
|
|
]",
|
|
|
|
);
|
|
|
|
assert_eq!(tmp, val);
|
|
|
|
//
|
|
|
|
let tmp = res.remove(0).result?;
|
|
|
|
let val = Value::parse(
|
|
|
|
"[
|
|
|
|
{
|
|
|
|
names: ['Tobie', 'Morgan Hitchcock']
|
|
|
|
}
|
|
|
|
]",
|
|
|
|
);
|
|
|
|
assert_eq!(tmp, val);
|
|
|
|
//
|
|
|
|
let tmp = res.remove(0).result?;
|
|
|
|
let val = Value::parse(
|
|
|
|
"[
|
|
|
|
['Tobie', 'Morgan Hitchcock']
|
|
|
|
]",
|
|
|
|
);
|
|
|
|
assert_eq!(tmp, val);
|
|
|
|
//
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2023-08-20 02:53:50 +00:00
|
|
|
#[tokio::test]
|
|
|
|
async fn field_definition_default_value() -> Result<(), Error> {
|
|
|
|
let sql = "
|
|
|
|
DEFINE TABLE product SCHEMAFULL;
|
|
|
|
DEFINE FIELD primary ON product TYPE number VALUE 123.456;
|
|
|
|
DEFINE FIELD secondary ON product TYPE bool DEFAULT true VALUE $value;
|
|
|
|
DEFINE FIELD tertiary ON product TYPE string DEFAULT 'hello' VALUE 'tester';
|
2023-12-14 21:23:58 +00:00
|
|
|
DEFINE FIELD quaternary ON product TYPE bool VALUE array::all([1, 2]);
|
2023-08-20 02:53:50 +00:00
|
|
|
--
|
|
|
|
CREATE product:test SET primary = NULL;
|
|
|
|
CREATE product:test SET secondary = 'oops';
|
|
|
|
CREATE product:test SET tertiary = 123;
|
|
|
|
CREATE product:test;
|
|
|
|
--
|
|
|
|
UPDATE product:test SET primary = 654.321;
|
|
|
|
UPDATE product:test SET secondary = false;
|
|
|
|
UPDATE product:test SET tertiary = 'something';
|
|
|
|
";
|
2023-08-30 18:01:30 +00:00
|
|
|
let dbs = new_ds().await?;
|
2023-08-20 02:53:50 +00:00
|
|
|
let ses = Session::owner().with_ns("test").with_db("test");
|
|
|
|
let res = &mut dbs.execute(sql, &ses, None).await?;
|
2023-12-14 21:23:58 +00:00
|
|
|
assert_eq!(res.len(), 12);
|
|
|
|
//
|
|
|
|
let tmp = res.remove(0).result;
|
|
|
|
assert!(tmp.is_ok());
|
2023-08-20 02:53:50 +00:00
|
|
|
//
|
|
|
|
let tmp = res.remove(0).result;
|
|
|
|
assert!(tmp.is_ok());
|
|
|
|
//
|
|
|
|
let tmp = res.remove(0).result;
|
|
|
|
assert!(tmp.is_ok());
|
|
|
|
//
|
|
|
|
let tmp = res.remove(0).result;
|
|
|
|
assert!(tmp.is_ok());
|
|
|
|
//
|
|
|
|
let tmp = res.remove(0).result;
|
|
|
|
assert!(tmp.is_ok());
|
|
|
|
//
|
|
|
|
let tmp = res.remove(0).result;
|
|
|
|
assert!(
|
|
|
|
matches!(
|
|
|
|
&tmp,
|
|
|
|
Err(e) if e.to_string() == "Found NULL for field `primary`, with record `product:test`, but expected a number"
|
|
|
|
),
|
|
|
|
"{}",
|
|
|
|
tmp.unwrap_err().to_string()
|
|
|
|
);
|
|
|
|
//
|
|
|
|
let tmp = res.remove(0).result;
|
|
|
|
assert!(
|
|
|
|
matches!(
|
|
|
|
&tmp,
|
|
|
|
Err(e) if e.to_string() == "Found 'oops' for field `secondary`, with record `product:test`, but expected a bool"
|
|
|
|
),
|
|
|
|
"{}",
|
|
|
|
tmp.unwrap_err().to_string()
|
|
|
|
);
|
|
|
|
//
|
|
|
|
let tmp = res.remove(0).result;
|
|
|
|
assert!(
|
|
|
|
matches!(
|
|
|
|
&tmp,
|
|
|
|
Err(e) if e.to_string() == "Found 123 for field `tertiary`, with record `product:test`, but expected a string"
|
|
|
|
),
|
|
|
|
"{}",
|
|
|
|
tmp.unwrap_err().to_string()
|
|
|
|
);
|
|
|
|
//
|
|
|
|
let tmp = res.remove(0).result?;
|
|
|
|
let val = Value::parse(
|
|
|
|
"[
|
|
|
|
{
|
|
|
|
id: product:test,
|
|
|
|
primary: 123.456,
|
2023-12-14 21:23:58 +00:00
|
|
|
quaternary: true,
|
2023-08-20 02:53:50 +00:00
|
|
|
secondary: true,
|
|
|
|
tertiary: 'tester',
|
|
|
|
}
|
|
|
|
]",
|
|
|
|
);
|
|
|
|
assert_eq!(tmp, val);
|
|
|
|
//
|
|
|
|
let tmp = res.remove(0).result?;
|
|
|
|
let val = Value::parse(
|
|
|
|
"[
|
|
|
|
{
|
|
|
|
id: product:test,
|
|
|
|
primary: 123.456,
|
2023-12-14 21:23:58 +00:00
|
|
|
quaternary: true,
|
2023-08-20 02:53:50 +00:00
|
|
|
secondary: true,
|
|
|
|
tertiary: 'tester',
|
|
|
|
}
|
|
|
|
]",
|
|
|
|
);
|
|
|
|
assert_eq!(tmp, val);
|
|
|
|
//
|
|
|
|
let tmp = res.remove(0).result?;
|
|
|
|
let val = Value::parse(
|
|
|
|
"[
|
|
|
|
{
|
|
|
|
id: product:test,
|
|
|
|
primary: 123.456,
|
2023-12-14 21:23:58 +00:00
|
|
|
quaternary: true,
|
2023-08-20 02:53:50 +00:00
|
|
|
secondary: false,
|
|
|
|
tertiary: 'tester',
|
|
|
|
}
|
|
|
|
]",
|
|
|
|
);
|
|
|
|
assert_eq!(tmp, val);
|
|
|
|
//
|
|
|
|
let tmp = res.remove(0).result?;
|
|
|
|
let val = Value::parse(
|
|
|
|
"[
|
|
|
|
{
|
|
|
|
id: product:test,
|
|
|
|
primary: 123.456,
|
2023-12-14 21:23:58 +00:00
|
|
|
quaternary: true,
|
2023-08-20 02:53:50 +00:00
|
|
|
secondary: false,
|
|
|
|
tertiary: 'tester',
|
|
|
|
}
|
|
|
|
]",
|
|
|
|
);
|
|
|
|
assert_eq!(tmp, val);
|
|
|
|
//
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2023-08-19 19:22:15 +00:00
|
|
|
#[tokio::test]
|
|
|
|
async fn field_definition_value_reference() -> Result<(), Error> {
|
|
|
|
let sql = "
|
|
|
|
DEFINE TABLE product;
|
|
|
|
DEFINE FIELD subproducts ON product VALUE ->contains->product;
|
|
|
|
CREATE product:one, product:two;
|
|
|
|
RELATE product:one->contains:test->product:two;
|
|
|
|
SELECT * FROM product;
|
|
|
|
UPDATE product;
|
|
|
|
SELECT * FROM product;
|
|
|
|
";
|
2023-08-30 18:01:30 +00:00
|
|
|
let dbs = new_ds().await?;
|
2023-08-19 19:22:15 +00:00
|
|
|
let ses = Session::owner().with_ns("test").with_db("test");
|
|
|
|
let res = &mut dbs.execute(sql, &ses, None).await?;
|
|
|
|
assert_eq!(res.len(), 7);
|
|
|
|
//
|
|
|
|
let tmp = res.remove(0).result;
|
|
|
|
assert!(tmp.is_ok());
|
|
|
|
//
|
|
|
|
let tmp = res.remove(0).result;
|
|
|
|
assert!(tmp.is_ok());
|
|
|
|
//
|
|
|
|
let tmp = res.remove(0).result?;
|
|
|
|
let val = Value::parse(
|
|
|
|
"[
|
|
|
|
{
|
|
|
|
id: product:one,
|
|
|
|
subproducts: [],
|
|
|
|
},
|
|
|
|
{
|
|
|
|
id: product:two,
|
|
|
|
subproducts: [],
|
|
|
|
},
|
|
|
|
]",
|
|
|
|
);
|
|
|
|
assert_eq!(tmp, val);
|
|
|
|
//
|
|
|
|
let tmp = res.remove(0).result?;
|
|
|
|
let val = Value::parse(
|
|
|
|
"[
|
|
|
|
{
|
|
|
|
id: contains:test,
|
|
|
|
in: product:one,
|
|
|
|
out: product:two,
|
|
|
|
},
|
|
|
|
]",
|
|
|
|
);
|
|
|
|
assert_eq!(tmp, val);
|
|
|
|
//
|
|
|
|
let tmp = res.remove(0).result?;
|
|
|
|
let val = Value::parse(
|
|
|
|
"[
|
|
|
|
{
|
|
|
|
id: product:one,
|
|
|
|
subproducts: [],
|
|
|
|
},
|
|
|
|
{
|
|
|
|
id: product:two,
|
|
|
|
subproducts: [],
|
|
|
|
},
|
|
|
|
]",
|
|
|
|
);
|
|
|
|
assert_eq!(tmp, val);
|
|
|
|
//
|
|
|
|
let tmp = res.remove(0).result?;
|
|
|
|
let val = Value::parse(
|
|
|
|
"[
|
|
|
|
{
|
|
|
|
id: product:one,
|
|
|
|
subproducts: [
|
|
|
|
product:two,
|
|
|
|
],
|
|
|
|
},
|
|
|
|
{
|
|
|
|
id: product:two,
|
|
|
|
subproducts: [],
|
|
|
|
},
|
|
|
|
]",
|
|
|
|
);
|
|
|
|
assert_eq!(tmp, val);
|
|
|
|
//
|
|
|
|
let tmp = res.remove(0).result?;
|
|
|
|
let val = Value::parse(
|
|
|
|
"[
|
|
|
|
{
|
|
|
|
id: product:one,
|
|
|
|
subproducts: [
|
|
|
|
product:two,
|
|
|
|
],
|
|
|
|
},
|
|
|
|
{
|
|
|
|
id: product:two,
|
|
|
|
subproducts: [],
|
|
|
|
},
|
|
|
|
]",
|
|
|
|
);
|
|
|
|
assert_eq!(tmp, val);
|
|
|
|
//
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
async fn field_definition_value_reference_with_future() -> Result<(), Error> {
|
2024-01-09 16:57:52 +00:00
|
|
|
with_enough_stack(async {
|
|
|
|
let sql = "
|
2023-08-19 19:22:15 +00:00
|
|
|
DEFINE TABLE product;
|
|
|
|
DEFINE FIELD subproducts ON product VALUE <future> { ->contains->product };
|
|
|
|
CREATE product:one, product:two;
|
|
|
|
RELATE product:one->contains:test->product:two;
|
|
|
|
SELECT * FROM product;
|
|
|
|
UPDATE product;
|
|
|
|
SELECT * FROM product;
|
|
|
|
";
|
2024-01-09 16:57:52 +00:00
|
|
|
let dbs = new_ds().await?;
|
|
|
|
let ses = Session::owner().with_ns("test").with_db("test");
|
|
|
|
let res = &mut dbs.execute(sql, &ses, None).await?;
|
|
|
|
assert_eq!(res.len(), 7);
|
|
|
|
//
|
|
|
|
let tmp = res.remove(0).result;
|
|
|
|
assert!(tmp.is_ok());
|
|
|
|
//
|
|
|
|
let tmp = res.remove(0).result;
|
|
|
|
assert!(tmp.is_ok());
|
|
|
|
//
|
|
|
|
let tmp = res.remove(0).result?;
|
|
|
|
let val = Value::parse(
|
|
|
|
"[
|
2023-08-19 19:22:15 +00:00
|
|
|
{
|
|
|
|
id: product:one,
|
|
|
|
subproducts: [],
|
|
|
|
},
|
|
|
|
{
|
|
|
|
id: product:two,
|
|
|
|
subproducts: [],
|
|
|
|
},
|
|
|
|
]",
|
2024-01-09 16:57:52 +00:00
|
|
|
);
|
|
|
|
assert_eq!(tmp, val);
|
|
|
|
//
|
|
|
|
let tmp = res.remove(0).result?;
|
|
|
|
let val = Value::parse(
|
|
|
|
"[
|
2023-08-19 19:22:15 +00:00
|
|
|
{
|
|
|
|
id: contains:test,
|
|
|
|
in: product:one,
|
|
|
|
out: product:two,
|
|
|
|
},
|
|
|
|
]",
|
2024-01-09 16:57:52 +00:00
|
|
|
);
|
|
|
|
assert_eq!(tmp, val);
|
|
|
|
//
|
|
|
|
let tmp = res.remove(0).result?;
|
|
|
|
let val = Value::parse(
|
|
|
|
"[
|
2023-08-19 19:22:15 +00:00
|
|
|
{
|
|
|
|
id: product:one,
|
|
|
|
subproducts: [
|
|
|
|
product:two,
|
|
|
|
],
|
|
|
|
},
|
|
|
|
{
|
|
|
|
id: product:two,
|
|
|
|
subproducts: [],
|
|
|
|
},
|
|
|
|
]",
|
2024-01-09 16:57:52 +00:00
|
|
|
);
|
|
|
|
assert_eq!(tmp, val);
|
|
|
|
//
|
|
|
|
let tmp = res.remove(0).result?;
|
|
|
|
let val = Value::parse(
|
|
|
|
"[
|
2023-08-19 19:22:15 +00:00
|
|
|
{
|
|
|
|
id: product:one,
|
|
|
|
subproducts: [
|
|
|
|
product:two,
|
|
|
|
],
|
|
|
|
},
|
|
|
|
{
|
|
|
|
id: product:two,
|
|
|
|
subproducts: [],
|
|
|
|
},
|
|
|
|
]",
|
2024-01-09 16:57:52 +00:00
|
|
|
);
|
|
|
|
assert_eq!(tmp, val);
|
|
|
|
//
|
|
|
|
let tmp = res.remove(0).result?;
|
|
|
|
let val = Value::parse(
|
|
|
|
"[
|
2023-08-19 19:22:15 +00:00
|
|
|
{
|
|
|
|
id: product:one,
|
|
|
|
subproducts: [
|
|
|
|
product:two,
|
|
|
|
],
|
|
|
|
},
|
|
|
|
{
|
|
|
|
id: product:two,
|
|
|
|
subproducts: [],
|
|
|
|
},
|
|
|
|
]",
|
2024-01-09 16:57:52 +00:00
|
|
|
);
|
|
|
|
assert_eq!(tmp, val);
|
|
|
|
//
|
|
|
|
Ok(())
|
|
|
|
})
|
2023-08-19 19:22:15 +00:00
|
|
|
}
|
2023-08-19 22:19:16 +00:00
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
async fn field_definition_edge_permissions() -> Result<(), Error> {
|
|
|
|
let sql = "
|
|
|
|
DEFINE TABLE user SCHEMAFULL;
|
|
|
|
DEFINE TABLE business SCHEMAFULL;
|
|
|
|
DEFINE FIELD owner ON TABLE business TYPE record<user>;
|
|
|
|
DEFINE TABLE contact SCHEMAFULL PERMISSIONS FOR create WHERE in.owner.id = $auth.id;
|
|
|
|
INSERT INTO user (id, name) VALUES (user:one, 'John'), (user:two, 'Lucy');
|
|
|
|
INSERT INTO business (id, owner) VALUES (business:one, user:one), (business:two, user:two);
|
|
|
|
";
|
2023-08-30 18:01:30 +00:00
|
|
|
let dbs = new_ds().await?.with_auth_enabled(true);
|
2023-08-19 22:19:16 +00:00
|
|
|
let ses = Session::owner().with_ns("test").with_db("test");
|
|
|
|
let res = &mut dbs.execute(sql, &ses, None).await?;
|
|
|
|
assert_eq!(res.len(), 6);
|
|
|
|
//
|
|
|
|
let tmp = res.remove(0).result;
|
|
|
|
assert!(tmp.is_ok());
|
|
|
|
//
|
|
|
|
let tmp = res.remove(0).result;
|
|
|
|
assert!(tmp.is_ok());
|
|
|
|
//
|
|
|
|
let tmp = res.remove(0).result;
|
|
|
|
assert!(tmp.is_ok());
|
|
|
|
//
|
|
|
|
let tmp = res.remove(0).result;
|
|
|
|
assert!(tmp.is_ok());
|
|
|
|
//
|
|
|
|
let tmp = res.remove(0).result?;
|
|
|
|
let val = Value::parse(
|
|
|
|
"[
|
|
|
|
{
|
|
|
|
id: user:one,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
id: user:two,
|
|
|
|
},
|
|
|
|
]",
|
|
|
|
);
|
|
|
|
assert_eq!(tmp, val);
|
|
|
|
//
|
|
|
|
let tmp = res.remove(0).result?;
|
|
|
|
let val = Value::parse(
|
|
|
|
"[
|
|
|
|
{
|
|
|
|
id: business:one,
|
|
|
|
owner: user:one,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
id: business:two,
|
|
|
|
owner: user:two,
|
|
|
|
},
|
|
|
|
]",
|
|
|
|
);
|
|
|
|
assert_eq!(tmp, val);
|
|
|
|
//
|
|
|
|
let sql = "
|
|
|
|
RELATE business:one->contact:one->business:two;
|
|
|
|
RELATE business:two->contact:two->business:one;
|
|
|
|
";
|
|
|
|
let ses = Session::for_scope("test", "test", "test", Thing::from(("user", "one")).into());
|
|
|
|
let res = &mut dbs.execute(sql, &ses, None).await?;
|
|
|
|
assert_eq!(res.len(), 2);
|
|
|
|
//
|
|
|
|
let tmp = res.remove(0).result?;
|
|
|
|
let val = Value::parse(
|
|
|
|
"[
|
|
|
|
{
|
|
|
|
id: contact:one,
|
|
|
|
in: business:one,
|
|
|
|
out: business:two,
|
|
|
|
},
|
|
|
|
]",
|
|
|
|
);
|
|
|
|
assert_eq!(tmp, val);
|
|
|
|
//
|
|
|
|
let tmp = res.remove(0).result?;
|
|
|
|
let val = Value::parse("[]");
|
|
|
|
assert_eq!(tmp, val);
|
|
|
|
//
|
|
|
|
Ok(())
|
|
|
|
}
|