2022-08-28 22:07:38 +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-08-28 22:07:38 +00:00
|
|
|
use surrealdb::sql::Value;
|
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
async fn define_foreign_table() -> Result<(), Error> {
|
|
|
|
let sql = "
|
|
|
|
DEFINE TABLE person SCHEMALESS;
|
|
|
|
DEFINE TABLE person_by_age AS
|
|
|
|
SELECT
|
|
|
|
count(),
|
|
|
|
age,
|
|
|
|
math::sum(age) AS total,
|
|
|
|
math::mean(score) AS average
|
|
|
|
FROM person
|
|
|
|
GROUP BY age
|
|
|
|
;
|
|
|
|
INFO FOR TABLE person;
|
|
|
|
UPDATE person:one SET age = 39, score = 70;
|
|
|
|
SELECT * FROM person_by_age;
|
|
|
|
UPDATE person:two SET age = 39, score = 80;
|
|
|
|
SELECT * FROM person_by_age;
|
|
|
|
UPDATE person:two SET age = 39, score = 90;
|
|
|
|
SELECT * FROM person_by_age;
|
|
|
|
";
|
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-28 22:07:38 +00:00
|
|
|
assert_eq!(res.len(), 9);
|
|
|
|
//
|
|
|
|
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(
|
|
|
|
"{
|
2023-05-31 12:35:29 +00:00
|
|
|
events: {},
|
|
|
|
fields: {},
|
|
|
|
tables: { person_by_age: 'DEFINE TABLE person_by_age SCHEMALESS AS SELECT count(), age, math::sum(age) AS total, math::mean(score) AS average FROM person GROUP BY age' },
|
|
|
|
indexes: {},
|
2022-08-28 22:07:38 +00:00
|
|
|
}",
|
|
|
|
);
|
|
|
|
assert_eq!(tmp, val);
|
|
|
|
//
|
|
|
|
let tmp = res.remove(0).result?;
|
|
|
|
let val = Value::parse("[{ id: person:one, age: 39, score: 70 }]");
|
|
|
|
assert_eq!(tmp, val);
|
|
|
|
//
|
|
|
|
let tmp = res.remove(0).result?;
|
|
|
|
let val = Value::parse(
|
|
|
|
"[
|
|
|
|
{
|
|
|
|
age: 39,
|
|
|
|
average: 70,
|
|
|
|
count: 1,
|
2022-10-03 23:27:38 +00:00
|
|
|
id: person_by_age:[39],
|
2022-08-28 22:07:38 +00:00
|
|
|
total: 39
|
|
|
|
}
|
|
|
|
]",
|
|
|
|
);
|
|
|
|
assert_eq!(tmp, val);
|
|
|
|
//
|
|
|
|
let tmp = res.remove(0).result?;
|
|
|
|
let val = Value::parse("[{ id: person:two, age: 39, score: 80 }]");
|
|
|
|
assert_eq!(tmp, val);
|
|
|
|
//
|
|
|
|
let tmp = res.remove(0).result?;
|
|
|
|
let val = Value::parse(
|
|
|
|
"[
|
|
|
|
{
|
|
|
|
age: 39,
|
|
|
|
average: 75,
|
|
|
|
count: 2,
|
2022-10-03 23:27:38 +00:00
|
|
|
id: person_by_age:[39],
|
2022-08-28 22:07:38 +00:00
|
|
|
total: 78
|
|
|
|
}
|
|
|
|
]",
|
|
|
|
);
|
|
|
|
assert_eq!(tmp, val);
|
|
|
|
//
|
|
|
|
let tmp = res.remove(0).result?;
|
|
|
|
let val = Value::parse("[{ id: person:two, age: 39, score: 90 }]");
|
|
|
|
assert_eq!(tmp, val);
|
|
|
|
//
|
|
|
|
let tmp = res.remove(0).result?;
|
|
|
|
let val = Value::parse(
|
|
|
|
"[
|
|
|
|
{
|
|
|
|
age: 39,
|
|
|
|
average: 80,
|
|
|
|
count: 2,
|
2022-10-03 23:27:38 +00:00
|
|
|
id: person_by_age:[39],
|
2022-08-28 22:07:38 +00:00
|
|
|
total: 78
|
|
|
|
}
|
|
|
|
]",
|
|
|
|
);
|
|
|
|
assert_eq!(tmp, val);
|
|
|
|
//
|
|
|
|
Ok(())
|
|
|
|
}
|