Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Creation of test functions for contract near-contract-standards #1229

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

fabrobles92
Copy link

@frol

Opening a draft PR to get early feedback, if any; just wanted to make sure I am following the expected structure :)

Thanks in advance!

…:core and creation of test "test_fungible_token_core" in examples/fungible-token
@frol
Copy link
Collaborator

frol commented Aug 1, 2024

@fabrobles92 It is somewhat right direction, but I want to point out that test function should be hidden behind a feature flag (unit-testing), used in the example, and documented that usage for contract developers who want to use it in their contracts where they implemented the interface.

- Added test test_transfer_call_ok in FungibleTokenCore trait
- Added test for FungibleTokenMetadataProvider trait
let result = contract.ft_transfer_call(accounts(1), amount.into(), None, "".to_string());
match result {
PromiseOrValue::Promise(_promise) => {
println!("Handle promise here");
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@frol I am having a hard time here trying to get the data from the Promise, I read documentation on how to resolve promises, and they need a callback, the problem is that I dont know if I should make the call back function in here or where I am calling this function.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You won't be able to read the content of _promise, instead, use near_sdk::test_utils::get_created_receipts

@fabrobles92
Copy link
Author

@frol Hello, pr updated, I added a tests module inside core and metadata to group easier the tests and to avoid linting warning if I only put the test behind the feature flag.

Also I made a comment in the code about how to interact with the promise of ft_transfer_call. Thanks in advance :D

@fabrobles92 fabrobles92 marked this pull request as ready for review August 8, 2024 16:06
Copy link
Collaborator

@frol frol left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@fabrobles92 Thank you for your patience. I have provided more feedback and answered your question

Comment on lines +40 to +46
assert_eq!(metadata.spec, FT_METADATA_SPEC.to_string());
assert_eq!(metadata.name, "Example NEAR fungible token");
assert_eq!(metadata.symbol, "EXAMPLE".to_string());
assert_eq!(metadata.icon, Some("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 288 288'%3E%3Cg id='l' data-name='l'%3E%3Cpath d='M187.58,79.81l-30.1,44.69a3.2,3.2,0,0,0,4.75,4.2L191.86,103a1.2,1.2,0,0,1,2,.91v80.46a1.2,1.2,0,0,1-2.12.77L102.18,77.93A15.35,15.35,0,0,0,90.47,72.5H87.34A15.34,15.34,0,0,0,72,87.84V201.16A15.34,15.34,0,0,0,87.34,216.5h0a15.35,15.35,0,0,0,13.08-7.31l30.1-44.69a3.2,3.2,0,0,0-4.75-4.2L96.14,186a1.2,1.2,0,0,1-2-.91V104.61a1.2,1.2,0,0,1,2.12-.77l89.55,107.23a15.35,15.35,0,0,0,11.71,5.43h3.13A15.34,15.34,0,0,0,216,201.16V87.84A15.34,15.34,0,0,0,200.66,72.5h0A15.35,15.35,0,0,0,187.58,79.81Z'/%3E%3C/g%3E%3C/svg%3E".to_string()));
assert_eq!(metadata.reference, None);
assert_eq!(metadata.reference_hash, None);
assert_eq!(metadata.decimals, 24);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test should only validate the structure and logic. The contract is not required to hold specific metadata values. In the case of metadata, we may just test that calling the method does not crash, so just calling it will be enough and the rest is handled by Rust (we already know that the contract implements FungibleTokenMetadataProvider, and as such we know that the returned value is standard-compliant.

Comment on lines +134 to +135
test_transfer_ok(contract);
test_transfer_call_ok(contract);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We also need negative tests, such as:

  1. Attempt to transfer more tokens that sender account has on its balance - should fail and balance remain the same for both accounts
  2. Attempt to transfer to a non-registered account - should fail since storage cost is not covered
  3. Attempt to transfer from a non-registered account

let result = contract.ft_transfer_call(accounts(1), amount.into(), None, "".to_string());
match result {
PromiseOrValue::Promise(_promise) => {
println!("Handle promise here");
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You won't be able to read the content of _promise, instead, use near_sdk::test_utils::get_created_receipts

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants