2022-07-28 21:40:38 +00:00
|
|
|
mod parse;
|
2023-08-30 18:01:30 +00:00
|
|
|
use parse::Parse;
|
2023-07-29 18:47:25 +00:00
|
|
|
|
|
|
|
mod helpers;
|
|
|
|
use helpers::*;
|
|
|
|
|
|
|
|
use std::collections::HashMap;
|
|
|
|
|
2022-12-30 08:23:19 +00:00
|
|
|
use surrealdb::dbs::Session;
|
|
|
|
use surrealdb::err::Error;
|
2023-07-29 18:47:25 +00:00
|
|
|
use surrealdb::iam::Role;
|
2023-06-19 18:41:13 +00:00
|
|
|
use surrealdb::sql::Idiom;
|
|
|
|
use surrealdb::sql::{Part, Value};
|
2022-07-28 21:40:38 +00:00
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
async fn define_statement_namespace() -> Result<(), Error> {
|
|
|
|
let sql = "
|
|
|
|
DEFINE NAMESPACE test;
|
2023-07-29 18:47:25 +00:00
|
|
|
INFO FOR ROOT;
|
2022-07-28 21:40:38 +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?;
|
2022-07-28 21:40:38 +00:00
|
|
|
assert_eq!(res.len(), 2);
|
|
|
|
//
|
|
|
|
let tmp = res.remove(0).result;
|
2023-07-29 18:47:25 +00:00
|
|
|
assert!(tmp.is_ok(), "{:?}", tmp);
|
2022-07-28 21:40:38 +00:00
|
|
|
//
|
|
|
|
let tmp = res.remove(0).result?;
|
|
|
|
let val = Value::parse(
|
|
|
|
"{
|
2023-05-31 12:35:29 +00:00
|
|
|
namespaces: { test: 'DEFINE NAMESPACE test' },
|
2023-07-29 18:47:25 +00:00
|
|
|
users: {},
|
2022-07-28 21:40:38 +00:00
|
|
|
}",
|
|
|
|
);
|
|
|
|
assert_eq!(tmp, val);
|
|
|
|
//
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
async fn define_statement_database() -> Result<(), Error> {
|
|
|
|
let sql = "
|
|
|
|
DEFINE DATABASE test;
|
|
|
|
INFO FOR NS;
|
|
|
|
";
|
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 21:40:38 +00:00
|
|
|
assert_eq!(res.len(), 2);
|
|
|
|
//
|
|
|
|
let tmp = res.remove(0).result;
|
|
|
|
assert!(tmp.is_ok());
|
|
|
|
//
|
|
|
|
let tmp = res.remove(0).result?;
|
|
|
|
let val = Value::parse(
|
|
|
|
"{
|
2023-05-31 12:35:29 +00:00
|
|
|
databases: { test: 'DEFINE DATABASE test' },
|
|
|
|
tokens: {},
|
2023-07-29 18:47:25 +00:00
|
|
|
users: {},
|
2022-07-28 21:40:38 +00:00
|
|
|
}",
|
|
|
|
);
|
|
|
|
assert_eq!(tmp, val);
|
|
|
|
//
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::test]
|
2023-03-25 19:44:03 +00:00
|
|
|
async fn define_statement_function() -> Result<(), Error> {
|
2022-07-28 21:40:38 +00:00
|
|
|
let sql = "
|
2023-03-25 19:44:03 +00:00
|
|
|
DEFINE FUNCTION fn::test($first: string, $last: string) {
|
|
|
|
RETURN $first + $last;
|
|
|
|
};
|
2022-07-28 21:40:38 +00:00
|
|
|
INFO FOR DB;
|
|
|
|
";
|
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 21:40:38 +00:00
|
|
|
assert_eq!(res.len(), 2);
|
|
|
|
//
|
|
|
|
let tmp = res.remove(0).result;
|
|
|
|
assert!(tmp.is_ok());
|
|
|
|
//
|
|
|
|
let tmp = res.remove(0).result?;
|
|
|
|
let val = Value::parse(
|
|
|
|
"{
|
2023-05-31 12:35:29 +00:00
|
|
|
analyzers: {},
|
|
|
|
tokens: {},
|
2023-12-06 21:30:18 +00:00
|
|
|
functions: { test: 'DEFINE FUNCTION fn::test($first: string, $last: string) { RETURN $first + $last; } PERMISSIONS FULL' },
|
2023-12-12 13:51:43 +00:00
|
|
|
models: {},
|
2023-05-31 12:35:29 +00:00
|
|
|
params: {},
|
|
|
|
scopes: {},
|
|
|
|
tables: {},
|
2023-07-29 18:47:25 +00:00
|
|
|
users: {},
|
2023-03-25 19:44:03 +00:00
|
|
|
}",
|
|
|
|
);
|
|
|
|
assert_eq!(tmp, val);
|
|
|
|
//
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
async fn define_statement_table_drop() -> Result<(), Error> {
|
|
|
|
let sql = "
|
|
|
|
DEFINE TABLE test DROP;
|
|
|
|
INFO FOR DB;
|
|
|
|
";
|
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-03-25 19:44:03 +00:00
|
|
|
assert_eq!(res.len(), 2);
|
|
|
|
//
|
|
|
|
let tmp = res.remove(0).result;
|
|
|
|
assert!(tmp.is_ok());
|
|
|
|
//
|
|
|
|
let tmp = res.remove(0).result?;
|
|
|
|
let val = Value::parse(
|
|
|
|
"{
|
2023-05-31 12:35:29 +00:00
|
|
|
analyzers: {},
|
|
|
|
tokens: {},
|
|
|
|
functions: {},
|
2023-12-12 13:51:43 +00:00
|
|
|
models: {},
|
2023-05-31 12:35:29 +00:00
|
|
|
params: {},
|
|
|
|
scopes: {},
|
2024-03-19 11:20:58 +00:00
|
|
|
tables: { test: 'DEFINE TABLE test TYPE ANY DROP SCHEMALESS PERMISSIONS NONE' },
|
2023-07-29 18:47:25 +00:00
|
|
|
users: {},
|
2022-07-28 21:40:38 +00:00
|
|
|
}",
|
|
|
|
);
|
|
|
|
assert_eq!(tmp, val);
|
|
|
|
//
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
async fn define_statement_table_schemaless() -> Result<(), Error> {
|
|
|
|
let sql = "
|
|
|
|
DEFINE TABLE test SCHEMALESS;
|
|
|
|
INFO FOR DB;
|
|
|
|
";
|
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 21:40:38 +00:00
|
|
|
assert_eq!(res.len(), 2);
|
|
|
|
//
|
|
|
|
let tmp = res.remove(0).result;
|
|
|
|
assert!(tmp.is_ok());
|
|
|
|
//
|
|
|
|
let tmp = res.remove(0).result?;
|
|
|
|
let val = Value::parse(
|
|
|
|
"{
|
2023-05-31 12:35:29 +00:00
|
|
|
analyzers: {},
|
|
|
|
tokens: {},
|
|
|
|
functions: {},
|
2023-12-12 13:51:43 +00:00
|
|
|
models: {},
|
2023-05-31 12:35:29 +00:00
|
|
|
params: {},
|
|
|
|
scopes: {},
|
2024-03-19 11:20:58 +00:00
|
|
|
tables: { test: 'DEFINE TABLE test TYPE ANY SCHEMALESS PERMISSIONS NONE' },
|
2023-07-29 18:47:25 +00:00
|
|
|
users: {},
|
2022-07-28 21:40:38 +00:00
|
|
|
}",
|
|
|
|
);
|
|
|
|
assert_eq!(tmp, val);
|
|
|
|
//
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
async fn define_statement_table_schemafull() -> Result<(), Error> {
|
|
|
|
let sql = "
|
2022-09-20 07:11:14 +00:00
|
|
|
DEFINE TABLE test SCHEMAFUL;
|
2022-07-28 21:40:38 +00:00
|
|
|
DEFINE TABLE test SCHEMAFULL;
|
|
|
|
INFO FOR DB;
|
|
|
|
";
|
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-09-20 07:11:14 +00:00
|
|
|
assert_eq!(res.len(), 3);
|
|
|
|
//
|
|
|
|
let tmp = res.remove(0).result;
|
|
|
|
assert!(tmp.is_ok());
|
2022-07-28 21:40:38 +00:00
|
|
|
//
|
|
|
|
let tmp = res.remove(0).result;
|
|
|
|
assert!(tmp.is_ok());
|
|
|
|
//
|
|
|
|
let tmp = res.remove(0).result?;
|
|
|
|
let val = Value::parse(
|
|
|
|
"{
|
2023-05-31 12:35:29 +00:00
|
|
|
analyzers: {},
|
|
|
|
tokens: {},
|
|
|
|
functions: {},
|
2023-12-12 13:51:43 +00:00
|
|
|
models: {},
|
2023-05-31 12:35:29 +00:00
|
|
|
params: {},
|
|
|
|
scopes: {},
|
2024-03-19 11:20:58 +00:00
|
|
|
tables: { test: 'DEFINE TABLE test TYPE ANY SCHEMAFULL PERMISSIONS NONE' },
|
2023-07-29 18:47:25 +00:00
|
|
|
users: {},
|
2022-07-28 21:40:38 +00:00
|
|
|
}",
|
|
|
|
);
|
|
|
|
assert_eq!(tmp, val);
|
|
|
|
//
|
|
|
|
Ok(())
|
2022-09-11 08:08:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
async fn define_statement_table_schemaful() -> Result<(), Error> {
|
|
|
|
let sql = "
|
|
|
|
DEFINE TABLE test SCHEMAFUL;
|
|
|
|
INFO FOR DB;
|
|
|
|
";
|
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-09-11 08:08:36 +00:00
|
|
|
assert_eq!(res.len(), 2);
|
|
|
|
//
|
|
|
|
let tmp = res.remove(0).result;
|
|
|
|
assert!(tmp.is_ok());
|
|
|
|
//
|
|
|
|
let tmp = res.remove(0).result?;
|
|
|
|
let val = Value::parse(
|
|
|
|
"{
|
2023-05-31 12:35:29 +00:00
|
|
|
analyzers: {},
|
|
|
|
tokens: {},
|
|
|
|
functions: {},
|
2023-12-12 13:51:43 +00:00
|
|
|
models: {},
|
2023-05-31 12:35:29 +00:00
|
|
|
params: {},
|
|
|
|
scopes: {},
|
2024-03-19 11:20:58 +00:00
|
|
|
tables: { test: 'DEFINE TABLE test TYPE ANY SCHEMAFULL PERMISSIONS NONE' },
|
2023-07-29 18:47:25 +00:00
|
|
|
users: {},
|
2022-09-11 08:08:36 +00:00
|
|
|
}",
|
|
|
|
);
|
|
|
|
assert_eq!(tmp, val);
|
|
|
|
//
|
|
|
|
Ok(())
|
2022-07-28 21:40:38 +00:00
|
|
|
}
|
|
|
|
|
2023-08-20 01:07:15 +00:00
|
|
|
#[tokio::test]
|
|
|
|
async fn define_statement_table_foreigntable() -> Result<(), Error> {
|
|
|
|
let sql = "
|
|
|
|
DEFINE TABLE test SCHEMAFUL;
|
|
|
|
DEFINE TABLE view AS SELECT count() FROM test GROUP ALL;
|
|
|
|
INFO FOR DB;
|
|
|
|
INFO FOR TB test;
|
|
|
|
REMOVE TABLE view;
|
|
|
|
INFO FOR DB;
|
|
|
|
INFO FOR TB test;
|
|
|
|
";
|
2023-08-30 18:01:30 +00:00
|
|
|
let dbs = new_ds().await?;
|
2023-08-20 01:07: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(
|
|
|
|
"{
|
|
|
|
analyzers: {},
|
|
|
|
tokens: {},
|
|
|
|
functions: {},
|
2023-12-12 13:51:43 +00:00
|
|
|
models: {},
|
2023-08-20 01:07:15 +00:00
|
|
|
params: {},
|
|
|
|
scopes: {},
|
|
|
|
tables: {
|
2024-03-19 11:20:58 +00:00
|
|
|
test: 'DEFINE TABLE test TYPE ANY SCHEMAFULL PERMISSIONS NONE',
|
|
|
|
view: 'DEFINE TABLE view TYPE ANY SCHEMALESS AS SELECT count() FROM test GROUP ALL PERMISSIONS NONE',
|
2023-08-20 01:07:15 +00:00
|
|
|
},
|
|
|
|
users: {},
|
|
|
|
}",
|
|
|
|
);
|
|
|
|
assert_eq!(tmp, val);
|
|
|
|
//
|
|
|
|
let tmp = res.remove(0).result?;
|
|
|
|
let val = Value::parse(
|
|
|
|
"{
|
|
|
|
events: {},
|
|
|
|
fields: {},
|
2024-03-19 11:20:58 +00:00
|
|
|
tables: { view: 'DEFINE TABLE view TYPE ANY SCHEMALESS AS SELECT count() FROM test GROUP ALL PERMISSIONS NONE' },
|
2023-08-20 01:07:15 +00:00
|
|
|
indexes: {},
|
2023-09-11 19:35:32 +00:00
|
|
|
lives: {},
|
2023-08-20 01:07:15 +00:00
|
|
|
}",
|
|
|
|
);
|
|
|
|
assert_eq!(tmp, val);
|
|
|
|
//
|
|
|
|
let tmp = res.remove(0).result;
|
|
|
|
assert!(tmp.is_ok());
|
|
|
|
//
|
|
|
|
let tmp = res.remove(0).result?;
|
|
|
|
let val = Value::parse(
|
|
|
|
"{
|
|
|
|
analyzers: {},
|
|
|
|
tokens: {},
|
|
|
|
functions: {},
|
2023-12-12 13:51:43 +00:00
|
|
|
models: {},
|
2023-08-20 01:07:15 +00:00
|
|
|
params: {},
|
|
|
|
scopes: {},
|
|
|
|
tables: {
|
2024-03-19 11:20:58 +00:00
|
|
|
test: 'DEFINE TABLE test TYPE ANY SCHEMAFULL PERMISSIONS NONE',
|
2023-08-20 01:07:15 +00:00
|
|
|
},
|
|
|
|
users: {},
|
|
|
|
}",
|
|
|
|
);
|
|
|
|
assert_eq!(tmp, val);
|
|
|
|
//
|
|
|
|
let tmp = res.remove(0).result?;
|
|
|
|
let val = Value::parse(
|
|
|
|
"{
|
|
|
|
events: {},
|
|
|
|
fields: {},
|
|
|
|
tables: {},
|
|
|
|
indexes: {},
|
2023-09-11 19:35:32 +00:00
|
|
|
lives: {},
|
2023-08-20 01:07:15 +00:00
|
|
|
}",
|
|
|
|
);
|
|
|
|
assert_eq!(tmp, val);
|
|
|
|
//
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2022-07-28 21:40:38 +00:00
|
|
|
#[tokio::test]
|
|
|
|
async fn define_statement_event() -> Result<(), Error> {
|
|
|
|
let sql = "
|
|
|
|
DEFINE EVENT test ON user WHEN true THEN (
|
|
|
|
CREATE activity SET user = $this, value = $after.email, action = $event
|
|
|
|
);
|
|
|
|
DEFINE EVENT test ON TABLE user WHEN true THEN (
|
|
|
|
CREATE activity SET user = $this, value = $after.email, action = $event
|
|
|
|
);
|
|
|
|
INFO FOR TABLE user;
|
|
|
|
UPDATE user:test SET email = 'info@surrealdb.com', updated_at = time::now();
|
|
|
|
UPDATE user:test SET email = 'info@surrealdb.com', updated_at = time::now();
|
|
|
|
UPDATE user:test SET email = 'test@surrealdb.com', updated_at = time::now();
|
2023-01-08 17:11:35 +00:00
|
|
|
SELECT count() FROM activity GROUP ALL;
|
2022-07-28 21:40:38 +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?;
|
2022-07-28 21:40:38 +00:00
|
|
|
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?;
|
2024-03-12 15:43:56 +00:00
|
|
|
#[cfg(feature = "parser2")]
|
|
|
|
let val = Value::parse(
|
|
|
|
"{
|
|
|
|
events: { test: 'DEFINE EVENT test ON user WHEN true THEN (CREATE activity SET user = $this, `value` = $after.email, action = $event)' },
|
|
|
|
fields: {},
|
|
|
|
tables: {},
|
|
|
|
indexes: {},
|
|
|
|
lives: {},
|
|
|
|
}",
|
|
|
|
);
|
|
|
|
#[cfg(not(feature = "parser2"))]
|
2022-07-28 21:40:38 +00:00
|
|
|
let val = Value::parse(
|
|
|
|
"{
|
2023-05-31 12:35:29 +00:00
|
|
|
events: { test: 'DEFINE EVENT test ON user WHEN true THEN (CREATE activity SET user = $this, value = $after.email, action = $event)' },
|
|
|
|
fields: {},
|
|
|
|
tables: {},
|
|
|
|
indexes: {},
|
2023-09-11 19:35:32 +00:00
|
|
|
lives: {},
|
2022-07-28 21:40:38 +00:00
|
|
|
}",
|
|
|
|
);
|
|
|
|
assert_eq!(tmp, val);
|
|
|
|
//
|
|
|
|
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(
|
|
|
|
"[{
|
|
|
|
count: 3
|
|
|
|
}]",
|
|
|
|
);
|
|
|
|
assert_eq!(tmp, val);
|
|
|
|
//
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
async fn define_statement_event_when_event() -> Result<(), Error> {
|
|
|
|
let sql = "
|
|
|
|
DEFINE EVENT test ON user WHEN $event = 'CREATE' THEN (
|
|
|
|
CREATE activity SET user = $this, value = $after.email, action = $event
|
|
|
|
);
|
|
|
|
DEFINE EVENT test ON TABLE user WHEN $event = 'CREATE' THEN (
|
|
|
|
CREATE activity SET user = $this, value = $after.email, action = $event
|
|
|
|
);
|
|
|
|
INFO FOR TABLE user;
|
|
|
|
UPDATE user:test SET email = 'info@surrealdb.com', updated_at = time::now();
|
|
|
|
UPDATE user:test SET email = 'info@surrealdb.com', updated_at = time::now();
|
|
|
|
UPDATE user:test SET email = 'test@surrealdb.com', updated_at = time::now();
|
2023-01-08 17:11:35 +00:00
|
|
|
SELECT count() FROM activity GROUP ALL;
|
2022-07-28 21:40:38 +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?;
|
2022-07-28 21:40:38 +00:00
|
|
|
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?;
|
2024-03-12 15:43:56 +00:00
|
|
|
#[cfg(feature = "parser2")]
|
|
|
|
let val = Value::parse(
|
|
|
|
r#"{
|
|
|
|
events: { test: "DEFINE EVENT test ON user WHEN $event = 'CREATE' THEN (CREATE activity SET user = $this, `value` = $after.email, action = $event)" },
|
|
|
|
fields: {},
|
|
|
|
tables: {},
|
|
|
|
indexes: {},
|
|
|
|
lives: {},
|
|
|
|
}"#,
|
|
|
|
);
|
|
|
|
#[cfg(not(feature = "parser2"))]
|
2022-07-28 21:40:38 +00:00
|
|
|
let val = Value::parse(
|
2022-10-19 14:48:50 +00:00
|
|
|
r#"{
|
2023-05-31 12:35:29 +00:00
|
|
|
events: { test: "DEFINE EVENT test ON user WHEN $event = 'CREATE' THEN (CREATE activity SET user = $this, value = $after.email, action = $event)" },
|
|
|
|
fields: {},
|
|
|
|
tables: {},
|
|
|
|
indexes: {},
|
2023-09-11 19:35:32 +00:00
|
|
|
lives: {},
|
2022-10-19 14:48:50 +00:00
|
|
|
}"#,
|
2022-07-28 21:40:38 +00:00
|
|
|
);
|
|
|
|
assert_eq!(tmp, val);
|
|
|
|
//
|
|
|
|
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(
|
|
|
|
"[{
|
|
|
|
count: 1
|
|
|
|
}]",
|
|
|
|
);
|
|
|
|
assert_eq!(tmp, val);
|
|
|
|
//
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
async fn define_statement_event_when_logic() -> Result<(), Error> {
|
|
|
|
let sql = "
|
|
|
|
DEFINE EVENT test ON user WHEN $before.email != $after.email THEN (
|
|
|
|
CREATE activity SET user = $this, value = $after.email, action = $event
|
|
|
|
);
|
|
|
|
DEFINE EVENT test ON TABLE user WHEN $before.email != $after.email THEN (
|
|
|
|
CREATE activity SET user = $this, value = $after.email, action = $event
|
|
|
|
);
|
|
|
|
INFO FOR TABLE user;
|
|
|
|
UPDATE user:test SET email = 'info@surrealdb.com', updated_at = time::now();
|
|
|
|
UPDATE user:test SET email = 'info@surrealdb.com', updated_at = time::now();
|
|
|
|
UPDATE user:test SET email = 'test@surrealdb.com', updated_at = time::now();
|
2023-01-08 17:11:35 +00:00
|
|
|
SELECT count() FROM activity GROUP ALL;
|
2022-07-28 21:40:38 +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?;
|
2022-07-28 21:40:38 +00:00
|
|
|
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?;
|
2024-03-12 15:43:56 +00:00
|
|
|
#[cfg(feature = "parser2")]
|
|
|
|
let val = Value::parse(
|
|
|
|
"{
|
|
|
|
events: { test: 'DEFINE EVENT test ON user WHEN $before.email != $after.email THEN (CREATE activity SET user = $this, `value` = $after.email, action = $event)' },
|
|
|
|
fields: {},
|
|
|
|
tables: {},
|
|
|
|
indexes: {},
|
|
|
|
lives: {},
|
|
|
|
}",
|
|
|
|
);
|
|
|
|
#[cfg(not(feature = "parser2"))]
|
2022-07-28 21:40:38 +00:00
|
|
|
let val = Value::parse(
|
|
|
|
"{
|
2023-05-31 12:35:29 +00:00
|
|
|
events: { test: 'DEFINE EVENT test ON user WHEN $before.email != $after.email THEN (CREATE activity SET user = $this, value = $after.email, action = $event)' },
|
|
|
|
fields: {},
|
|
|
|
tables: {},
|
|
|
|
indexes: {},
|
2023-09-11 19:35:32 +00:00
|
|
|
lives: {},
|
2022-07-28 21:40:38 +00:00
|
|
|
}",
|
|
|
|
);
|
|
|
|
assert_eq!(tmp, val);
|
|
|
|
//
|
|
|
|
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(
|
|
|
|
"[{
|
|
|
|
count: 2
|
|
|
|
}]",
|
|
|
|
);
|
|
|
|
assert_eq!(tmp, val);
|
|
|
|
//
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
async fn define_statement_field() -> Result<(), Error> {
|
|
|
|
let sql = "
|
|
|
|
DEFINE FIELD test ON user;
|
|
|
|
DEFINE FIELD test ON TABLE user;
|
|
|
|
INFO FOR TABLE user;
|
|
|
|
";
|
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 21:40:38 +00:00
|
|
|
assert_eq!(res.len(), 3);
|
|
|
|
//
|
|
|
|
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-05-31 12:35:29 +00:00
|
|
|
events: {},
|
2023-12-06 21:30:18 +00:00
|
|
|
fields: { test: 'DEFINE FIELD test ON user PERMISSIONS FULL' },
|
2023-05-31 12:35:29 +00:00
|
|
|
tables: {},
|
|
|
|
indexes: {},
|
2023-09-11 19:35:32 +00:00
|
|
|
lives: {},
|
2022-07-28 21:40:38 +00:00
|
|
|
}",
|
|
|
|
);
|
|
|
|
assert_eq!(tmp, val);
|
|
|
|
//
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
async fn define_statement_field_type() -> Result<(), Error> {
|
|
|
|
let sql = "
|
|
|
|
DEFINE FIELD test ON user TYPE string;
|
|
|
|
DEFINE FIELD test ON TABLE user TYPE string;
|
|
|
|
INFO FOR TABLE user;
|
|
|
|
";
|
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 21:40:38 +00:00
|
|
|
assert_eq!(res.len(), 3);
|
|
|
|
//
|
|
|
|
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-05-31 12:35:29 +00:00
|
|
|
events: {},
|
2023-12-06 21:30:18 +00:00
|
|
|
fields: { test: 'DEFINE FIELD test ON user TYPE string PERMISSIONS FULL' },
|
2023-05-31 12:35:29 +00:00
|
|
|
tables: {},
|
|
|
|
indexes: {},
|
2023-09-11 19:35:32 +00:00
|
|
|
lives: {},
|
2022-07-28 21:40:38 +00:00
|
|
|
}",
|
|
|
|
);
|
|
|
|
assert_eq!(tmp, val);
|
|
|
|
//
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
async fn define_statement_field_value() -> Result<(), Error> {
|
|
|
|
let sql = "
|
|
|
|
DEFINE FIELD test ON user VALUE $value OR 'GBR';
|
|
|
|
DEFINE FIELD test ON TABLE user VALUE $value OR 'GBR';
|
|
|
|
INFO FOR TABLE user;
|
|
|
|
";
|
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 21:40:38 +00:00
|
|
|
assert_eq!(res.len(), 3);
|
|
|
|
//
|
|
|
|
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(
|
2022-10-19 14:48:50 +00:00
|
|
|
r#"{
|
2023-05-31 12:35:29 +00:00
|
|
|
events: {},
|
2023-12-06 21:30:18 +00:00
|
|
|
fields: { test: "DEFINE FIELD test ON user VALUE $value OR 'GBR' PERMISSIONS FULL" },
|
2023-05-31 12:35:29 +00:00
|
|
|
tables: {},
|
|
|
|
indexes: {},
|
2023-09-11 19:35:32 +00:00
|
|
|
lives: {},
|
2022-10-19 14:48:50 +00:00
|
|
|
}"#,
|
2022-07-28 21:40:38 +00:00
|
|
|
);
|
|
|
|
assert_eq!(tmp, val);
|
|
|
|
//
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
async fn define_statement_field_assert() -> Result<(), Error> {
|
|
|
|
let sql = "
|
|
|
|
DEFINE FIELD test ON user ASSERT $value != NONE AND $value = /[A-Z]{3}/;
|
|
|
|
DEFINE FIELD test ON TABLE user ASSERT $value != NONE AND $value = /[A-Z]{3}/;
|
|
|
|
INFO FOR TABLE user;
|
|
|
|
";
|
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 21:40:38 +00:00
|
|
|
assert_eq!(res.len(), 3);
|
|
|
|
//
|
|
|
|
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-05-31 12:35:29 +00:00
|
|
|
events: {},
|
2023-12-06 21:30:18 +00:00
|
|
|
fields: { test: 'DEFINE FIELD test ON user ASSERT $value != NONE AND $value = /[A-Z]{3}/ PERMISSIONS FULL' },
|
2023-05-31 12:35:29 +00:00
|
|
|
tables: {},
|
|
|
|
indexes: {},
|
2023-09-11 19:35:32 +00:00
|
|
|
lives: {},
|
2022-07-28 21:40:38 +00:00
|
|
|
}",
|
|
|
|
);
|
|
|
|
assert_eq!(tmp, val);
|
|
|
|
//
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
async fn define_statement_field_type_value_assert() -> Result<(), Error> {
|
|
|
|
let sql = "
|
|
|
|
DEFINE FIELD test ON user TYPE string VALUE $value OR 'GBR' ASSERT $value != NONE AND $value = /[A-Z]{3}/;
|
|
|
|
DEFINE FIELD test ON TABLE user TYPE string VALUE $value OR 'GBR' ASSERT $value != NONE AND $value = /[A-Z]{3}/;
|
|
|
|
INFO FOR TABLE user;
|
|
|
|
";
|
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 21:40:38 +00:00
|
|
|
assert_eq!(res.len(), 3);
|
|
|
|
//
|
|
|
|
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(
|
2022-10-19 14:48:50 +00:00
|
|
|
r#"{
|
2023-05-31 12:35:29 +00:00
|
|
|
events: {},
|
2023-12-06 21:30:18 +00:00
|
|
|
fields: { test: "DEFINE FIELD test ON user TYPE string VALUE $value OR 'GBR' ASSERT $value != NONE AND $value = /[A-Z]{3}/ PERMISSIONS FULL" },
|
2023-05-31 12:35:29 +00:00
|
|
|
tables: {},
|
|
|
|
indexes: {},
|
2023-09-11 19:35:32 +00:00
|
|
|
lives: {},
|
2022-10-19 14:48:50 +00:00
|
|
|
}"#,
|
2022-07-28 21:40:38 +00:00
|
|
|
);
|
|
|
|
assert_eq!(tmp, val);
|
|
|
|
//
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2024-01-22 07:24:14 +00:00
|
|
|
#[tokio::test]
|
|
|
|
async fn define_field_with_recursive_types() -> Result<(), Error> {
|
|
|
|
let sql = "
|
|
|
|
DEFINE TABLE bar;
|
|
|
|
// defining a type for the parent type should overwrite permissions for the child.
|
|
|
|
DEFINE FIELD foo.*.* ON bar TYPE number PERMISSIONS FOR UPDATE NONE;
|
|
|
|
// this should recursively define types for foo, foo.*, and foo.*.*
|
|
|
|
DEFINE FIELD foo ON bar TYPE array<float | array<bool>> | set<number>;
|
|
|
|
INFO FOR TABLE bar;
|
|
|
|
";
|
|
|
|
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(), 4);
|
|
|
|
res.remove(0).result.unwrap();
|
|
|
|
res.remove(0).result.unwrap();
|
|
|
|
res.remove(0).result.unwrap();
|
|
|
|
|
|
|
|
let tmp = res.remove(0).result?;
|
|
|
|
let val = Value::parse(
|
|
|
|
r#"{
|
|
|
|
events: {},
|
|
|
|
fields: {
|
|
|
|
"foo": "DEFINE FIELD foo ON bar TYPE array<float | array<bool>> | set<number> PERMISSIONS FULL",
|
|
|
|
"foo[*]": "DEFINE FIELD foo[*] ON bar TYPE float | array<bool> | number PERMISSIONS FULL",
|
|
|
|
"foo[*][*]": "DEFINE FIELD foo[*][*] ON bar TYPE bool PERMISSIONS FOR select, create, delete FULL, FOR update NONE"
|
|
|
|
},
|
|
|
|
indexes: {},
|
|
|
|
lives: {},
|
|
|
|
tables: {}
|
|
|
|
}"#,
|
|
|
|
);
|
|
|
|
assert_eq!(tmp, val);
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2022-08-25 13:49:33 +00:00
|
|
|
#[tokio::test]
|
|
|
|
async fn define_statement_index_single_simple() -> Result<(), Error> {
|
|
|
|
let sql = "
|
|
|
|
CREATE user:1 SET age = 23;
|
|
|
|
CREATE user:2 SET age = 10;
|
|
|
|
DEFINE INDEX test ON user FIELDS age;
|
|
|
|
DEFINE INDEX test ON user COLUMNS age;
|
|
|
|
INFO FOR TABLE user;
|
|
|
|
UPDATE user:1 SET age = 24;
|
|
|
|
UPDATE user:2 SET age = 11;
|
|
|
|
";
|
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-08-25 13:49:33 +00:00
|
|
|
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;
|
|
|
|
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-05-31 12:35:29 +00:00
|
|
|
events: {},
|
|
|
|
fields: {},
|
|
|
|
tables: {},
|
|
|
|
indexes: { test: 'DEFINE INDEX test ON user FIELDS age' },
|
2023-09-11 19:35:32 +00:00
|
|
|
lives: {},
|
2022-08-25 13:49:33 +00:00
|
|
|
}",
|
|
|
|
);
|
|
|
|
assert_eq!(tmp, val);
|
|
|
|
//
|
|
|
|
let tmp = res.remove(0).result?;
|
|
|
|
let val = Value::parse("[{ id: user:1, age: 24 }]");
|
|
|
|
assert_eq!(tmp, val);
|
|
|
|
//
|
|
|
|
let tmp = res.remove(0).result?;
|
|
|
|
let val = Value::parse("[{ id: user:2, age: 11 }]");
|
|
|
|
assert_eq!(tmp, val);
|
|
|
|
//
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2022-07-28 21:40:38 +00:00
|
|
|
#[tokio::test]
|
|
|
|
async fn define_statement_index_single() -> Result<(), Error> {
|
|
|
|
let sql = "
|
|
|
|
DEFINE INDEX test ON user FIELDS email;
|
|
|
|
DEFINE INDEX test ON user COLUMNS email;
|
|
|
|
INFO FOR TABLE user;
|
|
|
|
CREATE user:1 SET email = 'test@surrealdb.com';
|
|
|
|
CREATE user:2 SET email = 'test@surrealdb.com';
|
|
|
|
";
|
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 21:40:38 +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?;
|
|
|
|
let val = Value::parse(
|
|
|
|
"{
|
2023-05-31 12:35:29 +00:00
|
|
|
events: {},
|
|
|
|
fields: {},
|
|
|
|
tables: {},
|
|
|
|
indexes: { test: 'DEFINE INDEX test ON user FIELDS email' },
|
2023-09-11 19:35:32 +00:00
|
|
|
lives: {},
|
2022-07-28 21:40:38 +00:00
|
|
|
}",
|
|
|
|
);
|
|
|
|
assert_eq!(tmp, val);
|
|
|
|
//
|
|
|
|
let tmp = res.remove(0).result?;
|
|
|
|
let val = Value::parse("[{ id: user:1, email: 'test@surrealdb.com' }]");
|
|
|
|
assert_eq!(tmp, val);
|
|
|
|
//
|
|
|
|
let tmp = res.remove(0).result?;
|
|
|
|
let val = Value::parse("[{ id: user:2, email: 'test@surrealdb.com' }]");
|
|
|
|
assert_eq!(tmp, val);
|
|
|
|
//
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
async fn define_statement_index_multiple() -> Result<(), Error> {
|
|
|
|
let sql = "
|
|
|
|
DEFINE INDEX test ON user FIELDS account, email;
|
|
|
|
DEFINE INDEX test ON user COLUMNS account, email;
|
|
|
|
INFO FOR TABLE user;
|
|
|
|
CREATE user:1 SET account = 'apple', email = 'test@surrealdb.com';
|
|
|
|
CREATE user:2 SET account = 'tesla', email = 'test@surrealdb.com';
|
|
|
|
CREATE user:3 SET account = 'apple', email = 'test@surrealdb.com';
|
|
|
|
CREATE user:4 SET account = 'tesla', email = 'test@surrealdb.com';
|
|
|
|
";
|
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 21:40:38 +00:00
|
|
|
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-05-31 12:35:29 +00:00
|
|
|
events: {},
|
|
|
|
fields: {},
|
|
|
|
tables: {},
|
|
|
|
indexes: { test: 'DEFINE INDEX test ON user FIELDS account, email' },
|
2023-09-11 19:35:32 +00:00
|
|
|
lives: {},
|
2022-07-28 21:40:38 +00:00
|
|
|
}",
|
|
|
|
);
|
|
|
|
assert_eq!(tmp, val);
|
|
|
|
//
|
|
|
|
let tmp = res.remove(0).result?;
|
|
|
|
let val = Value::parse("[{ id: user:1, account: 'apple', email: 'test@surrealdb.com' }]");
|
|
|
|
assert_eq!(tmp, val);
|
|
|
|
//
|
|
|
|
let tmp = res.remove(0).result?;
|
|
|
|
let val = Value::parse("[{ id: user:2, account: 'tesla', email: 'test@surrealdb.com' }]");
|
|
|
|
assert_eq!(tmp, val);
|
|
|
|
//
|
|
|
|
let tmp = res.remove(0).result?;
|
|
|
|
let val = Value::parse("[{ id: user:3, account: 'apple', email: 'test@surrealdb.com' }]");
|
|
|
|
assert_eq!(tmp, val);
|
|
|
|
//
|
|
|
|
let tmp = res.remove(0).result?;
|
|
|
|
let val = Value::parse("[{ id: user:4, account: 'tesla', email: 'test@surrealdb.com' }]");
|
|
|
|
assert_eq!(tmp, val);
|
|
|
|
//
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
async fn define_statement_index_single_unique() -> Result<(), Error> {
|
|
|
|
let sql = "
|
|
|
|
DEFINE INDEX test ON user FIELDS email UNIQUE;
|
|
|
|
DEFINE INDEX test ON user COLUMNS email UNIQUE;
|
|
|
|
INFO FOR TABLE user;
|
|
|
|
CREATE user:1 SET email = 'test@surrealdb.com';
|
|
|
|
CREATE user:2 SET email = 'test@surrealdb.com';
|
2022-09-24 17:58:56 +00:00
|
|
|
DELETE user:1;
|
|
|
|
CREATE user:2 SET email = 'test@surrealdb.com';
|
2022-07-28 21:40:38 +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?;
|
2022-09-24 17:58:56 +00:00
|
|
|
assert_eq!(res.len(), 7);
|
2022-07-28 21:40:38 +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?;
|
|
|
|
let val = Value::parse(
|
|
|
|
"{
|
2023-05-31 12:35:29 +00:00
|
|
|
events: {},
|
|
|
|
fields: {},
|
|
|
|
tables: {},
|
|
|
|
indexes: { test: 'DEFINE INDEX test ON user FIELDS email UNIQUE' },
|
2023-09-11 19:35:32 +00:00
|
|
|
lives: {},
|
2022-07-28 21:40:38 +00:00
|
|
|
}",
|
|
|
|
);
|
|
|
|
assert_eq!(tmp, val);
|
|
|
|
//
|
|
|
|
let tmp = res.remove(0).result?;
|
|
|
|
let val = Value::parse("[{ id: user:1, email: 'test@surrealdb.com' }]");
|
|
|
|
assert_eq!(tmp, val);
|
|
|
|
//
|
|
|
|
let tmp = res.remove(0).result;
|
|
|
|
assert!(matches!(
|
|
|
|
tmp.err(),
|
2023-08-29 08:46:48 +00:00
|
|
|
Some(e) if e.to_string() == r#"Database index `test` already contains 'test@surrealdb.com', with record `user:1`"#
|
2022-07-28 21:40:38 +00:00
|
|
|
));
|
|
|
|
//
|
2022-09-24 17:58:56 +00:00
|
|
|
let tmp = res.remove(0).result;
|
|
|
|
assert!(tmp.is_ok());
|
|
|
|
//
|
|
|
|
let tmp = res.remove(0).result?;
|
|
|
|
let val = Value::parse("[{ id: user:2, email: 'test@surrealdb.com' }]");
|
|
|
|
assert_eq!(tmp, val);
|
|
|
|
//
|
2022-07-28 21:40:38 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
async fn define_statement_index_multiple_unique() -> Result<(), Error> {
|
|
|
|
let sql = "
|
|
|
|
DEFINE INDEX test ON user FIELDS account, email UNIQUE;
|
|
|
|
DEFINE INDEX test ON user COLUMNS account, email UNIQUE;
|
|
|
|
INFO FOR TABLE user;
|
|
|
|
CREATE user:1 SET account = 'apple', email = 'test@surrealdb.com';
|
|
|
|
CREATE user:2 SET account = 'tesla', email = 'test@surrealdb.com';
|
|
|
|
CREATE user:3 SET account = 'apple', email = 'test@surrealdb.com';
|
|
|
|
CREATE user:4 SET account = 'tesla', email = 'test@surrealdb.com';
|
2022-09-24 17:58:56 +00:00
|
|
|
DELETE user:1;
|
|
|
|
CREATE user:3 SET account = 'apple', email = 'test@surrealdb.com';
|
|
|
|
CREATE user:4 SET account = 'tesla', email = 'test@surrealdb.com';
|
|
|
|
DELETE user:2;
|
|
|
|
CREATE user:4 SET account = 'tesla', email = 'test@surrealdb.com';
|
2022-07-28 21:40:38 +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?;
|
2022-09-24 17:58:56 +00:00
|
|
|
assert_eq!(res.len(), 12);
|
2022-07-28 21:40:38 +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?;
|
|
|
|
let val = Value::parse(
|
|
|
|
"{
|
2023-05-31 12:35:29 +00:00
|
|
|
events: {},
|
|
|
|
fields: {},
|
|
|
|
tables: {},
|
|
|
|
indexes: { test: 'DEFINE INDEX test ON user FIELDS account, email UNIQUE' },
|
2023-09-11 19:35:32 +00:00
|
|
|
lives: {},
|
2022-07-28 21:40:38 +00:00
|
|
|
}",
|
|
|
|
);
|
|
|
|
assert_eq!(tmp, val);
|
|
|
|
//
|
|
|
|
let tmp = res.remove(0).result?;
|
|
|
|
let val = Value::parse("[{ id: user:1, account: 'apple', email: 'test@surrealdb.com' }]");
|
|
|
|
assert_eq!(tmp, val);
|
|
|
|
//
|
|
|
|
let tmp = res.remove(0).result?;
|
|
|
|
let val = Value::parse("[{ id: user:2, account: 'tesla', email: 'test@surrealdb.com' }]");
|
|
|
|
assert_eq!(tmp, val);
|
|
|
|
//
|
|
|
|
let tmp = res.remove(0).result;
|
|
|
|
assert!(matches!(
|
|
|
|
tmp.err(),
|
2023-08-29 08:46:48 +00:00
|
|
|
Some(e) if e.to_string() == r#"Database index `test` already contains ['apple', 'test@surrealdb.com'], with record `user:1`"#
|
2022-07-28 21:40:38 +00:00
|
|
|
));
|
|
|
|
//
|
|
|
|
let tmp = res.remove(0).result;
|
|
|
|
assert!(matches!(
|
|
|
|
tmp.err(),
|
2023-08-29 08:46:48 +00:00
|
|
|
Some(e) if e.to_string() == r#"Database index `test` already contains ['tesla', 'test@surrealdb.com'], with record `user:2`"#
|
2022-07-28 21:40:38 +00:00
|
|
|
));
|
|
|
|
//
|
2022-09-24 17:58:56 +00:00
|
|
|
let tmp = res.remove(0).result;
|
|
|
|
assert!(tmp.is_ok());
|
|
|
|
//
|
|
|
|
let tmp = res.remove(0).result?;
|
|
|
|
let val = Value::parse("[{ id: user:3, account: 'apple', email: 'test@surrealdb.com' }]");
|
|
|
|
assert_eq!(tmp, val);
|
|
|
|
//
|
|
|
|
let tmp = res.remove(0).result;
|
|
|
|
assert!(matches!(
|
|
|
|
tmp.err(),
|
2023-08-29 08:46:48 +00:00
|
|
|
Some(e) if e.to_string() == r#"Database index `test` already contains ['tesla', 'test@surrealdb.com'], with record `user:2`"#
|
2022-09-24 17:58:56 +00:00
|
|
|
));
|
|
|
|
//
|
|
|
|
let tmp = res.remove(0).result;
|
|
|
|
assert!(tmp.is_ok());
|
|
|
|
//
|
|
|
|
let tmp = res.remove(0).result?;
|
|
|
|
let val = Value::parse("[{ id: user:4, account: 'tesla', email: 'test@surrealdb.com' }]");
|
|
|
|
assert_eq!(tmp, val);
|
|
|
|
//
|
2022-07-28 21:40:38 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
2022-08-25 13:49:33 +00:00
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
async fn define_statement_index_single_unique_existing() -> Result<(), Error> {
|
|
|
|
let sql = "
|
|
|
|
CREATE user:1 SET email = 'info@surrealdb.com';
|
|
|
|
CREATE user:2 SET email = 'test@surrealdb.com';
|
|
|
|
CREATE user:3 SET email = 'test@surrealdb.com';
|
|
|
|
DEFINE INDEX test ON user FIELDS email UNIQUE;
|
|
|
|
DEFINE INDEX test ON user COLUMNS email UNIQUE;
|
|
|
|
INFO FOR TABLE user;
|
|
|
|
";
|
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-08-25 13:49:33 +00:00
|
|
|
assert_eq!(res.len(), 6);
|
|
|
|
//
|
2023-05-29 11:46:41 +00:00
|
|
|
for _ in 0..3 {
|
|
|
|
let tmp = res.remove(0).result;
|
|
|
|
assert!(tmp.is_ok());
|
|
|
|
}
|
2022-08-25 13:49:33 +00:00
|
|
|
//
|
|
|
|
let tmp = res.remove(0).result;
|
|
|
|
assert!(matches!(
|
|
|
|
tmp.err(),
|
2023-08-29 08:46:48 +00:00
|
|
|
Some(e) if e.to_string() == r#"Database index `test` already contains 'test@surrealdb.com', with record `user:2`"#
|
2022-08-25 13:49:33 +00:00
|
|
|
));
|
|
|
|
//
|
|
|
|
let tmp = res.remove(0).result;
|
|
|
|
assert!(matches!(
|
|
|
|
tmp.err(),
|
2023-08-29 08:46:48 +00:00
|
|
|
Some(e) if e.to_string() == r#"Database index `test` already contains 'test@surrealdb.com', with record `user:2`"#
|
2022-08-25 13:49:33 +00:00
|
|
|
));
|
|
|
|
//
|
|
|
|
let tmp = res.remove(0).result?;
|
|
|
|
let val = Value::parse(
|
|
|
|
"{
|
2023-05-31 12:35:29 +00:00
|
|
|
events: {},
|
|
|
|
fields: {},
|
|
|
|
tables: {},
|
|
|
|
indexes: {},
|
2023-09-11 19:35:32 +00:00
|
|
|
lives: {},
|
2022-08-25 13:49:33 +00:00
|
|
|
}",
|
|
|
|
);
|
|
|
|
assert_eq!(tmp, val);
|
|
|
|
//
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
async fn define_statement_index_multiple_unique_existing() -> Result<(), Error> {
|
|
|
|
let sql = "
|
|
|
|
CREATE user:1 SET account = 'apple', email = 'test@surrealdb.com';
|
|
|
|
CREATE user:2 SET account = 'tesla', email = 'test@surrealdb.com';
|
|
|
|
CREATE user:3 SET account = 'apple', email = 'test@surrealdb.com';
|
|
|
|
CREATE user:4 SET account = 'tesla', email = 'test@surrealdb.com';
|
|
|
|
DEFINE INDEX test ON user FIELDS account, email UNIQUE;
|
|
|
|
DEFINE INDEX test ON user COLUMNS account, email UNIQUE;
|
|
|
|
INFO FOR TABLE user;
|
|
|
|
";
|
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-08-25 13:49:33 +00:00
|
|
|
assert_eq!(res.len(), 7);
|
|
|
|
//
|
2023-05-29 11:46:41 +00:00
|
|
|
for _ in 0..4 {
|
|
|
|
let tmp = res.remove(0).result;
|
|
|
|
assert!(tmp.is_ok());
|
|
|
|
}
|
2022-08-25 13:49:33 +00:00
|
|
|
//
|
|
|
|
let tmp = res.remove(0).result;
|
|
|
|
assert!(matches!(
|
|
|
|
tmp.err(),
|
2023-08-29 08:46:48 +00:00
|
|
|
Some(e) if e.to_string() == r#"Database index `test` already contains ['apple', 'test@surrealdb.com'], with record `user:1`"#
|
2022-08-25 13:49:33 +00:00
|
|
|
));
|
|
|
|
//
|
|
|
|
let tmp = res.remove(0).result;
|
|
|
|
assert!(matches!(
|
|
|
|
tmp.err(),
|
2023-08-29 08:46:48 +00:00
|
|
|
Some(e) if e.to_string() == r#"Database index `test` already contains ['apple', 'test@surrealdb.com'], with record `user:1`"#
|
2022-08-25 13:49:33 +00:00
|
|
|
));
|
2023-07-05 13:30:44 +00:00
|
|
|
|
2022-08-25 13:49:33 +00:00
|
|
|
let tmp = res.remove(0).result?;
|
|
|
|
let val = Value::parse(
|
|
|
|
"{
|
2023-05-31 12:35:29 +00:00
|
|
|
events: {},
|
|
|
|
fields: {},
|
|
|
|
tables: {},
|
|
|
|
indexes: {},
|
2023-09-11 19:35:32 +00:00
|
|
|
lives: {},
|
2022-08-25 13:49:33 +00:00
|
|
|
}",
|
|
|
|
);
|
|
|
|
assert_eq!(tmp, val);
|
|
|
|
//
|
|
|
|
Ok(())
|
|
|
|
}
|
2023-05-10 02:08:09 +00:00
|
|
|
|
2023-07-06 16:46:49 +00:00
|
|
|
#[tokio::test]
|
|
|
|
async fn define_statement_index_single_unique_embedded_multiple() -> Result<(), Error> {
|
|
|
|
let sql = "
|
|
|
|
DEFINE INDEX test ON user FIELDS tags UNIQUE;
|
|
|
|
DEFINE INDEX test ON user COLUMNS tags UNIQUE;
|
|
|
|
INFO FOR TABLE user;
|
|
|
|
CREATE user:1 SET tags = ['one', 'two'];
|
|
|
|
CREATE user:2 SET tags = ['two', 'three'];
|
|
|
|
";
|
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-10-04 09:51:34 +00:00
|
|
|
let res = &mut dbs.execute(sql, &ses, None).await?;
|
2023-07-06 16:46:49 +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?;
|
|
|
|
let val = Value::parse(
|
|
|
|
"{
|
|
|
|
events: {},
|
|
|
|
fields: {},
|
|
|
|
tables: {},
|
|
|
|
indexes: { test: 'DEFINE INDEX test ON user FIELDS tags UNIQUE' },
|
2023-09-11 19:35:32 +00:00
|
|
|
lives: {},
|
2023-07-06 16:46:49 +00:00
|
|
|
}",
|
|
|
|
);
|
|
|
|
assert_eq!(tmp, val);
|
|
|
|
//
|
|
|
|
let tmp = res.remove(0).result?;
|
|
|
|
let val = Value::parse("[{ id: user:1, tags: ['one', 'two'] }]");
|
|
|
|
assert_eq!(tmp, val);
|
|
|
|
//
|
|
|
|
let tmp = res.remove(0).result;
|
2023-08-01 07:30:13 +00:00
|
|
|
if let Err(e) = tmp {
|
|
|
|
assert_eq!(
|
|
|
|
e.to_string(),
|
2023-08-29 08:46:48 +00:00
|
|
|
"Database index `test` already contains 'two', with record `user:1`"
|
2023-08-01 07:30:13 +00:00
|
|
|
);
|
|
|
|
} else {
|
|
|
|
panic!("An error was expected.")
|
|
|
|
}
|
2023-07-06 16:46:49 +00:00
|
|
|
//
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
async fn define_statement_index_multiple_unique_embedded_multiple() -> Result<(), Error> {
|
|
|
|
let sql = "
|
|
|
|
DEFINE INDEX test ON user FIELDS account, tags UNIQUE;
|
|
|
|
DEFINE INDEX test ON user COLUMNS account, tags UNIQUE;
|
|
|
|
INFO FOR TABLE user;
|
|
|
|
CREATE user:1 SET account = 'apple', tags = ['one', 'two'];
|
|
|
|
CREATE user:2 SET account = 'tesla', tags = ['one', 'two'];
|
|
|
|
CREATE user:3 SET account = 'apple', tags = ['two', 'three'];
|
|
|
|
CREATE user:4 SET account = 'tesla', tags = ['two', 'three'];
|
|
|
|
";
|
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-10-04 09:51:34 +00:00
|
|
|
let res = &mut dbs.execute(sql, &ses, None).await?;
|
2023-07-06 16:46:49 +00:00
|
|
|
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(
|
|
|
|
"{
|
|
|
|
events: {},
|
|
|
|
fields: {},
|
|
|
|
tables: {},
|
|
|
|
indexes: { test: 'DEFINE INDEX test ON user FIELDS account, tags UNIQUE' },
|
2023-09-11 19:35:32 +00:00
|
|
|
lives: {},
|
2023-07-06 16:46:49 +00:00
|
|
|
}",
|
|
|
|
);
|
|
|
|
assert_eq!(tmp, val);
|
|
|
|
//
|
|
|
|
let tmp = res.remove(0).result?;
|
|
|
|
let val = Value::parse("[{ id: user:1, account: 'apple', tags: ['one', 'two'] }]");
|
|
|
|
assert_eq!(tmp, val);
|
|
|
|
//
|
|
|
|
let tmp = res.remove(0).result?;
|
2023-08-01 07:30:13 +00:00
|
|
|
let val = Value::parse("[{ id: user:2, account: 'tesla', tags: ['one', 'two'] }]");
|
2023-07-06 16:46:49 +00:00
|
|
|
assert_eq!(tmp, val);
|
|
|
|
//
|
|
|
|
let tmp = res.remove(0).result;
|
2023-08-01 07:30:13 +00:00
|
|
|
if let Err(e) = tmp {
|
|
|
|
assert_eq!(
|
|
|
|
e.to_string(),
|
2023-08-29 08:46:48 +00:00
|
|
|
"Database index `test` already contains ['apple', 'two'], with record `user:1`"
|
2023-08-01 07:30:13 +00:00
|
|
|
);
|
|
|
|
} else {
|
|
|
|
panic!("An error was expected.")
|
|
|
|
}
|
2023-07-06 16:46:49 +00:00
|
|
|
//
|
|
|
|
let tmp = res.remove(0).result;
|
2023-08-01 07:30:13 +00:00
|
|
|
if let Err(e) = tmp {
|
|
|
|
assert_eq!(
|
|
|
|
e.to_string(),
|
2023-08-29 08:46:48 +00:00
|
|
|
"Database index `test` already contains ['tesla', 'two'], with record `user:2`"
|
2023-08-01 07:30:13 +00:00
|
|
|
);
|
|
|
|
} else {
|
|
|
|
panic!("An error was expected.")
|
|
|
|
}
|
2023-07-06 16:46:49 +00:00
|
|
|
//
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2023-05-10 02:08:09 +00:00
|
|
|
#[tokio::test]
|
2024-02-05 16:21:23 +00:00
|
|
|
#[cfg(feature = "sql2")]
|
2023-05-10 02:08:09 +00:00
|
|
|
async fn define_statement_analyzer() -> Result<(), Error> {
|
2023-11-20 18:36:21 +00:00
|
|
|
let sql = r#"
|
2023-06-19 18:41:13 +00:00
|
|
|
DEFINE ANALYZER english TOKENIZERS blank,class FILTERS lowercase,snowball(english);
|
2023-05-10 02:08:09 +00:00
|
|
|
DEFINE ANALYZER autocomplete FILTERS lowercase,edgengram(2,10);
|
2023-11-20 18:36:21 +00:00
|
|
|
DEFINE FUNCTION fn::stripHtml($html: string) {
|
|
|
|
RETURN string::replace($html, /<[^>]*>/, "");
|
|
|
|
};
|
|
|
|
DEFINE ANALYZER htmlAnalyzer FUNCTION fn::stripHtml TOKENIZERS blank,class;
|
2023-05-10 02:08:09 +00:00
|
|
|
INFO FOR DB;
|
2023-11-20 18:36:21 +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-11-20 18:36:21 +00:00
|
|
|
assert_eq!(res.len(), 5);
|
2023-05-10 02:08:09 +00:00
|
|
|
//
|
2023-11-20 18:36:21 +00:00
|
|
|
for _ in 0..4 {
|
|
|
|
let tmp = res.remove(0).result;
|
|
|
|
assert!(tmp.is_ok());
|
|
|
|
}
|
2023-05-10 02:08:09 +00:00
|
|
|
//
|
|
|
|
let tmp = res.remove(0).result?;
|
|
|
|
let val = Value::parse(
|
2023-11-20 18:36:21 +00:00
|
|
|
r#"{
|
2023-05-31 12:35:29 +00:00
|
|
|
analyzers: {
|
2023-05-10 02:08:09 +00:00
|
|
|
autocomplete: 'DEFINE ANALYZER autocomplete FILTERS LOWERCASE,EDGENGRAM(2,10)',
|
2023-06-19 18:41:13 +00:00
|
|
|
english: 'DEFINE ANALYZER english TOKENIZERS BLANK,CLASS FILTERS LOWERCASE,SNOWBALL(ENGLISH)',
|
2023-11-20 18:36:21 +00:00
|
|
|
htmlAnalyzer: 'DEFINE ANALYZER htmlAnalyzer FUNCTION fn::stripHtml TOKENIZERS BLANK,CLASS'
|
2023-05-10 02:08:09 +00:00
|
|
|
},
|
2023-05-31 12:35:29 +00:00
|
|
|
tokens: {},
|
2023-11-20 18:36:21 +00:00
|
|
|
functions: {
|
2023-12-06 21:30:18 +00:00
|
|
|
stripHtml: "DEFINE FUNCTION fn::stripHtml($html: string) { RETURN string::replace($html, /<[^>]*>/, ''); } PERMISSIONS FULL"
|
2023-11-20 18:36:21 +00:00
|
|
|
},
|
2023-12-12 13:51:43 +00:00
|
|
|
models: {},
|
2023-05-31 12:35:29 +00:00
|
|
|
params: {},
|
|
|
|
scopes: {},
|
2023-07-29 18:47:25 +00:00
|
|
|
tables: {},
|
|
|
|
users: {},
|
2023-11-20 18:36:21 +00:00
|
|
|
}"#,
|
2023-05-10 02:08:09 +00:00
|
|
|
);
|
2023-11-20 18:36:21 +00:00
|
|
|
assert_eq!(format!("{:#}", tmp), format!("{:#}", val));
|
2023-05-10 02:08:09 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
2023-05-29 11:46:41 +00:00
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
async fn define_statement_search_index() -> Result<(), Error> {
|
|
|
|
let sql = r#"
|
|
|
|
CREATE blog:1 SET title = 'Understanding SurrealQL and how it is different from PostgreSQL';
|
|
|
|
CREATE blog:3 SET title = 'This blog is going to be deleted';
|
2023-06-19 18:41:13 +00:00
|
|
|
DEFINE ANALYZER simple TOKENIZERS blank,class FILTERS lowercase;
|
|
|
|
DEFINE INDEX blog_title ON blog FIELDS title SEARCH ANALYZER simple BM25(1.2,0.75) HIGHLIGHTS;
|
2023-05-29 11:46:41 +00:00
|
|
|
CREATE blog:2 SET title = 'Behind the scenes of the exciting beta 9 release';
|
|
|
|
DELETE blog:3;
|
|
|
|
INFO FOR TABLE blog;
|
2023-06-19 18:41:13 +00:00
|
|
|
ANALYZE INDEX blog_title ON blog;
|
2023-05-29 11:46:41 +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-06-19 18:41:13 +00:00
|
|
|
assert_eq!(res.len(), 8);
|
2023-05-29 11:46:41 +00:00
|
|
|
//
|
2023-06-19 18:41:13 +00:00
|
|
|
for i in 0..6 {
|
2023-05-29 11:46:41 +00:00
|
|
|
let tmp = res.remove(0).result;
|
2023-06-19 18:41:13 +00:00
|
|
|
assert!(tmp.is_ok(), "{}", i);
|
2023-05-29 11:46:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
let tmp = res.remove(0).result?;
|
|
|
|
let val = Value::parse(
|
|
|
|
"{
|
2023-05-31 12:35:29 +00:00
|
|
|
events: {},
|
|
|
|
fields: {},
|
|
|
|
tables: {},
|
2023-09-12 20:26:03 +00:00
|
|
|
indexes: { blog_title: 'DEFINE INDEX blog_title ON blog FIELDS title \
|
|
|
|
SEARCH ANALYZER simple BM25(1.2,0.75) \
|
2023-12-13 13:37:24 +00:00
|
|
|
DOC_IDS_ORDER 100 DOC_LENGTHS_ORDER 100 POSTINGS_ORDER 100 TERMS_ORDER 100 \
|
|
|
|
DOC_IDS_CACHE 100 DOC_LENGTHS_CACHE 100 POSTINGS_CACHE 100 TERMS_CACHE 100 HIGHLIGHTS' },
|
2023-09-11 19:35:32 +00:00
|
|
|
lives: {},
|
2023-05-29 11:46:41 +00:00
|
|
|
}",
|
|
|
|
);
|
2023-12-13 13:37:24 +00:00
|
|
|
assert_eq!(format!("{:#}", tmp), format!("{:#}", val));
|
2023-06-19 18:41:13 +00:00
|
|
|
|
|
|
|
let tmp = res.remove(0).result?;
|
|
|
|
|
|
|
|
check_path(&tmp, &["doc_ids", "keys_count"], |v| assert_eq!(v, Value::from(2)));
|
|
|
|
check_path(&tmp, &["doc_ids", "max_depth"], |v| assert_eq!(v, Value::from(1)));
|
|
|
|
check_path(&tmp, &["doc_ids", "nodes_count"], |v| assert_eq!(v, Value::from(1)));
|
2023-08-17 18:03:46 +00:00
|
|
|
check_path(&tmp, &["doc_ids", "total_size"], |v| assert_eq!(v, Value::from(63)));
|
2023-06-19 18:41:13 +00:00
|
|
|
|
|
|
|
check_path(&tmp, &["doc_lengths", "keys_count"], |v| assert_eq!(v, Value::from(2)));
|
|
|
|
check_path(&tmp, &["doc_lengths", "max_depth"], |v| assert_eq!(v, Value::from(1)));
|
|
|
|
check_path(&tmp, &["doc_lengths", "nodes_count"], |v| assert_eq!(v, Value::from(1)));
|
2023-08-01 09:57:05 +00:00
|
|
|
check_path(&tmp, &["doc_lengths", "total_size"], |v| assert_eq!(v, Value::from(56)));
|
2023-06-19 18:41:13 +00:00
|
|
|
|
|
|
|
check_path(&tmp, &["postings", "keys_count"], |v| assert_eq!(v, Value::from(17)));
|
|
|
|
check_path(&tmp, &["postings", "max_depth"], |v| assert_eq!(v, Value::from(1)));
|
|
|
|
check_path(&tmp, &["postings", "nodes_count"], |v| assert_eq!(v, Value::from(1)));
|
|
|
|
check_path(&tmp, &["postings", "total_size"], |v| assert!(v > Value::from(150)));
|
|
|
|
|
|
|
|
check_path(&tmp, &["terms", "keys_count"], |v| assert_eq!(v, Value::from(17)));
|
|
|
|
check_path(&tmp, &["terms", "max_depth"], |v| assert_eq!(v, Value::from(1)));
|
|
|
|
check_path(&tmp, &["terms", "nodes_count"], |v| assert_eq!(v, Value::from(1)));
|
|
|
|
check_path(&tmp, &["terms", "total_size"], |v| assert!(v.gt(&Value::from(150))));
|
|
|
|
|
2023-05-29 11:46:41 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
2023-06-19 18:41:13 +00:00
|
|
|
|
2023-07-29 18:47:25 +00:00
|
|
|
#[tokio::test]
|
|
|
|
async fn define_statement_user_root() -> Result<(), Error> {
|
|
|
|
let sql = "
|
|
|
|
DEFINE USER test ON ROOT PASSWORD 'test';
|
|
|
|
|
|
|
|
INFO FOR ROOT;
|
|
|
|
";
|
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();
|
|
|
|
let res = &mut dbs.execute(sql, &ses, None).await?;
|
|
|
|
|
|
|
|
assert_eq!(res.len(), 2);
|
|
|
|
//
|
|
|
|
let tmp = res.remove(0).result;
|
|
|
|
|
|
|
|
assert!(tmp.is_ok());
|
|
|
|
//
|
|
|
|
let tmp = res.remove(0).result?;
|
|
|
|
let define_str = tmp.pick(&["users".into(), "test".into()]).to_string();
|
|
|
|
|
|
|
|
assert!(define_str
|
2023-10-04 09:51:34 +00:00
|
|
|
.strip_prefix('\"')
|
2023-07-29 18:47:25 +00:00
|
|
|
.unwrap()
|
|
|
|
.starts_with("DEFINE USER test ON ROOT PASSHASH '$argon2id$"));
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
async fn define_statement_user_ns() -> Result<(), Error> {
|
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();
|
|
|
|
|
|
|
|
// Create a NS user and retrieve it.
|
|
|
|
let sql = "
|
|
|
|
USE NS ns;
|
|
|
|
DEFINE USER test ON NS PASSWORD 'test';
|
2023-08-17 18:03:46 +00:00
|
|
|
|
2023-07-29 18:47:25 +00:00
|
|
|
INFO FOR USER test;
|
|
|
|
INFO FOR USER test ON NS;
|
|
|
|
INFO FOR USER test ON NAMESPACE;
|
|
|
|
INFO FOR USER test ON ROOT;
|
|
|
|
";
|
|
|
|
let res = &mut dbs.execute(sql, &ses, None).await?;
|
|
|
|
|
|
|
|
assert!(res[1].result.is_ok());
|
|
|
|
assert!(res[2].result.is_ok());
|
|
|
|
assert!(res[3].result.is_ok());
|
|
|
|
assert!(res[4].result.is_ok());
|
|
|
|
assert_eq!(
|
|
|
|
res[5].result.as_ref().unwrap_err().to_string(),
|
|
|
|
"The root user 'test' does not exist"
|
|
|
|
); // User doesn't exist at the NS level
|
|
|
|
|
|
|
|
assert!(res[2]
|
|
|
|
.result
|
|
|
|
.as_ref()
|
|
|
|
.unwrap()
|
|
|
|
.to_string()
|
|
|
|
.starts_with("\"DEFINE USER test ON NAMESPACE PASSHASH '$argon2id$"));
|
|
|
|
assert!(res[3]
|
|
|
|
.result
|
|
|
|
.as_ref()
|
|
|
|
.unwrap()
|
|
|
|
.to_string()
|
|
|
|
.starts_with("\"DEFINE USER test ON NAMESPACE PASSHASH '$argon2id$"));
|
|
|
|
assert!(res[4]
|
|
|
|
.result
|
|
|
|
.as_ref()
|
|
|
|
.unwrap()
|
|
|
|
.to_string()
|
|
|
|
.starts_with("\"DEFINE USER test ON NAMESPACE PASSHASH '$argon2id$"));
|
|
|
|
|
|
|
|
// If it tries to create a NS user without specifying a NS, it should fail
|
|
|
|
let sql = "
|
|
|
|
DEFINE USER test ON NS PASSWORD 'test';
|
|
|
|
";
|
|
|
|
let res = &mut dbs.execute(sql, &ses, None).await?;
|
|
|
|
|
|
|
|
assert!(res.remove(0).result.is_err());
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
async fn define_statement_user_db() -> Result<(), Error> {
|
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();
|
|
|
|
|
|
|
|
// Create a NS user and retrieve it.
|
|
|
|
let sql = "
|
|
|
|
USE NS ns;
|
|
|
|
USE DB db;
|
|
|
|
DEFINE USER test ON DB PASSWORD 'test';
|
2023-08-17 18:03:46 +00:00
|
|
|
|
2023-07-29 18:47:25 +00:00
|
|
|
INFO FOR USER test;
|
|
|
|
INFO FOR USER test ON DB;
|
|
|
|
INFO FOR USER test ON DATABASE;
|
|
|
|
INFO FOR USER test ON NS;
|
|
|
|
";
|
|
|
|
let res = &mut dbs.execute(sql, &ses, None).await?;
|
|
|
|
|
|
|
|
assert!(res[2].result.is_ok());
|
|
|
|
assert!(res[3].result.is_ok());
|
|
|
|
assert!(res[4].result.is_ok());
|
|
|
|
assert!(res[5].result.is_ok());
|
|
|
|
assert_eq!(
|
|
|
|
res[6].result.as_ref().unwrap_err().to_string(),
|
|
|
|
"The user 'test' does not exist in the namespace 'ns'"
|
|
|
|
); // User doesn't exist at the NS level
|
|
|
|
|
|
|
|
assert!(res[3]
|
|
|
|
.result
|
|
|
|
.as_ref()
|
|
|
|
.unwrap()
|
|
|
|
.to_string()
|
|
|
|
.starts_with("\"DEFINE USER test ON DATABASE PASSHASH '$argon2id$"));
|
|
|
|
assert!(res[4]
|
|
|
|
.result
|
|
|
|
.as_ref()
|
|
|
|
.unwrap()
|
|
|
|
.to_string()
|
|
|
|
.starts_with("\"DEFINE USER test ON DATABASE PASSHASH '$argon2id$"));
|
|
|
|
assert!(res[5]
|
|
|
|
.result
|
|
|
|
.as_ref()
|
|
|
|
.unwrap()
|
|
|
|
.to_string()
|
|
|
|
.starts_with("\"DEFINE USER test ON DATABASE PASSHASH '$argon2id$"));
|
|
|
|
|
|
|
|
// If it tries to create a NS user without specifying a NS, it should fail
|
|
|
|
let sql = "
|
|
|
|
DEFINE USER test ON DB PASSWORD 'test';
|
|
|
|
";
|
|
|
|
let res = &mut dbs.execute(sql, &ses, None).await?;
|
|
|
|
|
|
|
|
assert!(res.remove(0).result.is_err());
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2023-06-19 18:41:13 +00:00
|
|
|
fn check_path<F>(val: &Value, path: &[&str], check: F)
|
|
|
|
where
|
|
|
|
F: Fn(Value),
|
|
|
|
{
|
|
|
|
let part: Vec<Part> = path.iter().map(|p| Part::from(*p)).collect();
|
|
|
|
let res = val.walk(&part);
|
|
|
|
for (i, v) in res {
|
|
|
|
assert_eq!(Idiom(part.clone()), i);
|
|
|
|
check(v);
|
|
|
|
}
|
|
|
|
}
|
2023-07-29 18:47:25 +00:00
|
|
|
|
|
|
|
//
|
|
|
|
// Permissions
|
|
|
|
//
|
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
async fn permissions_checks_define_ns() {
|
|
|
|
let scenario = HashMap::from([
|
|
|
|
("prepare", ""),
|
|
|
|
("test", "DEFINE NAMESPACE NS"),
|
|
|
|
("check", "INFO FOR ROOT"),
|
|
|
|
]);
|
|
|
|
|
|
|
|
// Define the expected results for the check statement when the test statement succeeded and when it failed
|
|
|
|
let check_results = [
|
|
|
|
vec!["{ namespaces: { NS: 'DEFINE NAMESPACE NS' }, users: { } }"],
|
|
|
|
vec!["{ namespaces: { }, users: { } }"],
|
|
|
|
];
|
|
|
|
|
|
|
|
let test_cases = [
|
|
|
|
// Root level
|
|
|
|
((().into(), Role::Owner), ("NS", "DB"), true),
|
|
|
|
((().into(), Role::Editor), ("NS", "DB"), true),
|
|
|
|
((().into(), Role::Viewer), ("NS", "DB"), false),
|
|
|
|
// Namespace level
|
|
|
|
((("NS",).into(), Role::Owner), ("NS", "DB"), false),
|
|
|
|
((("NS",).into(), Role::Owner), ("OTHER_NS", "DB"), false),
|
|
|
|
((("NS",).into(), Role::Editor), ("NS", "DB"), false),
|
|
|
|
((("NS",).into(), Role::Editor), ("OTHER_NS", "DB"), false),
|
|
|
|
((("NS",).into(), Role::Viewer), ("NS", "DB"), false),
|
|
|
|
((("NS",).into(), Role::Viewer), ("OTHER_NS", "DB"), false),
|
|
|
|
// Database level
|
|
|
|
((("NS", "DB").into(), Role::Owner), ("NS", "DB"), false),
|
|
|
|
((("NS", "DB").into(), Role::Owner), ("NS", "OTHER_DB"), false),
|
|
|
|
((("NS", "DB").into(), Role::Owner), ("OTHER_NS", "DB"), false),
|
|
|
|
((("NS", "DB").into(), Role::Editor), ("NS", "DB"), false),
|
|
|
|
((("NS", "DB").into(), Role::Editor), ("NS", "OTHER_DB"), false),
|
|
|
|
((("NS", "DB").into(), Role::Editor), ("OTHER_NS", "DB"), false),
|
|
|
|
((("NS", "DB").into(), Role::Viewer), ("NS", "DB"), false),
|
|
|
|
((("NS", "DB").into(), Role::Viewer), ("NS", "OTHER_DB"), false),
|
|
|
|
((("NS", "DB").into(), Role::Viewer), ("OTHER_NS", "DB"), false),
|
|
|
|
];
|
|
|
|
|
|
|
|
let res = iam_check_cases(test_cases.iter(), &scenario, check_results).await;
|
|
|
|
assert!(res.is_ok(), "{}", res.unwrap_err());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
async fn permissions_checks_define_db() {
|
|
|
|
let scenario =
|
|
|
|
HashMap::from([("prepare", ""), ("test", "DEFINE DATABASE DB"), ("check", "INFO FOR NS")]);
|
|
|
|
|
|
|
|
// Define the expected results for the check statement when the test statement succeeded and when it failed
|
|
|
|
let check_results = [
|
2023-08-18 13:21:02 +00:00
|
|
|
vec!["{ databases: { DB: 'DEFINE DATABASE DB' }, tokens: { }, users: { } }"],
|
|
|
|
vec!["{ databases: { }, tokens: { }, users: { } }"],
|
2023-07-29 18:47:25 +00:00
|
|
|
];
|
|
|
|
|
|
|
|
let test_cases = [
|
|
|
|
// Root level
|
|
|
|
((().into(), Role::Owner), ("NS", "DB"), true),
|
|
|
|
((().into(), Role::Editor), ("NS", "DB"), true),
|
|
|
|
((().into(), Role::Viewer), ("NS", "DB"), false),
|
|
|
|
// Namespace level
|
|
|
|
((("NS",).into(), Role::Owner), ("NS", "DB"), true),
|
|
|
|
((("NS",).into(), Role::Owner), ("OTHER_NS", "DB"), false),
|
|
|
|
((("NS",).into(), Role::Editor), ("NS", "DB"), true),
|
|
|
|
((("NS",).into(), Role::Editor), ("OTHER_NS", "DB"), false),
|
|
|
|
((("NS",).into(), Role::Viewer), ("NS", "DB"), false),
|
|
|
|
((("NS",).into(), Role::Viewer), ("OTHER_NS", "DB"), false),
|
|
|
|
// Database level
|
|
|
|
((("NS", "DB").into(), Role::Owner), ("NS", "DB"), false),
|
|
|
|
((("NS", "DB").into(), Role::Owner), ("NS", "OTHER_DB"), false),
|
|
|
|
((("NS", "DB").into(), Role::Owner), ("OTHER_NS", "DB"), false),
|
|
|
|
((("NS", "DB").into(), Role::Editor), ("NS", "DB"), false),
|
|
|
|
((("NS", "DB").into(), Role::Editor), ("NS", "OTHER_DB"), false),
|
|
|
|
((("NS", "DB").into(), Role::Editor), ("OTHER_NS", "DB"), false),
|
|
|
|
((("NS", "DB").into(), Role::Viewer), ("NS", "DB"), false),
|
|
|
|
((("NS", "DB").into(), Role::Viewer), ("NS", "OTHER_DB"), false),
|
|
|
|
((("NS", "DB").into(), Role::Viewer), ("OTHER_NS", "DB"), false),
|
|
|
|
];
|
|
|
|
|
|
|
|
let res = iam_check_cases(test_cases.iter(), &scenario, check_results).await;
|
|
|
|
assert!(res.is_ok(), "{}", res.unwrap_err());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
async fn permissions_checks_define_function() {
|
|
|
|
let scenario = HashMap::from([
|
|
|
|
("prepare", ""),
|
|
|
|
("test", "DEFINE FUNCTION fn::greet() {RETURN \"Hello\";}"),
|
|
|
|
("check", "INFO FOR DB"),
|
|
|
|
]);
|
|
|
|
|
|
|
|
// Define the expected results for the check statement when the test statement succeeded and when it failed
|
|
|
|
let check_results = [
|
2023-12-12 13:51:43 +00:00
|
|
|
vec!["{ analyzers: { }, functions: { greet: \"DEFINE FUNCTION fn::greet() { RETURN 'Hello'; } PERMISSIONS FULL\" }, models: { }, params: { }, scopes: { }, tables: { }, tokens: { }, users: { } }"],
|
|
|
|
vec!["{ analyzers: { }, functions: { }, models: { }, params: { }, scopes: { }, tables: { }, tokens: { }, users: { } }"]
|
2023-07-29 18:47:25 +00:00
|
|
|
];
|
|
|
|
|
|
|
|
let test_cases = [
|
|
|
|
// Root level
|
|
|
|
((().into(), Role::Owner), ("NS", "DB"), true),
|
|
|
|
((().into(), Role::Editor), ("NS", "DB"), true),
|
|
|
|
((().into(), Role::Viewer), ("NS", "DB"), false),
|
|
|
|
// Namespace level
|
|
|
|
((("NS",).into(), Role::Owner), ("NS", "DB"), true),
|
|
|
|
((("NS",).into(), Role::Owner), ("OTHER_NS", "DB"), false),
|
|
|
|
((("NS",).into(), Role::Editor), ("NS", "DB"), true),
|
|
|
|
((("NS",).into(), Role::Editor), ("OTHER_NS", "DB"), false),
|
|
|
|
((("NS",).into(), Role::Viewer), ("NS", "DB"), false),
|
|
|
|
((("NS",).into(), Role::Viewer), ("OTHER_NS", "DB"), false),
|
|
|
|
// Database level
|
|
|
|
((("NS", "DB").into(), Role::Owner), ("NS", "DB"), true),
|
|
|
|
((("NS", "DB").into(), Role::Owner), ("NS", "OTHER_DB"), false),
|
|
|
|
((("NS", "DB").into(), Role::Owner), ("OTHER_NS", "DB"), false),
|
|
|
|
((("NS", "DB").into(), Role::Editor), ("NS", "DB"), true),
|
|
|
|
((("NS", "DB").into(), Role::Editor), ("NS", "OTHER_DB"), false),
|
|
|
|
((("NS", "DB").into(), Role::Editor), ("OTHER_NS", "DB"), false),
|
|
|
|
((("NS", "DB").into(), Role::Viewer), ("NS", "DB"), false),
|
|
|
|
((("NS", "DB").into(), Role::Viewer), ("NS", "OTHER_DB"), false),
|
|
|
|
((("NS", "DB").into(), Role::Viewer), ("OTHER_NS", "DB"), false),
|
|
|
|
];
|
|
|
|
|
|
|
|
let res = iam_check_cases(test_cases.iter(), &scenario, check_results).await;
|
|
|
|
assert!(res.is_ok(), "{}", res.unwrap_err());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
async fn permissions_checks_define_analyzer() {
|
|
|
|
let scenario = HashMap::from([
|
|
|
|
("prepare", ""),
|
|
|
|
("test", "DEFINE ANALYZER analyzer TOKENIZERS BLANK"),
|
|
|
|
("check", "INFO FOR DB"),
|
|
|
|
]);
|
|
|
|
|
|
|
|
// Define the expected results for the check statement when the test statement succeeded and when it failed
|
|
|
|
let check_results = [
|
2023-12-12 13:51:43 +00:00
|
|
|
vec!["{ analyzers: { analyzer: 'DEFINE ANALYZER analyzer TOKENIZERS BLANK' }, functions: { }, models: { }, params: { }, scopes: { }, tables: { }, tokens: { }, users: { } }"],
|
|
|
|
vec!["{ analyzers: { }, functions: { }, models: { }, params: { }, scopes: { }, tables: { }, tokens: { }, users: { } }"]
|
2023-07-29 18:47:25 +00:00
|
|
|
];
|
|
|
|
|
|
|
|
let test_cases = [
|
|
|
|
// Root level
|
|
|
|
((().into(), Role::Owner), ("NS", "DB"), true),
|
|
|
|
((().into(), Role::Editor), ("NS", "DB"), true),
|
|
|
|
((().into(), Role::Viewer), ("NS", "DB"), false),
|
|
|
|
// Namespace level
|
|
|
|
((("NS",).into(), Role::Owner), ("NS", "DB"), true),
|
|
|
|
((("NS",).into(), Role::Owner), ("OTHER_NS", "DB"), false),
|
|
|
|
((("NS",).into(), Role::Editor), ("NS", "DB"), true),
|
|
|
|
((("NS",).into(), Role::Editor), ("OTHER_NS", "DB"), false),
|
|
|
|
((("NS",).into(), Role::Viewer), ("NS", "DB"), false),
|
|
|
|
((("NS",).into(), Role::Viewer), ("OTHER_NS", "DB"), false),
|
|
|
|
// Database level
|
|
|
|
((("NS", "DB").into(), Role::Owner), ("NS", "DB"), true),
|
|
|
|
((("NS", "DB").into(), Role::Owner), ("NS", "OTHER_DB"), false),
|
|
|
|
((("NS", "DB").into(), Role::Owner), ("OTHER_NS", "DB"), false),
|
|
|
|
((("NS", "DB").into(), Role::Editor), ("NS", "DB"), true),
|
|
|
|
((("NS", "DB").into(), Role::Editor), ("NS", "OTHER_DB"), false),
|
|
|
|
((("NS", "DB").into(), Role::Editor), ("OTHER_NS", "DB"), false),
|
|
|
|
((("NS", "DB").into(), Role::Viewer), ("NS", "DB"), false),
|
|
|
|
((("NS", "DB").into(), Role::Viewer), ("NS", "OTHER_DB"), false),
|
|
|
|
((("NS", "DB").into(), Role::Viewer), ("OTHER_NS", "DB"), false),
|
|
|
|
];
|
|
|
|
|
|
|
|
let res = iam_check_cases(test_cases.iter(), &scenario, check_results).await;
|
|
|
|
assert!(res.is_ok(), "{}", res.unwrap_err());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
async fn permissions_checks_define_token_ns() {
|
|
|
|
let scenario = HashMap::from([
|
|
|
|
("prepare", ""),
|
|
|
|
("test", "DEFINE TOKEN token ON NS TYPE HS512 VALUE 'secret'"),
|
|
|
|
("check", "INFO FOR NS"),
|
|
|
|
]);
|
|
|
|
|
|
|
|
// Define the expected results for the check statement when the test statement succeeded and when it failed
|
|
|
|
let check_results = [
|
2023-08-18 13:21:02 +00:00
|
|
|
vec!["{ databases: { }, tokens: { token: \"DEFINE TOKEN token ON NAMESPACE TYPE HS512 VALUE 'secret'\" }, users: { } }"],
|
|
|
|
vec!["{ databases: { }, tokens: { }, users: { } }"]
|
2023-07-29 18:47:25 +00:00
|
|
|
];
|
|
|
|
|
|
|
|
let test_cases = [
|
|
|
|
// Root level
|
|
|
|
((().into(), Role::Owner), ("NS", "DB"), true),
|
|
|
|
((().into(), Role::Editor), ("NS", "DB"), false),
|
|
|
|
((().into(), Role::Viewer), ("NS", "DB"), false),
|
|
|
|
// Namespace level
|
|
|
|
((("NS",).into(), Role::Owner), ("NS", "DB"), true),
|
|
|
|
((("NS",).into(), Role::Owner), ("OTHER_NS", "DB"), false),
|
|
|
|
((("NS",).into(), Role::Editor), ("NS", "DB"), false),
|
|
|
|
((("NS",).into(), Role::Editor), ("OTHER_NS", "DB"), false),
|
|
|
|
((("NS",).into(), Role::Viewer), ("NS", "DB"), false),
|
|
|
|
((("NS",).into(), Role::Viewer), ("OTHER_NS", "DB"), false),
|
|
|
|
// Database level
|
|
|
|
((("NS", "DB").into(), Role::Owner), ("NS", "DB"), false),
|
|
|
|
((("NS", "DB").into(), Role::Owner), ("NS", "OTHER_DB"), false),
|
|
|
|
((("NS", "DB").into(), Role::Owner), ("OTHER_NS", "DB"), false),
|
|
|
|
((("NS", "DB").into(), Role::Editor), ("NS", "DB"), false),
|
|
|
|
((("NS", "DB").into(), Role::Editor), ("NS", "OTHER_DB"), false),
|
|
|
|
((("NS", "DB").into(), Role::Editor), ("OTHER_NS", "DB"), false),
|
|
|
|
((("NS", "DB").into(), Role::Viewer), ("NS", "DB"), false),
|
|
|
|
((("NS", "DB").into(), Role::Viewer), ("NS", "OTHER_DB"), false),
|
|
|
|
((("NS", "DB").into(), Role::Viewer), ("OTHER_NS", "DB"), false),
|
|
|
|
];
|
|
|
|
|
|
|
|
let res = iam_check_cases(test_cases.iter(), &scenario, check_results).await;
|
|
|
|
assert!(res.is_ok(), "{}", res.unwrap_err());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
async fn permissions_checks_define_token_db() {
|
|
|
|
let scenario = HashMap::from([
|
|
|
|
("prepare", ""),
|
|
|
|
("test", "DEFINE TOKEN token ON DB TYPE HS512 VALUE 'secret'"),
|
|
|
|
("check", "INFO FOR DB"),
|
|
|
|
]);
|
|
|
|
|
|
|
|
// Define the expected results for the check statement when the test statement succeeded and when it failed
|
|
|
|
let check_results = [
|
2023-12-12 13:51:43 +00:00
|
|
|
vec!["{ analyzers: { }, functions: { }, models: { }, params: { }, scopes: { }, tables: { }, tokens: { token: \"DEFINE TOKEN token ON DATABASE TYPE HS512 VALUE 'secret'\" }, users: { } }"],
|
|
|
|
vec!["{ analyzers: { }, functions: { }, models: { }, params: { }, scopes: { }, tables: { }, tokens: { }, users: { } }"]
|
2023-07-29 18:47:25 +00:00
|
|
|
];
|
|
|
|
|
|
|
|
let test_cases = [
|
|
|
|
// Root level
|
|
|
|
((().into(), Role::Owner), ("NS", "DB"), true),
|
|
|
|
((().into(), Role::Editor), ("NS", "DB"), false),
|
|
|
|
((().into(), Role::Viewer), ("NS", "DB"), false),
|
|
|
|
// Namespace level
|
|
|
|
((("NS",).into(), Role::Owner), ("NS", "DB"), true),
|
|
|
|
((("NS",).into(), Role::Owner), ("OTHER_NS", "DB"), false),
|
|
|
|
((("NS",).into(), Role::Editor), ("NS", "DB"), false),
|
|
|
|
((("NS",).into(), Role::Editor), ("OTHER_NS", "DB"), false),
|
|
|
|
((("NS",).into(), Role::Viewer), ("NS", "DB"), false),
|
|
|
|
((("NS",).into(), Role::Viewer), ("OTHER_NS", "DB"), false),
|
|
|
|
// Database level
|
|
|
|
((("NS", "DB").into(), Role::Owner), ("NS", "DB"), true),
|
|
|
|
((("NS", "DB").into(), Role::Owner), ("NS", "OTHER_DB"), false),
|
|
|
|
((("NS", "DB").into(), Role::Owner), ("OTHER_NS", "DB"), false),
|
|
|
|
((("NS", "DB").into(), Role::Editor), ("NS", "DB"), false),
|
|
|
|
((("NS", "DB").into(), Role::Editor), ("NS", "OTHER_DB"), false),
|
|
|
|
((("NS", "DB").into(), Role::Editor), ("OTHER_NS", "DB"), false),
|
|
|
|
((("NS", "DB").into(), Role::Viewer), ("NS", "DB"), false),
|
|
|
|
((("NS", "DB").into(), Role::Viewer), ("NS", "OTHER_DB"), false),
|
|
|
|
((("NS", "DB").into(), Role::Viewer), ("OTHER_NS", "DB"), false),
|
|
|
|
];
|
|
|
|
|
|
|
|
let res = iam_check_cases(test_cases.iter(), &scenario, check_results).await;
|
|
|
|
assert!(res.is_ok(), "{}", res.unwrap_err());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
async fn permissions_checks_define_user_root() {
|
|
|
|
let scenario = HashMap::from([
|
|
|
|
("prepare", ""),
|
|
|
|
("test", "DEFINE USER user ON ROOT PASSHASH 'secret' ROLES VIEWER"),
|
|
|
|
("check", "INFO FOR ROOT"),
|
|
|
|
]);
|
|
|
|
|
|
|
|
// Define the expected results for the check statement when the test statement succeeded and when it failed
|
|
|
|
let check_results = [
|
|
|
|
vec!["{ namespaces: { }, users: { user: \"DEFINE USER user ON ROOT PASSHASH 'secret' ROLES VIEWER\" } }"],
|
|
|
|
vec!["{ namespaces: { }, users: { } }"]
|
|
|
|
];
|
|
|
|
|
|
|
|
let test_cases = [
|
|
|
|
// Root level
|
|
|
|
((().into(), Role::Owner), ("NS", "DB"), true),
|
|
|
|
((().into(), Role::Editor), ("NS", "DB"), false),
|
|
|
|
((().into(), Role::Viewer), ("NS", "DB"), false),
|
|
|
|
// Namespace level
|
|
|
|
((("NS",).into(), Role::Owner), ("NS", "DB"), false),
|
|
|
|
((("NS",).into(), Role::Owner), ("OTHER_NS", "DB"), false),
|
|
|
|
((("NS",).into(), Role::Editor), ("NS", "DB"), false),
|
|
|
|
((("NS",).into(), Role::Editor), ("OTHER_NS", "DB"), false),
|
|
|
|
((("NS",).into(), Role::Viewer), ("NS", "DB"), false),
|
|
|
|
((("NS",).into(), Role::Viewer), ("OTHER_NS", "DB"), false),
|
|
|
|
// Database level
|
|
|
|
((("NS", "DB").into(), Role::Owner), ("NS", "DB"), false),
|
|
|
|
((("NS", "DB").into(), Role::Owner), ("NS", "OTHER_DB"), false),
|
|
|
|
((("NS", "DB").into(), Role::Owner), ("OTHER_NS", "DB"), false),
|
|
|
|
((("NS", "DB").into(), Role::Editor), ("NS", "DB"), false),
|
|
|
|
((("NS", "DB").into(), Role::Editor), ("NS", "OTHER_DB"), false),
|
|
|
|
((("NS", "DB").into(), Role::Editor), ("OTHER_NS", "DB"), false),
|
|
|
|
((("NS", "DB").into(), Role::Viewer), ("NS", "DB"), false),
|
|
|
|
((("NS", "DB").into(), Role::Viewer), ("NS", "OTHER_DB"), false),
|
|
|
|
((("NS", "DB").into(), Role::Viewer), ("OTHER_NS", "DB"), false),
|
|
|
|
];
|
|
|
|
|
|
|
|
let res = iam_check_cases(test_cases.iter(), &scenario, check_results).await;
|
|
|
|
assert!(res.is_ok(), "{}", res.unwrap_err());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
async fn permissions_checks_define_user_ns() {
|
|
|
|
let scenario = HashMap::from([
|
|
|
|
("prepare", ""),
|
|
|
|
("test", "DEFINE USER user ON NS PASSHASH 'secret' ROLES VIEWER"),
|
|
|
|
("check", "INFO FOR NS"),
|
|
|
|
]);
|
|
|
|
|
|
|
|
// Define the expected results for the check statement when the test statement succeeded and when it failed
|
|
|
|
let check_results = [
|
2023-08-18 13:21:02 +00:00
|
|
|
vec!["{ databases: { }, tokens: { }, users: { user: \"DEFINE USER user ON NAMESPACE PASSHASH 'secret' ROLES VIEWER\" } }"],
|
|
|
|
vec!["{ databases: { }, tokens: { }, users: { } }"]
|
2023-07-29 18:47:25 +00:00
|
|
|
];
|
|
|
|
|
|
|
|
let test_cases = [
|
|
|
|
// Root level
|
|
|
|
((().into(), Role::Owner), ("NS", "DB"), true),
|
|
|
|
((().into(), Role::Editor), ("NS", "DB"), false),
|
|
|
|
((().into(), Role::Viewer), ("NS", "DB"), false),
|
|
|
|
// Namespace level
|
|
|
|
((("NS",).into(), Role::Owner), ("NS", "DB"), true),
|
|
|
|
((("NS",).into(), Role::Owner), ("OTHER_NS", "DB"), false),
|
|
|
|
((("NS",).into(), Role::Editor), ("NS", "DB"), false),
|
|
|
|
((("NS",).into(), Role::Editor), ("OTHER_NS", "DB"), false),
|
|
|
|
((("NS",).into(), Role::Viewer), ("NS", "DB"), false),
|
|
|
|
((("NS",).into(), Role::Viewer), ("OTHER_NS", "DB"), false),
|
|
|
|
// Database level
|
|
|
|
((("NS", "DB").into(), Role::Owner), ("NS", "DB"), false),
|
|
|
|
((("NS", "DB").into(), Role::Owner), ("NS", "OTHER_DB"), false),
|
|
|
|
((("NS", "DB").into(), Role::Owner), ("OTHER_NS", "DB"), false),
|
|
|
|
((("NS", "DB").into(), Role::Editor), ("NS", "DB"), false),
|
|
|
|
((("NS", "DB").into(), Role::Editor), ("NS", "OTHER_DB"), false),
|
|
|
|
((("NS", "DB").into(), Role::Editor), ("OTHER_NS", "DB"), false),
|
|
|
|
((("NS", "DB").into(), Role::Viewer), ("NS", "DB"), false),
|
|
|
|
((("NS", "DB").into(), Role::Viewer), ("NS", "OTHER_DB"), false),
|
|
|
|
((("NS", "DB").into(), Role::Viewer), ("OTHER_NS", "DB"), false),
|
|
|
|
];
|
|
|
|
|
|
|
|
let res = iam_check_cases(test_cases.iter(), &scenario, check_results).await;
|
|
|
|
assert!(res.is_ok(), "{}", res.unwrap_err());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
async fn permissions_checks_define_user_db() {
|
|
|
|
let scenario = HashMap::from([
|
|
|
|
("prepare", ""),
|
|
|
|
("test", "DEFINE USER user ON DB PASSHASH 'secret' ROLES VIEWER"),
|
|
|
|
("check", "INFO FOR DB"),
|
|
|
|
]);
|
|
|
|
|
|
|
|
// Define the expected results for the check statement when the test statement succeeded and when it failed
|
|
|
|
let check_results = [
|
2023-12-12 13:51:43 +00:00
|
|
|
vec!["{ analyzers: { }, functions: { }, models: { }, params: { }, scopes: { }, tables: { }, tokens: { }, users: { user: \"DEFINE USER user ON DATABASE PASSHASH 'secret' ROLES VIEWER\" } }"],
|
|
|
|
vec!["{ analyzers: { }, functions: { }, models: { }, params: { }, scopes: { }, tables: { }, tokens: { }, users: { } }"]
|
2023-07-29 18:47:25 +00:00
|
|
|
];
|
|
|
|
|
|
|
|
let test_cases = [
|
|
|
|
// Root level
|
|
|
|
((().into(), Role::Owner), ("NS", "DB"), true),
|
|
|
|
((().into(), Role::Editor), ("NS", "DB"), false),
|
|
|
|
((().into(), Role::Viewer), ("NS", "DB"), false),
|
|
|
|
// Namespace level
|
|
|
|
((("NS",).into(), Role::Owner), ("NS", "DB"), true),
|
|
|
|
((("NS",).into(), Role::Owner), ("OTHER_NS", "DB"), false),
|
|
|
|
((("NS",).into(), Role::Editor), ("NS", "DB"), false),
|
|
|
|
((("NS",).into(), Role::Editor), ("OTHER_NS", "DB"), false),
|
|
|
|
((("NS",).into(), Role::Viewer), ("NS", "DB"), false),
|
|
|
|
((("NS",).into(), Role::Viewer), ("OTHER_NS", "DB"), false),
|
|
|
|
// Database level
|
|
|
|
((("NS", "DB").into(), Role::Owner), ("NS", "DB"), true),
|
|
|
|
((("NS", "DB").into(), Role::Owner), ("NS", "OTHER_DB"), false),
|
|
|
|
((("NS", "DB").into(), Role::Owner), ("OTHER_NS", "DB"), false),
|
|
|
|
((("NS", "DB").into(), Role::Editor), ("NS", "DB"), false),
|
|
|
|
((("NS", "DB").into(), Role::Editor), ("NS", "OTHER_DB"), false),
|
|
|
|
((("NS", "DB").into(), Role::Editor), ("OTHER_NS", "DB"), false),
|
|
|
|
((("NS", "DB").into(), Role::Viewer), ("NS", "DB"), false),
|
|
|
|
((("NS", "DB").into(), Role::Viewer), ("NS", "OTHER_DB"), false),
|
|
|
|
((("NS", "DB").into(), Role::Viewer), ("OTHER_NS", "DB"), false),
|
|
|
|
];
|
|
|
|
|
|
|
|
let res = iam_check_cases(test_cases.iter(), &scenario, check_results).await;
|
|
|
|
assert!(res.is_ok(), "{}", res.unwrap_err());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
async fn permissions_checks_define_scope() {
|
|
|
|
let scenario = HashMap::from([
|
|
|
|
("prepare", ""),
|
|
|
|
("test", "DEFINE SCOPE account SESSION 1h;"),
|
|
|
|
("check", "INFO FOR DB"),
|
|
|
|
]);
|
|
|
|
|
|
|
|
// Define the expected results for the check statement when the test statement succeeded and when it failed
|
|
|
|
let check_results = [
|
2023-12-12 13:51:43 +00:00
|
|
|
vec!["{ analyzers: { }, functions: { }, models: { }, params: { }, scopes: { account: 'DEFINE SCOPE account SESSION 1h' }, tables: { }, tokens: { }, users: { } }"],
|
|
|
|
vec!["{ analyzers: { }, functions: { }, models: { }, params: { }, scopes: { }, tables: { }, tokens: { }, users: { } }"]
|
2023-07-29 18:47:25 +00:00
|
|
|
];
|
|
|
|
|
|
|
|
let test_cases = [
|
|
|
|
// Root level
|
|
|
|
((().into(), Role::Owner), ("NS", "DB"), true),
|
|
|
|
((().into(), Role::Editor), ("NS", "DB"), true),
|
|
|
|
((().into(), Role::Viewer), ("NS", "DB"), false),
|
|
|
|
// Namespace level
|
|
|
|
((("NS",).into(), Role::Owner), ("NS", "DB"), true),
|
|
|
|
((("NS",).into(), Role::Owner), ("OTHER_NS", "DB"), false),
|
|
|
|
((("NS",).into(), Role::Editor), ("NS", "DB"), true),
|
|
|
|
((("NS",).into(), Role::Editor), ("OTHER_NS", "DB"), false),
|
|
|
|
((("NS",).into(), Role::Viewer), ("NS", "DB"), false),
|
|
|
|
((("NS",).into(), Role::Viewer), ("OTHER_NS", "DB"), false),
|
|
|
|
// Database level
|
|
|
|
((("NS", "DB").into(), Role::Owner), ("NS", "DB"), true),
|
|
|
|
((("NS", "DB").into(), Role::Owner), ("NS", "OTHER_DB"), false),
|
|
|
|
((("NS", "DB").into(), Role::Owner), ("OTHER_NS", "DB"), false),
|
|
|
|
((("NS", "DB").into(), Role::Editor), ("NS", "DB"), true),
|
|
|
|
((("NS", "DB").into(), Role::Editor), ("NS", "OTHER_DB"), false),
|
|
|
|
((("NS", "DB").into(), Role::Editor), ("OTHER_NS", "DB"), false),
|
|
|
|
((("NS", "DB").into(), Role::Viewer), ("NS", "DB"), false),
|
|
|
|
((("NS", "DB").into(), Role::Viewer), ("NS", "OTHER_DB"), false),
|
|
|
|
((("NS", "DB").into(), Role::Viewer), ("OTHER_NS", "DB"), false),
|
|
|
|
];
|
|
|
|
|
|
|
|
let res = iam_check_cases(test_cases.iter(), &scenario, check_results).await;
|
|
|
|
assert!(res.is_ok(), "{}", res.unwrap_err());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
async fn permissions_checks_define_param() {
|
|
|
|
let scenario = HashMap::from([
|
|
|
|
("prepare", ""),
|
|
|
|
("test", "DEFINE PARAM $param VALUE 'foo'"),
|
|
|
|
("check", "INFO FOR DB"),
|
|
|
|
]);
|
|
|
|
|
|
|
|
// Define the expected results for the check statement when the test statement succeeded and when it failed
|
|
|
|
let check_results = [
|
2023-12-12 13:51:43 +00:00
|
|
|
vec!["{ analyzers: { }, functions: { }, models: { }, params: { param: \"DEFINE PARAM $param VALUE 'foo' PERMISSIONS FULL\" }, scopes: { }, tables: { }, tokens: { }, users: { } }"],
|
|
|
|
vec!["{ analyzers: { }, functions: { }, models: { }, params: { }, scopes: { }, tables: { }, tokens: { }, users: { } }"]
|
2023-07-29 18:47:25 +00:00
|
|
|
];
|
|
|
|
|
|
|
|
let test_cases = [
|
|
|
|
// Root level
|
|
|
|
((().into(), Role::Owner), ("NS", "DB"), true),
|
|
|
|
((().into(), Role::Editor), ("NS", "DB"), true),
|
|
|
|
((().into(), Role::Viewer), ("NS", "DB"), false),
|
|
|
|
// Namespace level
|
|
|
|
((("NS",).into(), Role::Owner), ("NS", "DB"), true),
|
|
|
|
((("NS",).into(), Role::Owner), ("OTHER_NS", "DB"), false),
|
|
|
|
((("NS",).into(), Role::Editor), ("NS", "DB"), true),
|
|
|
|
((("NS",).into(), Role::Editor), ("OTHER_NS", "DB"), false),
|
|
|
|
((("NS",).into(), Role::Viewer), ("NS", "DB"), false),
|
|
|
|
((("NS",).into(), Role::Viewer), ("OTHER_NS", "DB"), false),
|
|
|
|
// Database level
|
|
|
|
((("NS", "DB").into(), Role::Owner), ("NS", "DB"), true),
|
|
|
|
((("NS", "DB").into(), Role::Owner), ("NS", "OTHER_DB"), false),
|
|
|
|
((("NS", "DB").into(), Role::Owner), ("OTHER_NS", "DB"), false),
|
|
|
|
((("NS", "DB").into(), Role::Editor), ("NS", "DB"), true),
|
|
|
|
((("NS", "DB").into(), Role::Editor), ("NS", "OTHER_DB"), false),
|
|
|
|
((("NS", "DB").into(), Role::Editor), ("OTHER_NS", "DB"), false),
|
|
|
|
((("NS", "DB").into(), Role::Viewer), ("NS", "DB"), false),
|
|
|
|
((("NS", "DB").into(), Role::Viewer), ("NS", "OTHER_DB"), false),
|
|
|
|
((("NS", "DB").into(), Role::Viewer), ("OTHER_NS", "DB"), false),
|
|
|
|
];
|
|
|
|
|
|
|
|
let res = iam_check_cases(test_cases.iter(), &scenario, check_results).await;
|
|
|
|
assert!(res.is_ok(), "{}", res.unwrap_err());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
async fn permissions_checks_define_table() {
|
|
|
|
let scenario =
|
|
|
|
HashMap::from([("prepare", ""), ("test", "DEFINE TABLE TB"), ("check", "INFO FOR DB")]);
|
|
|
|
|
|
|
|
// Define the expected results for the check statement when the test statement succeeded and when it failed
|
|
|
|
let check_results = [
|
2024-03-19 11:20:58 +00:00
|
|
|
vec!["{ analyzers: { }, functions: { }, models: { }, params: { }, scopes: { }, tables: { TB: 'DEFINE TABLE TB TYPE ANY SCHEMALESS PERMISSIONS NONE' }, tokens: { }, users: { } }"],
|
2023-12-12 13:51:43 +00:00
|
|
|
vec!["{ analyzers: { }, functions: { }, models: { }, params: { }, scopes: { }, tables: { }, tokens: { }, users: { } }"]
|
2023-07-29 18:47:25 +00:00
|
|
|
];
|
|
|
|
|
|
|
|
let test_cases = [
|
|
|
|
// Root level
|
|
|
|
((().into(), Role::Owner), ("NS", "DB"), true),
|
|
|
|
((().into(), Role::Editor), ("NS", "DB"), true),
|
|
|
|
((().into(), Role::Viewer), ("NS", "DB"), false),
|
|
|
|
// Namespace level
|
|
|
|
((("NS",).into(), Role::Owner), ("NS", "DB"), true),
|
|
|
|
((("NS",).into(), Role::Owner), ("OTHER_NS", "DB"), false),
|
|
|
|
((("NS",).into(), Role::Editor), ("NS", "DB"), true),
|
|
|
|
((("NS",).into(), Role::Editor), ("OTHER_NS", "DB"), false),
|
|
|
|
((("NS",).into(), Role::Viewer), ("NS", "DB"), false),
|
|
|
|
((("NS",).into(), Role::Viewer), ("OTHER_NS", "DB"), false),
|
|
|
|
// Database level
|
|
|
|
((("NS", "DB").into(), Role::Owner), ("NS", "DB"), true),
|
|
|
|
((("NS", "DB").into(), Role::Owner), ("NS", "OTHER_DB"), false),
|
|
|
|
((("NS", "DB").into(), Role::Owner), ("OTHER_NS", "DB"), false),
|
|
|
|
((("NS", "DB").into(), Role::Editor), ("NS", "DB"), true),
|
|
|
|
((("NS", "DB").into(), Role::Editor), ("NS", "OTHER_DB"), false),
|
|
|
|
((("NS", "DB").into(), Role::Editor), ("OTHER_NS", "DB"), false),
|
|
|
|
((("NS", "DB").into(), Role::Viewer), ("NS", "DB"), false),
|
|
|
|
((("NS", "DB").into(), Role::Viewer), ("NS", "OTHER_DB"), false),
|
|
|
|
((("NS", "DB").into(), Role::Viewer), ("OTHER_NS", "DB"), false),
|
|
|
|
];
|
|
|
|
|
|
|
|
let res = iam_check_cases(test_cases.iter(), &scenario, check_results).await;
|
|
|
|
assert!(res.is_ok(), "{}", res.unwrap_err());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
async fn permissions_checks_define_event() {
|
|
|
|
let scenario = HashMap::from([
|
|
|
|
("prepare", ""),
|
|
|
|
("test", "DEFINE EVENT event ON TABLE TB WHEN true THEN RETURN 'foo'"),
|
|
|
|
("check", "INFO FOR TABLE TB"),
|
|
|
|
]);
|
|
|
|
|
|
|
|
// Define the expected results for the check statement when the test statement succeeded and when it failed
|
|
|
|
let check_results = [
|
2023-09-11 19:35:32 +00:00
|
|
|
vec!["{ events: { event: \"DEFINE EVENT event ON TB WHEN true THEN (RETURN 'foo')\" }, fields: { }, indexes: { }, lives: { }, tables: { } }"],
|
|
|
|
vec!["{ events: { }, fields: { }, indexes: { }, lives: { }, tables: { } }"]
|
2023-07-29 18:47:25 +00:00
|
|
|
];
|
|
|
|
|
|
|
|
let test_cases = [
|
|
|
|
// Root level
|
|
|
|
((().into(), Role::Owner), ("NS", "DB"), true),
|
|
|
|
((().into(), Role::Editor), ("NS", "DB"), true),
|
|
|
|
((().into(), Role::Viewer), ("NS", "DB"), false),
|
|
|
|
// Namespace level
|
|
|
|
((("NS",).into(), Role::Owner), ("NS", "DB"), true),
|
|
|
|
((("NS",).into(), Role::Owner), ("OTHER_NS", "DB"), false),
|
|
|
|
((("NS",).into(), Role::Editor), ("NS", "DB"), true),
|
|
|
|
((("NS",).into(), Role::Editor), ("OTHER_NS", "DB"), false),
|
|
|
|
((("NS",).into(), Role::Viewer), ("NS", "DB"), false),
|
|
|
|
((("NS",).into(), Role::Viewer), ("OTHER_NS", "DB"), false),
|
|
|
|
// Database level
|
|
|
|
((("NS", "DB").into(), Role::Owner), ("NS", "DB"), true),
|
|
|
|
((("NS", "DB").into(), Role::Owner), ("NS", "OTHER_DB"), false),
|
|
|
|
((("NS", "DB").into(), Role::Owner), ("OTHER_NS", "DB"), false),
|
|
|
|
((("NS", "DB").into(), Role::Editor), ("NS", "DB"), true),
|
|
|
|
((("NS", "DB").into(), Role::Editor), ("NS", "OTHER_DB"), false),
|
|
|
|
((("NS", "DB").into(), Role::Editor), ("OTHER_NS", "DB"), false),
|
|
|
|
((("NS", "DB").into(), Role::Viewer), ("NS", "DB"), false),
|
|
|
|
((("NS", "DB").into(), Role::Viewer), ("NS", "OTHER_DB"), false),
|
|
|
|
((("NS", "DB").into(), Role::Viewer), ("OTHER_NS", "DB"), false),
|
|
|
|
];
|
|
|
|
|
|
|
|
let res = iam_check_cases(test_cases.iter(), &scenario, check_results).await;
|
|
|
|
assert!(res.is_ok(), "{}", res.unwrap_err());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
async fn permissions_checks_define_field() {
|
|
|
|
let scenario = HashMap::from([
|
|
|
|
("prepare", ""),
|
|
|
|
("test", "DEFINE FIELD field ON TABLE TB"),
|
|
|
|
("check", "INFO FOR TABLE TB"),
|
|
|
|
]);
|
|
|
|
|
|
|
|
// Define the expected results for the check statement when the test statement succeeded and when it failed
|
|
|
|
let check_results = [
|
2023-12-06 21:30:18 +00:00
|
|
|
vec!["{ events: { }, fields: { field: 'DEFINE FIELD field ON TB PERMISSIONS FULL' }, indexes: { }, lives: { }, tables: { } }"],
|
2023-09-11 19:35:32 +00:00
|
|
|
vec!["{ events: { }, fields: { }, indexes: { }, lives: { }, tables: { } }"]
|
2023-07-29 18:47:25 +00:00
|
|
|
];
|
|
|
|
|
|
|
|
let test_cases = [
|
|
|
|
// Root level
|
|
|
|
((().into(), Role::Owner), ("NS", "DB"), true),
|
|
|
|
((().into(), Role::Editor), ("NS", "DB"), true),
|
|
|
|
((().into(), Role::Viewer), ("NS", "DB"), false),
|
|
|
|
// Namespace level
|
|
|
|
((("NS",).into(), Role::Owner), ("NS", "DB"), true),
|
|
|
|
((("NS",).into(), Role::Owner), ("OTHER_NS", "DB"), false),
|
|
|
|
((("NS",).into(), Role::Editor), ("NS", "DB"), true),
|
|
|
|
((("NS",).into(), Role::Editor), ("OTHER_NS", "DB"), false),
|
|
|
|
((("NS",).into(), Role::Viewer), ("NS", "DB"), false),
|
|
|
|
((("NS",).into(), Role::Viewer), ("OTHER_NS", "DB"), false),
|
|
|
|
// Database level
|
|
|
|
((("NS", "DB").into(), Role::Owner), ("NS", "DB"), true),
|
|
|
|
((("NS", "DB").into(), Role::Owner), ("NS", "OTHER_DB"), false),
|
|
|
|
((("NS", "DB").into(), Role::Owner), ("OTHER_NS", "DB"), false),
|
|
|
|
((("NS", "DB").into(), Role::Editor), ("NS", "DB"), true),
|
|
|
|
((("NS", "DB").into(), Role::Editor), ("NS", "OTHER_DB"), false),
|
|
|
|
((("NS", "DB").into(), Role::Editor), ("OTHER_NS", "DB"), false),
|
|
|
|
((("NS", "DB").into(), Role::Viewer), ("NS", "DB"), false),
|
|
|
|
((("NS", "DB").into(), Role::Viewer), ("NS", "OTHER_DB"), false),
|
|
|
|
((("NS", "DB").into(), Role::Viewer), ("OTHER_NS", "DB"), false),
|
|
|
|
];
|
|
|
|
|
|
|
|
let res = iam_check_cases(test_cases.iter(), &scenario, check_results).await;
|
|
|
|
assert!(res.is_ok(), "{}", res.unwrap_err());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
async fn permissions_checks_define_index() {
|
|
|
|
let scenario = HashMap::from([
|
|
|
|
("prepare", ""),
|
|
|
|
("test", "DEFINE INDEX index ON TABLE TB FIELDS field"),
|
|
|
|
("check", "INFO FOR TABLE TB"),
|
|
|
|
]);
|
|
|
|
|
|
|
|
// Define the expected results for the check statement when the test statement succeeded and when it failed
|
|
|
|
let check_results = [
|
2023-09-11 19:35:32 +00:00
|
|
|
vec!["{ events: { }, fields: { }, indexes: { index: 'DEFINE INDEX index ON TB FIELDS field' }, lives: { }, tables: { } }"],
|
|
|
|
vec!["{ events: { }, fields: { }, indexes: { }, lives: { }, tables: { } }"]
|
2023-07-29 18:47:25 +00:00
|
|
|
];
|
|
|
|
|
|
|
|
let test_cases = [
|
|
|
|
// Root level
|
|
|
|
((().into(), Role::Owner), ("NS", "DB"), true),
|
|
|
|
((().into(), Role::Editor), ("NS", "DB"), true),
|
|
|
|
((().into(), Role::Viewer), ("NS", "DB"), false),
|
|
|
|
// Namespace level
|
|
|
|
((("NS",).into(), Role::Owner), ("NS", "DB"), true),
|
|
|
|
((("NS",).into(), Role::Owner), ("OTHER_NS", "DB"), false),
|
|
|
|
((("NS",).into(), Role::Editor), ("NS", "DB"), true),
|
|
|
|
((("NS",).into(), Role::Editor), ("OTHER_NS", "DB"), false),
|
|
|
|
((("NS",).into(), Role::Viewer), ("NS", "DB"), false),
|
|
|
|
((("NS",).into(), Role::Viewer), ("OTHER_NS", "DB"), false),
|
|
|
|
// Database level
|
|
|
|
((("NS", "DB").into(), Role::Owner), ("NS", "DB"), true),
|
|
|
|
((("NS", "DB").into(), Role::Owner), ("NS", "OTHER_DB"), false),
|
|
|
|
((("NS", "DB").into(), Role::Owner), ("OTHER_NS", "DB"), false),
|
|
|
|
((("NS", "DB").into(), Role::Editor), ("NS", "DB"), true),
|
|
|
|
((("NS", "DB").into(), Role::Editor), ("NS", "OTHER_DB"), false),
|
|
|
|
((("NS", "DB").into(), Role::Editor), ("OTHER_NS", "DB"), false),
|
|
|
|
((("NS", "DB").into(), Role::Viewer), ("NS", "DB"), false),
|
|
|
|
((("NS", "DB").into(), Role::Viewer), ("NS", "OTHER_DB"), false),
|
|
|
|
((("NS", "DB").into(), Role::Viewer), ("OTHER_NS", "DB"), false),
|
|
|
|
];
|
|
|
|
|
|
|
|
let res = iam_check_cases(test_cases.iter(), &scenario, check_results).await;
|
|
|
|
assert!(res.is_ok(), "{}", res.unwrap_err());
|
|
|
|
}
|
2023-12-06 21:30:18 +00:00
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
async fn define_statement_table_permissions() -> Result<(), Error> {
|
|
|
|
// Permissions for tables, unlike other resources, are restrictive (NONE) by default.
|
|
|
|
// This test ensures that behaviour
|
|
|
|
let sql = "
|
|
|
|
DEFINE TABLE default;
|
|
|
|
DEFINE TABLE select_full PERMISSIONS FOR select FULL;
|
|
|
|
DEFINE TABLE full PERMISSIONS FULL;
|
|
|
|
INFO FOR DB;
|
|
|
|
";
|
|
|
|
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(), 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;
|
|
|
|
assert!(tmp.is_ok());
|
|
|
|
//
|
|
|
|
let tmp = res.remove(0).result?;
|
|
|
|
let val = Value::parse(
|
|
|
|
"{
|
|
|
|
analyzers: {},
|
|
|
|
functions: {},
|
2023-12-12 13:51:43 +00:00
|
|
|
models: {},
|
2023-12-06 21:30:18 +00:00
|
|
|
params: {},
|
|
|
|
scopes: {},
|
|
|
|
tables: {
|
2024-03-19 11:20:58 +00:00
|
|
|
default: 'DEFINE TABLE default TYPE ANY SCHEMALESS PERMISSIONS NONE',
|
|
|
|
full: 'DEFINE TABLE full TYPE ANY SCHEMALESS PERMISSIONS FULL',
|
|
|
|
select_full: 'DEFINE TABLE select_full TYPE ANY SCHEMALESS PERMISSIONS FOR select FULL, FOR create, update, delete NONE'
|
2023-12-06 21:30:18 +00:00
|
|
|
},
|
|
|
|
tokens: {},
|
|
|
|
users: {}
|
|
|
|
}",
|
|
|
|
);
|
|
|
|
assert_eq!(tmp, val);
|
|
|
|
//
|
|
|
|
Ok(())
|
|
|
|
}
|
2024-03-06 13:24:24 +00:00
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
async fn redefining_existing_analyzer_should_not_error() -> Result<(), Error> {
|
|
|
|
let sql = "
|
|
|
|
DEFINE ANALYZER example_blank TOKENIZERS blank;
|
|
|
|
DEFINE ANALYZER example_blank TOKENIZERS blank;
|
|
|
|
";
|
|
|
|
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(), 2);
|
|
|
|
//
|
|
|
|
let tmp = res.remove(0).result?;
|
|
|
|
assert_eq!(tmp, Value::None);
|
|
|
|
//
|
|
|
|
let tmp = res.remove(0).result?;
|
|
|
|
assert_eq!(tmp, Value::None);
|
|
|
|
//
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
#[cfg(feature = "sql2")]
|
|
|
|
async fn redefining_existing_analyzer_with_if_not_exists_should_error() -> Result<(), Error> {
|
|
|
|
let sql = "
|
|
|
|
DEFINE ANALYZER IF NOT EXISTS example TOKENIZERS blank;
|
|
|
|
DEFINE ANALYZER IF NOT EXISTS example TOKENIZERS blank;
|
|
|
|
";
|
|
|
|
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(), 2);
|
|
|
|
//
|
|
|
|
let tmp = res.remove(0).result?;
|
|
|
|
assert_eq!(tmp, Value::None);
|
|
|
|
//
|
|
|
|
let tmp = res.remove(0).result.unwrap_err();
|
|
|
|
assert!(matches!(tmp, Error::AzAlreadyExists { .. }),);
|
|
|
|
//
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
async fn redefining_existing_database_should_not_error() -> Result<(), Error> {
|
|
|
|
let sql = "
|
|
|
|
DEFINE DATABASE example;
|
|
|
|
DEFINE DATABASE example;
|
|
|
|
";
|
|
|
|
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(), 2);
|
|
|
|
//
|
|
|
|
let tmp = res.remove(0).result?;
|
|
|
|
assert_eq!(tmp, Value::None);
|
|
|
|
//
|
|
|
|
let tmp = res.remove(0).result?;
|
|
|
|
assert_eq!(tmp, Value::None);
|
|
|
|
//
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
#[cfg(feature = "sql2")]
|
|
|
|
async fn redefining_existing_database_with_if_not_exists_should_error() -> Result<(), Error> {
|
|
|
|
let sql = "
|
|
|
|
DEFINE DATABASE IF NOT EXISTS example;
|
|
|
|
DEFINE DATABASE IF NOT EXISTS example;
|
|
|
|
";
|
|
|
|
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(), 2);
|
|
|
|
//
|
|
|
|
let tmp = res.remove(0).result?;
|
|
|
|
assert_eq!(tmp, Value::None);
|
|
|
|
//
|
|
|
|
let tmp = res.remove(0).result.unwrap_err();
|
|
|
|
assert!(matches!(tmp, Error::DbAlreadyExists { .. }),);
|
|
|
|
//
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
async fn redefining_existing_event_should_not_error() -> Result<(), Error> {
|
|
|
|
let sql = "
|
|
|
|
DEFINE EVENT example ON example THEN {};
|
|
|
|
DEFINE EVENT example ON example THEN {};
|
|
|
|
";
|
|
|
|
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(), 2);
|
|
|
|
//
|
|
|
|
let tmp = res.remove(0).result?;
|
|
|
|
assert_eq!(tmp, Value::None);
|
|
|
|
//
|
|
|
|
let tmp = res.remove(0).result?;
|
|
|
|
assert_eq!(tmp, Value::None);
|
|
|
|
//
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
#[cfg(feature = "sql2")]
|
|
|
|
async fn redefining_existing_event_with_if_not_exists_should_error() -> Result<(), Error> {
|
|
|
|
let sql = "
|
|
|
|
DEFINE EVENT IF NOT EXISTS example ON example THEN {};
|
|
|
|
DEFINE EVENT IF NOT EXISTS example ON example THEN {};
|
|
|
|
";
|
|
|
|
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(), 2);
|
|
|
|
//
|
|
|
|
let tmp = res.remove(0).result?;
|
|
|
|
assert_eq!(tmp, Value::None);
|
|
|
|
//
|
|
|
|
let tmp = res.remove(0).result.unwrap_err();
|
|
|
|
assert!(matches!(tmp, Error::EvAlreadyExists { .. }),);
|
|
|
|
//
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
async fn redefining_existing_field_should_not_error() -> Result<(), Error> {
|
|
|
|
let sql = "
|
|
|
|
DEFINE FIELD example ON example;
|
|
|
|
DEFINE FIELD example ON example;
|
|
|
|
";
|
|
|
|
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(), 2);
|
|
|
|
//
|
|
|
|
let tmp = res.remove(0).result?;
|
|
|
|
assert_eq!(tmp, Value::None);
|
|
|
|
//
|
|
|
|
let tmp = res.remove(0).result?;
|
|
|
|
assert_eq!(tmp, Value::None);
|
|
|
|
//
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
#[cfg(feature = "sql2")]
|
|
|
|
async fn redefining_existing_field_with_if_not_exists_should_error() -> Result<(), Error> {
|
|
|
|
let sql = "
|
|
|
|
DEFINE FIELD IF NOT EXISTS example ON example;
|
|
|
|
DEFINE FIELD IF NOT EXISTS example ON example;
|
|
|
|
";
|
|
|
|
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(), 2);
|
|
|
|
//
|
|
|
|
let tmp = res.remove(0).result?;
|
|
|
|
assert_eq!(tmp, Value::None);
|
|
|
|
//
|
|
|
|
let tmp = res.remove(0).result.unwrap_err();
|
|
|
|
assert!(matches!(tmp, Error::FdAlreadyExists { .. }),);
|
|
|
|
//
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
async fn redefining_existing_function_should_not_error() -> Result<(), Error> {
|
|
|
|
let sql = "
|
|
|
|
DEFINE FUNCTION fn::example() {};
|
|
|
|
DEFINE FUNCTION fn::example() {};
|
|
|
|
";
|
|
|
|
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(), 2);
|
|
|
|
//
|
|
|
|
let tmp = res.remove(0).result?;
|
|
|
|
assert_eq!(tmp, Value::None);
|
|
|
|
//
|
|
|
|
let tmp = res.remove(0).result?;
|
|
|
|
assert_eq!(tmp, Value::None);
|
|
|
|
//
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
#[cfg(feature = "sql2")]
|
|
|
|
async fn redefining_existing_function_with_if_not_exists_should_error() -> Result<(), Error> {
|
|
|
|
let sql = "
|
|
|
|
DEFINE FUNCTION IF NOT EXISTS fn::example() {};
|
|
|
|
DEFINE FUNCTION IF NOT EXISTS fn::example() {};
|
|
|
|
";
|
|
|
|
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(), 2);
|
|
|
|
//
|
|
|
|
let tmp = res.remove(0).result?;
|
|
|
|
assert_eq!(tmp, Value::None);
|
|
|
|
//
|
|
|
|
let tmp = res.remove(0).result.unwrap_err();
|
|
|
|
assert!(matches!(tmp, Error::FcAlreadyExists { .. }),);
|
|
|
|
//
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
async fn redefining_existing_index_should_not_error() -> Result<(), Error> {
|
|
|
|
let sql = "
|
|
|
|
DEFINE INDEX example ON example FIELDS example;
|
|
|
|
DEFINE INDEX example ON example FIELDS example;
|
|
|
|
";
|
|
|
|
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(), 2);
|
|
|
|
//
|
|
|
|
let tmp = res.remove(0).result?;
|
|
|
|
assert_eq!(tmp, Value::None);
|
|
|
|
//
|
|
|
|
let tmp = res.remove(0).result?;
|
|
|
|
assert_eq!(tmp, Value::None);
|
|
|
|
//
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
#[cfg(feature = "sql2")]
|
|
|
|
async fn redefining_existing_index_with_if_not_exists_should_error() -> Result<(), Error> {
|
|
|
|
let sql = "
|
|
|
|
DEFINE INDEX IF NOT EXISTS example ON example FIELDS example;
|
|
|
|
DEFINE INDEX IF NOT EXISTS example ON example FIELDS example;
|
|
|
|
";
|
|
|
|
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(), 2);
|
|
|
|
//
|
|
|
|
let tmp = res.remove(0).result?;
|
|
|
|
assert_eq!(tmp, Value::None);
|
|
|
|
//
|
|
|
|
let tmp = res.remove(0).result.unwrap_err();
|
|
|
|
assert!(matches!(tmp, Error::IxAlreadyExists { .. }),);
|
|
|
|
//
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
async fn redefining_existing_namespace_should_not_error() -> Result<(), Error> {
|
|
|
|
let sql = "
|
|
|
|
DEFINE NAMESPACE example;
|
|
|
|
DEFINE NAMESPACE example;
|
|
|
|
";
|
|
|
|
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(), 2);
|
|
|
|
//
|
|
|
|
let tmp = res.remove(0).result?;
|
|
|
|
assert_eq!(tmp, Value::None);
|
|
|
|
//
|
|
|
|
let tmp = res.remove(0).result?;
|
|
|
|
assert_eq!(tmp, Value::None);
|
|
|
|
//
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
#[cfg(feature = "sql2")]
|
|
|
|
async fn redefining_existing_namespace_with_if_not_exists_should_error() -> Result<(), Error> {
|
|
|
|
let sql = "
|
|
|
|
DEFINE NAMESPACE IF NOT EXISTS example;
|
|
|
|
DEFINE NAMESPACE IF NOT EXISTS example;
|
|
|
|
";
|
|
|
|
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(), 2);
|
|
|
|
//
|
|
|
|
let tmp = res.remove(0).result?;
|
|
|
|
assert_eq!(tmp, Value::None);
|
|
|
|
//
|
|
|
|
let tmp = res.remove(0).result.unwrap_err();
|
|
|
|
assert!(matches!(tmp, Error::NsAlreadyExists { .. }),);
|
|
|
|
//
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
async fn redefining_existing_param_should_not_error() -> Result<(), Error> {
|
|
|
|
let sql = "
|
|
|
|
DEFINE PARAM $example VALUE 123;
|
|
|
|
DEFINE PARAM $example VALUE 123;
|
|
|
|
";
|
|
|
|
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(), 2);
|
|
|
|
//
|
|
|
|
let tmp = res.remove(0).result?;
|
|
|
|
assert_eq!(tmp, Value::None);
|
|
|
|
//
|
|
|
|
let tmp = res.remove(0).result?;
|
|
|
|
assert_eq!(tmp, Value::None);
|
|
|
|
//
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
#[cfg(feature = "sql2")]
|
|
|
|
async fn redefining_existing_param_with_if_not_exists_should_error() -> Result<(), Error> {
|
|
|
|
let sql = "
|
|
|
|
DEFINE PARAM IF NOT EXISTS $example VALUE 123;
|
|
|
|
DEFINE PARAM IF NOT EXISTS $example VALUE 123;
|
|
|
|
";
|
|
|
|
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(), 2);
|
|
|
|
//
|
|
|
|
let tmp = res.remove(0).result?;
|
|
|
|
assert_eq!(tmp, Value::None);
|
|
|
|
//
|
|
|
|
let tmp = res.remove(0).result.unwrap_err();
|
|
|
|
assert!(matches!(tmp, Error::PaAlreadyExists { .. }),);
|
|
|
|
//
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
async fn redefining_existing_scope_should_not_error() -> Result<(), Error> {
|
|
|
|
let sql = "
|
|
|
|
DEFINE SCOPE example;
|
|
|
|
DEFINE SCOPE example;
|
|
|
|
";
|
|
|
|
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(), 2);
|
|
|
|
//
|
|
|
|
let tmp = res.remove(0).result?;
|
|
|
|
assert_eq!(tmp, Value::None);
|
|
|
|
//
|
|
|
|
let tmp = res.remove(0).result?;
|
|
|
|
assert_eq!(tmp, Value::None);
|
|
|
|
//
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
#[cfg(feature = "sql2")]
|
|
|
|
async fn redefining_existing_scope_with_if_not_exists_should_error() -> Result<(), Error> {
|
|
|
|
let sql = "
|
|
|
|
DEFINE SCOPE IF NOT EXISTS example;
|
|
|
|
DEFINE SCOPE IF NOT EXISTS example;
|
|
|
|
";
|
|
|
|
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(), 2);
|
|
|
|
//
|
|
|
|
let tmp = res.remove(0).result?;
|
|
|
|
assert_eq!(tmp, Value::None);
|
|
|
|
//
|
|
|
|
let tmp = res.remove(0).result.unwrap_err();
|
|
|
|
assert!(matches!(tmp, Error::ScAlreadyExists { .. }),);
|
|
|
|
//
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
async fn redefining_existing_table_should_not_error() -> Result<(), Error> {
|
|
|
|
let sql = "
|
|
|
|
DEFINE TABLE example;
|
|
|
|
DEFINE TABLE example;
|
|
|
|
";
|
|
|
|
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(), 2);
|
|
|
|
//
|
|
|
|
let tmp = res.remove(0).result?;
|
|
|
|
assert_eq!(tmp, Value::None);
|
|
|
|
//
|
|
|
|
let tmp = res.remove(0).result?;
|
|
|
|
assert_eq!(tmp, Value::None);
|
|
|
|
//
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
#[cfg(feature = "sql2")]
|
|
|
|
async fn redefining_existing_table_with_if_not_exists_should_error() -> Result<(), Error> {
|
|
|
|
let sql = "
|
|
|
|
DEFINE TABLE IF NOT EXISTS example;
|
|
|
|
DEFINE TABLE IF NOT EXISTS example;
|
|
|
|
";
|
|
|
|
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(), 2);
|
|
|
|
//
|
|
|
|
let tmp = res.remove(0).result?;
|
|
|
|
assert_eq!(tmp, Value::None);
|
|
|
|
//
|
|
|
|
let tmp = res.remove(0).result.unwrap_err();
|
|
|
|
assert!(matches!(tmp, Error::TbAlreadyExists { .. }),);
|
|
|
|
//
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
async fn redefining_existing_token_should_not_error() -> Result<(), Error> {
|
|
|
|
let sql = "
|
|
|
|
DEFINE TOKEN example ON SCOPE example TYPE HS512 VALUE \"example\";
|
|
|
|
DEFINE TOKEN example ON SCOPE example TYPE HS512 VALUE \"example\";
|
|
|
|
";
|
|
|
|
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(), 2);
|
|
|
|
//
|
|
|
|
let tmp = res.remove(0).result?;
|
|
|
|
assert_eq!(tmp, Value::None);
|
|
|
|
//
|
|
|
|
let tmp = res.remove(0).result?;
|
|
|
|
assert_eq!(tmp, Value::None);
|
|
|
|
//
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
#[cfg(feature = "sql2")]
|
|
|
|
async fn redefining_existing_token_with_if_not_exists_should_error() -> Result<(), Error> {
|
|
|
|
let sql = "
|
|
|
|
DEFINE TOKEN IF NOT EXISTS example ON SCOPE example TYPE HS512 VALUE \"example\";
|
|
|
|
DEFINE TOKEN IF NOT EXISTS example ON SCOPE example TYPE HS512 VALUE \"example\";
|
|
|
|
";
|
|
|
|
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(), 2);
|
|
|
|
//
|
|
|
|
let tmp = res.remove(0).result?;
|
|
|
|
assert_eq!(tmp, Value::None);
|
|
|
|
//
|
|
|
|
let tmp = res.remove(0).result.unwrap_err();
|
|
|
|
assert!(matches!(tmp, Error::StAlreadyExists { .. }),);
|
|
|
|
//
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
async fn redefining_existing_user_should_not_error() -> Result<(), Error> {
|
|
|
|
let sql = "
|
|
|
|
DEFINE USER example ON ROOT PASSWORD \"example\" ROLES OWNER;
|
|
|
|
DEFINE USER example ON ROOT PASSWORD \"example\" ROLES OWNER;
|
|
|
|
";
|
|
|
|
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(), 2);
|
|
|
|
//
|
|
|
|
let tmp = res.remove(0).result?;
|
|
|
|
assert_eq!(tmp, Value::None);
|
|
|
|
//
|
|
|
|
let tmp = res.remove(0).result?;
|
|
|
|
assert_eq!(tmp, Value::None);
|
|
|
|
//
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
#[cfg(feature = "sql2")]
|
|
|
|
async fn redefining_existing_user_with_if_not_exists_should_error() -> Result<(), Error> {
|
|
|
|
let sql = "
|
|
|
|
DEFINE USER IF NOT EXISTS example ON ROOT PASSWORD \"example\" ROLES OWNER;
|
|
|
|
DEFINE USER IF NOT EXISTS example ON ROOT PASSWORD \"example\" ROLES OWNER;
|
|
|
|
";
|
|
|
|
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(), 2);
|
|
|
|
//
|
|
|
|
let tmp = res.remove(0).result?;
|
|
|
|
assert_eq!(tmp, Value::None);
|
|
|
|
//
|
|
|
|
let tmp = res.remove(0).result.unwrap_err();
|
|
|
|
assert!(matches!(tmp, Error::UserRootAlreadyExists { .. }),);
|
|
|
|
//
|
|
|
|
Ok(())
|
|
|
|
}
|
2024-03-19 11:20:58 +00:00
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
#[cfg(feature = "sql2")]
|
|
|
|
async fn define_table_relation() -> Result<(), Error> {
|
|
|
|
let sql = "
|
2024-03-21 10:59:08 +00:00
|
|
|
DEFINE TABLE likes TYPE RELATION;
|
2024-03-19 11:20:58 +00:00
|
|
|
CREATE person:raphael, person:tobie;
|
|
|
|
RELATE person:raphael->likes->person:tobie;
|
|
|
|
CREATE likes:1;
|
|
|
|
";
|
|
|
|
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(), 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;
|
|
|
|
assert!(tmp.is_ok());
|
|
|
|
//
|
|
|
|
let tmp = res.remove(0).result;
|
|
|
|
assert!(tmp.is_err());
|
|
|
|
//
|
|
|
|
Ok(())
|
|
|
|
}
|