Skip to content

Commit

Permalink
test(models): add basic tests for models
Browse files Browse the repository at this point in the history
  • Loading branch information
LeoNero committed Jul 13, 2022
1 parent cdab448 commit 7900386
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

- Add tests for types implementing `HashType` in `utils.rs` ([#59](https://github.com/monero-ecosystem/monero-rpc-rs/pull/59))
- Add tests for `HashString`'s implementation of the traits `Debug`, `Serialize`, and `Deserialize`, in `utils.rs` ([#59](https://github.com/monero-ecosystem/monero-rpc-rs/pull/59))
- Add tests for `models.rs` ([#63](https://github.com/monero-ecosystem/monero-rpc-rs/pull/63))

### Removed

Expand Down
82 changes: 80 additions & 2 deletions src/models.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ impl From<BlockHeaderResponseR> for BlockHeaderResponse {
}

/// Return type of daemon `get_block_header` and `get_block_headers_range`.
#[derive(Clone, Debug, Serialize, Deserialize)]
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct BlockHeaderResponse {
pub block_size: u64,
pub depth: u64,
Expand Down Expand Up @@ -413,7 +413,7 @@ pub struct BlockHeightFilter {
}

/// Sub-type of [`GotTransfer`].
#[derive(Clone, Debug)]
#[derive(Clone, Debug, PartialEq)]
pub enum TransferHeight {
Confirmed(NonZeroU64),
InPool,
Expand Down Expand Up @@ -498,3 +498,81 @@ pub struct KeyImageImportResponse {
/// Amount still available from key images.
pub unspent: u64,
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn monero_result_to_inner() {
let monero_res = MoneroResult::OK(123);
assert_eq!(monero_res.into_inner(), 123);
}

#[test]
fn block_header_response_from_block_header_response_r() {
let bhrr = BlockHeaderResponseR {
block_size: 123,
depth: 1234,
difficulty: 12345,
hash: HashString(BlockHash::zero()),
height: 123456,
major_version: 1234567,
minor_version: 12345678,
nonce: 123456789,
num_txes: 1,
orphan_status: true,
prev_hash: HashString(BlockHash::repeat_byte(12)),
reward: 12,
timestamp: DateTime::<Utc>::from_utc(NaiveDateTime::from_timestamp(61, 0), Utc),
};

let expected_bhr = BlockHeaderResponse {
block_size: 123,
depth: 1234,
difficulty: 12345,
hash: BlockHash::zero(),
height: 123456,
major_version: 1234567,
minor_version: 12345678,
nonce: 123456789,
num_txes: 1,
orphan_status: true,
prev_hash: BlockHash::repeat_byte(12),
reward: 12,
timestamp: DateTime::<Utc>::from_utc(NaiveDateTime::from_timestamp(61, 0), Utc),
};

assert_eq!(BlockHeaderResponse::from(bhrr), expected_bhr);
}

#[test]
fn str_from_get_transfers_category() {
use GetTransfersCategory::*;

let g_in: &str = In.into();
let g_out: &str = Out.into();
let g_pending: &str = Pending.into();
let g_failed: &str = Failed.into();
let g_pool: &str = Pool.into();
let g_block: &str = Block.into();

assert_eq!(g_in, "in");
assert_eq!(g_out, "out");
assert_eq!(g_pending, "pending");
assert_eq!(g_failed, "failed");
assert_eq!(g_pool, "pool");
assert_eq!(g_block, "block");
}

#[test]
fn deserialize_for_transfer_height() {
use serde_test::{assert_de_tokens, Token};

let confirmed = TransferHeight::Confirmed(NonZeroU64::new(10).unwrap());
let in_pool = TransferHeight::InPool;

assert_de_tokens(&confirmed, &[Token::U64(10)]);
assert_de_tokens(&in_pool, &[Token::U64(0)]);
}
}

0 comments on commit 7900386

Please sign in to comment.