surrealpatch/sdk/tests/strict.rs

298 lines
6 KiB
Rust
Raw Normal View History

2022-07-28 09:00:06 +00:00
mod parse;
use parse::Parse;
mod helpers;
use helpers::new_ds;
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;
SELECT * FROM test;
2022-07-28 09:00:06 +00:00
";
let dbs = new_ds().await?.with_strict_mode(true);
let ses = Session::owner().with_ns("test").with_db("test");
let res = &mut dbs.execute(sql, &ses, None).await?;
assert_eq!(res.len(), 5);
//
let tmp = res.remove(0).result;
assert!(matches!(
tmp.err(),
Some(Error::NsNotFound {
value: _
})
));
2022-07-28 09:00:06 +00:00
//
let tmp = res.remove(0).result;
assert!(matches!(
tmp.err(),
Some(Error::NsNotFound {
value: _
})
));
2022-07-28 09:00:06 +00:00
//
let tmp = res.remove(0).result;
assert!(matches!(
tmp.err(),
Some(Error::NsNotFound {
value: _
})
));
2022-07-28 09:00:06 +00:00
//
let tmp = res.remove(0).result;
assert!(matches!(
tmp.err(),
Some(Error::NsNotFound {
value: _
})
));
2022-07-28 09:00:06 +00:00
//
let tmp = res.remove(0).result;
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;
SELECT * FROM test;
2022-07-28 09:00:06 +00:00
";
let dbs = new_ds().await?.with_strict_mode(true);
let ses = Session::owner().with_ns("test").with_db("test");
let res = &mut dbs.execute(sql, &ses, None).await?;
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!(matches!(
tmp.err(),
Some(Error::DbNotFound {
value: _
})
));
2022-07-28 09:00:06 +00:00
//
let tmp = res.remove(0).result;
assert!(matches!(
tmp.err(),
Some(Error::DbNotFound {
value: _
})
));
2022-07-28 09:00:06 +00:00
//
let tmp = res.remove(0).result;
assert!(matches!(
tmp.err(),
Some(Error::DbNotFound {
value: _
})
));
2022-07-28 09:00:06 +00:00
//
let tmp = res.remove(0).result;
assert!(matches!(
tmp.err(),
Some(Error::DbNotFound {
value: _
})
));
//
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;
SELECT * FROM test;
2022-07-28 09:00:06 +00:00
";
let dbs = new_ds().await?.with_strict_mode(true);
let ses = Session::owner().with_ns("test").with_db("test");
let res = &mut dbs.execute(sql, &ses, None).await?;
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;
assert!(matches!(
tmp.err(),
Some(Error::TbNotFound {
value: _
})
));
2022-07-28 09:00:06 +00:00
//
let tmp = res.remove(0).result;
assert!(matches!(
tmp.err(),
Some(Error::TbNotFound {
value: _
})
));
2022-07-28 09:00:06 +00:00
//
let tmp = res.remove(0).result;
assert!(matches!(
tmp.err(),
Some(Error::TbNotFound {
value: _
})
));
//
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;
SELECT * FROM test;
2022-07-28 09:00:06 +00:00
";
let dbs = new_ds().await?.with_strict_mode(true);
let ses = Session::owner().with_ns("test").with_db("test");
let res = &mut dbs.execute(sql, &ses, None).await?;
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);
//
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;
SELECT * FROM test;
INFO FOR ROOT;
2022-07-28 09:00:06 +00:00
INFO FOR NS;
INFO FOR DB;
INFO FOR TABLE test;
";
let dbs = new_ds().await?;
let ses = Session::owner().with_ns("test").with_db("test");
let res = &mut dbs.execute(sql, &ses, None).await?;
assert_eq!(res.len(), 7);
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?;
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(
"{
accesses: {},
namespaces: { test: 'DEFINE NAMESPACE test' },
nodes: {},
users: {},
2022-07-28 09:00:06 +00:00
}",
);
assert_eq!(tmp, val);
//
let tmp = res.remove(0).result?;
let val = Value::parse(
"{
accesses: {},
databases: { test: 'DEFINE DATABASE test' },
users: {},
2022-07-28 09:00:06 +00:00
}",
);
assert_eq!(tmp, val);
//
let tmp = res.remove(0).result?;
let val = Value::parse(
"{
accesses: {},
analyzers: {},
functions: {},
models: {},
params: {},
tables: { test: 'DEFINE TABLE test TYPE ANY SCHEMALESS PERMISSIONS NONE' },
users: {},
2022-07-28 09:00:06 +00:00
}",
);
assert_eq!(tmp, val);
//
let tmp = res.remove(0).result?;
let val = Value::parse(
"{
events: {},
2023-12-06 21:30:18 +00:00
fields: { extra: 'DEFINE FIELD extra ON test VALUE true PERMISSIONS FULL' },
tables: {},
indexes: {},
lives: {},
2022-07-28 09:00:06 +00:00
}",
);
assert_eq!(tmp, val);
//
Ok(())
}
#[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(())
}