Skip to content
This repository was archived by the owner on May 20, 2025. It is now read-only.

Commit 55a6882

Browse files
Alex6323grtlr
andauthored
feat(test): add even more db query tests (#806)
* 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 * Add treasury update tests Co-authored-by: Jochen Görtler <[email protected]>
1 parent 9ffccbb commit 55a6882

File tree

6 files changed

+94
-5
lines changed

6 files changed

+94
-5
lines changed

src/types/stardust/block/payload/milestone/milestone_id.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use serde::{Deserialize, Serialize};
1010
use crate::types::util::bytify;
1111

1212
/// Uniquely identifies a milestone.
13-
#[derive(Copy, Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
13+
#[derive(Copy, Clone, Debug, PartialEq, Eq, Serialize, Deserialize, Hash)]
1414
#[serde(transparent)]
1515
pub struct MilestoneId(#[serde(with = "bytify")] pub [u8; Self::LENGTH]);
1616

src/types/tangle/milestone_index.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use serde::{Deserialize, Serialize};
1010

1111
/// The index of a given milestone.
1212
#[derive(
13-
Clone, Copy, PartialOrd, Ord, PartialEq, Eq, Debug, Default, Serialize, Deserialize, Add, Sub, Deref, DerefMut,
13+
Clone, Copy, PartialOrd, Ord, PartialEq, Eq, Debug, Default, Serialize, Deserialize, Add, Sub, Deref, DerefMut, Hash,
1414
)]
1515
#[serde(transparent)]
1616
pub struct MilestoneIndex(pub u32);

tests/outputs.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ mod common;
55

66
#[cfg(feature = "rand")]
77
mod test_rand {
8-
98
use chronicle::{
109
db::collections::{OutputCollection, OutputMetadataResult, OutputWithMetadataResult},
1110
types::{

tests/outputs_claiming.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ mod common;
55

66
#[cfg(feature = "rand")]
77
mod test_rand {
8-
98
use chronicle::{
109
db::collections::OutputCollection,
1110
types::{

tests/protocol_updates.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ mod common;
55

66
#[cfg(feature = "rand")]
77
mod test_rand {
8-
98
use bee_block_stardust::rand::number::rand_number_range;
109
use chronicle::{
1110
db::{collections::ProtocolUpdateCollection, MongoDbCollectionExt},

tests/treasury_updates.rs

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
// Copyright 2022 IOTA Stiftung
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
mod common;
5+
6+
#[cfg(feature = "rand")]
7+
mod test_rand {
8+
use std::collections::HashMap;
9+
10+
use bee_block_stardust::rand::number::rand_number_range;
11+
use chronicle::{
12+
db::{collections::TreasuryCollection, MongoDbCollectionExt},
13+
types::{
14+
stardust::block::payload::{MilestoneId, TreasuryTransactionPayload},
15+
tangle::MilestoneIndex,
16+
},
17+
};
18+
19+
use super::common::{setup_collection, setup_database, teardown};
20+
21+
#[tokio::test]
22+
async fn test_insert_treasury_updates() {
23+
let db = setup_database("test-insert-treasury-updates").await.unwrap();
24+
let update_collection = setup_collection::<TreasuryCollection>(&db).await.unwrap();
25+
26+
let ctx = bee_block_stardust::protocol::protocol_parameters();
27+
let mut milestones = HashMap::new();
28+
29+
for (milestone_index, payload) in
30+
(0..10u32).map(|milestone_index| (milestone_index, TreasuryTransactionPayload::rand(&ctx)))
31+
{
32+
milestones.insert(milestone_index, payload.input_milestone_id);
33+
34+
update_collection
35+
.insert_treasury(milestone_index.into(), &payload)
36+
.await
37+
.unwrap();
38+
}
39+
40+
assert_eq!(update_collection.count().await.unwrap(), 10);
41+
assert_eq!(
42+
&update_collection
43+
.get_latest_treasury()
44+
.await
45+
.unwrap()
46+
.unwrap()
47+
.milestone_id,
48+
milestones.get(&9).unwrap()
49+
);
50+
51+
teardown(db).await;
52+
}
53+
54+
#[tokio::test]
55+
async fn test_insert_many_treasury_updates() {
56+
let db = setup_database("test-insert-many-treasury-updates").await.unwrap();
57+
let update_collection = setup_collection::<TreasuryCollection>(&db).await.unwrap();
58+
59+
let mut milestones = HashMap::new();
60+
61+
let treasury_updates = (0..10u32)
62+
.map(|milestone_index| {
63+
(
64+
MilestoneIndex::from(milestone_index),
65+
MilestoneId::rand(),
66+
rand_number_range(1000..10000000u64),
67+
)
68+
})
69+
.inspect(|(milestone_index, milestone_id, _)| {
70+
milestones.insert(milestone_index.0, *milestone_id);
71+
})
72+
.collect::<Vec<_>>();
73+
74+
update_collection
75+
.insert_treasury_payloads(treasury_updates)
76+
.await
77+
.unwrap();
78+
79+
assert_eq!(update_collection.count().await.unwrap(), 10);
80+
assert_eq!(
81+
&update_collection
82+
.get_latest_treasury()
83+
.await
84+
.unwrap()
85+
.unwrap()
86+
.milestone_id,
87+
milestones.get(&9).unwrap()
88+
);
89+
90+
teardown(db).await;
91+
}
92+
}

0 commit comments

Comments
 (0)