Skip to content

Commit

Permalink
added tests for unordered set mapper read from address
Browse files Browse the repository at this point in the history
  • Loading branch information
mihaicalinluca committed May 22, 2024
1 parent 828b155 commit 34a3f2a
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,71 @@
"gas": "*",
"refund": "*"
}
},
{
"step": "scCall",
"id": "fill unordered set mapper",
"tx": {
"from": "address:an_account",
"to": "sc:to-be-called",
"function": "fill_unordered_set_mapper",
"arguments": [
"10"
],
"gasLimit": "50,000,000",
"gasPrice": "0"
},
"expect": {
"out": [],
"status": "",
"logs": "*",
"gas": "*",
"refund": "*"
}
},
{
"step": "scCall",
"id": "contains unordered at address",
"tx": {
"from": "address:an_account",
"to": "sc:caller",
"function": "contains_unordered_at_address",
"arguments": ["5"],
"gasLimit": "50,000,000",
"gasPrice": "0"
},
"expect": {
"out": [
"0x01"
],
"status": "",
"logs": "*",
"gas": "*",
"refund": "*"
}
},
{
"step": "scCall",
"id": "get by index at address",
"tx": {
"from": "address:an_account",
"to": "sc:caller",
"function": "get_by_index",
"arguments": [
"5"
],
"gasLimit": "50,000,000",
"gasPrice": "0"
},
"expect": {
"out": [
"5"
],
"status": "",
"logs": "*",
"gas": "*",
"refund": "*"
}
}
]
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,22 @@ pub trait StorageMapperGetAtAddress {
mapper.values().collect()
}

#[endpoint]
fn contains_unordered_at_address(&self, item: u32) -> bool {
let address = self.contract_address().get();
let mapper: UnorderedSetMapper<u32, _> =
UnorderedSetMapper::new_from_address(address, StorageKey::from("unordered_set_mapper"));
mapper.contains(&item)
}

#[endpoint]
fn get_by_index(&self, index: usize) -> u32 {
let address = self.contract_address().get();
let mapper: UnorderedSetMapper<u32, _> =
UnorderedSetMapper::new_from_address(address, StorageKey::from("unordered_set_mapper"));
mapper.get_by_index(index)
}

/// Storage to be called. For testing, this contract is deployed twice,
/// and this module acts both as caller and receiver
#[storage_mapper("set_mapper")]
Expand All @@ -94,6 +110,9 @@ pub trait StorageMapperGetAtAddress {
#[storage_mapper("map_mapper")]
fn map_mapper(&self) -> MapMapper<u32, u32>;

#[storage_mapper("unordered_set_mapper")]
fn unordered_set_mapper(&self) -> UnorderedSetMapper<u32>;

#[endpoint]
fn fill_set_mapper(&self, value: u32) {
for item in 1u32..=value {
Expand All @@ -108,4 +127,11 @@ pub trait StorageMapperGetAtAddress {
self.map_mapper().insert(key, item);
}
}

#[endpoint]
fn fill_unordered_set_mapper(&self, value: u32) {
for item in 1u32..=value {
self.unordered_set_mapper().insert(item);
}
}
}
7 changes: 5 additions & 2 deletions contracts/feature-tests/basic-features/wasm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
////////////////////////////////////////////////////

// Init: 1
// Endpoints: 386
// Endpoints: 389
// Async Callback: 1
// Total number of exported functions: 388
// Total number of exported functions: 391

#![no_std]

Expand Down Expand Up @@ -402,8 +402,11 @@ multiversx_sc_wasm_adapter::endpoints! {
back_at_address => back_at_address
keys_at_address => keys_at_address
values_at_address => values_at_address
contains_unordered_at_address => contains_unordered_at_address
get_by_index => get_by_index
fill_set_mapper => fill_set_mapper
fill_map_mapper => fill_map_mapper
fill_unordered_set_mapper => fill_unordered_set_mapper
)
}

Expand Down

0 comments on commit 34a3f2a

Please sign in to comment.