Testing
Why Testing Matters
Unit Testing
The map! Macro
map! Macrouse substreams::errors::Error;
use substreams_ethereum::pb::eth::v2::Block;
mod pb;
use pb::myproject::{Events, Event};
#[substreams::handlers::map]
pub fn map_events(block: Block) -> Result<Events, Error> {
let events: Vec<Event> = block.logs()
.filter(|log| !log.topics.is_empty())
.filter_map(|log| parse_event(log).ok())
.collect();
Ok(Events { events })
}
#[substreams::handlers::map]
pub fn filter_events(
event_type: String,
events: Events,
) -> Result<Events, Error> {
let filtered: Vec<Event> = events.events
.into_iter()
.filter(|e| e.event_type == event_type)
.collect();
Ok(Events { events: filtered })
}
#[cfg(test)]
mod tests {
use super::*;
use substreams::testing;
#[test]
fn test_map_events() {
// Arrange
let block = create_test_block();
// Act
let result = testing::map!(map_events(block)).unwrap();
// Assert
assert!(!result.events.is_empty());
}
#[test]
fn test_map_events_empty_block() {
let empty_block = Block::default();
let result = testing::map!(map_events(empty_block)).unwrap();
assert!(result.events.is_empty());
}
#[test]
fn test_chained_handlers() {
let block = create_test_block();
// Chain multiple handlers together
let all_events = testing::map!(map_events(block)).unwrap();
let transfers = testing::map!(filter_events("transfer".to_string(), all_events)).unwrap();
assert!(transfers.events.iter().all(|e| e.event_type == "transfer"));
}
fn create_test_block() -> Block {
// Create a test block with sample data
Block {
number: 17000000,
// ... populate with test data
..Default::default()
}
}
}The clock() Function
clock() FunctionOpting Out of Testable Generation
Test Data
Using Real Blockchain Data
Constructing Test Blocks
Test Data Builder Pattern
Integration Testing
End-to-End Testing
Performance Testing
Benchmarking with Criterion
Production Mode Validation
Testing Best Practices
Test Structure
Error Scenario Testing
Reorganization Testing
CI/CD Integration
GitHub Actions Example
Pre-commit Hooks
Summary
Test Level
Purpose
Tools
Cargo.toml Requirements
Last updated
Was this helpful?

