Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test(sequencer): add unit tests for src/asset/state_ext #874

Merged
merged 2 commits into from
Apr 1, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
149 changes: 149 additions & 0 deletions crates/astria-sequencer/src/asset/state_ext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,3 +72,152 @@ pub(crate) trait StateWriteExt: StateWrite {
}

impl<T: StateWrite> StateWriteExt for T {}

#[cfg(test)]
mod test {
use astria_core::sequencer::v1::asset::{
Denom,
Id,
};
use cnidarium::StateDelta;

use super::{
StateReadExt as _,
StateWriteExt as _,
};

#[tokio::test]
async fn get_ibc_asset_non_existent() {
let storage = cnidarium::TempStorage::new().await.unwrap();
let snapshot = storage.latest_snapshot();
let state = StateDelta::new(snapshot);

let asset = Id::from_denom("asset");

// gets for non existing assets fail
state
.get_ibc_asset(asset)
.await
.expect_err("gets for non existing ibc assets should fail");
}

#[tokio::test]
async fn has_ibc_asset() {
let storage = cnidarium::TempStorage::new().await.unwrap();
let snapshot = storage.latest_snapshot();
let mut state = StateDelta::new(snapshot);

let denom = Denom::from_base_denom("asset");

// non existing calls are ok for 'has'
assert!(
!state
.has_ibc_asset(denom.id())
.await
.expect("'has' for non existing ibc assets should be ok"),
"query for non existing asset should return false"
);

state
.put_ibc_asset(denom.id(), &denom)
.expect("putting ibc asset should not fail");

// existing calls are ok for 'has'
assert!(
state
.has_ibc_asset(denom.id())
.await
.expect("'has' for existing ibc assets should be ok"),
"query for existing asset should return true"
);
}

#[tokio::test]
async fn put_ibc_asset_simple() {
let storage = cnidarium::TempStorage::new().await.unwrap();
let snapshot = storage.latest_snapshot();
let mut state = StateDelta::new(snapshot);

// can write new
let denom = Denom::from_base_denom("asset");
state
.put_ibc_asset(denom.id(), &denom)
.expect("putting ibc asset should not fail");
assert_eq!(
state
.get_ibc_asset(denom.id())
.await
.expect("an ibc asset was written and must exist inside the database"),
denom,
"stored ibc asset was not what was expected"
);
}

#[tokio::test]
async fn put_ibc_asset_complex() {
let storage = cnidarium::TempStorage::new().await.unwrap();
let snapshot = storage.latest_snapshot();
let mut state = StateDelta::new(snapshot);

// can write new
let denom = Denom::from_base_denom("asset_0");
state
.put_ibc_asset(denom.id(), &denom)
.expect("putting ibc asset should not fail");
assert_eq!(
state
.get_ibc_asset(denom.id())
.await
.expect("an ibc asset was written and must exist inside the database"),
denom,
"stored ibc asset was not what was expected"
);

// can write another without affecting original
let denom_1 = Denom::from_base_denom("asset_1");
state
.put_ibc_asset(denom_1.id(), &denom_1)
.expect("putting ibc asset should not fail");
assert_eq!(
state
.get_ibc_asset(denom_1.id())
.await
.expect("an additional ibc asset was written and must exist inside the database"),
denom_1,
"additional ibc asset was not what was expected"
);
assert_eq!(
state
.get_ibc_asset(denom.id())
.await
.expect("an ibc asset was written and must exist inside the database"),
denom,
"original ibc asset was not what was expected"
);
}

#[tokio::test]
async fn put_ibc_asset_can_write_unrelated_ids_to_denoms() {
let storage = cnidarium::TempStorage::new().await.unwrap();
let snapshot = storage.latest_snapshot();
let mut state = StateDelta::new(snapshot);

// can write unrelated ids and denoms
let id_key = Id::from_denom("asset_0");
let denom = Denom::from_base_denom("asset_1");
state
.put_ibc_asset(id_key, &denom)
.expect("putting ibc asset should not fail");

// see that id key and denom's stored id differ
assert_ne!(
state
.get_ibc_asset(id_key)
.await
.expect("an ibc asset was written and must exist inside the database")
.id(),
id_key,
"stored ibc asset was not what was expected"
);
}
}
Loading