Skip to content

Commit

Permalink
check module interaction
Browse files Browse the repository at this point in the history
  • Loading branch information
ngkuru committed Jul 14, 2023
1 parent 914999a commit e940e07
Showing 1 changed file with 31 additions and 4 deletions.
35 changes: 31 additions & 4 deletions crates/aptos-api-tester/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -398,13 +398,13 @@ async fn publish_module(client: &Client, account: &mut LocalAccount) -> Result<H
}

/// Helper function that interacts with the message module.
async fn set_message(client: &Client, account: &mut LocalAccount) -> Result<()> {
async fn set_message(client: &Client, account: &mut LocalAccount, message: &str) -> Result<()> {
// create payload
let payload = TransactionPayload::EntryFunction(EntryFunction::new(
ModuleId::new(account.address(), ident_str!("message").to_owned()),
ident_str!("set_message").to_owned(),
vec![],
vec![bcs::to_bytes("test message")?],
vec![bcs::to_bytes(message)?],
));

// create and submit transaction
Expand All @@ -416,6 +416,21 @@ async fn set_message(client: &Client, account: &mut LocalAccount) -> Result<()>
Ok(())
}

async fn get_message(client: &Client, address: AccountAddress) -> Option<String> {
let resource = match client
.get_account_resource(
address,
format!("{}::message::MessageHolder", address.to_hex_literal()).as_str(),
)
.await
{
Ok(response) => response.into_inner()?,
Err(_) => return None,
};

Some(resource.data.get("message")?.as_str()?.to_owned())
}

/// Tests module publishing and interaction. Checks that:
/// - module data exists
async fn test_module(
Expand All @@ -438,9 +453,21 @@ async fn test_module(
}

// interact with module
set_message(client, account).await?;
let message = "test message";
set_message(client, account, message.clone()).await?;

// check that the message is sent
let expected_message = message.to_string();
let actual_message = match get_message(client, account.address()).await {
Some(message) => message,
None => return Err(TestFailure::Error(anyhow!("module interaction isn't reflected")))
};

// todo: get account resource to see that interaction worked
if expected_message != actual_message {
return Err(TestFailure::Fail(
"module interaction isn't reflected correctly",
));
}

Ok(TestResult::Success)
}
Expand Down

0 comments on commit e940e07

Please sign in to comment.