Skip to content

Commit

Permalink
Add merge tests
Browse files Browse the repository at this point in the history
  • Loading branch information
maltekliemann committed Oct 11, 2024
1 parent 9a9e126 commit 6523c39
Show file tree
Hide file tree
Showing 3 changed files with 177 additions and 3 deletions.
51 changes: 49 additions & 2 deletions zrml/combinatorial-tokens/src/tests/integration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,7 @@ fn split_followed_by_merge_vertical_with_parent_in_opposite_order() {
// This test shows that splitting a token horizontally can be accomplished by splitting the parent
// token vertically with a finer partition.
#[test]
fn vertical_split_followed_by_horizontal_split_no_parent() {
fn split_vertical_followed_by_horizontal_split_no_parent() {
ExtBuilder::build().execute_with(|| {
let alice = Account::new(0).deposit(Asset::Ztg, _100).unwrap();

Expand Down Expand Up @@ -353,7 +353,7 @@ fn vertical_split_followed_by_horizontal_split_no_parent() {
// This test shows that splitting a token horizontally can be accomplished by splitting a the parent
// token vertically with a finer partition.
#[test]
fn vertical_split_followed_by_horizontal_split_with_parent() {
fn split_vertical_followed_by_horizontal_split_with_parent() {
ExtBuilder::build().execute_with(|| {
let alice = Account::new(0).deposit(Asset::Ztg, _100).unwrap();
let pallet = Account::new(Pallet::<Runtime>::account_id());
Expand Down Expand Up @@ -448,3 +448,50 @@ fn vertical_split_followed_by_horizontal_split_with_parent() {
assert_eq!(pallet.free_balance(ct_001_1100), 0);
});
}

#[test]
fn split_horizontal_followed_by_merge_horizontal() {
ExtBuilder::build().execute_with(|| {
let alice = Account::new(0).deposit(Asset::Ztg, _100).unwrap();
let pallet = Account::new(Pallet::<Runtime>::account_id());

let market_id = create_market(Asset::Ztg, MarketType::Categorical(3));
let amount = _1;

let ct_001 = CombinatorialToken([
207, 168, 160, 93, 238, 221, 197, 1, 171, 102, 28, 24, 18, 107, 205, 231, 227, 98, 220,
105, 211, 29, 181, 30, 53, 7, 200, 154, 134, 246, 38, 139,
]);
let ct_110 = CombinatorialToken([
101, 210, 61, 196, 5, 247, 150, 41, 186, 49, 11, 63, 139, 53, 25, 65, 161, 83, 24, 142,
225, 102, 57, 241, 199, 18, 226, 137, 68, 3, 219, 131,
]);

assert_ok!(CombinatorialTokens::split_position(
alice.signed(),
None,
market_id,
vec![vec![B0, B0, B1], vec![B1, B1, B0]],
amount,
));

assert_ok!(CombinatorialTokens::split_position(
alice.signed(),
None,
market_id,
vec![vec![B1, B0, B0], vec![B0, B1, B0]],
amount,
));

assert_ok!(CombinatorialTokens::merge_position(
alice.signed(),
None,
market_id,
vec![vec![B1, B0, B0], vec![B0, B1, B0]],
amount,
));

assert_eq!(alice.free_balance(ct_001), _1);
assert_eq!(alice.free_balance(ct_110), _1);
});
}
127 changes: 127 additions & 0 deletions zrml/combinatorial-tokens/src/tests/merge_position.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
use super::*;

#[test]
fn merge_position_fails_if_market_not_found() {
ExtBuilder::build().execute_with(|| {
let alice = Account::new(0).deposit(Asset::Ztg, _100).unwrap();

assert_noop!(
CombinatorialTokens::merge_position(
alice.signed(),
None,
0,
vec![vec![B0, B0, B1], vec![B1, B1, B0]],
1,
),
zrml_market_commons::Error::<Runtime>::MarketDoesNotExist,
);
});
}

#[test]
fn merge_position_fails_on_invalid_partition_length() {
ExtBuilder::build().execute_with(|| {
let alice = Account::new(0).deposit(Asset::Ztg, _100).unwrap();

// Market has three outcomes, but there's an element in the partition of size two.
let market_id = create_market(Asset::Ztg, MarketType::Categorical(3));
let partition = vec![vec![B1, B0, B1], vec![B0, B1]];

assert_noop!(
CombinatorialTokens::merge_position(
alice.signed(),
None,
market_id,
partition,
_1,
),
Error::<Runtime>::InvalidPartition
);
});
}

#[test]
fn merge_position_fails_on_trivial_partition_member() {
ExtBuilder::build().execute_with(|| {
let alice = Account::new(0).deposit(Asset::Ztg, _100).unwrap();

// Market has three outcomes, but there's an element in the partition of size two.
let market_id = create_market(Asset::Ztg, MarketType::Categorical(3));
let partition = vec![vec![B1, B0, B1], vec![B0, B0, B0]];

assert_noop!(
CombinatorialTokens::merge_position(
alice.signed(),
None,
market_id,
partition,
_1,
),
Error::<Runtime>::InvalidPartition
);
});
}

#[test]
fn merge_position_fails_on_overlapping_partition_members() {
ExtBuilder::build().execute_with(|| {
let alice = Account::new(0).deposit(Asset::Ztg, _100).unwrap();

// Market has three outcomes, but there's an element in the partition of size two.
let market_id = create_market(Asset::Ztg, MarketType::Categorical(3));
let partition = vec![vec![B1, B0, B1], vec![B0, B0, B1]];

assert_noop!(
CombinatorialTokens::merge_position(
alice.signed(),
None,
market_id,
partition,
_1,
),
Error::<Runtime>::InvalidPartition
);
});
}

#[test]
fn merge_position_fails_on_insufficient_funds() {
ExtBuilder::build().execute_with(|| {
let alice = Account::new(0).deposit(Asset::Ztg, _99).unwrap();

// Market has three outcomes, but there's an element in the partition of size two.
let market_id = create_market(Asset::Ztg, MarketType::Categorical(3));

assert_noop!(
CombinatorialTokens::merge_position(
alice.signed(),
None,
market_id,
vec![vec![B1, B0, B1], vec![B0, B1, B0]],
_100,
),
orml_tokens::Error::<Runtime>::BalanceTooLow
);
});
}

#[test]
fn merge_position_fails_on_insufficient_funds_foreign_token() {
ExtBuilder::build().execute_with(|| {
let alice = Account::new(0).deposit(Asset::ForeignAsset(1), _99).unwrap();

// Market has three outcomes, but there's an element in the partition of size two.
let market_id = create_market(Asset::Ztg, MarketType::Categorical(3));

assert_noop!(
CombinatorialTokens::merge_position(
alice.signed(),
None,
market_id,
vec![vec![B1, B0, B1], vec![B0, B1, B0]],
_100,
),
orml_tokens::Error::<Runtime>::BalanceTooLow
);
});
}
2 changes: 1 addition & 1 deletion zrml/combinatorial-tokens/src/tests/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#![cfg(all(feature = "mock", test))]

mod integration;
// mod merge_position;
mod merge_position;
mod split_position;

use crate::{
Expand Down

0 comments on commit 6523c39

Please sign in to comment.