migrate tests to new migration framework
This commit is contained in:
parent
1b759c1462
commit
d4c9c7cc23
@ -14,13 +14,43 @@
|
|||||||
// You should have received a copy of the GNU General Public License
|
// You should have received a copy of the GNU General Public License
|
||||||
// along with Parity. If not, see <http://www.gnu.org/licenses/>.
|
// along with Parity. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
use std::collections::BTreeMap;
|
//! Tests for migrations.
|
||||||
use migration::{Error, Destination, Migration, Config, SimpleMigration};
|
//! A random temp directory is created. A database
|
||||||
|
|
||||||
impl Destination for BTreeMap<Vec<u8>, Vec<u8>> {
|
use common::*;
|
||||||
fn commit(&mut self, batch: &BTreeMap<Vec<u8>, Vec<u8>>) -> Result<(), Error> {
|
use migration::{Config, SimpleMigration, Manager};
|
||||||
self.extend(batch.cloned());
|
use kvdb::{Database, DBTransaction};
|
||||||
Ok(())
|
|
||||||
|
use devtools::RandomTempPath;
|
||||||
|
use std::path::PathBuf;
|
||||||
|
|
||||||
|
fn db_path(path: &Path) -> PathBuf {
|
||||||
|
let mut p = path.to_owned();
|
||||||
|
p.push("db");
|
||||||
|
p
|
||||||
|
}
|
||||||
|
|
||||||
|
// initialize a database at the given directory with the given values.
|
||||||
|
fn make_db(path: &Path, pairs: BTreeMap<Vec<u8>, Vec<u8>>) {
|
||||||
|
let db = Database::open_default(path.to_str().unwrap()).expect("failed to open temp database");
|
||||||
|
{
|
||||||
|
let transaction = DBTransaction::new();
|
||||||
|
for (k, v) in pairs {
|
||||||
|
transaction.put(&k, &v).expect("failed to add pair to transaction");
|
||||||
|
}
|
||||||
|
|
||||||
|
db.write(transaction).expect("failed to write db transaction");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// helper for verifying a a migrated database.
|
||||||
|
fn verify_migration(path: &Path, pairs: BTreeMap<Vec<u8>, Vec<u8>>) {
|
||||||
|
let db = Database::open_default(path.to_str().unwrap()).unwrap();
|
||||||
|
|
||||||
|
for (k, v) in pairs {
|
||||||
|
let x = db.get(&k).unwrap().unwrap();
|
||||||
|
|
||||||
|
assert_eq!(&x[..], &v[..]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -54,68 +84,58 @@ impl SimpleMigration for Migration1 {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn one_simple_migration() {
|
fn one_simple_migration() {
|
||||||
|
let dir = RandomTempPath::create_dir();
|
||||||
|
let db_path = db_path(dir.as_path());
|
||||||
let mut manager = Manager::new(Config::default());
|
let mut manager = Manager::new(Config::default());
|
||||||
let keys = vec![vec![], vec![1u8]];
|
make_db(&db_path, map![vec![] => vec![], vec![1] => vec![1]]);
|
||||||
let values = vec![vec![], vec![1u8]];
|
let expected = map![vec![0x11] => vec![0x22], vec![1, 0x11] => vec![1, 0x22]];
|
||||||
let db = keys.into_iter().zip(values.into_iter());
|
|
||||||
|
|
||||||
let expected_keys = vec![vec![0x11u8], vec![1, 0x11]];
|
|
||||||
let expected_values = vec![vec![0x22u8], vec![1, 0x22]];
|
|
||||||
let expected_db = expected_keys.into_iter().zip(expected_values.into_iter()).collect::<BTreeMap<_, _>>();
|
|
||||||
|
|
||||||
let mut result = BTreeMap::new();
|
|
||||||
manager.add_migration(Migration0).unwrap();
|
manager.add_migration(Migration0).unwrap();
|
||||||
manager.execute(db, 0, &mut result).unwrap();
|
let end_path = manager.execute(&db_path, 0).unwrap();
|
||||||
assert_eq!(expected_db, result);
|
|
||||||
|
verify_migration(&end_path, expected);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
#[should_panic]
|
#[should_panic]
|
||||||
fn no_migration_needed() {
|
fn no_migration_needed() {
|
||||||
|
let dir = RandomTempPath::create_dir();
|
||||||
|
let db_path = db_path(dir.as_path());
|
||||||
let mut manager = Manager::new(Config::default());
|
let mut manager = Manager::new(Config::default());
|
||||||
let keys = vec![vec![], vec![1u8]];
|
make_db(&db_path, map![vec![] => vec![], vec![1] => vec![1]]);
|
||||||
let values = vec![vec![], vec![1u8]];
|
|
||||||
let db = keys.into_iter().zip(values.into_iter());
|
|
||||||
|
|
||||||
let mut result = BTreeMap::new();
|
|
||||||
manager.add_migration(Migration0).unwrap();
|
manager.add_migration(Migration0).unwrap();
|
||||||
manager.execute(db, 1, &mut result).unwrap();
|
manager.execute(&db_path, 1).unwrap();
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn multiple_migrations() {
|
fn multiple_migrations() {
|
||||||
|
let dir = RandomTempPath::create_dir();
|
||||||
|
let db_path = db_path(dir.as_path());
|
||||||
let mut manager = Manager::new(Config::default());
|
let mut manager = Manager::new(Config::default());
|
||||||
let keys = vec![vec![], vec![1u8]];
|
make_db(&db_path, map![vec![] => vec![], vec![1] => vec![1]]);
|
||||||
let values = vec![vec![], vec![1u8]];
|
let expected = map![vec![0x11] => vec![], vec![1, 0x11] => vec![]];
|
||||||
let db = keys.into_iter().zip(values.into_iter());
|
|
||||||
|
|
||||||
let expected_keys = vec![vec![0x11u8], vec![1, 0x11]];
|
|
||||||
let expected_values = vec![vec![], vec![]];
|
|
||||||
let expected_db = expected_keys.into_iter().zip(expected_values.into_iter()).collect::<BTreeMap<_, _>>();
|
|
||||||
|
|
||||||
let mut result = BTreeMap::new();
|
|
||||||
manager.add_migration(Migration0).unwrap();
|
manager.add_migration(Migration0).unwrap();
|
||||||
manager.add_migration(Migration1).unwrap();
|
manager.add_migration(Migration1).unwrap();
|
||||||
manager.execute(db, 0, &mut result).unwrap();
|
let end_path = manager.execute(&db_path, 0).unwrap();
|
||||||
assert_eq!(expected_db, result);
|
|
||||||
|
verify_migration(&end_path, expected);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn second_migration() {
|
fn second_migration() {
|
||||||
|
let dir = RandomTempPath::create_dir();
|
||||||
|
let db_path = db_path(dir.as_path());
|
||||||
let mut manager = Manager::new(Config::default());
|
let mut manager = Manager::new(Config::default());
|
||||||
let keys = vec![vec![], vec![1u8]];
|
make_db(&db_path, map![vec![] => vec![], vec![1] => vec![1]]);
|
||||||
let values = vec![vec![], vec![1u8]];
|
let expected = map![vec![] => vec![], vec![1] => vec![]];
|
||||||
let db = keys.into_iter().zip(values.into_iter());
|
|
||||||
|
|
||||||
let expected_keys = vec![vec![], vec![1u8]];
|
|
||||||
let expected_values = vec![vec![], vec![]];
|
|
||||||
let expected_db = expected_keys.into_iter().zip(expected_values.into_iter()).collect::<BTreeMap<_, _>>();
|
|
||||||
|
|
||||||
let mut result = BTreeMap::new();
|
|
||||||
manager.add_migration(Migration0).unwrap();
|
manager.add_migration(Migration0).unwrap();
|
||||||
manager.add_migration(Migration1).unwrap();
|
manager.add_migration(Migration1).unwrap();
|
||||||
manager.execute(db, 1, &mut result).unwrap();
|
let end_path = manager.execute(&db_path, 1).unwrap();
|
||||||
assert_eq!(expected_db, result);
|
|
||||||
|
verify_migration(&end_path, expected);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
Loading…
Reference in New Issue
Block a user