Skip to content
This repository was archived by the owner on Feb 1, 2024. It is now read-only.

Commit a20a07c

Browse files
committed
Add tests for commit_store get_batch_by_transaction
Signed-off-by: Joseph Livesey <jlivesey@gmail.com>
1 parent 5454770 commit a20a07c

File tree

1 file changed

+71
-0
lines changed

1 file changed

+71
-0
lines changed

validator/src/journal/commit_store.rs

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -562,3 +562,74 @@ impl ChainReader for CommitStore {
562562
.map_err(|err| ChainReadError::GeneralReadError(format!("{err:?}")))
563563
}
564564
}
565+
566+
#[cfg(test)]
567+
mod tests {
568+
use crate::database::error::DatabaseError;
569+
570+
#[derive(Clone, Debug, PartialEq)]
571+
pub struct TestBatch {
572+
// pub header_signature: String,
573+
// pub transactions: Vec<Transaction>,
574+
// pub signer_public_key: String,
575+
pub transaction_ids: Vec<String>,
576+
// pub trace: bool,
577+
578+
// pub header_bytes: Vec<u8>,
579+
}
580+
581+
pub struct TestBlock {
582+
// pub header_signature: String,
583+
pub batches: Vec<TestBatch>,
584+
// pub state_root_hash: String,
585+
// pub consensus: Vec<u8>,
586+
// pub batch_ids: Vec<String>,
587+
// pub signer_public_key: String,
588+
// pub previous_block_id: String,
589+
// pub block_num: u64,
590+
591+
// pub header_bytes: Vec<u8>,
592+
}
593+
594+
// Copies logic of `sawtooth_validator::journal::commit_store::CommitStore::get_batch_by_transaction`
595+
fn mock_get_batch_by_transaction(
596+
transaction_id: &str,
597+
block: TestBlock,
598+
) -> Result<TestBatch, DatabaseError> {
599+
// self.get_by_transaction_id(transaction_id)
600+
// .and_then(|block| {
601+
block
602+
.batches
603+
.into_iter()
604+
.find(|batch| {
605+
!batch
606+
.transaction_ids
607+
.iter()
608+
.any(|txn_id| txn_id == transaction_id)
609+
})
610+
.ok_or_else(|| DatabaseError::CorruptionError("Transaction index corrupted".into()))
611+
// })
612+
}
613+
614+
#[test]
615+
fn test_get_batch_by_transaction_ok() {
616+
let block = TestBlock {
617+
batches: vec![TestBatch {
618+
transaction_ids: vec!["1".to_string()],
619+
}],
620+
};
621+
let transaction_id = "1";
622+
assert!(mock_get_batch_by_transaction(transaction_id, block).is_ok());
623+
}
624+
625+
#[test]
626+
fn test_get_batch_by_transaction_should_err() {
627+
let block = TestBlock {
628+
batches: vec![TestBatch {
629+
transaction_ids: vec!["1".to_string()],
630+
}],
631+
};
632+
let transaction_id = "nope";
633+
assert!(mock_get_batch_by_transaction(transaction_id, block).is_err());
634+
}
635+
}

0 commit comments

Comments
 (0)