-
Notifications
You must be signed in to change notification settings - Fork 228
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
Showing
122 changed files
with
6,350 additions
and
6,339 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
); | ||
} | ||
} |
Oops, something went wrong.