Skip to content

Commit

Permalink
Refactor tests: splite nft_test.rs to nft_test.rs and meta_loader_tes…
Browse files Browse the repository at this point in the history
…t.rd (#129)

* Splitted tests in nft_test.rs.
  • Loading branch information
Selarion authored Dec 18, 2024
1 parent 8e241d3 commit df765d6
Show file tree
Hide file tree
Showing 4 changed files with 203 additions and 125 deletions.
2 changes: 1 addition & 1 deletion client/src/contract/nft/collection_contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use crate::meta::MetaDataContent;
use crate::types::TvmStackEntry;

/// Data returned by get_collection_data according to TEP-62
#[derive(Debug, Clone)]
#[derive(Debug, Clone, PartialEq)]
pub struct NftCollectionData {
/// The count of currently deployed NFT items in collection.
/// Generally, collection should issue NFT with sequential indexes (see Rationale(2) ).
Expand Down
3 changes: 2 additions & 1 deletion client/src/contract/nft/item_contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use crate::meta::MetaDataContent;
use crate::types::TvmStackEntry;

/// Data returned by get_static_data according to TEP-62
#[derive(Debug, Clone)]
#[derive(Debug, Clone, PartialEq)]
pub struct NftItemData {
/// if not zero, then this NFT is fully initialized and ready for interaction.
pub init: bool,
Expand Down Expand Up @@ -81,6 +81,7 @@ pub trait NftItemContract: TonContractInterface {
/// the individual content of this NFT item and
/// returns the full content of the NFT item in format
/// that complies with standard TEP-64.
// TODO: Issue https://github.com/Selarion/tonlib-rs/issues/2
async fn get_nft_content(
&self,
index: &BigUint,
Expand Down
135 changes: 135 additions & 0 deletions client/tests/meta_loader_test.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
use serde_json::json;
use tokio_test::assert_ok;
use tonlib_client::meta::{
LoadMeta, MetaDataContent, NftCollectionMetaData, NftColletionMetaLoader, NftItemMetaData,
NftItemMetaLoader,
};

mod common;

// ---- Nft item metadata load tests
#[tokio::test]
async fn test_load_item_metadata() -> anyhow::Result<()> {
common::init_logging();
let meta_loader = assert_ok!(NftItemMetaLoader::default());
let content = MetaDataContent::External {
uri: "https://nft.fragment.com/number/88805397120.json".to_string(),
};

let expected_res = NftItemMetaData {
name: Some("+888 0539 7120".to_string()),
description: Some(
"The anonymous number +888 0539 7120 that can be used to create a Telegram \
account that is not tied to a SIM card."
.to_string(),
),
image: Some("https://nft.fragment.com/number/88805397120.webp".to_string()),
content_url: None,
attributes: None,
};

let res = assert_ok!(meta_loader.load(&content).await);
assert_eq!(expected_res, res);
Ok(())
}

#[tokio::test]
async fn test_load_item_metadata_arkenston() -> anyhow::Result<()> {
common::init_logging();
let meta_loader = assert_ok!(NftItemMetaLoader::default());
let content = MetaDataContent::External {
uri: "https://meta.ston.fi/meta/stake/v1/0:E1477E82F29495B7284E16D13D9E1D8316AE166146D36459AF8DF9D2865AD7AB.json".to_string(),
};

let expected_res = NftItemMetaData {
name: Some("ARKENSTON NFT".to_string()),
description: Some("Staked 30.000000000 STON on STON.fi from 04 Sep 2023.".to_string()),
image: Some("https://static.ston.fi/stake-nft/i4.jpg".to_string()),
content_url: None,
attributes: None,
};

let res = assert_ok!(meta_loader.load(&content).await);
assert_eq!(expected_res, res);
Ok(())
}

#[tokio::test]
async fn test_load_item_metadata_with_attributes() -> anyhow::Result<()> {
common::init_logging();
let meta_loader = assert_ok!(NftItemMetaLoader::default());
let content = MetaDataContent::External {
uri: "https://s.getgems.io/nft/c/64284ddbde940b5d6ebc34f8/12/meta.json".to_string(),
};

let attributes = json!([
{"trait_type": "Pikachu","value": "100"},
{"trait_type": "Pokemon","value": "100"},
{"trait_type": "cocktail","value": "100"},
{"trait_type": "rocks","value": "100"}
]);
let expected_res = NftItemMetaData {
name: Some("Pokemon Pikachu #013 💎".to_string()),
description: Some(
"The legendary Pokemon Pikachu from the exclusive collection. Gather everyone!"
.to_string(),
),
image: Some("https://s.getgems.io/nft/c/64284ddbde940b5d6ebc34f8/12/image.png".to_string()),
attributes: Some(attributes),
content_url: None,
};

let res = assert_ok!(meta_loader.load(&content).await);
assert_eq!(expected_res, res);
Ok(())
}

// ---- Nft collection metadata load tests
#[tokio::test]
async fn test_load_collection_metadata_content_arkenston() -> anyhow::Result<()> {
common::init_logging();
let meta_loader = assert_ok!(NftColletionMetaLoader::default());
let content = MetaDataContent::External {
uri: "https://meta.ston.fi/meta/stake-collection/v1/0:AC8495DB6DC9FB72F4A468CD19F0DC88FB6A4D0890B945319907E71117E3DAC7.json".to_string()
};

let social_links = json!(["https://app.ston.fi/staking"]);
let expected_res = NftCollectionMetaData {
image: Some("https://static.ston.fi/stake-nft/i1.jpg".to_string()),
name: Some("ARKENSTON NFT".to_string()),
description: Some("psSTON STON.fi Stake".to_string()),
social_links: Some(social_links),
marketplace: None,
};

let res = assert_ok!(meta_loader.load(&content).await);
assert_eq!(expected_res, res);
Ok(())
}

// Note: HTTP-Response to this URI has an additional key "external_link".
// We don't handle it. It's okay. FYI.
#[tokio::test]
async fn test_load_collection_metadata_content() -> anyhow::Result<()> {
common::init_logging();
let meta_loader = NftColletionMetaLoader::default()?;
let content = MetaDataContent::External {
uri: "https://nft.fragment.com/numbers.json".to_string(),
};

let expected_res = NftCollectionMetaData {
image: Some("https://nft.fragment.com/numbers.svg".to_string()),
name: Some("Anonymous Telegram Numbers".to_string()),
description: Some(
"These anonymous numbers can be used to create Telegram accounts \
that are not tied to SIM cards."
.to_string(),
),
social_links: None,
marketplace: None,
};

let res = assert_ok!(meta_loader.load(&content).await);
assert_eq!(expected_res, res);
Ok(())
}
Loading

0 comments on commit df765d6

Please sign in to comment.