-
Notifications
You must be signed in to change notification settings - Fork 336
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
Allow customising WasmQuerier in MockQuerier #1050
Comments
Yes, I also miss the ability to configure the wasm-querier. I wanted to test a contract on Terra. It queries both prices/swap rates both on-chain and on dexes. For now I use a copy of mock.rs, but clearly would prefer a configurable querier. |
you want a mock querier for wasm and for custom messages, right? |
I was thinking about this, as yesterday I needed to mock a raw query, and ended up copying and adapting a good chunk of code. Didn't saw the associated PR (#1137) yet, so excuse me if I say something that's already been done / addressed. Why not just provide a builder-like pattern for the So, we could do something like
Just an idea. Will take a look at the PR later, and comment back in any case. |
Both Ethan's example and @maurolacy's comment focus on raw queries only. The solution in #1137 is more geneeral and handles raw, smart and contract info queries in one handler. Maybe we should allow the user to set one per query type such that the implementation gets shorter. |
Yes, I think often we will only want to override one. Allowing to do so simply would be nice. Once the manner to plug in support is solid, we could add some helpers to do what Mauro says, but I would say something like. MockQuerier::with_raw_query(raw_querier_from_map(data)); |
Nevertheless, it would be nice that, with a simple setup, you could affect all queries consistently. Not sure the associated PR does that. Will take a look at it for sure. |
If we split the queries in 3 configurations, we can make them configurable in a different way.
|
Not sure about this one... the use case is to simulate some data from a real contract. Which will come from We could hard-code some raw_querier but allow writing to the storage. eg. let mut querier = MockQuerier::new();
querier.write_contract(&contract_addr, |storage: &mut dyn Storage| {
CONFIG.save(storage, &Config{foo: 42}).unwrap();
}); This would set up some data on the RawQuerier handler. Basically chopping out parts of some instantiate functions you should be able to write the mock data here. |
You are right, both keys and values are not just plain strings or byte slices, but structured data. This will require passing something like a Maybe not so complex, but probably not worth the effort for such a limited / specific use case. What about an initial implementation when keys are strings, and values are byte slices? What about leveraging already existing serializations for keys and values? |
This is not the case of most real contract we try to mock
Yup. That is what my I think all of these could be implemented in contracts and when standardised could come here. For now just allowing registering mocks on those 3 entry points separately would be good. And maybe a simple implementation to mock ContractInfo query with a HashMap |
I was thinking about this, and here's another idea: why not providing a Something like That, and then modifying / extending We just write the raw values we need in the mock storage, and then the mock querier works transparently over them. And we complement that by providing some helpers / examples / tests on how to properly build those raw keys (along the lines of Is this feasible? Is it a good idea? |
Hmm... so we do something like const CONFIG: Item<Config> = Item::new("config");
let mut contract_space = deps.storage.namespaced(contract_addr);
CONFIG.save(&mut contract_space, &config).unwrap(); If you make the namespacing easy like that and it provides a |
I was thinking more along the lines of deps.storage.save_raw(namespaced(contract_addr, "config"), &config); The problem here is deps.storage.save_raw(namespaced(contract_addr, "config"), to_binary(config)); or similar. But whatever works / is more or less intuitive / is simple to use. If we make this work seamlessly over |
If it does not work with I would provide to directly use the I assume the goal is to make setup of a mock state for an external contract easy to do. |
I agree using the deps.storage.save_raw(namespaced_map(contract_addr, "map_name", "map_key"), to_binary(map_value));
Unless we can build something like a global map of storage mocks, and honour it across all query functions, this would only work for raw queries, yes. Need to take a more detailed look at the mock smart query impl, but, what prevents doing that global map?
Agreed, seems this will solve just part of the problem. For smart queries, perhaps what @hashedone suggests, of using them only in the context of multi tests, is the way to go? |
I wanted to extend a unit test to provide mocks for some wasm queries and found myself copying much code from MockQuerier as I couldn't customise it. See:
when I just would like to implement
new
andquery_wasm
This is commented twice is cosmwasm_std as a TODO/FIXME:
cosmwasm/packages/std/src/mock.rs
Lines 477 to 479 in ef56646
cosmwasm/packages/std/src/mock.rs
Lines 383 to 389 in ef56646
It would be good to allow someway to customise the wasm handler there to easily mock out contracts.
The text was updated successfully, but these errors were encountered: