Skip to content

Commit

Permalink
test: more test helpers and more chain tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mateuszjasiuk committed Aug 27, 2024
1 parent e1105ed commit e374001
Show file tree
Hide file tree
Showing 12 changed files with 188 additions and 15 deletions.
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[workspace]
resolver = "2"

members = ["chain", "shared", "rewards", "orm", "pos", "governance", "webserver", "seeder", "parameters", "transactions"]
members = ["chain", "shared", "rewards", "orm", "pos", "governance", "webserver", "seeder", "parameters", "transactions", "test_helpers"]

[workspace.package]
authors = ["Heliax <hello@heliax.dev>"]
Expand Down Expand Up @@ -74,6 +74,7 @@ diesel = { version = "2.1.0", features = [
] }
diesel-derive-enum = { version = "2.1.0", features = ["postgres"] }
orm = { path = "orm" }
test_helpers = { path = "test_helpers" }
shared = { path = "shared" }
lazy_static = "1.4.0"
validator = { version = "0.16.0", features = ["derive"] }
Expand Down
1 change: 1 addition & 0 deletions chain/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ name = "chain"
path = "src/main.rs"

[dependencies]
test_helpers.workspace = true
tokio.workspace = true
tracing.workspace = true
tracing-subscriber.workspace = true
Expand Down
6 changes: 0 additions & 6 deletions chain/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,3 @@ pub struct AppConfig {
#[command(flatten)]
pub verbosity: Verbosity<InfoLevel>,
}

#[derive(clap::Parser)]
pub struct TestConfig {
#[clap(long, env)]
pub database_url_test: String,
}
1 change: 0 additions & 1 deletion chain/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,3 @@ pub mod app_state;
pub mod config;
pub mod repository;
pub mod services;
pub mod test_db;
6 changes: 3 additions & 3 deletions chain/src/repository/balance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,14 +73,14 @@ mod tests {
use orm::balances::BalanceDb;
use shared::balance::{Amount, Balance};
use shared::id::Id;
use test_helpers::config::TestConfig;
use test_helpers::db::TestDb;

use super::*;
use crate::config::TestConfig;
use crate::test_db::TestDb;

/// Test that the function correctly handles an empty `balances` input.
#[tokio::test]
async fn test_insert_balance_with_empty_balances_new() {
async fn test_insert_balance_with_empty_balances() {
let config = TestConfig::parse();
let db = TestDb::new(&config);

Expand Down
148 changes: 148 additions & 0 deletions chain/src/repository/crawler_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,151 @@ pub fn update_crawler_timestamp(

anyhow::Ok(())
}

#[cfg(test)]
mod tests {

use anyhow::Context;
use clap::Parser;
use diesel::QueryDsl;
use orm::crawler_state::ChainCrawlerStateDb;
use shared::block::{BlockHeight, Epoch};
use test_helpers::config::TestConfig;
use test_helpers::db::TestDb;

use super::*;

// Test case for successfully inserting a new crawler state
#[tokio::test]
async fn test_upsert_crawler_state_insert_success() {
let config = TestConfig::parse();
let db = TestDb::new(&config);

db.run_test(|conn| {
let crawler_state = ChainCrawlerState {
last_processed_block: 1,
last_processed_epoch: 1,
first_block_in_epoch: 1,
timestamp: 1,
};
upsert_crawler_state(conn, crawler_state.clone())?;

let queried_state = query_chain_state(conn)?;

assert_eq!(queried_state, crawler_state);

anyhow::Ok(())
})
.await
.expect("Failed to run test");
}

// Test case for successfully updating an existing crawler state
#[tokio::test]
async fn test_upsert_crawler_state_update_success() {
let config = TestConfig::parse();
let db = TestDb::new(&config);

db.run_test(|conn| {
let initial_crawler_state = ChainCrawlerState {
last_processed_block: 1,
last_processed_epoch: 1,
first_block_in_epoch: 1,
timestamp: 1,
};

let crawler_state = ChainCrawlerState {
last_processed_block: 2,
last_processed_epoch: 2,
first_block_in_epoch: 2,
timestamp: 2,
};
seed_chain_state(conn, initial_crawler_state.clone())?;

upsert_crawler_state(conn, crawler_state.clone())?;

let queried_state = query_chain_state(conn)?;

assert_eq!(queried_state, crawler_state);

anyhow::Ok(())
})
.await
.expect("Failed to run test");
}

// Test case for successfully updating an existing crawler timestamp
#[tokio::test]
async fn test_update_crawler_timestamp_success() {
let config = TestConfig::parse();
let db = TestDb::new(&config);

db.run_test(|conn| {
let initial_crawler_state = ChainCrawlerState {
last_processed_block: 1,
last_processed_epoch: 1,
first_block_in_epoch: 1,
timestamp: 1,
};

seed_chain_state(conn, initial_crawler_state.clone())?;

let new_timestamp = chrono::Utc::now().naive_utc();
update_crawler_timestamp(conn, new_timestamp)?;

let queried_state = query_chain_state(conn)?;

assert_eq!(
queried_state,
ChainCrawlerState {
timestamp: new_timestamp.and_utc().timestamp(),
..initial_crawler_state
}
);

anyhow::Ok(())
})
.await
.expect("Failed to run test");
}

fn seed_chain_state(
conn: &mut PgConnection,
crawler_state: ChainCrawlerState,
) -> anyhow::Result<()> {
let crawler_state_db = ChainStateInsertDb::from((
CrawlerName::Chain,
crawler_state.clone(),
));
diesel::insert_into(crawler_state::table)
.values::<ChainStateInsertDb>(crawler_state_db)
.execute(conn)
.context("Failed to update crawler_state in db")?;

anyhow::Ok(())
}

fn query_chain_state(
conn: &mut PgConnection,
) -> anyhow::Result<ChainCrawlerState> {
let crawler_state: ChainCrawlerStateDb = crawler_state::table
.filter(crawler_state::name.eq(CrawlerNameDb::Chain))
.select((
crawler_state::dsl::last_processed_block,
crawler_state::dsl::last_processed_epoch,
crawler_state::dsl::first_block_in_epoch,
crawler_state::dsl::timestamp,
))
.first(conn)
.context("Failed to query all balances")?;

Ok(ChainCrawlerState {
last_processed_block: crawler_state.last_processed_block
as BlockHeight,
last_processed_epoch: crawler_state.last_processed_epoch as Epoch,
first_block_in_epoch: crawler_state.first_block_in_epoch
as BlockHeight,
timestamp: crawler_state.timestamp.and_utc().timestamp(),
})
}
}
2 changes: 2 additions & 0 deletions orm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ name = "orm"
path = "src/lib.rs"

[dependencies]
anyhow.workspace = true
clap.workspace = true
deadpool-diesel.workspace = true
diesel-derive-enum.workspace = true
diesel.workspace = true
Expand Down
2 changes: 1 addition & 1 deletion shared/src/crawler_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ pub enum CrawlerName {
Transactions,
}

#[derive(Debug)]
#[derive(Clone, Debug, PartialEq)]
pub struct ChainCrawlerState {
pub last_processed_block: BlockHeight,
pub last_processed_epoch: Epoch,
Expand Down
22 changes: 22 additions & 0 deletions test_helpers/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
[package]
name = "test_helpers"
description = "Namada indexer test_helpers."
resolver = "2"
authors.workspace = true
edition.workspace = true
license.workspace = true
readme.workspace = true
version.workspace = true

[lib]
name = "test_helpers"
path = "src/lib.rs"

[dependencies]
orm.workspace = true
anyhow.workspace = true
clap.workspace = true
deadpool-diesel.workspace = true
diesel.workspace = true
serde.workspace = true
shared.workspace = true
5 changes: 5 additions & 0 deletions test_helpers/src/config.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#[derive(clap::Parser)]
pub struct TestConfig {
#[clap(long, env)]
pub database_url_test: String,
}
5 changes: 2 additions & 3 deletions chain/src/test_db.rs → test_helpers/src/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ pub struct TestDb {
name: String,
pool: Pool,
}

impl TestDb {
pub fn new(config: &TestConfig) -> Self {
let name = format!(
Expand Down Expand Up @@ -57,9 +58,7 @@ impl TestDb {

pub async fn run_test(
&self,
test: impl Fn(&mut PgConnection) -> anyhow::Result<()>
+ namada_sdk::MaybeSend
+ 'static,
test: impl Fn(&mut PgConnection) -> anyhow::Result<()> + Send + 'static,
) -> anyhow::Result<()> {
let conn = &mut self.pool.get().await?;

Expand Down
2 changes: 2 additions & 0 deletions test_helpers/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
pub mod config;
pub mod db;

0 comments on commit e374001

Please sign in to comment.