Skip to content
Permalink
Browse files

Merge pull request #719 from input-output-hk/store-tests

Add MemoryBlockStore and SQLiteBlockStore tests
  • Loading branch information...
vincenthz committed Jun 12, 2019
2 parents 64131bf + 5d84138 commit 0a6fb795fb90aefe8a1f7ecf171bf33c9aa2b6c5
@@ -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"] }
@@ -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)?;
@@ -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> {
@@ -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);
}
}
@@ -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"]
@@ -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"),
}
}
}
@@ -1,5 +1,3 @@
extern crate chain_core;

pub mod error;
pub mod memory;
pub mod store;
@@ -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> {
@@ -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.
You can’t perform that action at this time.