From d4c9c7cc2341425b243b28d0af12509c4a15897f Mon Sep 17 00:00:00 2001 From: Robert Habermeier Date: Tue, 5 Jul 2016 17:12:20 +0200 Subject: [PATCH] migrate tests to new migration framework --- util/src/migration/tests.rs | 102 +++++++++++++++++++++--------------- 1 file changed, 61 insertions(+), 41 deletions(-) diff --git a/util/src/migration/tests.rs b/util/src/migration/tests.rs index 162bec054..e17a194b8 100644 --- a/util/src/migration/tests.rs +++ b/util/src/migration/tests.rs @@ -14,13 +14,43 @@ // You should have received a copy of the GNU General Public License // along with Parity. If not, see . -use std::collections::BTreeMap; -use migration::{Error, Destination, Migration, Config, SimpleMigration}; +//! Tests for migrations. +//! A random temp directory is created. A database -impl Destination for BTreeMap, Vec> { - fn commit(&mut self, batch: &BTreeMap, Vec>) -> Result<(), Error> { - self.extend(batch.cloned()); - Ok(()) +use common::*; +use migration::{Config, SimpleMigration, Manager}; +use kvdb::{Database, DBTransaction}; + +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>) { + 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>) { + 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] 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 keys = vec![vec![], vec![1u8]]; - let values = vec![vec![], vec![1u8]]; - let db = keys.into_iter().zip(values.into_iter()); + make_db(&db_path, map![vec![] => vec![], vec![1] => vec![1]]); + let expected = map![vec![0x11] => vec![0x22], vec![1, 0x11] => vec![1, 0x22]]; - 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::>(); - - let mut result = BTreeMap::new(); manager.add_migration(Migration0).unwrap(); - manager.execute(db, 0, &mut result).unwrap(); - assert_eq!(expected_db, result); + let end_path = manager.execute(&db_path, 0).unwrap(); + + verify_migration(&end_path, expected); } #[test] #[should_panic] 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 keys = vec![vec![], vec![1u8]]; - let values = vec![vec![], vec![1u8]]; - let db = keys.into_iter().zip(values.into_iter()); + make_db(&db_path, map![vec![] => vec![], vec![1] => vec![1]]); - let mut result = BTreeMap::new(); manager.add_migration(Migration0).unwrap(); - manager.execute(db, 1, &mut result).unwrap(); + manager.execute(&db_path, 1).unwrap(); } #[test] fn multiple_migrations() { + let dir = RandomTempPath::create_dir(); + let db_path = db_path(dir.as_path()); let mut manager = Manager::new(Config::default()); - let keys = vec![vec![], vec![1u8]]; - let values = vec![vec![], vec![1u8]]; - let db = keys.into_iter().zip(values.into_iter()); + make_db(&db_path, map![vec![] => vec![], vec![1] => vec![1]]); + let expected = map![vec![0x11] => vec![], vec![1, 0x11] => vec![]]; - 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::>(); - - let mut result = BTreeMap::new(); manager.add_migration(Migration0).unwrap(); manager.add_migration(Migration1).unwrap(); - manager.execute(db, 0, &mut result).unwrap(); - assert_eq!(expected_db, result); + let end_path = manager.execute(&db_path, 0).unwrap(); + + verify_migration(&end_path, expected); } #[test] fn second_migration() { + let dir = RandomTempPath::create_dir(); + let db_path = db_path(dir.as_path()); let mut manager = Manager::new(Config::default()); - let keys = vec![vec![], vec![1u8]]; - let values = vec![vec![], vec![1u8]]; - let db = keys.into_iter().zip(values.into_iter()); + make_db(&db_path, map![vec![] => vec![], vec![1] => vec![1]]); + let expected = map![vec![] => vec![], vec![1] => vec![]]; - 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::>(); - - let mut result = BTreeMap::new(); manager.add_migration(Migration0).unwrap(); manager.add_migration(Migration1).unwrap(); - manager.execute(db, 1, &mut result).unwrap(); - assert_eq!(expected_db, result); + let end_path = manager.execute(&db_path, 1).unwrap(); + + verify_migration(&end_path, expected); } #[test]