surrealpatch/lib/tests/return.rs
2023-09-05 16:22:13 +01:00

161 lines
4.3 KiB
Rust

mod parse;
use parse::Parse;
mod helpers;
use helpers::new_ds;
use surrealdb::dbs::Session;
use surrealdb::err::Error;
use surrealdb::sql::Value;
#[tokio::test]
async fn return_subquery_only() -> Result<(), Error> {
let sql = "
CREATE person:tobie SET name = 'Tobie';
CREATE person:jaime SET name = 'Jaime';
LET $single = person:tobie;
--
SELECT name FROM person;
SELECT VALUE name FROM person;
SELECT name FROM ONLY person;
SELECT VALUE name FROM ONLY person;
SELECT name FROM person:tobie;
SELECT VALUE name FROM person:tobie;
SELECT name FROM ONLY person:tobie;
SELECT VALUE name FROM ONLY person:tobie;
SELECT name FROM $single;
SELECT VALUE name FROM $single;
SELECT name FROM ONLY $single;
SELECT VALUE name FROM ONLY $single;
--
RETURN SELECT name FROM person;
RETURN SELECT VALUE name FROM person;
RETURN SELECT name FROM ONLY person;
RETURN SELECT VALUE name FROM ONLY person;
RETURN SELECT name FROM person:tobie;
RETURN SELECT VALUE name FROM person:tobie;
RETURN SELECT name FROM ONLY person:tobie;
RETURN SELECT VALUE name FROM ONLY person:tobie;
RETURN SELECT name FROM $single;
RETURN SELECT VALUE name FROM $single;
RETURN SELECT name FROM ONLY $single;
RETURN SELECT VALUE name FROM ONLY $single;
";
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(), 27);
//
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("[{ name: 'Jaime' }, { name: 'Tobie' }]");
assert_eq!(tmp, val);
//
let tmp = res.remove(0).result?;
let val = Value::parse("['Jaime', 'Tobie']");
assert_eq!(tmp, val);
//
let tmp = res.remove(0).result;
assert!(matches!(
tmp.err(),
Some(e) if e.to_string() == r#"Expected a single result output when using the ONLY keyword"#
));
//
let tmp = res.remove(0).result;
assert!(matches!(
tmp.err(),
Some(e) if e.to_string() == r#"Expected a single result output when using the ONLY keyword"#
));
//
let tmp = res.remove(0).result?;
let val = Value::parse("[{ name: 'Tobie' }]");
assert_eq!(tmp, val);
//
let tmp = res.remove(0).result?;
let val = Value::parse("['Tobie']");
assert_eq!(tmp, val);
//
let tmp = res.remove(0).result?;
let val = Value::parse("{ name: 'Tobie' }");
assert_eq!(tmp, val);
//
let tmp = res.remove(0).result?;
let val = Value::from("Tobie");
assert_eq!(tmp, val);
//
let tmp = res.remove(0).result?;
let val = Value::parse("[{ name: 'Tobie' }]");
assert_eq!(tmp, val);
//
let tmp = res.remove(0).result?;
let val = Value::parse("['Tobie']");
assert_eq!(tmp, val);
//
let tmp = res.remove(0).result?;
let val = Value::parse("{ name: 'Tobie' }");
assert_eq!(tmp, val);
//
let tmp = res.remove(0).result?;
let val = Value::from("Tobie");
assert_eq!(tmp, val);
//
let tmp = res.remove(0).result?;
let val = Value::parse("[{ name: 'Jaime' }, { name: 'Tobie' }]");
assert_eq!(tmp, val);
//
let tmp = res.remove(0).result?;
let val = Value::parse("['Jaime', 'Tobie']");
assert_eq!(tmp, val);
//
let tmp = res.remove(0).result;
assert!(matches!(
tmp.err(),
Some(e) if e.to_string() == r#"Expected a single result output when using the ONLY keyword"#
));
//
let tmp = res.remove(0).result;
assert!(matches!(
tmp.err(),
Some(e) if e.to_string() == r#"Expected a single result output when using the ONLY keyword"#
));
//
let tmp = res.remove(0).result?;
let val = Value::parse("[{ name: 'Tobie' }]");
assert_eq!(tmp, val);
//
let tmp = res.remove(0).result?;
let val = Value::parse("['Tobie']");
assert_eq!(tmp, val);
//
let tmp = res.remove(0).result?;
let val = Value::parse("{ name: 'Tobie' }");
assert_eq!(tmp, val);
//
let tmp = res.remove(0).result?;
let val = Value::from("Tobie");
assert_eq!(tmp, val);
//
let tmp = res.remove(0).result?;
let val = Value::parse("[{ name: 'Tobie' }]");
assert_eq!(tmp, val);
//
let tmp = res.remove(0).result?;
let val = Value::parse("['Tobie']");
assert_eq!(tmp, val);
//
let tmp = res.remove(0).result?;
let val = Value::parse("{ name: 'Tobie' }");
assert_eq!(tmp, val);
//
let tmp = res.remove(0).result?;
let val = Value::from("Tobie");
assert_eq!(tmp, val);
//
Ok(())
}