2022-07-28 21:40:38 +00:00
|
|
|
mod parse;
|
|
|
|
use parse::Parse;
|
2022-12-30 08:23:19 +00:00
|
|
|
use surrealdb::dbs::Session;
|
|
|
|
use surrealdb::err::Error;
|
|
|
|
use surrealdb::kvs::Datastore;
|
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;
|
|
|
|
INFO FOR KV;
|
|
|
|
";
|
|
|
|
let dbs = Datastore::new("memory").await?;
|
|
|
|
let ses = Session::for_kv().with_ns("test").with_db("test");
|
|
|
|
let res = &mut dbs.execute(&sql, &ses, None, false).await?;
|
|
|
|
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
|
|
|
namespaces: { test: 'DEFINE NAMESPACE test' },
|
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;
|
|
|
|
";
|
|
|
|
let dbs = Datastore::new("memory").await?;
|
|
|
|
let ses = Session::for_kv().with_ns("test").with_db("test");
|
|
|
|
let res = &mut dbs.execute(&sql, &ses, None, false).await?;
|
|
|
|
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' },
|
|
|
|
logins: {},
|
|
|
|
tokens: {},
|
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;
|
|
|
|
";
|
|
|
|
let dbs = Datastore::new("memory").await?;
|
|
|
|
let ses = Session::for_kv().with_ns("test").with_db("test");
|
|
|
|
let res = &mut dbs.execute(&sql, &ses, None, false).await?;
|
|
|
|
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: {},
|
|
|
|
logins: {},
|
|
|
|
tokens: {},
|
|
|
|
functions: { test: 'DEFINE FUNCTION fn::test($first: string, $last: string) { RETURN $first + $last; }' },
|
|
|
|
params: {},
|
|
|
|
scopes: {},
|
|
|
|
params: {},
|
|
|
|
scopes: {},
|
|
|
|
tables: {},
|
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;
|
|
|
|
";
|
|
|
|
let dbs = Datastore::new("memory").await?;
|
|
|
|
let ses = Session::for_kv().with_ns("test").with_db("test");
|
|
|
|
let res = &mut dbs.execute(&sql, &ses, None, false).await?;
|
|
|
|
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: {},
|
|
|
|
logins: {},
|
|
|
|
tokens: {},
|
|
|
|
functions: {},
|
|
|
|
params: {},
|
|
|
|
scopes: {},
|
|
|
|
tables: { test: 'DEFINE TABLE test DROP SCHEMALESS' },
|
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;
|
|
|
|
";
|
|
|
|
let dbs = Datastore::new("memory").await?;
|
|
|
|
let ses = Session::for_kv().with_ns("test").with_db("test");
|
|
|
|
let res = &mut dbs.execute(&sql, &ses, None, false).await?;
|
|
|
|
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: {},
|
|
|
|
logins: {},
|
|
|
|
tokens: {},
|
|
|
|
functions: {},
|
|
|
|
params: {},
|
|
|
|
scopes: {},
|
|
|
|
tables: { test: 'DEFINE TABLE test SCHEMALESS' },
|
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;
|
|
|
|
";
|
|
|
|
let dbs = Datastore::new("memory").await?;
|
|
|
|
let ses = Session::for_kv().with_ns("test").with_db("test");
|
|
|
|
let res = &mut dbs.execute(&sql, &ses, None, false).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: {},
|
|
|
|
logins: {},
|
|
|
|
tokens: {},
|
|
|
|
functions: {},
|
|
|
|
params: {},
|
|
|
|
scopes: {},
|
|
|
|
tables: { test: 'DEFINE TABLE test SCHEMAFULL' },
|
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;
|
|
|
|
";
|
|
|
|
let dbs = Datastore::new("memory").await?;
|
|
|
|
let ses = Session::for_kv().with_ns("test").with_db("test");
|
|
|
|
let res = &mut dbs.execute(&sql, &ses, None, false).await?;
|
|
|
|
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: {},
|
|
|
|
logins: {},
|
|
|
|
tokens: {},
|
|
|
|
functions: {},
|
|
|
|
params: {},
|
|
|
|
scopes: {},
|
|
|
|
tables: { test: 'DEFINE TABLE test SCHEMAFULL' },
|
2022-09-11 08:08:36 +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
|
|
|
";
|
|
|
|
let dbs = Datastore::new("memory").await?;
|
|
|
|
let ses = Session::for_kv().with_ns("test").with_db("test");
|
|
|
|
let res = &mut dbs.execute(&sql, &ses, None, false).await?;
|
|
|
|
assert_eq!(res.len(), 7);
|
|
|
|
//
|
|
|
|
let tmp = res.remove(0).result;
|
|
|
|
assert!(tmp.is_ok());
|
|
|
|
//
|
|
|
|
let tmp = res.remove(0).result;
|
|
|
|
assert!(tmp.is_ok());
|
|
|
|
//
|
|
|
|
let tmp = res.remove(0).result?;
|
|
|
|
let val = Value::parse(
|
|
|
|
"{
|
2023-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: {},
|
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
|
|
|
";
|
|
|
|
let dbs = Datastore::new("memory").await?;
|
|
|
|
let ses = Session::for_kv().with_ns("test").with_db("test");
|
|
|
|
let res = &mut dbs.execute(&sql, &ses, None, false).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(
|
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: {},
|
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
|
|
|
";
|
|
|
|
let dbs = Datastore::new("memory").await?;
|
|
|
|
let ses = Session::for_kv().with_ns("test").with_db("test");
|
|
|
|
let res = &mut dbs.execute(&sql, &ses, None, false).await?;
|
|
|
|
assert_eq!(res.len(), 7);
|
|
|
|
//
|
|
|
|
let tmp = res.remove(0).result;
|
|
|
|
assert!(tmp.is_ok());
|
|
|
|
//
|
|
|
|
let tmp = res.remove(0).result;
|
|
|
|
assert!(tmp.is_ok());
|
|
|
|
//
|
|
|
|
let tmp = res.remove(0).result?;
|
|
|
|
let val = Value::parse(
|
|
|
|
"{
|
2023-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: {},
|
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;
|
|
|
|
";
|
|
|
|
let dbs = Datastore::new("memory").await?;
|
|
|
|
let ses = Session::for_kv().with_ns("test").with_db("test");
|
|
|
|
let res = &mut dbs.execute(&sql, &ses, None, false).await?;
|
|
|
|
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: {},
|
|
|
|
fields: { test: 'DEFINE FIELD test ON user' },
|
|
|
|
tables: {},
|
|
|
|
indexes: {},
|
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;
|
|
|
|
";
|
|
|
|
let dbs = Datastore::new("memory").await?;
|
|
|
|
let ses = Session::for_kv().with_ns("test").with_db("test");
|
|
|
|
let res = &mut dbs.execute(&sql, &ses, None, false).await?;
|
|
|
|
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: {},
|
|
|
|
fields: { test: 'DEFINE FIELD test ON user TYPE string' },
|
|
|
|
tables: {},
|
|
|
|
indexes: {},
|
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;
|
|
|
|
";
|
|
|
|
let dbs = Datastore::new("memory").await?;
|
|
|
|
let ses = Session::for_kv().with_ns("test").with_db("test");
|
|
|
|
let res = &mut dbs.execute(&sql, &ses, None, false).await?;
|
|
|
|
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: {},
|
|
|
|
fields: { test: "DEFINE FIELD test ON user VALUE $value OR 'GBR'" },
|
|
|
|
tables: {},
|
|
|
|
indexes: {},
|
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;
|
|
|
|
";
|
|
|
|
let dbs = Datastore::new("memory").await?;
|
|
|
|
let ses = Session::for_kv().with_ns("test").with_db("test");
|
|
|
|
let res = &mut dbs.execute(&sql, &ses, None, false).await?;
|
|
|
|
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: {},
|
|
|
|
fields: { test: 'DEFINE FIELD test ON user ASSERT $value != NONE AND $value = /[A-Z]{3}/' },
|
|
|
|
tables: {},
|
|
|
|
indexes: {},
|
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;
|
|
|
|
";
|
|
|
|
let dbs = Datastore::new("memory").await?;
|
|
|
|
let ses = Session::for_kv().with_ns("test").with_db("test");
|
|
|
|
let res = &mut dbs.execute(&sql, &ses, None, false).await?;
|
|
|
|
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: {},
|
|
|
|
fields: { test: "DEFINE FIELD test ON user TYPE string VALUE $value OR 'GBR' ASSERT $value != NONE AND $value = /[A-Z]{3}/" },
|
|
|
|
tables: {},
|
|
|
|
indexes: {},
|
2022-10-19 14:48:50 +00:00
|
|
|
}"#,
|
2022-07-28 21:40:38 +00:00
|
|
|
);
|
|
|
|
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;
|
|
|
|
";
|
|
|
|
let dbs = Datastore::new("memory").await?;
|
|
|
|
let ses = Session::for_kv().with_ns("test").with_db("test");
|
|
|
|
let res = &mut dbs.execute(&sql, &ses, None, false).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;
|
|
|
|
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' },
|
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';
|
|
|
|
";
|
|
|
|
let dbs = Datastore::new("memory").await?;
|
|
|
|
let ses = Session::for_kv().with_ns("test").with_db("test");
|
|
|
|
let res = &mut dbs.execute(&sql, &ses, None, false).await?;
|
|
|
|
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' },
|
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';
|
|
|
|
";
|
|
|
|
let dbs = Datastore::new("memory").await?;
|
|
|
|
let ses = Session::for_kv().with_ns("test").with_db("test");
|
|
|
|
let res = &mut dbs.execute(&sql, &ses, None, false).await?;
|
|
|
|
assert_eq!(res.len(), 7);
|
|
|
|
//
|
|
|
|
let tmp = res.remove(0).result;
|
|
|
|
assert!(tmp.is_ok());
|
|
|
|
//
|
|
|
|
let tmp = res.remove(0).result;
|
|
|
|
assert!(tmp.is_ok());
|
|
|
|
//
|
|
|
|
let tmp = res.remove(0).result?;
|
|
|
|
let val = Value::parse(
|
|
|
|
"{
|
2023-05-31 12:35:29 +00:00
|
|
|
events: {},
|
|
|
|
fields: {},
|
|
|
|
tables: {},
|
|
|
|
indexes: { test: 'DEFINE INDEX test ON user FIELDS account, email' },
|
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
|
|
|
";
|
|
|
|
let dbs = Datastore::new("memory").await?;
|
|
|
|
let ses = Session::for_kv().with_ns("test").with_db("test");
|
|
|
|
let res = &mut dbs.execute(&sql, &ses, None, false).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' },
|
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(),
|
2022-10-19 14:48:50 +00:00
|
|
|
Some(e) if e.to_string() == r#"Database index `test` already contains '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: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
|
|
|
";
|
|
|
|
let dbs = Datastore::new("memory").await?;
|
|
|
|
let ses = Session::for_kv().with_ns("test").with_db("test");
|
|
|
|
let res = &mut dbs.execute(&sql, &ses, None, false).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' },
|
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(),
|
2022-10-19 14:48:50 +00:00
|
|
|
Some(e) if e.to_string() == r#"Database index `test` already contains ['apple', 'test@surrealdb.com'], with record `user:3`"#
|
2022-07-28 21:40:38 +00:00
|
|
|
));
|
|
|
|
//
|
|
|
|
let tmp = res.remove(0).result;
|
|
|
|
assert!(matches!(
|
|
|
|
tmp.err(),
|
2022-10-19 14:48:50 +00:00
|
|
|
Some(e) if e.to_string() == r#"Database index `test` already contains ['tesla', 'test@surrealdb.com'], with record `user:4`"#
|
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(),
|
2022-10-19 14:48:50 +00:00
|
|
|
Some(e) if e.to_string() == r#"Database index `test` already contains ['tesla', 'test@surrealdb.com'], with record `user:4`"#
|
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;
|
|
|
|
";
|
|
|
|
let dbs = Datastore::new("memory").await?;
|
|
|
|
let ses = Session::for_kv().with_ns("test").with_db("test");
|
|
|
|
let res = &mut dbs.execute(&sql, &ses, None, false).await?;
|
|
|
|
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(),
|
2022-10-19 14:48:50 +00:00
|
|
|
Some(e) if e.to_string() == r#"Database index `test` already contains 'test@surrealdb.com', with record `user:3`"#
|
2022-08-25 13:49:33 +00:00
|
|
|
));
|
|
|
|
//
|
|
|
|
let tmp = res.remove(0).result;
|
|
|
|
assert!(matches!(
|
|
|
|
tmp.err(),
|
2022-10-19 14:48:50 +00:00
|
|
|
Some(e) if e.to_string() == r#"Database index `test` already contains 'test@surrealdb.com', with record `user:3`"#
|
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: {},
|
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;
|
|
|
|
";
|
|
|
|
let dbs = Datastore::new("memory").await?;
|
|
|
|
let ses = Session::for_kv().with_ns("test").with_db("test");
|
|
|
|
let res = &mut dbs.execute(&sql, &ses, None, false).await?;
|
|
|
|
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;
|
2023-06-19 18:41:13 +00:00
|
|
|
println!("{:?}", tmp);
|
2022-08-25 13:49:33 +00:00
|
|
|
assert!(matches!(
|
|
|
|
tmp.err(),
|
2023-06-19 18:41:13 +00:00
|
|
|
Some(e) if e.to_string() == r#"Database index `test` already contains ['tesla', 'test@surrealdb.com'], with record `user:4`"#
|
2022-08-25 13:49:33 +00:00
|
|
|
));
|
|
|
|
//
|
|
|
|
let tmp = res.remove(0).result;
|
|
|
|
assert!(matches!(
|
|
|
|
tmp.err(),
|
2023-06-19 18:41:13 +00:00
|
|
|
Some(e) if e.to_string() == r#"Database index `test` already contains ['tesla', 'test@surrealdb.com'], with record `user:4`"#
|
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: {},
|
2022-08-25 13:49:33 +00:00
|
|
|
}",
|
|
|
|
);
|
|
|
|
assert_eq!(tmp, val);
|
|
|
|
//
|
|
|
|
Ok(())
|
|
|
|
}
|
2023-05-10 02:08:09 +00:00
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
async fn define_statement_analyzer() -> Result<(), Error> {
|
|
|
|
let sql = "
|
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);
|
|
|
|
INFO FOR DB;
|
|
|
|
";
|
|
|
|
let dbs = Datastore::new("memory").await?;
|
|
|
|
let ses = Session::for_kv().with_ns("test").with_db("test");
|
|
|
|
let res = &mut dbs.execute(&sql, &ses, None, false).await?;
|
|
|
|
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
|
|
|
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-05-10 02:08:09 +00:00
|
|
|
},
|
2023-05-31 12:35:29 +00:00
|
|
|
logins: {},
|
|
|
|
tokens: {},
|
|
|
|
functions: {},
|
|
|
|
params: {},
|
|
|
|
scopes: {},
|
|
|
|
tables: {}
|
2023-05-10 02:08:09 +00:00
|
|
|
}",
|
|
|
|
);
|
|
|
|
assert_eq!(tmp, val);
|
|
|
|
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
|
|
|
"#;
|
|
|
|
|
|
|
|
let dbs = Datastore::new("memory").await?;
|
|
|
|
let ses = Session::for_kv().with_ns("test").with_db("test");
|
|
|
|
let res = &mut dbs.execute(&sql, &ses, None, false).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-06-19 18:41:13 +00:00
|
|
|
indexes: { blog_title: 'DEFINE INDEX blog_title ON blog FIELDS title SEARCH ANALYZER simple BM25(1.2,0.75) ORDER 100 HIGHLIGHTS' },
|
2023-05-29 11:46:41 +00:00
|
|
|
}",
|
|
|
|
);
|
|
|
|
assert_eq!(tmp, 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)));
|
|
|
|
check_path(&tmp, &["doc_ids", "total_size"], |v| assert_eq!(v, Value::from(72)));
|
|
|
|
|
|
|
|
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)));
|
|
|
|
check_path(&tmp, &["doc_lengths", "total_size"], |v| assert_eq!(v, Value::from(59)));
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|