From 7f2a3c469e0768f33789ff85303d20d057932d0f Mon Sep 17 00:00:00 2001 From: Eline Jorritsma Date: Mon, 20 Nov 2023 11:14:24 +0100 Subject: [PATCH] Add SingleLedger tests --- src/horizon_client/horizon_client_new.rs | 177 +++++++++++++++++++++-- 1 file changed, 163 insertions(+), 14 deletions(-) diff --git a/src/horizon_client/horizon_client_new.rs b/src/horizon_client/horizon_client_new.rs index 4a2fc6a..aecd5dc 100644 --- a/src/horizon_client/horizon_client_new.rs +++ b/src/horizon_client/horizon_client_new.rs @@ -1,7 +1,16 @@ use derive_getters::Getters; use url::Url; -use crate::models::Response; +use crate::{models::{Response, Request}, ledgers::single_ledger_response::SingleLedgerResponse}; + +pub trait RequestNew { + fn build_url(&self, base_url: &BaseUrl) -> String; +} + +// pub trait RequestBuilder { +// fn build(self) -> Result; +// } + #[derive(Clone)] pub enum HttpMethod { @@ -19,8 +28,8 @@ pub struct SingleLedgerRequest { method: HttpMethod } -impl SingleLedgerRequest { - pub fn build_url(&self, base_url: &BaseUrl) -> String { +impl RequestNew for SingleLedgerRequest { + fn build_url(&self, base_url: &BaseUrl) -> String { format!( "{}{}/{}", base_url.0, @@ -117,23 +126,29 @@ impl HorizonClientNew { } impl HorizonClientNew { - pub async fn send( + /// Gets a single ledger from the server + /// # Arguments + /// * `self` - The Horizon client + /// * request - The single ledger request + /// # Returns + /// The single ledger response + /// # Errors + /// Returns an error if the request fails + /// [GET /ledgers/{ledger_id}](https://www.stellar.org/developers/horizon/reference/endpoints/ledgers-single.html) + pub async fn get_single_ledger( &self, - request: SingleLedgerRequest - ) -> Result { - match request.method() { - HttpMethod::GET => Self::get(&self, request).await, - HttpMethod::POST => todo!() - } + request: &SingleLedgerRequest, + ) -> Result { + self.get::(request).await } async fn get( &self, - request: SingleLedgerRequest + request: &impl RequestNew ) -> Result { let url = request.build_url(&self.base_url); let response = reqwest::get(&url).await.map_err(|e| e.to_string())?; - println!("\n\nREQWEST RESPONSE: {:?}", response); + // println!("\n\nREQWEST RESPONSE: {:?}", response); let result: TResponse = handle_response(response).await?; Ok(result) @@ -172,6 +187,8 @@ fn url_validate(url_to_validate: impl Into) -> Result<(), String> { #[cfg(test)] mod tests { + use base64::encode; + use crate::ledgers::single_ledger_response::SingleLedgerResponse; use super::*; @@ -201,8 +218,140 @@ mod tests { .build() .unwrap(); - let response: Result = horizon_client.send(request).await; + let _single_ledger_response = horizon_client.get_single_ledger(&request).await; + + assert!(_single_ledger_response.clone().is_ok()); + + assert_eq!( + _single_ledger_response.clone().unwrap().id(), + "eca856e0073dc2087249dc929ed31c09c3babfef2e687b685d0513dbe6489a18" + ); + + assert_eq!( + _single_ledger_response.clone().unwrap().paging_token(), + "8589934592" + ); + + assert_eq!( + _single_ledger_response.clone().unwrap().hash(), + "eca856e0073dc2087249dc929ed31c09c3babfef2e687b685d0513dbe6489a18" + ); + + assert_eq!( + _single_ledger_response.clone().unwrap().prev_hash(), + "63d98f536ee68d1b27b5b89f23af5311b7569a24faf1403ad0b52b633b07be99" + ); + + assert_eq!(*_single_ledger_response.clone().unwrap().sequence(), 2); + + assert_eq!( + *_single_ledger_response + .clone() + .unwrap() + .successful_transaction_count(), + 0 + ); + + assert_eq!( + *_single_ledger_response + .clone() + .unwrap() + .failed_transaction_count(), + 0 + ); + + assert_eq!( + *_single_ledger_response.clone().unwrap().operation_count(), + 0 + ); + + assert_eq!( + *_single_ledger_response + .clone() + .unwrap() + .tx_set_operation_count(), + 0 + ); + + assert_eq!( + _single_ledger_response.clone().unwrap().closed_at(), + "2023-06-14T09:19:48Z" + ); + + assert_eq!( + _single_ledger_response.clone().unwrap().total_coins(), + "100000000000.0000000" + ); + + assert_eq!( + _single_ledger_response.clone().unwrap().fee_pool(), + "0.0000000" + ); + + assert_eq!( + *_single_ledger_response + .clone() + .unwrap() + .base_fee_in_stroops(), + 100 + ); + + assert_eq!( + *_single_ledger_response + .clone() + .unwrap() + .base_reserve_in_stroops(), + 100000000 + ); + + assert_eq!( + *_single_ledger_response.clone().unwrap().max_tx_set_size(), + 100 + ); + + assert_eq!( + *_single_ledger_response.clone().unwrap().protocol_version(), + 0 + ); + + let decoded_xdr_header = _single_ledger_response + .unwrap() + .decoded_header_xdr() + .unwrap(); + + assert_eq!( + decoded_xdr_header.bucket_list_hash.to_string(), + "735227ed398461291237687b08446aa2c9b096e0c98a462dadda569f05dd2484" + ); + + assert_eq!(decoded_xdr_header.ledger_seq, 2); + + assert_eq!(decoded_xdr_header.total_coins, 1000000000000000000); + + assert_eq!(decoded_xdr_header.fee_pool, 0); + + assert_eq!(decoded_xdr_header.inflation_seq, 0); + + assert_eq!(decoded_xdr_header.id_pool, 0); + + assert_eq!(decoded_xdr_header.base_fee, 100); + + assert_eq!(decoded_xdr_header.base_reserve, 100000000); + + assert_eq!(decoded_xdr_header.max_tx_set_size, 100); + + let tx_set_hash = decoded_xdr_header.scp_value.tx_set_hash.to_string(); + let tx_set_hash_bytes = hex::decode(tx_set_hash).expect("Failed to decode hex"); + let tx_set_hash_base64 = encode(&tx_set_hash_bytes); + + assert_eq!( + tx_set_hash_base64, + "uZRHr9UdXKbTKiclfOjy72YZFJUkJPVcKT5htvorm1Q=" + ); - assert!(response.clone().is_ok()); + assert_eq!( + decoded_xdr_header.scp_value.close_time, + stellar_xdr::TimePoint(1686734388) + ); } } \ No newline at end of file