Skip to content
This repository has been archived by the owner on Jun 11, 2022. It is now read-only.

Commit

Permalink
Merge pull request #719 from input-output-hk/store-tests
Browse files Browse the repository at this point in the history
Add MemoryBlockStore and SQLiteBlockStore tests
  • Loading branch information
vincenthz committed Jun 12, 2019
2 parents 64131bf + 5d84138 commit 0a6fb79
Show file tree
Hide file tree
Showing 7 changed files with 344 additions and 13 deletions.
1 change: 1 addition & 0 deletions chain-storage-sqlite/Cargo.toml
Expand Up @@ -23,3 +23,4 @@ features = ["bundled"]
cardano = { path = "../cardano" }
cardano-storage = { path = "../storage" }
exe-common = { path = "../exe-common" }
chain-storage = { path = "../chain-storage", features=["test-api"] }
45 changes: 38 additions & 7 deletions chain-storage-sqlite/src/lib.rs
Expand Up @@ -138,7 +138,7 @@ where
})
.map_err(|err| match err {
rusqlite::Error::QueryReturnedNoRows => Error::BlockNotFound,
_ => panic!(err),
err => Error::BackendError(Box::new(err)),
})?;

let info = self.get_block_info(block_hash)?;
Expand Down Expand Up @@ -178,22 +178,29 @@ where
})
.map_err(|err| match err {
rusqlite::Error::QueryReturnedNoRows => Error::BlockNotFound,
_ => panic!(err),
err => Error::BackendError(Box::new(err)),
})
}

fn put_tag(&mut self, tag_name: &str, block_hash: &B::Id) -> Result<(), Error> {
self.pool
match self
.pool
.get()
.unwrap()
.prepare_cached("insert or replace into Tags (name, hash) values(?, ?)")
.unwrap()
.execute(&[
Value::Text(tag_name.to_string()),
Value::Blob(block_hash.serialize_as_vec().unwrap()),
])
.unwrap();
Ok(())
]) {
Ok(_) => Ok(()),
Err(rusqlite::Error::SqliteFailure(err, _))
if err.code == rusqlite::ErrorCode::ConstraintViolation =>
{
Err(Error::BlockNotFound)
}
Err(err) => Err(Error::BackendError(Box::new(err))),
}
}

fn get_tag(&self, tag_name: &str) -> Result<Option<B::Id>, Error> {
Expand All @@ -207,11 +214,35 @@ where
{
Ok(s) => Ok(Some(s)),
Err(rusqlite::Error::QueryReturnedNoRows) => Ok(None),
Err(err) => panic!(err),
Err(err) => Err(Error::BackendError(Box::new(err))),
}
}

fn as_trait(&self) -> &BlockStore<Block = Self::Block> {
self as &BlockStore<Block = Self::Block>
}
}

#[cfg(test)]
mod tests {
use super::*;
use chain_storage::store::testing::Block;

#[test]
pub fn put_get() {
let mut store = SQLiteBlockStore::<Block>::new(":memory:");
chain_storage::store::testing::test_put_get(&mut store);
}

#[test]
pub fn nth_ancestor() {
let mut store = SQLiteBlockStore::<Block>::new(":memory:");
chain_storage::store::testing::test_nth_ancestor(&mut store);
}

#[test]
pub fn iterate_range() {
let mut store = SQLiteBlockStore::<Block>::new(":memory:");
chain_storage::store::testing::test_iterate_range(&mut store);
}
}
3 changes: 3 additions & 0 deletions chain-storage/Cargo.toml
Expand Up @@ -11,7 +11,10 @@ edition = "2018"

[dependencies]
chain-core = { path = "../chain-core" }
rand = { version = "0.6", optional = true }

[dev-dependencies]
rand = { version = "0.6" }

[features]
test-api = ["rand"]
4 changes: 3 additions & 1 deletion chain-storage/src/error.rs
@@ -1,16 +1,18 @@
use std::{error, fmt};

#[derive(Debug, Clone, Eq, PartialEq)]
#[derive(Debug)]
pub enum Error {
BlockNotFound, // FIXME: add BlockId
CannotIterate,
BackendError(Box<dyn std::error::Error + Send + Sync>),
}

impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
Error::BlockNotFound => write!(f, "block not found"),
Error::CannotIterate => write!(f, "cannot iterate between the 2 given blocks"),
Error::BackendError(_) => write!(f, "miscellaneous storage error"),
}
}
}
Expand Down
2 changes: 0 additions & 2 deletions chain-storage/src/lib.rs
@@ -1,5 +1,3 @@
extern crate chain_core;

pub mod error;
pub mod memory;
pub mod store;
34 changes: 31 additions & 3 deletions chain-storage/src/memory.rs
Expand Up @@ -54,9 +54,13 @@ where
}

fn put_tag(&mut self, tag_name: &str, block_hash: &B::Id) -> Result<(), Error> {
assert!(self.blocks.get(block_hash).is_some()); // FIXME: return error
self.tags.insert(tag_name.to_string(), block_hash.clone());
Ok(())
match self.blocks.get(block_hash) {
None => Err(Error::BlockNotFound),
Some(_) => {
self.tags.insert(tag_name.to_string(), block_hash.clone());
Ok(())
}
}
}

fn get_tag(&self, tag_name: &str) -> Result<Option<B::Id>, Error> {
Expand All @@ -71,3 +75,27 @@ where
self as &BlockStore<Block = Self::Block>
}
}

#[cfg(test)]
mod tests {
use super::*;
use crate::store::testing::Block;

#[test]
pub fn put_get() {
let mut store = MemoryBlockStore::<Block>::new();
crate::store::testing::test_put_get(&mut store);
}

#[test]
pub fn nth_ancestor() {
let mut store = MemoryBlockStore::<Block>::new();
crate::store::testing::test_nth_ancestor(&mut store);
}

#[test]
pub fn iterate_range() {
let mut store = MemoryBlockStore::<Block>::new();
crate::store::testing::test_iterate_range(&mut store);
}
}

0 comments on commit 0a6fb79

Please sign in to comment.