|
| 1 | +use cosmwasm_std::{ |
| 2 | + attr, from_json, |
| 3 | + testing::{mock_env, mock_info}, |
| 4 | + Empty, Event, |
| 5 | +}; |
| 6 | +use cw2::{ContractVersion, VersionError}; |
| 7 | +use cw721_base::InstantiateMsg; |
| 8 | +use mars_account_nft::{ |
| 9 | + contract::{execute, migrate, query, Parent}, |
| 10 | + error::ContractError, |
| 11 | + state::NEXT_ID, |
| 12 | +}; |
| 13 | +use mars_testing::mock_dependencies; |
| 14 | + |
| 15 | +#[test] |
| 16 | +fn wrong_contract_name() { |
| 17 | + let mut deps = mock_dependencies(&[]); |
| 18 | + cw2::set_contract_version(deps.as_mut().storage, "contract_xyz", "2.0.0").unwrap(); |
| 19 | + |
| 20 | + let err = migrate(deps.as_mut(), mock_env(), Empty {}).unwrap_err(); |
| 21 | + |
| 22 | + assert_eq!( |
| 23 | + err, |
| 24 | + ContractError::Version(VersionError::WrongContract { |
| 25 | + expected: "crates.io:mars-account-nft".to_string(), |
| 26 | + found: "contract_xyz".to_string() |
| 27 | + }) |
| 28 | + ); |
| 29 | +} |
| 30 | + |
| 31 | +#[test] |
| 32 | +fn wrong_contract_version() { |
| 33 | + let mut deps = mock_dependencies(&[]); |
| 34 | + cw2::set_contract_version(deps.as_mut().storage, "crates.io:mars-account-nft", "4.1.0") |
| 35 | + .unwrap(); |
| 36 | + |
| 37 | + let err = migrate(deps.as_mut(), mock_env(), Empty {}).unwrap_err(); |
| 38 | + |
| 39 | + assert_eq!( |
| 40 | + err, |
| 41 | + ContractError::Version(VersionError::WrongVersion { |
| 42 | + expected: "2.0.0".to_string(), |
| 43 | + found: "4.1.0".to_string() |
| 44 | + }) |
| 45 | + ); |
| 46 | +} |
| 47 | + |
| 48 | +#[test] |
| 49 | +fn successful_migration() { |
| 50 | + let mut deps = mock_dependencies(&[]); |
| 51 | + cw2::set_contract_version(deps.as_mut().storage, "crates.io:mars-account-nft", "2.0.0") |
| 52 | + .unwrap(); |
| 53 | + |
| 54 | + let env = mock_env(); |
| 55 | + // Credit-Manager contract address |
| 56 | + let owner = "osmo1f2m24wktq0sw3c0lexlg7fv4kngwyttvzws3a3r3al9ld2s2pvds87jqvf"; |
| 57 | + let owner_info = mock_info(owner, &[]); |
| 58 | + |
| 59 | + // Init counter with 1 |
| 60 | + NEXT_ID.save(deps.as_mut().storage, &1).unwrap(); |
| 61 | + |
| 62 | + // Instantiate the contract |
| 63 | + Parent::default() |
| 64 | + .instantiate( |
| 65 | + deps.as_mut(), |
| 66 | + env.clone(), |
| 67 | + owner_info.clone(), |
| 68 | + InstantiateMsg { |
| 69 | + name: "mock_nft".to_string(), |
| 70 | + symbol: "MOCK".to_string(), |
| 71 | + minter: owner.to_string(), |
| 72 | + }, |
| 73 | + ) |
| 74 | + .unwrap(); |
| 75 | + |
| 76 | + // Mint a random token |
| 77 | + execute( |
| 78 | + deps.as_mut(), |
| 79 | + env.clone(), |
| 80 | + owner_info.clone(), |
| 81 | + mars_types::account_nft::ExecuteMsg::Mint { |
| 82 | + user: "user_1".to_string(), |
| 83 | + }, |
| 84 | + ) |
| 85 | + .unwrap(); |
| 86 | + |
| 87 | + // Move counter to 3000 |
| 88 | + NEXT_ID.save(deps.as_mut().storage, &3000).unwrap(); |
| 89 | + |
| 90 | + // Mint a random token |
| 91 | + execute( |
| 92 | + deps.as_mut(), |
| 93 | + env.clone(), |
| 94 | + owner_info.clone(), |
| 95 | + mars_types::account_nft::ExecuteMsg::Mint { |
| 96 | + user: "user_2".to_string(), |
| 97 | + }, |
| 98 | + ) |
| 99 | + .unwrap(); |
| 100 | + |
| 101 | + // Check if counter is moved to 3001 |
| 102 | + let next_id = NEXT_ID.load(deps.as_ref().storage).unwrap(); |
| 103 | + assert_eq!(next_id, 3001); |
| 104 | + |
| 105 | + // Query should fail because token_id 2321 is not minted yet |
| 106 | + query( |
| 107 | + deps.as_ref(), |
| 108 | + env.clone(), |
| 109 | + mars_types::account_nft::QueryMsg::OwnerOf { |
| 110 | + token_id: "2321".to_string(), |
| 111 | + include_expired: None, |
| 112 | + }, |
| 113 | + ) |
| 114 | + .unwrap_err(); |
| 115 | + |
| 116 | + let res = migrate(deps.as_mut(), mock_env(), Empty {}).unwrap(); |
| 117 | + |
| 118 | + // Query should now return the owner of token_id 2321, which is Rewards-Collector contract address |
| 119 | + let owner_of_res_binary = query( |
| 120 | + deps.as_ref(), |
| 121 | + env.clone(), |
| 122 | + mars_types::account_nft::QueryMsg::OwnerOf { |
| 123 | + token_id: "2321".to_string(), |
| 124 | + include_expired: None, |
| 125 | + }, |
| 126 | + ) |
| 127 | + .unwrap(); |
| 128 | + let owner_of_res: cw721::OwnerOfResponse = from_json(owner_of_res_binary).unwrap(); |
| 129 | + assert_eq!( |
| 130 | + owner_of_res.owner, |
| 131 | + "osmo1urvqe5mw00ws25yqdd4c4hlh8kdyf567mpcml7cdve9w08z0ydcqvsrgdy".to_string() |
| 132 | + ); |
| 133 | + |
| 134 | + assert_eq!(res.messages, vec![]); |
| 135 | + assert_eq!(res.events, vec![] as Vec<Event>); |
| 136 | + assert!(res.data.is_none()); |
| 137 | + assert_eq!( |
| 138 | + res.attributes, |
| 139 | + vec![attr("action", "migrate"), attr("from_version", "2.0.0"), attr("to_version", "2.1.0")] |
| 140 | + ); |
| 141 | + |
| 142 | + let new_contract_version = ContractVersion { |
| 143 | + contract: "crates.io:mars-account-nft".to_string(), |
| 144 | + version: "2.1.0".to_string(), |
| 145 | + }; |
| 146 | + assert_eq!(cw2::get_contract_version(deps.as_ref().storage).unwrap(), new_contract_version); |
| 147 | +} |
0 commit comments