Skip to content

Commit

Permalink
fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
notV4l committed May 22, 2024
1 parent 2055102 commit ef2fefe
Showing 1 changed file with 44 additions and 37 deletions.
81 changes: 44 additions & 37 deletions crates/dojo-core/src/base_test.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -8,44 +8,47 @@ use dojo::components::upgradeable::{IUpgradeableDispatcher, IUpgradeableDispatch
use dojo::test_utils::{spawn_test_world};
use dojo::world::{IWorldDispatcher, IWorldDispatcherTrait};

mod v0 {
#[dojo::contract]
mod contract_upgrade {
#[starknet::interface]
trait IQuantumLeap<TState> {
fn plz_more_tps(self: @TState) -> felt252;
}

#[starknet::contract]
mod contract_upgrade {
use dojo::world::{IWorldDispatcher, IWorldDispatcherTrait, IWorldProvider};

#[storage]
struct Storage {}

#[starknet::interface]
trait IQuantumLeap<TState> {
fn plz_more_tps(self: @TState) -> felt252;
#[abi(embed_v0)]
impl QuantumLeap of IQuantumLeap<ContractState> {
fn plz_more_tps(self: @ContractState) -> felt252 {
'no its v0'
}
}
}
}

#[constructor]
fn constructor(ref self: ContractState) {}

#[abi(embed_v0)]
impl QuantumLeap of IQuantumLeap<ContractState> {
fn plz_more_tps(self: @ContractState) -> felt252 {
'daddy'
mod v1 {
#[dojo::contract]
mod contract_upgrade {
#[starknet::interface]
trait IQuantumLeap<TState> {
fn plz_more_tps(self: @TState) -> felt252;
}
}

#[abi(embed_v0)]
impl WorldProviderImpl of IWorldProvider<ContractState> {
fn world(self: @ContractState) -> IWorldDispatcher {
IWorldDispatcher { contract_address: starknet::contract_address_const::<'world'>() }
#[abi(embed_v0)]
impl QuantumLeap of IQuantumLeap<ContractState> {
fn plz_more_tps(self: @ContractState) -> felt252 {
'daddy'
}
}
}
}

#[starknet::contract]
mod contract_invalid_upgrade {
mod contract_not_world_provider {
#[storage]
struct Storage {}
}

use contract_upgrade::{IQuantumLeapDispatcher, IQuantumLeapDispatcherTrait};
use v1::contract_upgrade::{IQuantumLeapDispatcher, IQuantumLeapDispatcherTrait};

// Utils
fn deploy_world() -> IWorldDispatcher {
Expand All @@ -57,12 +60,13 @@ fn deploy_world() -> IWorldDispatcher {
fn test_upgrade_from_world() {
let world = deploy_world();

let base_address = world.deploy_contract('salt', base::TEST_CLASS_HASH.try_into().unwrap());
let new_class_hash: ClassHash = contract_upgrade::TEST_CLASS_HASH.try_into().unwrap();
let v0_address = world
.deploy_contract('salt', v0::contract_upgrade::TEST_CLASS_HASH.try_into().unwrap());
let new_class_hash: ClassHash = v1::contract_upgrade::TEST_CLASS_HASH.try_into().unwrap();

world.upgrade_contract(base_address, new_class_hash);
world.upgrade_contract(v0_address, new_class_hash);

let quantum_dispatcher = IQuantumLeapDispatcher { contract_address: base_address };
let quantum_dispatcher = IQuantumLeapDispatcher { contract_address: v0_address };
assert(quantum_dispatcher.plz_more_tps() == 'daddy', 'quantum leap failed');
}

Expand All @@ -74,10 +78,11 @@ fn test_upgrade_from_world() {
fn test_upgrade_from_world_not_world_provider() {
let world = deploy_world();

let base_address = world.deploy_contract('salt', base::TEST_CLASS_HASH.try_into().unwrap());
let new_class_hash: ClassHash = contract_invalid_upgrade::TEST_CLASS_HASH.try_into().unwrap();
let v0_address = world
.deploy_contract('salt', v0::contract_upgrade::TEST_CLASS_HASH.try_into().unwrap());
let new_class_hash: ClassHash = contract_not_world_provider::TEST_CLASS_HASH.try_into().unwrap();

world.upgrade_contract(base_address, new_class_hash);
world.upgrade_contract(v0_address, new_class_hash);
}

#[test]
Expand All @@ -86,10 +91,11 @@ fn test_upgrade_from_world_not_world_provider() {
fn test_upgrade_direct() {
let world = deploy_world();

let base_address = world.deploy_contract('salt', base::TEST_CLASS_HASH.try_into().unwrap());
let new_class_hash: ClassHash = contract_upgrade::TEST_CLASS_HASH.try_into().unwrap();
let v0_address = world
.deploy_contract('salt', v0::contract_upgrade::TEST_CLASS_HASH.try_into().unwrap());
let new_class_hash: ClassHash = v1::contract_upgrade::TEST_CLASS_HASH.try_into().unwrap();

let upgradeable_dispatcher = IUpgradeableDispatcher { contract_address: base_address };
let upgradeable_dispatcher = IUpgradeableDispatcher { contract_address: v0_address };
upgradeable_dispatcher.upgrade(new_class_hash);
}

Expand All @@ -109,7 +115,7 @@ mod invalid_model {
// Pre-computed address of a contract deployed through the world.
// To print this addres, run:
// sozo test --manifest-path crates/dojo-core/Scarb.toml -f test_deploy_from_world_invalid_model
0x2a9a994dd0f235c8f032fdc6252947e4418e5a3001764049016279b8c0ad19a
0x5c5e3c5bfe74b54f17d81863851b6f87fc9684828e61e6ce2894fb885b51ff1
}
}
}
Expand All @@ -135,10 +141,11 @@ mod invalid_model_world {
fn test_deploy_from_world_invalid_model() {
let world = deploy_world();

let base_address = world.deploy_contract(0, base::TEST_CLASS_HASH.try_into().unwrap());
let v0_address = world
.deploy_contract(0, v0::contract_upgrade::TEST_CLASS_HASH.try_into().unwrap());
// The print is required for invalid_model name to be a valid address as the
// register_model will use the gas consumed as salt.
base_address.print();
v0_address.print();

world.register_model(invalid_model::TEST_CLASS_HASH.try_into().unwrap());
}
Expand Down

0 comments on commit ef2fefe

Please sign in to comment.