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(models): add basic tests for models #63

Merged
merged 1 commit into from
Jul 13, 2022
Merged
Show file tree
Hide file tree
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
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)]);
}
}