Skip to content

Commit

Permalink
batch insert coins test
Browse files Browse the repository at this point in the history
  • Loading branch information
0o-de-lally committed Oct 24, 2024
1 parent 9f84641 commit 847d42f
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 29 deletions.
2 changes: 1 addition & 1 deletion warehouse/src/load_account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ pub async fn insert_one_account(
VALUES ($1,$2)
"#,
)
.bind(acc.address.to_string())
.bind(acc.address.to_hex_literal())
.bind(true)
.execute(pool)
.await?;
Expand Down
4 changes: 2 additions & 2 deletions warehouse/src/load_coin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ pub async fn impl_batch_coin_insert(pool: &SqlitePool, batch_accounts: &[Warehou
);

query_builder.push_values(filtered, |mut b, acc| {
b.push_bind(acc.account.address.to_hex_literal()).push_bind(true)
.push_bind(acc.balance.as_ref().unwrap().legacy_balance.unwrap() as i64).push_bind(true)
b.push_bind(acc.account.address.to_hex_literal())
.push_bind(acc.balance.as_ref().unwrap().legacy_balance.unwrap() as i64)
.push_bind(0) // todo
.push_bind(0) // todo
.push_bind(0); // todo
Expand Down
79 changes: 53 additions & 26 deletions warehouse/tests/test_load.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use std::path::PathBuf;

use libra_types::exports::AccountAddress;
use libra_warehouse::table_structs::WarehouseAccount;
use libra_warehouse::extract::extract_v5_snapshot;
use libra_warehouse::table_structs::{WarehouseAccount, WarehouseBalance, WarehouseState};

use sqlx::SqlitePool;

Expand All @@ -12,62 +12,61 @@ fn v5_state_manifest_fixtures_path() -> PathBuf {
project_root.join("compatibility/fixtures/v5/state_ver_119757649.17a8/state.manifest")
}


#[sqlx::test]
async fn insert_one_account(pool: SqlitePool) -> anyhow::Result<()> {
libra_warehouse::migrate::maybe_init(&pool).await?;
let marlon = AccountAddress::random();
let acc = WarehouseAccount {
address: marlon
};
let acc = WarehouseAccount { address: marlon };

libra_warehouse::load_account::insert_one_account(&pool, &acc).await?;

// second time should error if we are using the same account
assert!(libra_warehouse::load_account::insert_one_account(&pool, &acc).await.is_err());
assert!(
libra_warehouse::load_account::insert_one_account(&pool, &acc)
.await
.is_err()
);

Ok(())
}

#[sqlx::test]
async fn batch_insert(pool: SqlitePool) -> anyhow::Result<()>{
libra_warehouse::migrate::maybe_init(&pool).await?;
async fn batch_insert_account(pool: SqlitePool) -> anyhow::Result<()> {
libra_warehouse::migrate::maybe_init(&pool).await?;
let mut vec_acct: Vec<WarehouseAccount> = vec![];

for _i in [..3] {
let acc = WarehouseAccount {
// uniques
address: AccountAddress::random()
};
vec_acct.push(acc);
let acc = WarehouseAccount {
// uniques
address: AccountAddress::random(),
};
vec_acct.push(acc);
}

libra_warehouse::load_account::impl_batch_insert(&pool, &vec_acct).await?;
Ok(())
libra_warehouse::load_account::impl_batch_insert(&pool, &vec_acct).await?;
Ok(())
}

#[sqlx::test]
async fn batch_duplicates_fail_gracefully(pool: SqlitePool) -> anyhow::Result<()>{
libra_warehouse::migrate::maybe_init(&pool).await?;
async fn batch_duplicates_fail_gracefully(pool: SqlitePool) -> anyhow::Result<()> {
libra_warehouse::migrate::maybe_init(&pool).await?;
let mut vec_acct: Vec<WarehouseAccount> = vec![];

// will create duplicates
let marlon = AccountAddress::random();

for _i in [..3] {
let acc = WarehouseAccount {
address: marlon
};
vec_acct.push(acc);
let acc = WarehouseAccount { address: marlon };
vec_acct.push(acc);
}

// should not fail if duplicates exists on same batch
libra_warehouse::load_account::impl_batch_insert(&pool, &vec_acct).await?;
// should not fail if duplicates exists on same batch
libra_warehouse::load_account::impl_batch_insert(&pool, &vec_acct).await?;

// also should not fail if duplicates are on separate batches
libra_warehouse::load_account::impl_batch_insert(&pool, &vec_acct).await?;
// also should not fail if duplicates are on separate batches
libra_warehouse::load_account::impl_batch_insert(&pool, &vec_acct).await?;

Ok(())
Ok(())
}

#[sqlx::test]
Expand All @@ -86,3 +85,31 @@ async fn test_e2e_load_v5_snapshot(pool: SqlitePool) -> anyhow::Result<()> {
assert!(res == 17338);
Ok(())
}

#[sqlx::test]
async fn batch_insert_coin(pool: SqlitePool) -> anyhow::Result<()> {
libra_warehouse::migrate::maybe_init(&pool).await?;
let mut vec_state: Vec<WarehouseState> = vec![];

for _i in [..3] {
let state = WarehouseState {
account: WarehouseAccount {
// uniques
address: AccountAddress::random(),
},
balance: Some(WarehouseBalance {
balance: 0,
legacy_balance: Some(10),
}),
};

vec_state.push(state);
}

// fist must load accounts
let _res = libra_warehouse::load_account::load_account_state(&pool, &vec_state).await?;

libra_warehouse::load_coin::impl_batch_coin_insert(&pool, &vec_state).await?;

Ok(())
}

0 comments on commit 847d42f

Please sign in to comment.