Skip to content
This repository has been archived by the owner on Nov 6, 2020. It is now read-only.

Commit

Permalink
add a test
Browse files Browse the repository at this point in the history
  • Loading branch information
rphmeier committed Sep 26, 2016
1 parent 3ebfbf3 commit b8b3f06
Showing 1 changed file with 41 additions and 4 deletions.
45 changes: 41 additions & 4 deletions util/src/migration/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
//! are performed in temp sub-directories.
use common::*;
use migration::{Config, SimpleMigration, Manager};
use migration::{Batch, Config, Error, SimpleMigration, Migration, Manager};
use kvdb::Database;

use devtools::RandomTempPath;
Expand Down Expand Up @@ -62,11 +62,10 @@ impl SimpleMigration for Migration0 {

fn version(&self) -> u32 { 1 }

fn simple_migrate(&mut self, key: Vec<u8>, value: Vec<u8>) -> Option<(Vec<u8>, Vec<u8>)> {
let mut key = key;
fn simple_migrate(&mut self, mut key: Vec<u8>, mut value: Vec<u8>) -> Option<(Vec<u8>, Vec<u8>)> {
key.push(0x11);
let mut value = value;
value.push(0x22);

Some((key, value))
}
}
Expand All @@ -83,6 +82,31 @@ impl SimpleMigration for Migration1 {
}
}

struct AddsColumn;

impl Migration for AddsColumn {
fn pre_columns(&self) -> Option<u32> { None }

fn columns(&self) -> Option<u32> { Some(1) }

fn version(&self) -> u32 { 1 }

fn migrate(&mut self, source: &Database, config: &Config, dest: &mut Database, col: Option<u32>) -> Result<(), Error> {
let mut batch = Batch::new(config, col);

for (key, value) in source.iter(col) {
try!(batch.insert(key.to_vec(), value.to_vec(), dest));
}


if col == Some(1) {
try!(batch.insert(vec![1, 2, 3], vec![4, 5, 6], dest));
}

batch.commit(dest)
}
}

#[test]
fn one_simple_migration() {
let dir = RandomTempPath::create_dir();
Expand Down Expand Up @@ -189,3 +213,16 @@ fn is_migration_needed() {
assert!(manager.is_needed(1));
assert!(!manager.is_needed(2));
}

#[test]
fn pre_columns() {
let mut manager = Manager::new(Config::default());
manager.add_migration(AddsColumn).unwrap();

let dir = RandomTempPath::create_dir();
let db_path = db_path(dir.as_path());

// this shouldn't fail to open the database even though it's one column
// short of the one before it.
manager.execute(&db_path, 0).unwrap();
}

0 comments on commit b8b3f06

Please sign in to comment.