Skip to content

Commit

Permalink
extract common kvdb tests into a crate (#301)
Browse files Browse the repository at this point in the history
* kvdb-test-utils: extract common KeyValueDB tests into a crate

* kvdb-memorydb: use kvdb-test-utils for tests

* kvdb-test-utils: un-unwrap-ify

* kvdb-rocksdb: use kvdb-test-utils for tests

* kvdb-web: use kvdb-test-utils for tests

* update year in license headers

* Cargo.toml: add newlines

* rename kvdb-test-utils to kvdb-shared-tests
  • Loading branch information
ordian authored Jan 3, 2020
1 parent 5371e63 commit 072d8e8
Show file tree
Hide file tree
Showing 9 changed files with 369 additions and 273 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ members = [
"kvdb",
"kvdb-memorydb",
"kvdb-rocksdb",
"kvdb-shared-tests",
"kvdb-web",
"parity-bytes",
"parity-crypto",
Expand Down
3 changes: 3 additions & 0 deletions kvdb-memorydb/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,6 @@ edition = "2018"
parity-util-mem = { path = "../parity-util-mem", version = "0.3" }
parking_lot = "0.9.0"
kvdb = { version = "0.2", path = "../kvdb" }

[dev-dependencies]
kvdb-shared-tests = { path = "../kvdb-shared-tests", version = "0.1" }
99 changes: 19 additions & 80 deletions kvdb-memorydb/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2015-2018 Parity Technologies (UK) Ltd.
// Copyright 2015-2020 Parity Technologies (UK) Ltd.
// This file is part of Parity.

// Parity is free software: you can redistribute it and/or modify
Expand Down Expand Up @@ -116,104 +116,43 @@ impl KeyValueDB for InMemory {

#[cfg(test)]
mod tests {
use super::{create, KeyValueDB};
use super::create;
use kvdb_shared_tests as st;
use std::io;

#[test]
fn get_fails_with_non_existing_column() {
fn get_fails_with_non_existing_column() -> io::Result<()> {
let db = create(1);
assert!(db.get(1, &[]).is_err());
st::test_get_fails_with_non_existing_column(&db)
}

#[test]
fn put_and_get() {
fn put_and_get() -> io::Result<()> {
let db = create(1);

let key1 = b"key1";

let mut transaction = db.transaction();
transaction.put(0, key1, b"horse");
db.write_buffered(transaction);
assert_eq!(&*db.get(0, key1).unwrap().unwrap(), b"horse");
st::test_put_and_get(&db)
}

#[test]
fn delete_and_get() {
fn delete_and_get() -> io::Result<()> {
let db = create(1);

let key1 = b"key1";

let mut transaction = db.transaction();
transaction.put(0, key1, b"horse");
db.write_buffered(transaction);
assert_eq!(&*db.get(0, key1).unwrap().unwrap(), b"horse");

let mut transaction = db.transaction();
transaction.delete(0, key1);
db.write_buffered(transaction);
assert!(db.get(0, key1).unwrap().is_none());
st::test_delete_and_get(&db)
}

#[test]
fn iter() {
fn iter() -> io::Result<()> {
let db = create(1);

let key1 = b"key1";
let key2 = b"key2";

let mut transaction = db.transaction();
transaction.put(0, key1, key1);
transaction.put(0, key2, key2);
db.write_buffered(transaction);

let contents: Vec<_> = db.iter(0).into_iter().collect();
assert_eq!(contents.len(), 2);
assert_eq!(&*contents[0].0, key1);
assert_eq!(&*contents[0].1, key1);
assert_eq!(&*contents[1].0, key2);
assert_eq!(&*contents[1].1, key2);
st::test_iter(&db)
}

#[test]
fn iter_from_prefix() {
fn iter_from_prefix() -> io::Result<()> {
let db = create(1);
st::test_iter_from_prefix(&db)
}

let key1 = b"0";
let key2 = b"a";
let key3 = b"ab";

let mut transaction = db.transaction();
transaction.put(0, key1, key1);
transaction.put(0, key2, key2);
transaction.put(0, key3, key3);
db.write_buffered(transaction);

let contents: Vec<_> = db.iter_from_prefix(0, b"").into_iter().collect();
assert_eq!(contents.len(), 3);
assert_eq!(&*contents[0].0, key1);
assert_eq!(&*contents[0].1, key1);
assert_eq!(&*contents[1].0, key2);
assert_eq!(&*contents[1].1, key2);
assert_eq!(&*contents[2].0, key3);
assert_eq!(&*contents[2].1, key3);

let contents: Vec<_> = db.iter_from_prefix(0, b"0").into_iter().collect();
assert_eq!(contents.len(), 1);
assert_eq!(&*contents[0].0, key1);
assert_eq!(&*contents[0].1, key1);

let contents: Vec<_> = db.iter_from_prefix(0, b"a").into_iter().collect();
assert_eq!(contents.len(), 2);
assert_eq!(&*contents[0].0, key2);
assert_eq!(&*contents[0].1, key2);
assert_eq!(&*contents[1].0, key3);
assert_eq!(&*contents[1].1, key3);

let contents: Vec<_> = db.iter_from_prefix(0, b"ab").into_iter().collect();
assert_eq!(contents.len(), 1);
assert_eq!(&*contents[0].0, key3);
assert_eq!(&*contents[0].1, key3);

let contents: Vec<_> = db.iter_from_prefix(0, b"abc").into_iter().collect();
assert_eq!(contents.len(), 0);
#[test]
fn complex() -> io::Result<()> {
let db = create(1);
st::test_complex(&db)
}
}
4 changes: 2 additions & 2 deletions kvdb-rocksdb/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ name = "kvdb-rocksdb"
version = "0.3.0"
authors = ["Parity Technologies <admin@parity.io>"]
repository = "https://github.com/paritytech/parity-common"
description = "kvdb implementation backed by rocksDB"
description = "kvdb implementation backed by RocksDB"
license = "GPL-3.0"
edition = "2018"

Expand All @@ -27,6 +27,6 @@ parity-util-mem = { path = "../parity-util-mem", version = "0.3" }
[dev-dependencies]
alloc_counter = "0.0.4"
criterion = "0.3"
ethereum-types = { version = "0.8.0", path = "../ethereum-types" }
kvdb-shared-tests = { path = "../kvdb-shared-tests", version = "0.1" }
rand = "0.7.2"
tempdir = "0.3.7"
Loading

0 comments on commit 072d8e8

Please sign in to comment.