Skip to content
Permalink
Browse files

Merge pull request #337 from input-output-hk/unit_tests_jcli

unit tests for new & staging commands
  • Loading branch information...
NicolasDP committed May 15, 2019
2 parents ba0d7fb + ada3751 commit 9cb52dfb9406b8579e6f98b44e30163799c5d6e9
@@ -60,6 +60,7 @@ custom_error = "1.6"
num-traits = "0.2"
jormungandr-utils = { path = "./utils" }
strfmt = "0.1"
mktemp = "0.4.0"

[dependencies.actix-web]
version = "0.7.18"
@@ -40,3 +40,53 @@ impl AddInput {
self.common.store(&transaction)
}
}

#[cfg(test)]
mod tests {

extern crate mktemp;
use self::common::CommonTransaction;
use super::*;
use crate::jcli_app::transaction::staging::Staging;
use crate::jcli_app::utils::io;
use chain_impl_mockchain::key::Hash;
use std::str::FromStr;

#[test]
pub fn test_input_transaction_is_saved() {
let temp_staging_file = io::get_path_in_temp("staging_file.tx").unwrap();
let transaction_id: TransactionId =
Hash::from_str("c355a02d3b5337ad0e5f5940582675229f25bc03e7feebc3aa929738e1fec35e")
.unwrap();
let transaction_index: TransactionIndex = 0;
let value: Value = Value(200);

let staging = Staging::new();
staging
.store(&Some(&temp_staging_file))
.expect("cannot store staging file");

let add_input = AddInput {
common: CommonTransaction {
staging_file: Some(temp_staging_file.clone()),
},
transaction_id: transaction_id,
index: transaction_index,
value: value,
};
add_input
.exec()
.expect("error while executing AddInput action");

let staging = Staging::load(&Some(&temp_staging_file)).unwrap();

assert_eq!(staging.inputs.len(), 1, "only one input should be created");
let input = &staging.inputs[0];
assert_eq!(transaction_id.as_ref(), &input.input_ptr, "transaction_id");
assert_eq!(
transaction_index, input.index_or_account,
"transaction_index"
);
assert_eq!(value, input.value, "value");
}
}
@@ -22,3 +22,30 @@ impl New {
Ok(self.common.store(&staging)?)
}
}

#[cfg(test)]
mod tests {

use self::common::CommonTransaction;
use super::*;
use jcli_app::utils::io;

#[test]
pub fn test_staging_file_is_created() {
let temp_staging_file = io::get_path_in_temp("staging_file.tx").unwrap();

let new = New {
common: CommonTransaction {
staging_file: Some(temp_staging_file.clone()),
},
};
new.exec().expect(" error while executing New action");;

assert_eq!(
temp_staging_file.is_file(),
true,
"staging file {:?} not created",
&temp_staging_file
);
}
}
@@ -269,3 +269,49 @@ impl Staging {
.collect()
}
}

#[cfg(test)]
mod tests {

use super::*;
use chain_impl_mockchain as chain;
use chain_impl_mockchain::key::Hash;
use std::str::FromStr;

#[test]
pub fn test_initial_stage_is_balancing() {
let staging = Staging::new();
let expected_kind = StagingKind::Balancing;
assert_eq!(
staging.kind, expected_kind,
"'initial staging kind should be {}",
expected_kind
);
}

#[test]
pub fn test_cannot_add_input_when_stage_is_finalizing() {
let hash =
Hash::from_str("c355a02d3b5337ad0e5f5940582675229f25bc03e7feebc3aa929738e1fec35e")
.unwrap();
let incorrect_stage = StagingKind::Finalizing;

let mut staging = Staging::new();
staging.kind = incorrect_stage.clone();

let mut input_ptr = [0u8; INPUT_PTR_SIZE];
input_ptr.clone_from_slice(hash.as_ref());

let result = staging.add_input(chain::transaction::Input {
input_ptr: input_ptr,
index_or_account: 0,
value: Value(200),
});

assert!(
result.is_err(),
"add_input message should throw exception when adding inputs while in {:?} stage",
&incorrect_stage
);
}
}
@@ -1,5 +1,8 @@
use std::io::{stdin, stdout, BufRead, BufReader, Error, Write};
use std::path::Path;
use std::path::PathBuf;

extern crate mktemp;

/// open the given file path as a writable stream, or stdout if no path
/// provided
@@ -35,3 +38,18 @@ pub fn open_file_read<P: AsRef<Path>>(path: &Option<P>) -> Result<impl BufRead,
None => Ok(Box::new(BufReader::new(stdin())) as Box<dyn BufRead>),
}
}

/// get path to unique file in temp folder
pub fn get_path_in_temp<T: Into<String>>(file_path: T) -> Result<PathBuf, std::io::Error> {
let mut path = get_temp_folder().unwrap();
path.push(file_path.into());
Ok(path)
}

/// get os temp folder
pub fn get_temp_folder() -> Result<PathBuf, std::io::Error> {
let temp_dir = mktemp::Temp::new_dir().unwrap();
let path = temp_dir.to_path_buf();
temp_dir.release();
Ok(path)
}

0 comments on commit 9cb52df

Please sign in to comment.
You can’t perform that action at this time.