2023-08-30 18:01:30 +00:00
|
|
|
mod helpers;
|
|
|
|
use helpers::new_ds;
|
2023-08-23 22:30:13 +00:00
|
|
|
use surrealdb::dbs::Session;
|
|
|
|
use surrealdb::err::Error;
|
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
async fn throw_basic() -> Result<(), Error> {
|
|
|
|
let sql = "
|
|
|
|
THROW 'there was an error';
|
|
|
|
";
|
2023-08-30 18:01:30 +00:00
|
|
|
let dbs = new_ds().await?;
|
2023-08-23 22:30:13 +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(), 1);
|
|
|
|
//
|
|
|
|
let tmp = res.remove(0).result;
|
|
|
|
assert!(matches!(
|
|
|
|
tmp.err(),
|
|
|
|
Some(e) if e.to_string() == r#"An error occurred: there was an error"#
|
|
|
|
));
|
|
|
|
//
|
|
|
|
Ok(())
|
|
|
|
}
|
2023-09-05 15:21:34 +00:00
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
async fn throw_param() -> Result<(), Error> {
|
|
|
|
let sql = "
|
|
|
|
LET $err = 'there was an error';
|
|
|
|
THROW $err;
|
|
|
|
";
|
|
|
|
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!(tmp.is_ok());
|
|
|
|
//
|
|
|
|
let tmp = res.remove(0).result;
|
|
|
|
assert!(matches!(
|
|
|
|
tmp.err(),
|
|
|
|
Some(e) if e.to_string() == r#"An error occurred: there was an error"#
|
|
|
|
));
|
|
|
|
//
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
async fn throw_value() -> Result<(), Error> {
|
|
|
|
let sql = "
|
|
|
|
LET $err = string::concat('found unexpected value: ', {
|
|
|
|
test: true
|
|
|
|
});
|
|
|
|
THROW $err;
|
|
|
|
";
|
|
|
|
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!(tmp.is_ok());
|
|
|
|
//
|
|
|
|
let tmp = res.remove(0).result;
|
|
|
|
assert!(matches!(
|
|
|
|
tmp.err(),
|
|
|
|
Some(e) if e.to_string() == r#"An error occurred: found unexpected value: { test: true }"#
|
|
|
|
));
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|