Skip to content

Commit

Permalink
Merge pull request #14 from zianksm/test-callback-executor
Browse files Browse the repository at this point in the history
Test callback executor
  • Loading branch information
ramandabhijana authored Jun 15, 2023
2 parents 5c0aba2 + 2b05ebf commit c9146c3
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 0 deletions.
2 changes: 2 additions & 0 deletions dhatu/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ thiserror = "1.0.40"
[dev-dependencies]
# mock traits and structs
mockall = "0.11.4"
# mock http requests
mockito = "1.0.2"

[features]
default = ["tokio", "serde"]
Expand Down
92 changes: 92 additions & 0 deletions dhatu/src/tx/extrinsics/callback_executor/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,3 +102,95 @@ impl<Data: Serialize> CallBackBody<Data> {
}
}
}

#[cfg(test)]
mod tests {
use crate::{tx::extrinsics::extrinsics_tracker::enums::{ExtrinsicResult, Reason}, types::MandalaConfig};
use super::*;

#[cfg(test)]
use mockito;

use serde_json::json;
use subxt::{OnlineClient, blocks::ExtrinsicEvents};
use tokio::time::{sleep, Duration};

fn mock_url(server_url: String) -> Url {
Url::from_str((server_url + "/callback").as_str()).unwrap()
}

#[tokio::test]
async fn test_execute_pending_status() {
let mut server = mockito::Server::new();

let mock = server.mock("POST", "/callback")
.match_body(
mockito::Matcher::JsonString(r#"{"status":false,"message":"pending","data":null}"#.to_string())
)
.create();

let executor = Executor::new();
let status = ExtrinsicStatus::Pending;

let result = executor.execute(status, mock_url(server.url()));
assert!(result.is_ok());

sleep(Duration::from_millis(1000)).await;
mock.assert();
}

#[tokio::test]
async fn test_execute_failed_status() {
let mut server = mockito::Server::new();

let mock = server.mock("POST", "/callback")
.match_body(
mockito::Matcher::JsonString(r#"{"status":false,"message":"failed with reason : NotFound","data":null}"#.to_string())
)
.create();

let executor = Executor::new();
let status = ExtrinsicStatus::Failed(Reason::from("NotFound".to_string()));

let result = executor.execute(status, mock_url(server.url()));
assert!(result.is_ok());

sleep(Duration::from_millis(1000)).await;
mock.assert();
}

#[tokio::test]
async fn test_execute_success_status() {
let mut server = mockito::Server::new();

let events = get_extrinsic_events().await.unwrap();
let extrinsic_result = ExtrinsicResult::from(events);

let mock = server.mock("POST", "/callback")
.match_body(
mockito::Matcher::Json(json!({"status": true, "message": "success", "data": extrinsic_result.hash().to_string()}))
)
.create();

let executor = Executor::new();
let status = ExtrinsicStatus::Success(extrinsic_result);

let result = executor.execute(status, mock_url(server.url()));
assert!(result.is_ok());

sleep(Duration::from_millis(1000)).await;
mock.assert();
}

async fn get_extrinsic_events() -> Result<ExtrinsicEvents<MandalaConfig>, Box<dyn std::error::Error>> {
let api = OnlineClient::<MandalaConfig>::new().await.unwrap();
let latest_block = api.blocks().at_latest().await?;
let body = latest_block.body().await?;
for ext in body.extrinsics().iter() {
let ext = ext?;
let events = ext.events().await?;
return Ok(events);
}
Err("something went south".into())
}
}

0 comments on commit c9146c3

Please sign in to comment.