Initial Table Data
Hydra allows loading of smart contract table data which is very useful for defining the initial context the tests will run in. Instead of defining a sequence of actions that results in the table state, the table state is simply defined in JSON files.
hydraload
action to smart contracts#
Adding the Every contract template making use of this feature requires adding the hydraload
action which can be added by including the hydra.hpp
helper file.
This C++ header file is created in tests/hydra.hpp
when running the init command and can be copied to the smart contract source files.
Why is this needed?
The ABI of a contract defines the table structure, but the primary and secondary key derivation functions are WASM-internal. To make sure the loaded data can also be used from within the contract, the corresponding keys for each index defined need to be computed for each table record.
Adding the hydraload
action is done using the HYDRA_FIXTURE_ACTION
macro:
It is recommended to remove the hydraload
action before deploying the smart contract to production.
However, even if this precaution is not performed it shouldn't be a security concern as the hydraload
action requires the authorization of the eosio
system account.
Including or excluding the hydraload
action can be toggled by defining the HYDRA_SKIP_HELPERS
macro.
#
Creating the JSON fixturesThe contract table data to load is defined through JSON files - one JSON file per table. Running the init command creates example boilerplate files in tests/fixtures/<contract>/<table>.json
The structure of a JSON fixture is a JSON object mapping scopes to an array of table records.
The path to the fixtures must be added to the hydra.yml
configuration file.
#
Loading FixturesOnce the contract is compiled with the hydraload action exposed in the ABI, tables can be loaded by using the Account's loadFixtures
method.