Skip to content

Commit

Permalink
Make range integration tests faster (ordinals#547)
Browse files Browse the repository at this point in the history
  • Loading branch information
casey authored Sep 22, 2022
1 parent 87a5a66 commit f4d13a1
Show file tree
Hide file tree
Showing 10 changed files with 64 additions and 60 deletions.
46 changes: 46 additions & 0 deletions src/index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ pub(crate) struct Index {
height_limit: Option<u64>,
}

#[derive(Debug, PartialEq)]
pub(crate) enum List {
Spent(Txid),
Unspent(Vec<(u64, u64)>),
Expand Down Expand Up @@ -596,4 +597,49 @@ mod tests {
assert_eq!(index.height().unwrap(), 1);
}
}

#[test]
fn first_coinbase_transaction() {
let bitcoin_rpc_server = BitcoinRpcServer::spawn();

bitcoin_rpc_server.mine_block();

let tempdir = TempDir::new().unwrap();

let cookie_file = tempdir.path().join("cookie");

fs::write(&cookie_file, "username:password").unwrap();

let options = Options::try_parse_from(
format!(
"
ord
--rpc-url http://127.0.0.1:{}
--data-dir {}
--cookie-file {}
--height-limit 0
--chain regtest
",
bitcoin_rpc_server.port,
tempdir.path().display(),
cookie_file.display(),
)
.split_whitespace(),
)
.unwrap();

let index = Index::open(&options).unwrap();

assert_eq!(
index
.list(
"4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7afdeda33b:0"
.parse()
.unwrap()
)
.unwrap()
.unwrap(),
List::Unspent(vec![(0, 5000000000)])
)
}
}
3 changes: 1 addition & 2 deletions tests/epochs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ use super::*;

#[test]
fn empty() {
TestCommand::new()
.command("epochs")
TestCommand::new("epochs")
.expected_stdout(
"
0
Expand Down
8 changes: 0 additions & 8 deletions tests/list.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,5 @@
use super::*;

#[test]
fn first_coinbase_transaction() {
SlowTest::new()
.command("list 4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7afdeda33b:0")
.expected_stdout("[0,5000000000)\n")
.run();
}

#[test]
fn second_coinbase_transaction() {
SlowTest::new()
Expand Down
6 changes: 2 additions & 4 deletions tests/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,14 @@ use super::*;

#[test]
fn ok() {
TestCommand::new()
.command("parse a")
TestCommand::new("parse a")
.expected_stdout("2099999997689999\n")
.run();
}

#[test]
fn err() {
TestCommand::new()
.command("parse A")
TestCommand::new("parse A")
.stderr_regex("error: .*: invalid digit found in string.*")
.expected_status(2)
.run();
Expand Down
24 changes: 8 additions & 16 deletions tests/range.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,64 +2,56 @@ use super::*;

#[test]
fn genesis() {
SlowTest::new()
.args(&["range", "0"])
TestCommand::new("range 0")
.expected_stdout("[0,5000000000)\n")
.run()
}

#[test]
fn second_block() {
SlowTest::new()
.args(&["range", "1"])
TestCommand::new("range 1")
.expected_stdout("[5000000000,10000000000)\n")
.run()
}

#[test]
fn last_block_with_subsidy() {
SlowTest::new()
.args(&["range", "6929999"])
TestCommand::new("range 6929999")
.expected_stdout("[2099999997689999,2099999997690000)\n")
.run()
}

#[test]
fn first_block_without_subsidy() {
SlowTest::new()
.args(&["range", "6930000"])
TestCommand::new("range 6930000")
.expected_stdout("[2099999997690000,2099999997690000)\n")
.run()
}

#[test]
fn genesis_names() {
SlowTest::new()
.args(&["range", "--name", "0"])
TestCommand::new("range --name 0")
.expected_stdout("[nvtdijuwxlp,nvtcsezkbth)\n")
.run()
}

#[test]
fn names_before_last() {
SlowTest::new()
.args(&["range", "--name", "6929998"])
TestCommand::new("range --name 6929998")
.expected_stdout("[b,a)\n")
.run()
}

#[test]
fn last_name() {
SlowTest::new()
.args(&["range", "--name", "6929999"])
TestCommand::new("range --name 6929999")
.expected_stdout("[a,)\n")
.run()
}

#[test]
fn block_with_no_subsidy_range() {
SlowTest::new()
.args(&["range", "--name", "6930000"])
TestCommand::new("range --name 6930000")
.expected_stdout("[,)\n")
.run()
}
11 changes: 0 additions & 11 deletions tests/slow_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,17 +39,6 @@ impl SlowTest {
}
}

pub(crate) fn args(self, args: &[&str]) -> Self {
Self {
args: self
.args
.into_iter()
.chain(args.iter().cloned().map(str::to_owned))
.collect(),
..self
}
}

pub(crate) fn expected_stdout(self, expected_stdout: impl AsRef<str>) -> Self {
Self {
expected_stdout: Expected::String(expected_stdout.as_ref().to_owned()),
Expand Down
3 changes: 1 addition & 2 deletions tests/supply.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ use super::*;

#[test]
fn genesis() {
TestCommand::new()
.command("supply")
TestCommand::new("supply")
.expected_stdout(
"
supply: 2099999997690000
Expand Down
15 changes: 4 additions & 11 deletions tests/test_command.rs
Original file line number Diff line number Diff line change
@@ -1,28 +1,21 @@
use super::*;

pub(crate) struct TestCommand {
args: Vec<String>,
args: &'static str,
expected_status: i32,
expected_stderr: Expected,
expected_stdout: Expected,
tempdir: TempDir,
}

impl TestCommand {
pub(crate) fn new() -> Self {
pub(crate) fn new(args: &'static str) -> Self {
Self {
tempdir: TempDir::new().unwrap(),
args: Vec::new(),
expected_status: 0,
expected_stderr: Expected::Ignore,
expected_stdout: Expected::String(String::new()),
}
}

pub(crate) fn command(self, args: &str) -> Self {
Self {
args: args.split_whitespace().map(str::to_owned).collect(),
..self
args,
}
}

Expand Down Expand Up @@ -64,7 +57,7 @@ impl TestCommand {
Stdio::inherit()
})
.current_dir(&self.tempdir)
.args(self.args.clone())
.args(self.args.split_whitespace())
.output()
.unwrap();

Expand Down
3 changes: 1 addition & 2 deletions tests/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ use super::*;

#[test]
fn traits_command_prints_ordinal_traits() {
TestCommand::new()
.command("traits 0")
TestCommand::new("traits 0")
.expected_stdout(
"\
number: 0
Expand Down
5 changes: 1 addition & 4 deletions tests/version.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,5 @@ use super::*;

#[test]
fn version_flag_prints_version() {
TestCommand::new()
.command("--version")
.stdout_regex("ord .*\n")
.run();
TestCommand::new("--version").stdout_regex("ord .*\n").run();
}

0 comments on commit f4d13a1

Please sign in to comment.