2022-07-28 09:00:06 +00:00
|
|
|
mod parse;
|
|
|
|
use parse::Parse;
|
2022-12-30 08:23:19 +00:00
|
|
|
use surrealdb::dbs::Session;
|
|
|
|
use surrealdb::err::Error;
|
|
|
|
use surrealdb::kvs::Datastore;
|
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;
|
|
|
|
DEFINE FIELD email ON person TYPE string ASSERT 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
|
|
|
";
|
|
|
|
let dbs = Datastore::new("memory").await?;
|
|
|
|
let ses = Session::for_kv().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;
|
|
|
|
DEFINE FIELD email ON person TYPE string ASSERT 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;
|
|
|
|
";
|
|
|
|
let dbs = Datastore::new("memory").await?;
|
|
|
|
let ses = Session::for_kv().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;
|
|
|
|
";
|
|
|
|
let dbs = Datastore::new("memory").await?;
|
|
|
|
let ses = Session::for_kv().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;
|
|
|
|
";
|
|
|
|
let dbs = Datastore::new("memory").await?;
|
|
|
|
let ses = Session::for_kv().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;
|
|
|
|
";
|
|
|
|
let dbs = Datastore::new("memory").await?;
|
|
|
|
let ses = Session::for_kv().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(())
|
|
|
|
}
|