use vantage_expressions::ExprDataSource;
#[allow(unused_imports)]
use vantage_sql::postgres::PostgresType;
use vantage_sql::postgres::{AnyPostgresType, PostgresDB};
use vantage_sql::postgres_expr;
use vantage_types::{Record, TryFromRecord, entity};
const PG_URL: &str = "postgres://vantage:vantage@localhost:5433/vantage";
async fn setup(table: &str) -> PostgresDB {
let db = PostgresDB::connect(PG_URL).await.unwrap();
sqlx::query(&format!("DROP TABLE IF EXISTS \"{}\"", table))
.execute(db.pool())
.await
.unwrap();
sqlx::query(&format!(
"CREATE TABLE \"{}\" (
id TEXT PRIMARY KEY,
name TEXT NOT NULL,
price BIGINT NOT NULL
)",
table
))
.execute(db.pool())
.await
.unwrap();
sqlx::query(&format!(
"INSERT INTO \"{}\" VALUES ('a', 'Cheap', 50), ('b', 'Mid', 150), ('c', 'Expensive', 300)",
table
))
.execute(db.pool())
.await
.unwrap();
db
}
#[entity(PostgresType)]
struct Product {
id: String,
name: String,
price: i64,
}
#[tokio::test]
async fn test_associated_scalar() {
let db = setup("assoc_scalar").await;
let associated = db.associate::<i64>(postgres_expr!("SELECT COUNT(*) FROM \"assoc_scalar\""));
assert_eq!(associated.get().await.unwrap(), 3);
}
#[tokio::test]
async fn test_associated_entity() {
let db = setup("assoc_entity").await;
let associated = db.associate::<Record<AnyPostgresType>>(postgres_expr!(
"SELECT id, name, price FROM \"assoc_entity\" WHERE id = {}",
"c"
));
let record = associated.get().await.unwrap();
let product = Product::from_record(record).unwrap();
assert_eq!(product.id, "c");
assert_eq!(product.name, "Expensive");
assert_eq!(product.price, 300);
}