Skip to content

Commit

Permalink
add more scenarios
Browse files Browse the repository at this point in the history
  • Loading branch information
Nagaprasadvr committed Nov 20, 2024
1 parent 6aa175d commit bb2f67e
Show file tree
Hide file tree
Showing 24 changed files with 826 additions and 145 deletions.
2 changes: 1 addition & 1 deletion digital_asset_types/src/dao/full_asset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use crate::dao::{asset, asset_authority, asset_creators, asset_data, asset_group
#[derive(Clone, Debug, PartialEq)]
pub struct FullAsset {
pub asset: asset::Model,
pub data: asset_data::Model,
pub data: Option<asset_data::Model>,
pub authorities: Vec<asset_authority::Model>,
pub creators: Vec<asset_creators::Model>,
pub groups: Vec<asset_grouping::Model>,
Expand Down
6 changes: 3 additions & 3 deletions digital_asset_types/src/dao/scopes/asset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ pub async fn get_related_for_assets(
let id = asset.id.clone();
let fa = FullAsset {
asset,
data: ad.clone(),
data: Some(ad.clone()),
authorities: vec![],
creators: vec![],
groups: vec![],
Expand Down Expand Up @@ -391,9 +391,9 @@ pub async fn get_by_id(
if !include_no_supply {
asset_data = asset_data.filter(Condition::all().add(asset::Column::Supply.gt(0)));
}
let asset_data: (asset::Model, asset_data::Model) =
let asset_data: (asset::Model, Option<asset_data::Model>) =
asset_data.one(conn).await.and_then(|o| match o {
Some((a, Some(d))) => Ok((a, d)),
Some((a, d)) => Ok((a, d)),
_ => Err(DbErr::RecordNotFound("Asset Not Found".to_string())),
})?;

Expand Down
57 changes: 29 additions & 28 deletions digital_asset_types/src/dapi/common/asset.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use crate::dao::sea_orm_active_enums::SpecificationVersions;
use crate::dao::FullAsset;
use crate::dao::PageOptions;
use crate::dao::Pagination;
Expand Down Expand Up @@ -278,14 +277,8 @@ pub fn v1_content_from_json(asset_data: &asset_data::Model) -> Result<Content, D
})
}

pub fn get_content(asset: &asset::Model, data: &asset_data::Model) -> Option<Content> {
match asset.specification_version {
Some(SpecificationVersions::V1) | Some(SpecificationVersions::V0) => {
v1_content_from_json(data).ok()
}
Some(_) => None,
None => None,
}
pub fn get_content(data: &asset_data::Model) -> Option<Content> {
v1_content_from_json(data).ok()
}

pub fn to_authority(authority: Vec<asset_authority::Model>) -> Vec<Authority> {
Expand Down Expand Up @@ -357,14 +350,31 @@ pub fn asset_to_rpc(asset: FullAsset, options: &Options) -> Result<RpcAsset, DbE
let rpc_creators = to_creators(creators);
let rpc_groups = to_grouping(groups, options)?;
let interface = get_interface(&asset)?;
let content = get_content(&asset, &data);
let mut chain_data_selector_fn = jsonpath_lib::selector(&data.chain_data);
let chain_data_selector = &mut chain_data_selector_fn;
let basis_points = safe_select(chain_data_selector, "$.primary_sale_happened")
.and_then(|v| v.as_bool())
.unwrap_or(false);
let edition_nonce =
safe_select(chain_data_selector, "$.edition_nonce").and_then(|v| v.as_u64());

let (content, edition_nonce, basis_points, mutable, uses) = if let Some(data) = &data {
let mut chain_data_selector_fn = jsonpath_lib::selector(&data.chain_data);
let chain_data_selector = &mut chain_data_selector_fn;
(
get_content(data),
safe_select(chain_data_selector, "$.edition_nonce").and_then(|v| v.as_u64()),
safe_select(chain_data_selector, "$.primary_sale_happened")
.and_then(|v| v.as_bool())
.unwrap_or(false),
data.chain_data_mutability.clone().into(),
data.chain_data.get("uses").map(|u| Uses {
use_method: u
.get("use_method")
.and_then(|s| s.as_str())
.unwrap_or("Single")
.to_string()
.into(),
total: u.get("total").and_then(|t| t.as_u64()).unwrap_or(0),
remaining: u.get("remaining").and_then(|t| t.as_u64()).unwrap_or(0),
}),
)
} else {
(None, None, false, false, None)
};

let mpl_core_info = match interface {
Interface::MplCoreAsset | Interface::MplCoreCollection => Some(MplCoreInfo {
Expand All @@ -380,7 +390,7 @@ pub fn asset_to_rpc(asset: FullAsset, options: &Options) -> Result<RpcAsset, DbE
id: bs58::encode(asset.id).into_string(),
content,
authorities: Some(rpc_authorities),
mutable: data.chain_data_mutability.into(),
mutable,
compression: Some(Compression {
eligible: asset.compressible,
compressed: asset.compressed,
Expand Down Expand Up @@ -431,16 +441,7 @@ pub fn asset_to_rpc(asset: FullAsset, options: &Options) -> Result<RpcAsset, DbE
}),
_ => None,
},
uses: data.chain_data.get("uses").map(|u| Uses {
use_method: u
.get("use_method")
.and_then(|s| s.as_str())
.unwrap_or("Single")
.to_string()
.into(),
total: u.get("total").and_then(|t| t.as_u64()).unwrap_or(0),
remaining: u.get("remaining").and_then(|t| t.as_u64()).unwrap_or(0),
}),
uses,
burnt: asset.burnt,
mint_extensions: asset.mint_extensions,
plugins: asset.mpl_core_plugins,
Expand Down
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
use function_name::named;

use das_api::api::{self, ApiContract};

use itertools::Itertools;

use serial_test::serial;

use super::common::*;

#[tokio::test]
#[serial]
#[named]
async fn test_token_extensions_get_asset_scenario_1() {
let name = trim_test_name(function_name!());
let setup = TestSetup::new_with_options(
name.clone(),
TestSetupOptions {
network: Some(Network::Devnet),
},
)
.await;

let seeds: Vec<SeedEvent> = seed_accounts(["BPU5vrAHafRuVeK33CgfdwTKSsmC4p6t3aqyav3cFF7Y"]);

apply_migrations_and_delete_data(setup.db.clone()).await;
index_seed_events(&setup, seeds.iter().collect_vec()).await;

let request = r#"
{
"id": "BPU5vrAHafRuVeK33CgfdwTKSsmC4p6t3aqyav3cFF7Y"
}
"#;

let request: api::GetAsset = serde_json::from_str(request).unwrap();
let response = setup.das_api.get_asset(request).await.unwrap();

insta::assert_json_snapshot!(name, response);
}

#[tokio::test]
#[serial]
#[named]
async fn test_token_extensions_get_asset_scenario_2() {
let name = trim_test_name(function_name!());
let setup = TestSetup::new_with_options(
name.clone(),
TestSetupOptions {
network: Some(Network::Mainnet),
},
)
.await;

let seeds: Vec<SeedEvent> = seed_accounts(["HVbpJAQGNpkgBaYBZQBR1t7yFdvaYVp2vCQQfKKEN4tM"]);

apply_migrations_and_delete_data(setup.db.clone()).await;
index_seed_events(&setup, seeds.iter().collect_vec()).await;

let request = r#"
{
"id": "HVbpJAQGNpkgBaYBZQBR1t7yFdvaYVp2vCQQfKKEN4tM"
}
"#;

let request: api::GetAsset = serde_json::from_str(request).unwrap();
let response = setup.das_api.get_asset(request).await.unwrap();

insta::assert_json_snapshot!(name, response);
}

#[tokio::test]
#[serial]
#[named]
async fn test_token_extensions_get_asset_scenario_3() {
let name = trim_test_name(function_name!());
let setup = TestSetup::new_with_options(
name.clone(),
TestSetupOptions {
network: Some(Network::Mainnet),
},
)
.await;

let seeds: Vec<SeedEvent> = seed_accounts(["2b1kV6DkPAnxd5ixfnxCpjxmKwqjjaYmCZfHsFu24GXo"]);

apply_migrations_and_delete_data(setup.db.clone()).await;
index_seed_events(&setup, seeds.iter().collect_vec()).await;

let request = r#"
{
"id": "2b1kV6DkPAnxd5ixfnxCpjxmKwqjjaYmCZfHsFu24GXo"
}
"#;

let request: api::GetAsset = serde_json::from_str(request).unwrap();
let response = setup.das_api.get_asset(request).await.unwrap();

insta::assert_json_snapshot!(name, response);
}

#[tokio::test]
#[serial]
#[named]
async fn test_fungible_token_get_asset_scenario_1() {
let name = trim_test_name(function_name!());
let setup = TestSetup::new_with_options(
name.clone(),
TestSetupOptions {
network: Some(Network::Mainnet),
},
)
.await;

let seeds: Vec<SeedEvent> = seed_accounts([
"EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v",
"5x38Kp4hvdomTCnCrAny4UtMUt5rQBdB6px2K1Ui45Wq",
]);

apply_migrations_and_delete_data(setup.db.clone()).await;
index_seed_events(&setup, seeds.iter().collect_vec()).await;

let request = r#"
{
"id": "EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v"
}
"#;

let request: api::GetAsset = serde_json::from_str(request).unwrap();
let response = setup.das_api.get_asset(request).await.unwrap();

insta::assert_json_snapshot!(name, response);
}

#[tokio::test]
#[serial]
#[named]
async fn test_fungible_token_get_asset_scenario_2() {
let name = trim_test_name(function_name!());
let setup = TestSetup::new_with_options(
name.clone(),
TestSetupOptions {
network: Some(Network::Devnet),
},
)
.await;

let seeds: Vec<SeedEvent> = seed_accounts(["wKocBVvHQoVaiwWoCs9JYSVye4YZRrv5Cucf7fDqnz1"]);

apply_migrations_and_delete_data(setup.db.clone()).await;
index_seed_events(&setup, seeds.iter().collect_vec()).await;

let request = r#"
{
"id": "wKocBVvHQoVaiwWoCs9JYSVye4YZRrv5Cucf7fDqnz1"
}
"#;

let request: api::GetAsset = serde_json::from_str(request).unwrap();
let response = setup.das_api.get_asset(request).await.unwrap();

insta::assert_json_snapshot!(name, response);
}
2 changes: 1 addition & 1 deletion integration_tests/tests/integration_tests/main.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
mod account_update_tests;
mod cnft_tests;
mod common;
mod fungibles_and_token_extensions_tests;
mod general_scenario_tests;
mod mpl_core_tests;
mod regular_nft_tests;
mod token_extensions_tests;
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
---
source: integration_tests/tests/integration_tests/fungibles_and_token_extensions_tests.rs
expression: response
snapshot_kind: text
---
{
"interface": "Custom",
"id": "EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v",
"content": {
"$schema": "https://schema.metaplex.com/nft1.0.json",
"json_uri": "",
"files": [],
"metadata": {
"name": "USD Coin",
"symbol": "USDC"
},
"links": {}
},
"authorities": [
{
"address": "2wmVCSfPxGPjrnMMn7rchp4uaeoTqN39mXFC2zhPdri9",
"scopes": [
"full"
]
}
],
"compression": {
"eligible": false,
"compressed": false,
"data_hash": "",
"creator_hash": "",
"asset_hash": "",
"tree": "",
"seq": 0,
"leaf_id": 0
},
"grouping": [],
"royalty": {
"royalty_model": "creators",
"target": null,
"percent": 0.0,
"basis_points": 0,
"primary_sale_happened": false,
"locked": false
},
"creators": [],
"ownership": {
"frozen": false,
"delegated": false,
"delegate": null,
"ownership_model": "token",
"owner": ""
},
"mutable": true,
"burnt": false
}
Loading

0 comments on commit bb2f67e

Please sign in to comment.