Make tests more readable by following arrange-act-assert

This commit is contained in:
Thomas Eizinger 2021-03-05 16:45:50 +11:00
parent c2329b19a2
commit 418ad7089d
No known key found for this signature in database
GPG Key ID: 651AC83A6C6C8B96

View File

@ -225,7 +225,7 @@ impl Wallet {
} }
pub async fn get_block_height(&self) -> Result<BlockHeight> { pub async fn get_block_height(&self) -> Result<BlockHeight> {
let url = blocks_tip_height_url(&self.http_url)?; let url = make_blocks_tip_height_url(&self.http_url)?;
let height = retry(ConstantBackoff::new(Duration::from_secs(1)), || async { let height = retry(ConstantBackoff::new(Duration::from_secs(1)), || async {
let height = reqwest::Client::new() let height = reqwest::Client::new()
.request(Method::GET, url.clone()) .request(Method::GET, url.clone())
@ -246,7 +246,7 @@ impl Wallet {
} }
pub async fn transaction_block_height(&self, txid: Txid) -> Result<BlockHeight> { pub async fn transaction_block_height(&self, txid: Txid) -> Result<BlockHeight> {
let url = tx_status_url(txid, &self.http_url)?; let url = make_tx_status_url(&self.http_url, txid)?;
#[derive(Serialize, Deserialize, Debug, Clone)] #[derive(Serialize, Deserialize, Debug, Clone)]
struct TransactionStatus { struct TransactionStatus {
block_height: Option<u32>, block_height: Option<u32>,
@ -320,37 +320,42 @@ impl Wallet {
} }
} }
fn tx_status_url(txid: Txid, base_url: &Url) -> Result<Url> { fn make_tx_status_url(base_url: &Url, txid: Txid) -> Result<Url> {
let url = base_url.join(&format!("tx/{}/status", txid))?; let url = base_url.join(&format!("tx/{}/status", txid))?;
Ok(url) Ok(url)
} }
fn blocks_tip_height_url(base_url: &Url) -> Result<Url> { fn make_blocks_tip_height_url(base_url: &Url) -> Result<Url> {
let url = base_url.join("blocks/tip/height")?; let url = base_url.join("blocks/tip/height")?;
Ok(url) Ok(url)
} }
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use crate::bitcoin::wallet::{blocks_tip_height_url, tx_status_url}; use super::*;
use crate::bitcoin::Txid;
use crate::cli::config::DEFAULT_ELECTRUM_HTTP_URL; use crate::cli::config::DEFAULT_ELECTRUM_HTTP_URL;
use reqwest::Url;
#[test] #[test]
fn create_tx_status_url_from_default_base_url_success() { fn create_tx_status_url_from_default_base_url_success() {
let txid: Txid = Txid::default(); let base_url = DEFAULT_ELECTRUM_HTTP_URL.parse().unwrap();
let base_url = Url::parse(DEFAULT_ELECTRUM_HTTP_URL).expect("Could not parse url"); let txid = Txid::default;
let url = tx_status_url(txid, &base_url).expect("Could not create url");
let expected = format!("https://blockstream.info/testnet/api/tx/{}/status", txid); let url = make_tx_status_url(&base_url, txid()).unwrap();
assert_eq!(url.as_str(), expected);
assert_eq!(url.as_str(), "https://blockstream.info/testnet/api/tx/0000000000000000000000000000000000000000000000000000000000000000/status");
} }
#[test] #[test]
fn create_block_tip_height_url_from_default_base_url_success() { fn create_block_tip_height_url_from_default_base_url_success() {
let base_url = Url::parse(DEFAULT_ELECTRUM_HTTP_URL).expect("Could not parse url"); let base_url = DEFAULT_ELECTRUM_HTTP_URL.parse().unwrap();
let url = blocks_tip_height_url(&base_url).expect("Could not create url");
let expected = "https://blockstream.info/testnet/api/blocks/tip/height"; let url = make_blocks_tip_height_url(&base_url).unwrap();
assert_eq!(url.as_str(), expected);
assert_eq!(
url.as_str(),
"https://blockstream.info/testnet/api/blocks/tip/height"
);
} }
} }