Skip to content

Commit

Permalink
Add SingleLedger tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Eline Jorritsma committed Nov 20, 2023
1 parent 514833f commit 7f2a3c4
Showing 1 changed file with 163 additions and 14 deletions.
177 changes: 163 additions & 14 deletions src/horizon_client/horizon_client_new.rs
Original file line number Diff line number Diff line change
@@ -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<impl RequestNew, String>;
// }


#[derive(Clone)]
pub enum HttpMethod {
Expand All @@ -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,
Expand Down Expand Up @@ -117,23 +126,29 @@ impl HorizonClientNew<NoBaseUrl> {
}

impl HorizonClientNew<BaseUrl> {
pub async fn send<TResponse: Response + std::fmt::Debug>(
/// 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<TResponse, String> {
match request.method() {
HttpMethod::GET => Self::get(&self, request).await,
HttpMethod::POST => todo!()
}
request: &SingleLedgerRequest,
) -> Result<SingleLedgerResponse, String> {
self.get::<SingleLedgerResponse>(request).await
}

async fn get<TResponse: Response + std::fmt::Debug>(
&self,
request: SingleLedgerRequest
request: &impl RequestNew
) -> Result<TResponse, String> {
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)
Expand Down Expand Up @@ -172,6 +187,8 @@ fn url_validate(url_to_validate: impl Into<String>) -> Result<(), String> {

#[cfg(test)]
mod tests {
use base64::encode;

use crate::ledgers::single_ledger_response::SingleLedgerResponse;

use super::*;
Expand Down Expand Up @@ -201,8 +218,140 @@ mod tests {
.build()
.unwrap();

let response: Result<SingleLedgerResponse, String> = 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)
);
}
}

0 comments on commit 7f2a3c4

Please sign in to comment.