Skip to content

Commit

Permalink
test: add integration tests for migration (#55)
Browse files Browse the repository at this point in the history
  • Loading branch information
vasyafromrussia committed Apr 24, 2024
1 parent 8af2326 commit b060d6a
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 9 deletions.
7 changes: 4 additions & 3 deletions contract/src/migration/migration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use std::cmp::max;

use claim_model::{
account_record::{AccountRecordLegacy, AccountRecordVersioned},
api::MigrationApi,
Duration, TokensAmount, UnixTimestamp, UnixTimestampExtension,
};
use near_sdk::{
Expand All @@ -28,10 +29,10 @@ pub struct OldState {
}

#[near_bindgen]
impl Contract {
impl MigrationApi for Contract {
#[private]
#[init(ignore_state)]
pub fn migrate() -> Self {
fn migrate() -> Self {
let old_state: OldState = env::state_read().expect("Failed to read old state");

Self {
Expand All @@ -47,7 +48,7 @@ impl Contract {
}
}

pub fn migrate_accounts(&mut self, accounts: Vec<AccountId>) {
fn migrate_accounts(&mut self, accounts: Vec<AccountId>) {
self.assert_oracle();

for account_id in accounts {
Expand Down
1 change: 1 addition & 0 deletions contract/src/migration/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use claim_model::{
UnixTimestamp,
};
use near_sdk::json_types::U128;
use claim_model::api::MigrationApi;

use crate::common::tests::{days_to_seconds, sweat_to_atto, Context};

Expand Down
60 changes: 54 additions & 6 deletions integration-tests/src/migration.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
use anyhow::Result;
use claim_model::api::ClaimApiIntegration;
use claim_model::api::{ClaimApiIntegration, MigrationApiIntegration};
use nitka::misc::ToNear;
use sweat_model::SweatDeferIntegration;

use crate::prepare::{prepare_custom_contract, IntegrationContext};
use crate::{
common::PanicFinder,
prepare::{prepare_custom_contract, IntegrationContext},
};

#[tokio::test]
async fn migration_example() -> Result<()> {
let mut context = prepare_custom_contract(None, None, "sweat_claim_before_migration".into()).await?;
async fn failed_migration() -> Result<()> {
let mut context = prepare_custom_contract(None, None, "sweat_claim_legacy".into()).await?;

let alice = context.alice().await?;

Expand All @@ -21,13 +25,57 @@ async fn migration_example() -> Result<()> {
// Redeploy claim contract
context.redeploy_claim_contract("../res/sweat_claim.wasm").await?;

// Check data after migration
// Check that the contract cannot restore it's state
let result = context
.sweat_claim()
.get_claimable_balance_for_account(alice.to_near())
.result()
.await;

assert!(result.has_panic("Cannot deserialize the contract state."));

Ok(())
}

#[tokio::test]
async fn successful_migration() -> Result<()> {
let mut context = prepare_custom_contract(None, None, "sweat_claim_legacy".into()).await?;

let alice = context.alice().await?;
let oracle = context.manager().await?;

// Add some data before migration
context
.ft_contract()
.defer_batch(
vec![(alice.to_near(), 1000)],
context.sweat_claim().contract.as_account().to_near(),
)
.with_user(&oracle)
.await?;

let balance = context
.sweat_claim()
.get_claimable_balance_for_account(alice.to_near())
.await?;

assert_eq!(balance.0, 0);
assert!(balance.0 > 0);

// Redeploy claim contract
context.redeploy_claim_contract("../res/sweat_claim.wasm").await?;
context
.sweat_claim()
.migrate()
.with_user(context.sweat_claim().contract.as_account())
.await?;

// Check that the contract cannot restore it's state
let new_balance = context
.sweat_claim()
.get_claimable_balance_for_account(alice.to_near())
.await?;

assert_eq!(balance.0, new_balance.0);

Ok(())
}
7 changes: 7 additions & 0 deletions model/src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -241,3 +241,10 @@ pub trait ClaimApi {
/// their claim is available using the `is_claim_available` method prior to calling this.
fn claim(&mut self) -> PromiseOrValue<ClaimResultView>;
}

#[make_integration_version]
pub trait MigrationApi {
fn migrate() -> Self;

fn migrate_accounts(&mut self, accounts: Vec<AccountId>);
}
Binary file modified res/sweat_claim.wasm
Binary file not shown.
Binary file added res/sweat_claim_legacy.wasm
Binary file not shown.

0 comments on commit b060d6a

Please sign in to comment.