Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

extract common kvdb tests into a crate #301

Merged
merged 8 commits into from
Jan 3, 2020
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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-test-utils",
"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-test-utils = { path = "../kvdb-test-utils", 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_test_utils as utils;
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());
utils::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");
utils::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());
utils::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);
utils::test_iter(&db)
}

#[test]
fn iter_from_prefix() {
fn iter_from_prefix() -> io::Result<()> {
let db = create(1);
utils::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);
utils::test_complex(&db)
}
}
2 changes: 1 addition & 1 deletion kvdb-rocksdb/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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-test-utils = { path = "../kvdb-test-utils", version = "0.1" }
rand = "0.7.2"
tempdir = "0.3.7"
Loading