Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Stargate mock support #106

Merged
merged 9 commits into from
Dec 4, 2023
Merged

Stargate mock support #106

merged 9 commits into from
Dec 4, 2023

Conversation

DariuszDepta
Copy link
Member

@DariuszDepta DariuszDepta commented Nov 28, 2023

This PR defines Stargate trait, that is a hook for mocking stargate messages and queries in tests. Initial implementation provides two stargate "keepers": StargateFailing and StargateAccepting. StargateFailing always fails while processing stargate messages or queries - this is the default behaviour in App. StargateAccepting silently processes all stargate messages and queries, the returned values are just default AppResult and default Binary respectively.

Implementing custom Stargate keeper allows to customize the way the stargate messages and queries are processed. This way users may adjust this behaviour to their needs while testing contracts with stargate messages or queries.

Detailed usage examples are provided in test cases in MultiTest. General way to customize stargate keeper in tests may look like this:

  1. Implement your stargate keeper:
struct MyStargateKeeper;

impl Stargate for MyStargateKeeper {
    fn execute<ExecC, QueryC>(
        &self,
        api: &dyn Api,
        storage: &mut dyn Storage,
        router: &dyn CosmosRouter<ExecC = ExecC, QueryC = QueryC>,
        block: &BlockInfo,
        sender: Addr,
        type_url: String,
        value: Binary,
    ) -> AnyResult<AppResponse>
    where
        ExecC: Debug + Clone + PartialEq + JsonSchema + DeserializeOwned + 'static,
        QueryC: CustomQuery + DeserializeOwned + 'static,
    {
        // place your custom processing here
    }

    fn query(
        &self,
        api: &dyn Api,
        storage: &dyn Storage,
        querier: &dyn Querier,
        block: &BlockInfo,
        path: String,
        data: Binary,
    ) -> AnyResult<Binary> {
        // place your custom processing here
    }
}
  1. Provide your stargate keeper to App using AppBuilder:
let my_stargate_keeper = MyStargateKeeper;
let app_builder = AppBuilder::default();
let mut app = app_builder.with_stargate(my_stargate_keeper).build(no_init);
  1. Test your contract:
// place your testing logic here, below is shown an example taken from internal tests

let code = app.store_code(stargate::contract());
let owner_addr = app.api().addr_make("owner");
let contract_addr = app
    .instantiate_contract(code, owner_addr.clone(), &Empty {}, &[], "tauri", None)
    .unwrap();
app.execute_contract(owner_addr, contract_addr, &Empty {}, &[]).unwrap();

closes #88
closes #40
closes #37
closes #64

@DariuszDepta DariuszDepta force-pushed the stargate-mock-support branch from b1ce5ac to 7754d89 Compare December 1, 2023 11:32
Copy link
Contributor

@chipshort chipshort left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

src/stargate.rs Show resolved Hide resolved
@DariuszDepta DariuszDepta merged commit c874e0f into main Dec 4, 2023
7 checks passed
@DariuszDepta DariuszDepta deleted the stargate-mock-support branch December 4, 2023 08:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants