Skip to content

Commit

Permalink
fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
joel-u410 committed Dec 10, 2024
1 parent 274b8a5 commit 203c0b9
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 1 deletion.
34 changes: 34 additions & 0 deletions chain/src/repository/balance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,13 @@ mod tests {
use namada_sdk::token::Amount as NamadaAmount;
use namada_sdk::uint::MAX_SIGNED_VALUE;
use orm::balances::BalanceDb;
use orm::blocks::BlockInsertDb;
use orm::schema::blocks;
use orm::views::balances;
use shared::balance::{Amount, Balance};
use shared::id::Id;
use shared::token::IbcToken;
use std::collections::HashSet;
use test_helpers::db::TestDb;

use super::*;
Expand Down Expand Up @@ -130,6 +133,8 @@ mod tests {

insert_tokens(conn, vec![token.clone()])?;

seed_blocks_from_balances(conn, &vec![balance.clone()])?;

insert_balances(conn, vec![balance.clone()])?;

let queried_balance = query_balance_by_address(conn, owner, token)?;
Expand Down Expand Up @@ -175,6 +180,7 @@ mod tests {
..(balance.clone())
};

seed_blocks_from_balances(conn, &vec![new_balance.clone()])?;
insert_balances(conn, vec![new_balance])?;

let queried_balance =
Expand Down Expand Up @@ -376,6 +382,8 @@ mod tests {

seed_tokens_from_balance(conn, fake_balances.clone())?;

seed_blocks_from_balances(conn, &fake_balances)?;

insert_balances(conn, fake_balances.clone())?;

assert_eq!(query_all_balances(conn)?.len(), fake_balances.len());
Expand Down Expand Up @@ -410,6 +418,7 @@ mod tests {

insert_tokens(conn, vec![token.clone()])?;

seed_blocks_from_balances(conn, &vec![balance.clone()])?;
insert_balances(conn, vec![balance.clone()])?;

let queried_balance = query_balance_by_address(conn, owner, token)?;
Expand Down Expand Up @@ -442,6 +451,8 @@ mod tests {

insert_tokens(conn, vec![token])?;

seed_blocks_from_balances(conn, &balances)?;

let res = insert_balances(conn, balances);

assert!(res.is_ok());
Expand Down Expand Up @@ -475,6 +486,8 @@ mod tests {

seed_tokens_from_balance(conn, balances.clone())?;

seed_blocks_from_balances(conn, &balances)?;

let res = insert_balances(conn, balances);

assert!(res.is_ok());
Expand All @@ -500,12 +513,33 @@ mod tests {
anyhow::Ok(())
}

fn seed_blocks_from_balances(
conn: &mut PgConnection,
balances: &Vec<Balance>,
) -> anyhow::Result<()> {
for height in balances
.into_iter()
.map(|balance| balance.height as i32)
.collect::<HashSet<_>>()
{
diesel::insert_into(blocks::table)
.values::<&BlockInsertDb>(&BlockInsertDb::fake(height))
.on_conflict_do_nothing()
.execute(conn)
.context("Failed to insert block in db")?;
}

anyhow::Ok(())
}

fn seed_balance(
conn: &mut PgConnection,
balances: Vec<Balance>,
) -> anyhow::Result<()> {
seed_tokens_from_balance(conn, balances.clone())?;

seed_blocks_from_balances(conn, &balances)?;

diesel::insert_into(balance_changes::table)
.values::<&Vec<BalanceChangesInsertDb>>(
&balances
Expand Down
15 changes: 15 additions & 0 deletions orm/src/blocks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,18 @@ impl From<(Block, TendermintBlockResponse)> for BlockInsertDb {
}
}
}

impl BlockInsertDb {
pub fn fake(height: i32) -> Self {
Self {
height,
hash: height.to_string(), // fake hash but ensures uniqueness with height
app_hash: "fake_app_hash".to_string(), // doesn't require uniqueness
timestamp: chrono::DateTime::from_timestamp(0, 0)
.unwrap()
.naive_utc(),
proposer: "fake_proposer".to_string(),
epoch: 0,
}
}
}
2 changes: 1 addition & 1 deletion shared/src/balance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ impl Balance {
owner: Id::Account(address.to_string()),
token,
amount: Amount::fake(),
height: (0..10000).fake::<u32>(),
height: 0,
}
}
}

0 comments on commit 203c0b9

Please sign in to comment.