From 20961c612aecad2e4b00b8016e84422461041a98 Mon Sep 17 00:00:00 2001 From: Grant Wuerker Date: Sun, 11 Nov 2018 13:15:39 -0600 Subject: [PATCH] pow chain store tests added --- lighthouse/db/Cargo.toml | 1 + lighthouse/db/src/stores/pow_chain_store.rs | 41 ++++++++++++++++++++- 2 files changed, 41 insertions(+), 1 deletion(-) diff --git a/lighthouse/db/Cargo.toml b/lighthouse/db/Cargo.toml index a2718889c6..0d0bf4a2e3 100644 --- a/lighthouse/db/Cargo.toml +++ b/lighthouse/db/Cargo.toml @@ -11,3 +11,4 @@ rocksdb = "0.10.1" ssz = { path = "../../beacon_chain/utils/ssz" } ssz_helpers = { path = "../../beacon_chain/utils/ssz_helpers" } types = { path = "../../beacon_chain/types" } +rand = "0.5.5" diff --git a/lighthouse/db/src/stores/pow_chain_store.rs b/lighthouse/db/src/stores/pow_chain_store.rs index f1f050a390..f2f0f5df61 100644 --- a/lighthouse/db/src/stores/pow_chain_store.rs +++ b/lighthouse/db/src/stores/pow_chain_store.rs @@ -4,6 +4,7 @@ use super::{ DBError, }; use super::POW_CHAIN_DB_COLUMN as DB_COLUMN; +extern crate rand; pub struct PoWChainStore where T: ClientDB @@ -31,4 +32,42 @@ impl PoWChainStore { } } -// TODO: add tests once a memory-db is implemented +#[cfg(test)] +mod tests { + use super::*; + use super::super::super::MemoryDB; + + #[test] + fn test_put_block_hash() { + let db = Arc::new(MemoryDB::open()); + let store = PoWChainStore::new(db.clone()); + + let hash: &[u8] = &[rand::random()]; + store.put_block_hash(hash); + + assert!(db.exists(DB_COLUMN, hash).unwrap()); + } + + #[test] + fn test_block_hash_exists() { + let db = Arc::new(MemoryDB::open()); + let store = PoWChainStore::new(db.clone()); + + let hash: &[u8] = &[rand::random()]; + db.put(DB_COLUMN, hash, &[0]); + + assert!(store.block_hash_exists(hash).unwrap()); + } + + #[test] + fn test_block_hash_does_not_exist() { + let db = Arc::new(MemoryDB::open()); + let store = PoWChainStore::new(db.clone()); + + let hash: &[u8] = &[rand::random()]; + let other_hash: &[u8] = &[rand::random()]; + db.put(DB_COLUMN, hash, &[0]); + + assert!(!store.block_hash_exists(other_hash).unwrap()); + } +}