Skip to content

Commit

Permalink
Unit test per module
Browse files Browse the repository at this point in the history
  • Loading branch information
Thomas Luijken committed Mar 8, 2024
1 parent 4e0ea4a commit 29c1c98
Show file tree
Hide file tree
Showing 7 changed files with 558 additions and 583 deletions.
98 changes: 98 additions & 0 deletions src/accounts/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,3 +70,101 @@ pub mod prelude {
pub use super::response::*;
pub use super::single_account_request::*;
}

#[cfg(test)]
pub mod test {

use super::prelude::*;
use crate::horizon_client::HorizonClient;

static SEQUENCE: &str = "131988639973376";
static ACCOUNT_ID: &str = "GCAHCEGRUI7FFAQE3DBQWV7ULMQHFBUIVRZC4R2VISREAY6D52Z2NODN";
static LAST_MODIFIED_TIME: &str = "2024-02-08T14:25:14Z";
static LAST_MODIFIED_LEDGER: u64 = 30731;

#[tokio::test]
async fn test_get_account_list() {
// Initialize horizon client
let horizon_client =
HorizonClient::new("https://horizon-testnet.stellar.org".to_string()).unwrap();

// construct request
let accounts_request = AccountsRequest::new()
.set_signer_filter(ACCOUNT_ID)
.unwrap()
.set_limit(10)
.unwrap();

// call the get_account_list method to retrieve the account list response
let accounts_response: Result<AccountsResponse, String> =
horizon_client.get_account_list(&accounts_request).await;

assert!(accounts_response.is_ok());
let binding = accounts_response.unwrap();
let response = &binding.embedded().records()[0];
assert_eq!(response.account_id(), ACCOUNT_ID);
assert_eq!(response.id(), ACCOUNT_ID);
assert_eq!(response.sequence(), SEQUENCE);
assert_eq!(response.subentry_count(), &0);
assert_eq!(response.last_modified_ledger(), &LAST_MODIFIED_LEDGER);
assert_eq!(response.last_modified_time(), LAST_MODIFIED_TIME);
assert_eq!(response.thresholds().low_threshold(), &0);
assert_eq!(response.thresholds().med_threshold(), &0);
assert_eq!(response.thresholds().high_threshold(), &0);
assert_eq!(response.flags().auth_required(), &false);
assert_eq!(response.flags().auth_revocable(), &false);
assert_eq!(response.flags().auth_immutable(), &false);
assert_eq!(response.flags().auth_clawback_enabled(), &false);
assert_eq!(response.balances()[0].balance(), "10000.0000000");
assert_eq!(response.balances()[0].asset_type(), "native");
assert_eq!(response.balances()[0].buying_liabilities(), "0.0000000");
assert_eq!(response.balances()[0].selling_liabilities(), "0.0000000");
assert_eq!(response.signers()[0].key(), ACCOUNT_ID);
assert_eq!(response.signers()[0].weight(), &1);
assert_eq!(response.signers()[0].singer_type(), "ed25519_public_key");
assert_eq!(response.num_sponsoring(), &0);
assert_eq!(response.num_sponsored(), &0);
assert_eq!(response.paging_token(), ACCOUNT_ID);
}

#[tokio::test]
async fn test_get_single_account() {
// Initialize horizon client
let horizon_client =
HorizonClient::new("https://horizon-testnet.stellar.org".to_string()).unwrap();

// construct request
let single_account_request = SingleAccountRequest::new()
.set_account_id(ACCOUNT_ID.to_string())
.unwrap();

let single_account_response = horizon_client
.get_single_account(&single_account_request)
.await;

assert!(single_account_response.is_ok());
let response = single_account_response.unwrap();
assert_eq!(response.account_id().to_string(), ACCOUNT_ID);
assert_eq!(response.sequence().to_string(), SEQUENCE);
assert_eq!(response.subentry_count(), &0);
assert_eq!(response.last_modified_ledger(), &LAST_MODIFIED_LEDGER);
assert_eq!(response.last_modified_time(), LAST_MODIFIED_TIME);
assert_eq!(response.thresholds().low_threshold(), &0);
assert_eq!(response.thresholds().med_threshold(), &0);
assert_eq!(response.thresholds().high_threshold(), &0);
assert_eq!(response.flags().auth_required(), &false);
assert_eq!(response.flags().auth_revocable(), &false);
assert_eq!(response.flags().auth_immutable(), &false);
assert_eq!(response.flags().auth_clawback_enabled(), &false);
assert_eq!(response.balances()[0].balance(), "10000.0000000");
assert_eq!(response.balances()[0].asset_type(), "native");
assert_eq!(response.balances()[0].buying_liabilities(), "0.0000000");
assert_eq!(response.balances()[0].selling_liabilities(), "0.0000000");
assert_eq!(response.signers()[0].key(), ACCOUNT_ID);
assert_eq!(response.signers()[0].weight(), &1);
assert_eq!(response.signers()[0].singer_type(), "ed25519_public_key");
assert_eq!(response.num_sponsoring(), &0);
assert_eq!(response.num_sponsored(), &0);
assert_eq!(response.paging_token(), ACCOUNT_ID);
}
}
65 changes: 65 additions & 0 deletions src/assets/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,3 +72,68 @@ pub mod prelude {
pub use super::all_assets_request::*;
pub use super::response::*;
}

#[cfg(test)]
pub mod test {

use super::prelude::*;
use crate::horizon_client::HorizonClient;

#[tokio::test]
async fn test_get_all_assets() {
let asset_type = "credit_alphanum4";
let asset_code = "0";
let asset_issuer = "GAGNEED7RUE6PNAB3AKXFU6QZF4EUSVTICHE7YRHB53KDOEHGKWBL6BE";
let paging_token =
"0_GAGNEED7RUE6PNAB3AKXFU6QZF4EUSVTICHE7YRHB53KDOEHGKWBL6BE_credit_alphanum4";
let num_accounts = 0;
let amount = "0.0000000";
let num_authorized = 0;
let num_unauthorized = 0;
let balances_authorized = "0.0000000";
let balances_unauthorized = "0.0000000";

// Initialize horizon client
let horizon_client =
HorizonClient::new("https://horizon-testnet.stellar.org".to_string()).unwrap();

// construct request
let all_assets_request: AllAssetsRequest = AllAssetsRequest::new().set_limit(1).unwrap();

let response = horizon_client.get_all_assets(&all_assets_request).await;

assert!(response.is_ok());
let binding = response.unwrap();
let response = &binding.embedded().records()[0];
assert_eq!(response.asset_type(), asset_type);
assert_eq!(response.asset_code(), asset_code);
assert_eq!(response.asset_issuer(), asset_issuer);
assert_eq!(response.paging_token(), paging_token);
assert_eq!(
response.paging_token(),
&format!("{}_{}_{}", asset_code, asset_issuer, asset_type)
);
assert_eq!(response.num_accounts(), &num_accounts);
assert_eq!(response.num_claimable_balances(), &0);
assert_eq!(response.num_liquidity_pools(), &0);
assert_eq!(response.amount(), amount);
assert_eq!(response.accounts().authorized(), &num_authorized);
assert_eq!(response.accounts().authorized_to_maintain_liabilities(), &2);
assert_eq!(response.accounts().unauthorized(), &num_unauthorized);
assert_eq!(response.claimable_balances_amount(), "0.0000000");
assert_eq!(response.liquidity_pools_amount(), "0.0000000");
assert_eq!(response.contracts_amount(), "0.0000000");
assert_eq!(response.balances().authorized(), balances_authorized);
assert_eq!(
response.balances().authorized_to_maintain_liabilities(),
"1.0000000"
);
assert_eq!(response.balances().unauthorized(), balances_unauthorized);

let auth_required = true;
assert_eq!(response.flags().auth_required(), &auth_required);
assert_eq!(response.flags().auth_revocable(), &true);
assert_eq!(response.flags().auth_immutable(), &false);
assert_eq!(response.flags().auth_clawback_enabled(), &true);
}
}
125 changes: 125 additions & 0 deletions src/claimable_balances/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ pub mod prelude {
mod tests {
use super::parse_epoch;
use super::prelude::*;
use crate::horizon_client::HorizonClient;
use chrono::DateTime;
use chrono::{TimeZone, Utc};
use lazy_static::lazy_static;
Expand Down Expand Up @@ -153,4 +154,128 @@ mod tests {
fn test_parse_epoch() {
assert_eq!(parse_epoch(&EPOCH_STR.to_string()), *DATE);
}

#[tokio::test]
async fn test_get_all_claimable_balances() {
// Initialize horizon client
let horizon_client =
HorizonClient::new("https://horizon-testnet.stellar.org".to_string()).unwrap();

// construct request
let all_claimable_balances_request =
AllClaimableBalancesRequest::new().set_limit(4).unwrap();

let all_claimable_balances_response = horizon_client
.get_all_claimable_balances(&all_claimable_balances_request)
.await;

assert!(all_claimable_balances_response.is_ok());

let binding = all_claimable_balances_response.unwrap();
let predicate = binding.embedded().records()[1].claimants()[0].predicate();

let jan_first_2024 = Utc::with_ymd_and_hms(&Utc, 2024, 1, 1, 0, 0, 0).unwrap();
let valid_date = Utc::with_ymd_and_hms(&Utc, 2024, 2, 10, 0, 0, 0).unwrap();

assert_eq!(predicate.is_valid(jan_first_2024), true);
assert_eq!(predicate.is_valid(valid_date), true);
let record = &binding.embedded().records()[0];

assert_eq!(
record.id(),
"000000000a12cd57c169a34e7794bdcdf2d093fab135c59ea599e2d1233d7a53f26c1464"
);

assert_eq!(
record.asset(),
"USDC:GAKNDFRRWA3RPWNLTI3G4EBSD3RGNZZOY5WKWYMQ6CQTG3KIEKPYWAYC"
);

assert_eq!(record.amount(), "0.0010000");

assert_eq!(
record.sponsor(),
"GCENYNAX2UCY5RFUKA7AYEXKDIFITPRAB7UYSISCHVBTIAKPU2YO57OA"
);

assert_eq!(record.last_modified_ledger(), &591);

assert_eq!(
record.last_modified_time().to_string(),
"2024-02-06T18:25:07Z"
);

assert_eq!(record.flags().clawback_enabled(), &false);
}

#[tokio::test]
async fn test_get_single_claimable_balance() {
// Initialize horizon client
let horizon_client =
HorizonClient::new("https://horizon-testnet.stellar.org".to_string()).unwrap();

let single_claimable_balance_request = SingleClaimableBalanceRequest::new()
.set_claimable_balance_id(
"000000000a12cd57c169a34e7794bdcdf2d093fab135c59ea599e2d1233d7a53f26c1464"
.to_string(),
);

let single_claimable_balance_response = horizon_client
.get_single_claimable_balance(&single_claimable_balance_request)
.await;

assert!(single_claimable_balance_response.is_ok());

let binding = single_claimable_balance_response.clone().unwrap();
let predicate = binding.claimants()[0].predicate();

let jan_first_2024 = Utc::with_ymd_and_hms(&Utc, 2024, 1, 1, 0, 0, 0).unwrap();
let valid_date = Utc::with_ymd_and_hms(&Utc, 2024, 2, 10, 0, 0, 0).unwrap();

assert_eq!(predicate.is_valid(jan_first_2024), true);
assert_eq!(predicate.is_valid(valid_date), true);

let single_claimable_balance_response = single_claimable_balance_response.unwrap();
assert_eq!(
single_claimable_balance_response.id().to_string(),
"000000000a12cd57c169a34e7794bdcdf2d093fab135c59ea599e2d1233d7a53f26c1464"
);

assert_eq!(
single_claimable_balance_response.asset().to_string(),
"USDC:GAKNDFRRWA3RPWNLTI3G4EBSD3RGNZZOY5WKWYMQ6CQTG3KIEKPYWAYC"
);

assert_eq!(
single_claimable_balance_response.amount().to_string(),
"0.0010000"
);

assert_eq!(
single_claimable_balance_response.sponsor().to_string(),
"GCENYNAX2UCY5RFUKA7AYEXKDIFITPRAB7UYSISCHVBTIAKPU2YO57OA"
);

assert_eq!(
single_claimable_balance_response.last_modified_ledger(),
&591
);

assert_eq!(
single_claimable_balance_response
.last_modified_time()
.to_string(),
"2024-02-06T18:25:07Z"
);

assert_eq!(
single_claimable_balance_response.flags().clawback_enabled(),
&false
);

assert_eq!(
single_claimable_balance_response.paging_token().to_string(),
"591-000000000a12cd57c169a34e7794bdcdf2d093fab135c59ea599e2d1233d7a53f26c1464"
);
}
}
Loading

0 comments on commit 29c1c98

Please sign in to comment.