Skip to content

Commit

Permalink
feat(test): add more db query tests (#699)
Browse files Browse the repository at this point in the history
* Test 'get_block_children'

* Test 'get_spending_transaction'

* Add test 'test_ledger_updates_by_address'

* Add test 'test_ledger_updates_by_milestone'

* Add test 'test_spent_unspent_ledger_updates'

* Use 'count_documents'

* Prep milestone tests

* Add 'len' to 'MongoDbCollectionExt' trait with enabled 'rand' feature

* Use 'MongoDbCollectionExt' count

* Clippy

* Fmt

* Go away now

* Rename claiming test module

* Make MongoDbCollection trait async

* Fix merge

* Simplify collection creation in launch code

* Format

* Slight refactor

* Use generic setup helper

* Improve spending transaction test

* Temporarily create only 1 unspent output for the spending tx

* Ensure all inputs fetch the spending block

* Format

* Fix docs

* PR comments 1

* Simplify

* Cleanup

* PR comments 2

* Format

* Add protocol parameters test

* Format and clean up

* PR comments 3

* Fix

Co-authored-by: Jochen Görtler <jochen.goertler@iota.org>
  • Loading branch information
Alex6323 and grtlr committed Oct 13, 2022
1 parent 85cf674 commit 9ffccbb
Show file tree
Hide file tree
Showing 19 changed files with 588 additions and 74 deletions.
2 changes: 1 addition & 1 deletion src/db/collections/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ impl BlockCollection {
doc! { "$skip": (page_size * page) as i64 },
doc! { "$sort": {"metadata.referenced_by_milestone_index": -1} },
doc! { "$limit": page_size as i64 },
doc! { "$replaceWith": { "block_id": "$metadata.block_id" } },
doc! { "$replaceWith": { "block_id": "$_id" } },
],
None,
)
Expand Down
2 changes: 1 addition & 1 deletion src/db/collections/protocol_update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use crate::{
};

/// A milestone's metadata.
#[derive(Clone, Debug, Serialize, Deserialize)]
#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
pub struct ProtocolUpdateDocument {
#[serde(rename = "_id")]
pub tangle_index: MilestoneIndex,
Expand Down
10 changes: 10 additions & 0 deletions src/types/stardust/block/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,16 @@ mod rand {
nonce: rand_number(),
}
}

/// Generates a random [`Block`] with given parents.
pub fn rand_no_payload_with_parents(parents: Box<[BlockId]>) -> Self {
Self {
protocol_version: rand_number(),
parents,
payload: None,
nonce: rand_number(),
}
}
}
}

Expand Down
9 changes: 7 additions & 2 deletions src/types/stardust/block/output/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ pub use self::{
native_token::{NativeToken, NativeTokenAmount, TokenScheme},
nft::{NftId, NftOutput},
treasury::TreasuryOutput,
unlock_condition::{
AddressUnlockCondition, ExpirationUnlockCondition, GovernorAddressUnlockCondition,
ImmutableAliasAddressUnlockCondition, StateControllerAddressUnlockCondition,
StorageDepositReturnUnlockCondition, TimelockUnlockCondition,
},
};
use super::Address;
use crate::types::{
Expand All @@ -45,7 +50,7 @@ pub type OutputIndex = u16;

/// An id which uniquely identifies an output. It is computed from the corresponding [`TransactionId`], as well as the
/// [`OutputIndex`].
#[derive(Copy, Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[derive(Copy, Clone, Debug, PartialEq, Eq, Serialize, Deserialize, Hash)]
pub struct OutputId {
/// The transaction id part of the [`OutputId`].
pub transaction_id: TransactionId,
Expand Down Expand Up @@ -244,7 +249,7 @@ mod rand {
impl Output {
/// Generates a random [`Output`].
pub fn rand(ctx: &bee_block_stardust::protocol::ProtocolParameters) -> Self {
match rand_number_range(0..5) {
match rand_number_range(0..4) {
0 => Self::rand_basic(ctx),
1 => Self::rand_alias(ctx),
2 => Self::rand_foundry(ctx),
Expand Down
2 changes: 2 additions & 0 deletions src/types/stardust/block/output/unlock_condition/address.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@ use serde::{Deserialize, Serialize};

use crate::types::stardust::block::Address;

/// Defines the Address that owns an output.
#[derive(Copy, Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct AddressUnlockCondition {
/// The associated address of this [`AddressUnlockCondition`].
pub address: Address,
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ use serde::{Deserialize, Serialize};

use crate::types::stardust::{block::Address, milestone::MilestoneTimestamp};

/// Defines a unix time until which only Address, defined in Address Unlock Condition, is allowed to unlock the output.
/// After or at the unix time, only Return Address can unlock it.
#[derive(Copy, Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct ExpirationUnlockCondition {
return_address: Address,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,11 @@ use serde::{Deserialize, Serialize};

use crate::types::stardust::block::Address;

/// Defines the Governor Address that owns this output, that is, it can unlock it with the proper Unlock in a
/// transaction that governance transitions the alias output.
#[derive(Copy, Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct GovernorAddressUnlockCondition {
/// The associated address of this [`GovernorAddressUnlockCondition`].
pub address: Address,
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@ use serde::{Deserialize, Serialize};

use crate::types::stardust::block::Address;

/// Defines the permanent alias address that owns this output.
#[derive(Copy, Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct ImmutableAliasAddressUnlockCondition {
/// The associated address of this [`ImmutableAliasAddressUnlockCondition`].
pub address: Address,
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,11 @@ use serde::{Deserialize, Serialize};

use crate::types::stardust::block::Address;

/// Defines the State Controller Address that owns this output, that is, it can unlock it with the proper Unlock in a
/// transaction that state transitions the alias output.
#[derive(Copy, Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct StateControllerAddressUnlockCondition {
/// The associated address of this [`StateControllerAddressUnlockCondition`].
pub address: Address,
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use serde::{Deserialize, Serialize};
use super::OutputAmount;
use crate::types::{context::TryFromWithContext, stardust::block::Address};

/// Defines the amount of tokens used as storage deposit that have to be returned to the return address.
#[derive(Copy, Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct StorageDepositReturnUnlockCondition {
return_address: Address,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use serde::{Deserialize, Serialize};

use crate::types::stardust::milestone::MilestoneTimestamp;

/// Defines a unix timestamp until which the output can not be unlocked.
#[derive(Copy, Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct TimelockUnlockCondition {
timestamp: MilestoneTimestamp,
Expand Down
2 changes: 1 addition & 1 deletion src/types/stardust/block/payload/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use crate::types::{
};

/// Uniquely identifies a transaction.
#[derive(Copy, Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[derive(Copy, Clone, Debug, PartialEq, Eq, Serialize, Deserialize, Hash)]
#[serde(transparent)]
pub struct TransactionId(#[serde(with = "bytify")] pub [u8; Self::LENGTH]);

Expand Down
Loading

0 comments on commit 9ffccbb

Please sign in to comment.