Lines
100 %
Functions
Branches
//! Legacy-shaped rows: two transactions sharing one price, and an ordinary
//! transaction with a price of its own.
use sqlx::PgPool;
use sqlx::types::Uuid;
pub(crate) struct Rows {
pub(crate) tx_a: Uuid,
pub(crate) tx_b: Uuid,
pub(crate) tx_own: Uuid,
pub(crate) cross_price: Uuid,
pub(crate) own_price: Uuid,
}
fn id(n: u128) -> Uuid {
Uuid::from_u128(n)
async fn put_transaction(pool: &PgPool, tx: u128, splits: [u128; 2]) -> anyhow::Result<()> {
sqlx::query("INSERT INTO transactions (id, post_date, enter_date) VALUES ($1, now(), now())")
.bind(id(tx))
.execute(pool)
.await?;
for (split, value) in splits.into_iter().zip([-5_i64, 5]) {
sqlx::query(
"INSERT INTO splits (id, tx_id, account_id, commodity_id, value_num, value_denom) \
VALUES ($1, $2, $3, $4, $5, 1)",
)
.bind(id(split))
.bind(id(2))
.bind(id(1))
.bind(value)
Ok(())
async fn put_price(pool: &PgPool, price: u128, legs: [u128; 2]) -> anyhow::Result<()> {
"INSERT INTO prices (id, commodity_id, currency_id, commodity_split_id, \
currency_split_id, price_date, value_num, value_denom) \
VALUES ($1, $2, $2, $3, $4, now(), 1, 1)",
.bind(id(price))
.bind(id(legs[0]))
.bind(id(legs[1]))
pub(crate) async fn seed(pool: &PgPool) -> anyhow::Result<Rows> {
sqlx::query("INSERT INTO commodities (id) VALUES ($1)")
sqlx::query("INSERT INTO accounts (id) VALUES ($1)")
put_transaction(pool, 10, [11, 12]).await?;
put_transaction(pool, 20, [21, 22]).await?;
put_transaction(pool, 30, [31, 32]).await?;
put_price(pool, 100, [11, 21]).await?;
put_price(pool, 200, [31, 32]).await?;
Ok(Rows {
tx_a: id(10),
tx_b: id(20),
tx_own: id(30),
cross_price: id(100),
own_price: id(200),
})