From e26d86b41243f4995e5179f9c7cb6f1799e7e51c Mon Sep 17 00:00:00 2001 From: Tobie Morgan Hitchcock Date: Sun, 28 Aug 2022 23:07:38 +0100 Subject: [PATCH] =?UTF-8?q?Add=20initial=20tests=20for=20DEFINE=20TABLE=20?= =?UTF-8?q?=E2=80=A6=20AS=20=E2=80=A6=20statements?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/tests/table.rs | 106 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 106 insertions(+) create mode 100644 lib/tests/table.rs diff --git a/lib/tests/table.rs b/lib/tests/table.rs new file mode 100644 index 00000000..168974c4 --- /dev/null +++ b/lib/tests/table.rs @@ -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(()) +}