From 4944679fb2b9d3e4cbbe5c8d29375a1b2b0e649f Mon Sep 17 00:00:00 2001 From: Antioch Peverell Date: Fri, 25 Sep 2020 13:49:48 +0100 Subject: [PATCH] add test coverage around existing behavior for exists() and iter() (#3452) --- store/tests/lmdb.rs | 69 ++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 68 insertions(+), 1 deletion(-) diff --git a/store/tests/lmdb.rs b/store/tests/lmdb.rs index cceb4c11ef..54c08887f6 100644 --- a/store/tests/lmdb.rs +++ b/store/tests/lmdb.rs @@ -15,6 +15,7 @@ use grin_core as core; use grin_store as store; use grin_util as util; +use store::SerIterator; use crate::core::global; use crate::core::ser::{self, Readable, Reader, Writeable, Writer}; @@ -65,9 +66,75 @@ fn setup(test_dir: &str) { clean_output_dir(test_dir); } +#[test] +fn test_exists() -> Result<(), store::Error> { + let test_dir = "target/test_exists"; + setup(test_dir); + + let store = store::Store::new(test_dir, Some("test1"), None, None)?; + + let key = [0, 0, 0, 1]; + let value = [1, 1, 1, 1]; + + // Start new batch and insert a new key/value entry. + let batch = store.batch()?; + batch.put(&key, &value)?; + + // Check we can see the new entry in uncommitted batch. + assert!(batch.exists(&key)?); + + // Check we cannot see the new entry yet outside of the uncommitted batch. + assert!(!store.exists(&key)?); + + batch.commit()?; + + // Check we can see the new entry after committing the batch. + assert!(store.exists(&key)?); + + clean_output_dir(test_dir); + Ok(()) +} + +#[test] +fn test_iter() -> Result<(), store::Error> { + let test_dir = "target/test_iter"; + setup(test_dir); + + let store = store::Store::new(test_dir, Some("test1"), None, None)?; + + let key = [0, 0, 0, 1]; + let value = [1, 1, 1, 1]; + + // Start new batch and insert a new key/value entry. + let batch = store.batch()?; + batch.put(&key, &value)?; + + // TODO - This is not currently possible (and we need to be aware of this). + // Currently our SerIterator is limited to using a ReadTransaction only. + // + // Check we can see the new entry via an iterator using the uncommitted batch. + // let mut iter: SerIterator> = batch.iter(&[0])?; + // assert_eq!(iter.next(), Some((key.to_vec(), value.to_vec()))); + // assert_eq!(iter.next(), None); + + // Check we can not yet see the new entry via an iterator outside the uncommitted batch. + let mut iter: SerIterator> = store.iter(&[0])?; + assert_eq!(iter.next(), None); + + batch.commit()?; + + // Check we can see the new entry via an iterator after committing the batch. + let mut iter: SerIterator> = store.iter(&[0])?; + assert_eq!(iter.next(), Some((key.to_vec(), value.to_vec()))); + assert_eq!(iter.next(), None); + + clean_output_dir(test_dir); + Ok(()) +} + #[test] fn lmdb_allocate() -> Result<(), store::Error> { - let test_dir = "test_output/lmdb_allocate"; + let test_dir = "target/lmdb_allocate"; setup(test_dir); // Allocate more than the initial chunk, ensuring // the DB resizes underneath