From 418ad7089d64f1bc4fa3b1ed71307f5e3b63b0df Mon Sep 17 00:00:00 2001 From: Thomas Eizinger Date: Fri, 5 Mar 2021 16:45:50 +1100 Subject: [PATCH] Make tests more readable by following arrange-act-assert --- swap/src/bitcoin/wallet.rs | 37 +++++++++++++++++++++---------------- 1 file changed, 21 insertions(+), 16 deletions(-) diff --git a/swap/src/bitcoin/wallet.rs b/swap/src/bitcoin/wallet.rs index ff125f0f..cc305253 100644 --- a/swap/src/bitcoin/wallet.rs +++ b/swap/src/bitcoin/wallet.rs @@ -225,7 +225,7 @@ impl Wallet { } pub async fn get_block_height(&self) -> Result { - 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 = reqwest::Client::new() .request(Method::GET, url.clone()) @@ -246,7 +246,7 @@ impl Wallet { } pub async fn transaction_block_height(&self, txid: Txid) -> Result { - let url = tx_status_url(txid, &self.http_url)?; + let url = make_tx_status_url(&self.http_url, txid)?; #[derive(Serialize, Deserialize, Debug, Clone)] struct TransactionStatus { block_height: Option, @@ -320,37 +320,42 @@ impl Wallet { } } -fn tx_status_url(txid: Txid, base_url: &Url) -> Result { +fn make_tx_status_url(base_url: &Url, txid: Txid) -> Result { let url = base_url.join(&format!("tx/{}/status", txid))?; + Ok(url) } -fn blocks_tip_height_url(base_url: &Url) -> Result { +fn make_blocks_tip_height_url(base_url: &Url) -> Result { let url = base_url.join("blocks/tip/height")?; + Ok(url) } #[cfg(test)] mod tests { - use crate::bitcoin::wallet::{blocks_tip_height_url, tx_status_url}; - use crate::bitcoin::Txid; + use super::*; use crate::cli::config::DEFAULT_ELECTRUM_HTTP_URL; - use reqwest::Url; #[test] fn create_tx_status_url_from_default_base_url_success() { - let txid: Txid = Txid::default(); - let base_url = Url::parse(DEFAULT_ELECTRUM_HTTP_URL).expect("Could not parse url"); - let url = tx_status_url(txid, &base_url).expect("Could not create url"); - let expected = format!("https://blockstream.info/testnet/api/tx/{}/status", txid); - assert_eq!(url.as_str(), expected); + let base_url = DEFAULT_ELECTRUM_HTTP_URL.parse().unwrap(); + let txid = Txid::default; + + let url = make_tx_status_url(&base_url, txid()).unwrap(); + + assert_eq!(url.as_str(), "https://blockstream.info/testnet/api/tx/0000000000000000000000000000000000000000000000000000000000000000/status"); } #[test] 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 url = blocks_tip_height_url(&base_url).expect("Could not create url"); - let expected = "https://blockstream.info/testnet/api/blocks/tip/height"; - assert_eq!(url.as_str(), expected); + let base_url = DEFAULT_ELECTRUM_HTTP_URL.parse().unwrap(); + + let url = make_blocks_tip_height_url(&base_url).unwrap(); + + assert_eq!( + url.as_str(), + "https://blockstream.info/testnet/api/blocks/tip/height" + ); } }