surrealpatch/sdk/tests/cast.rs

134 lines
3.2 KiB
Rust
Raw Normal View History

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 cast_string_to_record() -> Result<(), Error> {
let sql = r#"
<record> <string> a:1
"#;
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(), 1);
//
let tmp = res.remove(0).result?;
let val = Value::parse("a:1");
assert_eq!(tmp, val);
//
Ok(())
}
#[tokio::test]
async fn cast_to_record_table() -> Result<(), Error> {
let sql = r#"
<record<a>> a:1;
<record<a>> "a:1";
<record<b>> a:1;
<record<b>> "a:1";
"#;
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(), 4);
//
let tmp = res.remove(0).result?;
let val = Value::parse("a:1");
assert_eq!(tmp, val);
//
let tmp = res.remove(0).result?;
let val = Value::parse("a:1");
assert_eq!(tmp, val);
//
match res.remove(0).result {
Err(Error::ConvertTo {
from,
into,
}) if into == "record<b>" && from == Value::parse("a:1") => (),
_ => panic!("Casting should have failed with error: Expected a record<b> but cannot convert a:1 into a record<b>"),
}
//
match res.remove(0).result {
Err(Error::ConvertTo {
from,
into,
}) if into == "record<b>" && from == Value::parse("'a:1'") => (),
_ => panic!("Casting should have failed with error: Expected a record<b> but cannot convert 'a:1' into a record<b>"),
}
//
Ok(())
}
2024-08-20 11:42:06 +00:00
#[tokio::test]
async fn cast_range_to_array() -> Result<(), Error> {
let sql = r#"
<array> 1..5;
<array> 1>..5;
<array> 1..=5;
<array> 1>..=5;
2024-08-21 14:50:37 +00:00
"#;
2024-08-20 11:42:06 +00:00
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(), 4);
//
let tmp = res.remove(0).result?;
let val = Value::parse("[1, 2, 3, 4]");
assert_eq!(tmp, val);
//
let tmp = res.remove(0).result?;
let val = Value::parse("[2, 3, 4]");
assert_eq!(tmp, val);
//
let tmp = res.remove(0).result?;
let val = Value::parse("[1, 2, 3, 4, 5]");
assert_eq!(tmp, val);
//
let tmp = res.remove(0).result?;
let val = Value::parse("[2, 3, 4, 5]");
assert_eq!(tmp, val);
//
Ok(())
}
2024-08-21 14:50:37 +00:00
#[tokio::test]
async fn cast_with_literal_kind() -> Result<(), Error> {
let sql = r#"
<"a" | "b"> "a";
<123 | 456> 123;
<123 | "b"> 123;
<[number, "abc"]> [123, "abc"];
<{ a: 1d | 2d }> { a: 1d };
"#;
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(), 5);
//
let tmp = res.remove(0).result?;
let val = Value::parse("'a'");
assert_eq!(tmp, val);
//
let tmp = res.remove(0).result?;
let val = Value::parse("123");
assert_eq!(tmp, val);
//
let tmp = res.remove(0).result?;
let val = Value::parse("123");
assert_eq!(tmp, val);
//
let tmp = res.remove(0).result?;
let val = Value::parse("[123, 'abc']");
assert_eq!(tmp, val);
//
let tmp = res.remove(0).result?;
let val = Value::parse("{ a: 1d }");
assert_eq!(tmp, val);
//
Ok(())
}