2022-09-30 20:42:16 +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-09-30 20:42:16 +00:00
|
|
|
use surrealdb::sql::Value;
|
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
async fn create_relate_select() -> Result<(), Error> {
|
|
|
|
let sql = "
|
|
|
|
CREATE user:tobie SET name = 'Tobie';
|
|
|
|
CREATE user:jaime SET name = 'Jaime';
|
|
|
|
CREATE product:phone SET price = 1000;
|
|
|
|
CREATE product:laptop SET price = 3000;
|
|
|
|
RELATE user:tobie->bought->product:phone SET id = bought:1, payment_method = 'VISA';
|
|
|
|
RELATE user:tobie->bought->product:laptop SET id = bought:2, payment_method = 'VISA';
|
|
|
|
RELATE user:jaime->bought->product:laptop SET id = bought:3, payment_method = 'VISA';
|
|
|
|
SELECT *, ->bought AS purchases FROM user;
|
|
|
|
SELECT *, ->bought.out.* AS products FROM user;
|
|
|
|
SELECT *, ->bought->product.* AS products FROM user;
|
|
|
|
SELECT *, ->bought AS products FROM user FETCH products;
|
|
|
|
SELECT *, ->(bought AS purchases) FROM user FETCH purchases, purchases.out;
|
|
|
|
";
|
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-09-30 20:42:16 +00:00
|
|
|
assert_eq!(res.len(), 12);
|
|
|
|
//
|
|
|
|
let tmp = res.remove(0).result?;
|
|
|
|
let val = Value::parse(
|
|
|
|
"[
|
|
|
|
{
|
|
|
|
id: user:tobie,
|
|
|
|
name: 'Tobie'
|
|
|
|
}
|
|
|
|
]",
|
|
|
|
);
|
|
|
|
assert_eq!(tmp, val);
|
|
|
|
//
|
|
|
|
let tmp = res.remove(0).result?;
|
|
|
|
let val = Value::parse(
|
|
|
|
"[
|
|
|
|
{
|
|
|
|
id: user:jaime,
|
|
|
|
name: 'Jaime'
|
|
|
|
}
|
|
|
|
]",
|
|
|
|
);
|
|
|
|
assert_eq!(tmp, val);
|
|
|
|
//
|
|
|
|
let tmp = res.remove(0).result?;
|
|
|
|
let val = Value::parse(
|
|
|
|
"[
|
|
|
|
{
|
|
|
|
id: product:phone,
|
|
|
|
price: 1000
|
|
|
|
}
|
|
|
|
]",
|
|
|
|
);
|
|
|
|
assert_eq!(tmp, val);
|
|
|
|
//
|
|
|
|
let tmp = res.remove(0).result?;
|
|
|
|
let val = Value::parse(
|
|
|
|
"[
|
|
|
|
{
|
|
|
|
id: product:laptop,
|
|
|
|
price: 3000
|
|
|
|
}
|
|
|
|
]",
|
|
|
|
);
|
|
|
|
assert_eq!(tmp, val);
|
|
|
|
//
|
|
|
|
let tmp = res.remove(0).result?;
|
|
|
|
let val = Value::parse(
|
|
|
|
"[
|
|
|
|
{
|
|
|
|
'id': bought:1,
|
|
|
|
'in': user:tobie,
|
|
|
|
'out': product:phone,
|
|
|
|
'payment_method': 'VISA'
|
|
|
|
}
|
|
|
|
]",
|
|
|
|
);
|
|
|
|
assert_eq!(tmp, val);
|
|
|
|
//
|
|
|
|
let tmp = res.remove(0).result?;
|
|
|
|
let val = Value::parse(
|
|
|
|
"[
|
|
|
|
{
|
|
|
|
'id': bought:2,
|
|
|
|
'in': user:tobie,
|
|
|
|
'out': product:laptop,
|
|
|
|
'payment_method': 'VISA'
|
|
|
|
}
|
|
|
|
]",
|
|
|
|
);
|
|
|
|
assert_eq!(tmp, val);
|
|
|
|
//
|
|
|
|
let tmp = res.remove(0).result?;
|
|
|
|
let val = Value::parse(
|
|
|
|
"[
|
|
|
|
{
|
|
|
|
'id': bought:3,
|
|
|
|
'in': user:jaime,
|
|
|
|
'out': product:laptop,
|
|
|
|
'payment_method': 'VISA'
|
|
|
|
}
|
|
|
|
]",
|
|
|
|
);
|
|
|
|
assert_eq!(tmp, val);
|
|
|
|
//
|
|
|
|
let tmp = res.remove(0).result?;
|
|
|
|
let val = Value::parse(
|
|
|
|
"[
|
|
|
|
{
|
|
|
|
id: user:jaime,
|
|
|
|
name: 'Jaime',
|
|
|
|
purchases: [
|
|
|
|
bought:3
|
|
|
|
]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
id: user:tobie,
|
|
|
|
name: 'Tobie',
|
|
|
|
purchases: [
|
|
|
|
bought:1,
|
|
|
|
bought:2
|
|
|
|
]
|
|
|
|
}
|
|
|
|
]",
|
|
|
|
);
|
|
|
|
assert_eq!(tmp, val);
|
|
|
|
//
|
|
|
|
let tmp = res.remove(0).result?;
|
|
|
|
let val = Value::parse(
|
|
|
|
"[
|
|
|
|
{
|
|
|
|
id: user:jaime,
|
|
|
|
name: 'Jaime',
|
|
|
|
products: [
|
|
|
|
{
|
|
|
|
id: product:laptop,
|
|
|
|
price: 3000
|
|
|
|
}
|
|
|
|
]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
id: user:tobie,
|
|
|
|
name: 'Tobie',
|
|
|
|
products: [
|
|
|
|
{
|
|
|
|
id: product:phone,
|
|
|
|
price: 1000
|
|
|
|
},
|
|
|
|
{
|
|
|
|
id: product:laptop,
|
|
|
|
price: 3000
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
]",
|
|
|
|
);
|
|
|
|
assert_eq!(tmp, val);
|
|
|
|
//
|
|
|
|
let tmp = res.remove(0).result?;
|
|
|
|
let val = Value::parse(
|
|
|
|
"[
|
|
|
|
{
|
|
|
|
id: user:jaime,
|
|
|
|
name: 'Jaime',
|
|
|
|
products: [
|
|
|
|
{
|
|
|
|
id: product:laptop,
|
|
|
|
price: 3000
|
|
|
|
}
|
|
|
|
]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
id: user:tobie,
|
|
|
|
name: 'Tobie',
|
|
|
|
products: [
|
|
|
|
{
|
|
|
|
id: product:phone,
|
|
|
|
price: 1000
|
|
|
|
},
|
|
|
|
{
|
|
|
|
id: product:laptop,
|
|
|
|
price: 3000
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
]",
|
|
|
|
);
|
|
|
|
assert_eq!(tmp, val);
|
|
|
|
//
|
|
|
|
let tmp = res.remove(0).result?;
|
|
|
|
let val = Value::parse(
|
|
|
|
"[
|
|
|
|
{
|
|
|
|
id: user:jaime,
|
|
|
|
name: 'Jaime',
|
|
|
|
products: [
|
|
|
|
{
|
|
|
|
id: bought:3,
|
|
|
|
in: user:jaime,
|
|
|
|
out: product:laptop,
|
|
|
|
payment_method: 'VISA'
|
|
|
|
}
|
|
|
|
]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
id: user:tobie,
|
|
|
|
name: 'Tobie',
|
|
|
|
products: [
|
|
|
|
{
|
|
|
|
id: bought:1,
|
|
|
|
in: user:tobie,
|
|
|
|
out: product:phone,
|
|
|
|
payment_method: 'VISA'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
id: bought:2,
|
|
|
|
in: user:tobie,
|
|
|
|
out: product:laptop,
|
|
|
|
payment_method: 'VISA'
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
]",
|
|
|
|
);
|
|
|
|
assert_eq!(tmp, val);
|
|
|
|
//
|
|
|
|
let tmp = res.remove(0).result?;
|
|
|
|
let val = Value::parse(
|
|
|
|
"[
|
|
|
|
{
|
|
|
|
id: user:jaime,
|
|
|
|
name: 'Jaime',
|
|
|
|
purchases: [
|
|
|
|
{
|
|
|
|
id: bought:3,
|
|
|
|
in: user:jaime,
|
|
|
|
out: {
|
|
|
|
id: product:laptop,
|
|
|
|
price: 3000
|
|
|
|
},
|
|
|
|
payment_method: 'VISA'
|
|
|
|
}
|
|
|
|
]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
id: user:tobie,
|
|
|
|
name: 'Tobie',
|
|
|
|
purchases: [
|
|
|
|
{
|
|
|
|
id: bought:1,
|
|
|
|
in: user:tobie,
|
|
|
|
out: {
|
|
|
|
id: product:phone,
|
|
|
|
price: 1000
|
|
|
|
},
|
|
|
|
payment_method: 'VISA'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
id: bought:2,
|
|
|
|
in: user:tobie,
|
|
|
|
out: {
|
|
|
|
id: product:laptop,
|
|
|
|
price: 3000
|
|
|
|
},
|
|
|
|
payment_method: 'VISA'
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
]",
|
|
|
|
);
|
|
|
|
assert_eq!(tmp, val);
|
|
|
|
//
|
|
|
|
Ok(())
|
|
|
|
}
|