Add initial tests for DEFINE TABLE … AS … statements

This commit is contained in:
Tobie Morgan Hitchcock 2022-08-28 23:07:38 +01:00
parent e6d3c1e977
commit e26d86b412

106
lib/tests/table.rs Normal file
View file

@ -0,0 +1,106 @@
mod parse;
use parse::Parse;
use surrealdb::sql::Value;
use surrealdb::Datastore;
use surrealdb::Error;
use surrealdb::Session;
#[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;
";
let dbs = Datastore::new("memory").await?;
let ses = Session::for_kv().with_ns("test").with_db("test");
let res = &mut dbs.execute(&sql, &ses, None, false).await?;
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(
"{
ev: {},
fd: {},
ft: { 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' },
ix: {},
}",
);
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,
id: 'person_by_age:39',
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,
id: 'person_by_age:39',
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,
id: 'person_by_age:39',
total: 78
}
]",
);
assert_eq!(tmp, val);
//
Ok(())
}