Skip to content

Commit

Permalink
Merge #2987
Browse files Browse the repository at this point in the history
2987: test: split all unit tests into separate files r=quake,chanhsu001 a=yangby-cryptape

### What problem does this PR solve?

Split all unit tests into separate files, so we can distinguish the source code more easily.

**NOTICE:** _After this PR, all unit tests should be put into a directory named `tests` or a file named `tests.rs`._

### Check List

Tests

- Unit test

Side effects

- Coverage of unit tests has dropped significantly:
  - Before this PR (commit: db8d371):
    - Lines: `26434 / 36061 = 73.3 %`; Functions: `4596 / 9473 = 48.5 %`
  - After this PR (commit: d29d633):
    - Lines: `21006 / 30477 = 68.9 %`; Functions: `4102 / 8860 = 46.0 %`

### Release note

```release-note
None: Exclude this PR from the release note.
```



Co-authored-by: Boyu Yang <yangby@cryptape.com>
  • Loading branch information
bors[bot] and yangby-cryptape committed Aug 28, 2021
2 parents db8d371 + d29d633 commit de62c91
Show file tree
Hide file tree
Showing 122 changed files with 6,350 additions and 6,339 deletions.
123 changes: 3 additions & 120 deletions db-migration/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ use std::cmp::Ordering;
use std::collections::BTreeMap;
use std::sync::Arc;

#[cfg(test)]
mod tests;

fn internal_error(reason: String) -> Error {
InternalErrorKind::Database.other(reason).into()
}
Expand Down Expand Up @@ -255,123 +258,3 @@ impl Migration for DefaultMigration {
false
}
}

#[cfg(test)]
mod tests {
use super::*;
use ckb_app_config::DBConfig;

#[test]
fn test_default_migration() {
let tmp_dir = tempfile::Builder::new()
.prefix("test_default_migration")
.tempdir()
.unwrap();
let config = DBConfig {
path: tmp_dir.as_ref().to_path_buf(),
..Default::default()
};
{
let mut migrations = Migrations::default();
migrations.add_migration(Box::new(DefaultMigration::new("20191116225943")));
let db = RocksDB::open(&config, 1);
migrations.init_db_version(&db).unwrap();
let r = migrations.migrate(db).unwrap();
assert_eq!(
b"20191116225943".to_vec(),
r.get_pinned_default(MIGRATION_VERSION_KEY)
.unwrap()
.unwrap()
.to_vec()
);
}
{
let mut migrations = Migrations::default();
migrations.add_migration(Box::new(DefaultMigration::new("20191116225943")));
migrations.add_migration(Box::new(DefaultMigration::new("20191127101121")));
let r = migrations.migrate(RocksDB::open(&config, 1)).unwrap();
assert_eq!(
b"20191127101121".to_vec(),
r.get_pinned_default(MIGRATION_VERSION_KEY)
.unwrap()
.unwrap()
.to_vec()
);
}
}

#[test]
fn test_customized_migration() {
struct CustomizedMigration;
const COLUMN: &str = "0";
const VERSION: &str = "20191127101121";

impl Migration for CustomizedMigration {
fn migrate(
&self,
db: RocksDB,
_pb: Arc<dyn Fn(u64) -> ProgressBar + Send + Sync>,
) -> Result<RocksDB, Error> {
let txn = db.transaction();
// append 1u8 to each value of column `0`
let mut migration = |key: &[u8], value: &[u8]| -> Result<(), Error> {
let mut new_value = value.to_vec();
new_value.push(1);
txn.put(COLUMN, key, &new_value)?;
Ok(())
};
db.full_traverse(COLUMN, &mut migration)?;
txn.commit()?;
Ok(db)
}

fn version(&self) -> &str {
VERSION
}
}

let tmp_dir = tempfile::Builder::new()
.prefix("test_customized_migration")
.tempdir()
.unwrap();
let config = DBConfig {
path: tmp_dir.as_ref().to_path_buf(),
..Default::default()
};

{
let mut migrations = Migrations::default();
migrations.add_migration(Box::new(DefaultMigration::new("20191116225943")));
let db = RocksDB::open(&config, 1);
migrations.init_db_version(&db).unwrap();
let db = migrations.migrate(db).unwrap();

let txn = db.transaction();
txn.put(COLUMN, &[1, 1], &[1, 1, 1]).unwrap();
txn.put(COLUMN, &[2, 2], &[2, 2, 2]).unwrap();
txn.commit().unwrap();
}
{
let mut migrations = Migrations::default();
migrations.add_migration(Box::new(DefaultMigration::new("20191116225943")));
migrations.add_migration(Box::new(CustomizedMigration));
let db = migrations.migrate(RocksDB::open(&config, 1)).unwrap();
assert!(
vec![1u8, 1, 1, 1].as_slice()
== db.get_pinned(COLUMN, &[1, 1]).unwrap().unwrap().as_ref()
);
assert!(
vec![2u8, 2, 2, 1].as_slice()
== db.get_pinned(COLUMN, &[2, 2]).unwrap().unwrap().as_ref()
);
assert_eq!(
VERSION.as_bytes(),
db.get_pinned_default(MIGRATION_VERSION_KEY)
.unwrap()
.unwrap()
.to_vec()
.as_slice()
);
}
}
}
122 changes: 122 additions & 0 deletions db-migration/src/tests.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
use ckb_app_config::DBConfig;
use ckb_db::RocksDB;
use ckb_db_schema::MIGRATION_VERSION_KEY;
use ckb_error::Error;
use indicatif::ProgressBar;
use std::sync::Arc;

use crate::{DefaultMigration, Migration, Migrations};

#[test]
fn test_default_migration() {
let tmp_dir = tempfile::Builder::new()
.prefix("test_default_migration")
.tempdir()
.unwrap();
let config = DBConfig {
path: tmp_dir.as_ref().to_path_buf(),
..Default::default()
};
{
let mut migrations = Migrations::default();
migrations.add_migration(Box::new(DefaultMigration::new("20191116225943")));
let db = RocksDB::open(&config, 1);
migrations.init_db_version(&db).unwrap();
let r = migrations.migrate(db).unwrap();
assert_eq!(
b"20191116225943".to_vec(),
r.get_pinned_default(MIGRATION_VERSION_KEY)
.unwrap()
.unwrap()
.to_vec()
);
}
{
let mut migrations = Migrations::default();
migrations.add_migration(Box::new(DefaultMigration::new("20191116225943")));
migrations.add_migration(Box::new(DefaultMigration::new("20191127101121")));
let r = migrations.migrate(RocksDB::open(&config, 1)).unwrap();
assert_eq!(
b"20191127101121".to_vec(),
r.get_pinned_default(MIGRATION_VERSION_KEY)
.unwrap()
.unwrap()
.to_vec()
);
}
}

#[test]
fn test_customized_migration() {
struct CustomizedMigration;
const COLUMN: &str = "0";
const VERSION: &str = "20191127101121";

impl Migration for CustomizedMigration {
fn migrate(
&self,
db: RocksDB,
_pb: Arc<dyn Fn(u64) -> ProgressBar + Send + Sync>,
) -> Result<RocksDB, Error> {
let txn = db.transaction();
// append 1u8 to each value of column `0`
let mut migration = |key: &[u8], value: &[u8]| -> Result<(), Error> {
let mut new_value = value.to_vec();
new_value.push(1);
txn.put(COLUMN, key, &new_value)?;
Ok(())
};
db.full_traverse(COLUMN, &mut migration)?;
txn.commit()?;
Ok(db)
}

fn version(&self) -> &str {
VERSION
}
}

let tmp_dir = tempfile::Builder::new()
.prefix("test_customized_migration")
.tempdir()
.unwrap();
let config = DBConfig {
path: tmp_dir.as_ref().to_path_buf(),
..Default::default()
};

{
let mut migrations = Migrations::default();
migrations.add_migration(Box::new(DefaultMigration::new("20191116225943")));
let db = RocksDB::open(&config, 1);
migrations.init_db_version(&db).unwrap();
let db = migrations.migrate(db).unwrap();

let txn = db.transaction();
txn.put(COLUMN, &[1, 1], &[1, 1, 1]).unwrap();
txn.put(COLUMN, &[2, 2], &[2, 2, 2]).unwrap();
txn.commit().unwrap();
}
{
let mut migrations = Migrations::default();
migrations.add_migration(Box::new(DefaultMigration::new("20191116225943")));
migrations.add_migration(Box::new(CustomizedMigration));
let db = migrations.migrate(RocksDB::open(&config, 1)).unwrap();
assert!(
vec![1u8, 1, 1, 1].as_slice()
== db.get_pinned(COLUMN, &[1, 1]).unwrap().unwrap().as_ref()
);
assert!(
vec![2u8, 2, 2, 1].as_slice()
== db.get_pinned(COLUMN, &[2, 2]).unwrap().unwrap().as_ref()
);
assert_eq!(
VERSION.as_bytes(),
db.get_pinned_default(MIGRATION_VERSION_KEY)
.unwrap()
.unwrap()
.to_vec()
.as_slice()
);
}
}

0 comments on commit de62c91

Please sign in to comment.