2022-07-28 09:00:06 +00:00
|
|
|
mod parse;
|
|
|
|
use parse::Parse;
|
2023-08-30 18:01:30 +00:00
|
|
|
mod helpers;
|
|
|
|
use helpers::new_ds;
|
2022-12-30 08:23:19 +00:00
|
|
|
use surrealdb::dbs::Session;
|
|
|
|
use surrealdb::err::Error;
|
2022-07-28 09:00:06 +00:00
|
|
|
use surrealdb::sql::Value;
|
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
async fn strict_mode_no_namespace() -> Result<(), Error> {
|
|
|
|
let sql = "
|
|
|
|
-- DEFINE NAMESPACE test;
|
|
|
|
DEFINE DATABASE test;
|
|
|
|
DEFINE TABLE test;
|
|
|
|
DEFINE FIELD extra ON test VALUE true;
|
|
|
|
CREATE test:tester;
|
2022-08-08 21:41:33 +00:00
|
|
|
SELECT * FROM test;
|
2022-07-28 09:00:06 +00:00
|
|
|
";
|
2023-08-30 18:01:30 +00:00
|
|
|
let dbs = new_ds().await?.with_strict_mode(true);
|
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-08 21:41:33 +00:00
|
|
|
assert_eq!(res.len(), 5);
|
|
|
|
//
|
|
|
|
let tmp = res.remove(0).result;
|
2023-03-25 19:44:03 +00:00
|
|
|
assert!(matches!(
|
|
|
|
tmp.err(),
|
|
|
|
Some(Error::NsNotFound {
|
|
|
|
value: _
|
|
|
|
})
|
|
|
|
));
|
2022-07-28 09:00:06 +00:00
|
|
|
//
|
|
|
|
let tmp = res.remove(0).result;
|
2023-03-25 19:44:03 +00:00
|
|
|
assert!(matches!(
|
|
|
|
tmp.err(),
|
|
|
|
Some(Error::NsNotFound {
|
|
|
|
value: _
|
|
|
|
})
|
|
|
|
));
|
2022-07-28 09:00:06 +00:00
|
|
|
//
|
|
|
|
let tmp = res.remove(0).result;
|
2023-03-25 19:44:03 +00:00
|
|
|
assert!(matches!(
|
|
|
|
tmp.err(),
|
|
|
|
Some(Error::NsNotFound {
|
|
|
|
value: _
|
|
|
|
})
|
|
|
|
));
|
2022-07-28 09:00:06 +00:00
|
|
|
//
|
|
|
|
let tmp = res.remove(0).result;
|
2023-03-25 19:44:03 +00:00
|
|
|
assert!(matches!(
|
|
|
|
tmp.err(),
|
|
|
|
Some(Error::NsNotFound {
|
|
|
|
value: _
|
|
|
|
})
|
|
|
|
));
|
2022-07-28 09:00:06 +00:00
|
|
|
//
|
|
|
|
let tmp = res.remove(0).result;
|
2023-03-25 19:44:03 +00:00
|
|
|
assert!(matches!(
|
|
|
|
tmp.err(),
|
|
|
|
Some(Error::NsNotFound {
|
|
|
|
value: _
|
|
|
|
})
|
|
|
|
));
|
2022-07-28 09:00:06 +00:00
|
|
|
//
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
async fn strict_mode_no_database() -> Result<(), Error> {
|
|
|
|
let sql = "
|
|
|
|
DEFINE NAMESPACE test;
|
|
|
|
-- DEFINE DATABASE test;
|
|
|
|
DEFINE TABLE test;
|
|
|
|
DEFINE FIELD extra ON test VALUE true;
|
|
|
|
CREATE test:tester;
|
2022-08-08 21:41:33 +00:00
|
|
|
SELECT * FROM test;
|
2022-07-28 09:00:06 +00:00
|
|
|
";
|
2023-08-30 18:01:30 +00:00
|
|
|
let dbs = new_ds().await?.with_strict_mode(true);
|
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-08 21:41:33 +00:00
|
|
|
assert_eq!(res.len(), 5);
|
2022-07-28 09:00:06 +00:00
|
|
|
//
|
|
|
|
let tmp = res.remove(0).result;
|
|
|
|
assert!(tmp.is_ok());
|
|
|
|
//
|
|
|
|
let tmp = res.remove(0).result;
|
2023-03-25 19:44:03 +00:00
|
|
|
assert!(matches!(
|
|
|
|
tmp.err(),
|
|
|
|
Some(Error::DbNotFound {
|
|
|
|
value: _
|
|
|
|
})
|
|
|
|
));
|
2022-07-28 09:00:06 +00:00
|
|
|
//
|
|
|
|
let tmp = res.remove(0).result;
|
2023-03-25 19:44:03 +00:00
|
|
|
assert!(matches!(
|
|
|
|
tmp.err(),
|
|
|
|
Some(Error::DbNotFound {
|
|
|
|
value: _
|
|
|
|
})
|
|
|
|
));
|
2022-07-28 09:00:06 +00:00
|
|
|
//
|
|
|
|
let tmp = res.remove(0).result;
|
2023-03-25 19:44:03 +00:00
|
|
|
assert!(matches!(
|
|
|
|
tmp.err(),
|
|
|
|
Some(Error::DbNotFound {
|
|
|
|
value: _
|
|
|
|
})
|
|
|
|
));
|
2022-07-28 09:00:06 +00:00
|
|
|
//
|
2022-08-08 21:41:33 +00:00
|
|
|
let tmp = res.remove(0).result;
|
2023-03-25 19:44:03 +00:00
|
|
|
assert!(matches!(
|
|
|
|
tmp.err(),
|
|
|
|
Some(Error::DbNotFound {
|
|
|
|
value: _
|
|
|
|
})
|
|
|
|
));
|
2022-08-08 21:41:33 +00:00
|
|
|
//
|
2022-07-28 09:00:06 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
async fn strict_mode_no_table() -> Result<(), Error> {
|
|
|
|
let sql = "
|
|
|
|
DEFINE NAMESPACE test;
|
|
|
|
DEFINE DATABASE test;
|
|
|
|
-- DEFINE TABLE test;
|
|
|
|
DEFINE FIELD extra ON test VALUE true;
|
|
|
|
CREATE test:tester;
|
2022-08-08 21:41:33 +00:00
|
|
|
SELECT * FROM test;
|
2022-07-28 09:00:06 +00:00
|
|
|
";
|
2023-08-30 18:01:30 +00:00
|
|
|
let dbs = new_ds().await?.with_strict_mode(true);
|
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-08 21:41:33 +00:00
|
|
|
assert_eq!(res.len(), 5);
|
2022-07-28 09:00:06 +00:00
|
|
|
//
|
|
|
|
let tmp = res.remove(0).result;
|
|
|
|
assert!(tmp.is_ok());
|
|
|
|
//
|
|
|
|
let tmp = res.remove(0).result;
|
|
|
|
assert!(tmp.is_ok());
|
|
|
|
//
|
|
|
|
let tmp = res.remove(0).result;
|
2023-03-25 19:44:03 +00:00
|
|
|
assert!(matches!(
|
|
|
|
tmp.err(),
|
|
|
|
Some(Error::TbNotFound {
|
|
|
|
value: _
|
|
|
|
})
|
|
|
|
));
|
2022-07-28 09:00:06 +00:00
|
|
|
//
|
|
|
|
let tmp = res.remove(0).result;
|
2023-03-25 19:44:03 +00:00
|
|
|
assert!(matches!(
|
|
|
|
tmp.err(),
|
|
|
|
Some(Error::TbNotFound {
|
|
|
|
value: _
|
|
|
|
})
|
|
|
|
));
|
2022-07-28 09:00:06 +00:00
|
|
|
//
|
2022-08-08 21:41:33 +00:00
|
|
|
let tmp = res.remove(0).result;
|
2023-03-25 19:44:03 +00:00
|
|
|
assert!(matches!(
|
|
|
|
tmp.err(),
|
|
|
|
Some(Error::TbNotFound {
|
|
|
|
value: _
|
|
|
|
})
|
|
|
|
));
|
2022-08-08 21:41:33 +00:00
|
|
|
//
|
2022-07-28 09:00:06 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
async fn strict_mode_all_ok() -> Result<(), Error> {
|
|
|
|
let sql = "
|
|
|
|
DEFINE NAMESPACE test;
|
|
|
|
DEFINE DATABASE test;
|
|
|
|
DEFINE TABLE test;
|
|
|
|
DEFINE FIELD extra ON test VALUE true;
|
|
|
|
CREATE test:tester;
|
2022-08-08 21:41:33 +00:00
|
|
|
SELECT * FROM test;
|
2022-07-28 09:00:06 +00:00
|
|
|
";
|
2023-08-30 18:01:30 +00:00
|
|
|
let dbs = new_ds().await?.with_strict_mode(true);
|
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-08 21:41:33 +00:00
|
|
|
assert_eq!(res.len(), 6);
|
2022-07-28 09:00:06 +00:00
|
|
|
//
|
|
|
|
let tmp = res.remove(0).result;
|
|
|
|
assert!(tmp.is_ok());
|
|
|
|
//
|
|
|
|
let tmp = res.remove(0).result;
|
|
|
|
assert!(tmp.is_ok());
|
|
|
|
//
|
|
|
|
let tmp = res.remove(0).result;
|
|
|
|
assert!(tmp.is_ok());
|
|
|
|
//
|
|
|
|
let tmp = res.remove(0).result;
|
|
|
|
assert!(tmp.is_ok());
|
|
|
|
//
|
|
|
|
let tmp = res.remove(0).result?;
|
|
|
|
let val = Value::parse("[{ id: test:tester, extra: true }]");
|
|
|
|
assert_eq!(tmp, val);
|
|
|
|
//
|
2022-08-08 21:41:33 +00:00
|
|
|
let tmp = res.remove(0).result?;
|
|
|
|
let val = Value::parse("[{ id: test:tester, extra: true }]");
|
|
|
|
assert_eq!(tmp, val);
|
|
|
|
//
|
2022-07-28 09:00:06 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
async fn loose_mode_all_ok() -> Result<(), Error> {
|
|
|
|
let sql = "
|
|
|
|
DEFINE FIELD extra ON test VALUE true;
|
|
|
|
CREATE test:tester;
|
2022-08-08 21:41:33 +00:00
|
|
|
SELECT * FROM test;
|
2023-07-29 18:47:25 +00:00
|
|
|
INFO FOR ROOT;
|
2022-07-28 09:00:06 +00:00
|
|
|
INFO FOR NS;
|
|
|
|
INFO FOR DB;
|
|
|
|
INFO FOR TABLE test;
|
|
|
|
";
|
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-08 21:41:33 +00:00
|
|
|
assert_eq!(res.len(), 7);
|
2022-07-28 09:00:06 +00:00
|
|
|
//
|
|
|
|
let tmp = res.remove(0).result;
|
|
|
|
assert!(tmp.is_ok());
|
|
|
|
//
|
|
|
|
let tmp = res.remove(0).result?;
|
|
|
|
let val = Value::parse("[{ id: test:tester, extra: true }]");
|
|
|
|
assert_eq!(tmp, val);
|
|
|
|
//
|
|
|
|
let tmp = res.remove(0).result?;
|
2022-08-08 21:41:33 +00:00
|
|
|
let val = Value::parse("[{ id: test:tester, extra: true }]");
|
|
|
|
assert_eq!(tmp, val);
|
|
|
|
//
|
|
|
|
let tmp = res.remove(0).result?;
|
2022-07-28 09:00:06 +00:00
|
|
|
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 09:00:06 +00:00
|
|
|
}",
|
|
|
|
);
|
|
|
|
assert_eq!(tmp, val);
|
|
|
|
//
|
|
|
|
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 09:00:06 +00:00
|
|
|
}",
|
|
|
|
);
|
|
|
|
assert_eq!(tmp, val);
|
|
|
|
//
|
|
|
|
let tmp = res.remove(0).result?;
|
|
|
|
let val = Value::parse(
|
|
|
|
"{
|
2023-05-31 12:35:29 +00:00
|
|
|
analyzers: {},
|
|
|
|
tokens: {},
|
|
|
|
functions: {},
|
|
|
|
params: {},
|
|
|
|
scopes: {},
|
|
|
|
tables: { test: 'DEFINE TABLE test SCHEMALESS PERMISSIONS NONE' },
|
2023-07-29 18:47:25 +00:00
|
|
|
users: {},
|
2022-07-28 09:00:06 +00:00
|
|
|
}",
|
|
|
|
);
|
|
|
|
assert_eq!(tmp, val);
|
|
|
|
//
|
|
|
|
let tmp = res.remove(0).result?;
|
|
|
|
let val = Value::parse(
|
|
|
|
"{
|
2023-05-31 12:35:29 +00:00
|
|
|
events: {},
|
|
|
|
fields: { extra: 'DEFINE FIELD extra ON test VALUE true' },
|
|
|
|
tables: {},
|
|
|
|
indexes: {},
|
2023-09-11 19:35:32 +00:00
|
|
|
lives: {},
|
2022-07-28 09:00:06 +00:00
|
|
|
}",
|
|
|
|
);
|
|
|
|
assert_eq!(tmp, val);
|
|
|
|
//
|
|
|
|
Ok(())
|
|
|
|
}
|
2023-09-12 14:33:24 +00:00
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
async fn strict_define_in_transaction() -> Result<(), Error> {
|
|
|
|
let sql = r"
|
|
|
|
DEFINE NS test; DEFINE DB test;
|
|
|
|
USE NS test DB test;
|
|
|
|
BEGIN;
|
|
|
|
DEFINE TABLE test;
|
|
|
|
DEFINE FIELD test ON test; -- Panic used to be caused when you add this query within the transaction
|
|
|
|
COMMIT;
|
|
|
|
";
|
|
|
|
let dbs = new_ds().await?.with_strict_mode(true);
|
|
|
|
let ses = Session::owner().with_ns("test").with_db("test");
|
|
|
|
dbs.execute(sql, &ses, None).await?;
|
|
|
|
Ok(())
|
|
|
|
}
|