Skip to content

Commit

Permalink
Ref AntelopeIO#97/AntelopeIO#98: Add unit test
Browse files Browse the repository at this point in the history
Create a unit test for a table with a name primary key. The new test
fails to build before the commits in PReq AntelopeIO#98, but passes when those
commits are included.
  • Loading branch information
nathanielhourt committed Mar 20, 2023
1 parent 9e8385b commit faa9747
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 0 deletions.
3 changes: 3 additions & 0 deletions tests/integration/contracts.hpp.in
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,8 @@ namespace eosio::testing {
static std::vector<char> get_code_hash_write_test_abi() { return read_abi("${CMAKE_BINARY_DIR}/../unit/test_contracts/get_code_hash_write.abi"); }
static std::vector<uint8_t> get_code_hash_read_test_wasm() { return read_wasm("${CMAKE_BINARY_DIR}/../unit/test_contracts/get_code_hash_read.wasm"); }
static std::vector<char> get_code_hash_read_test_abi() { return read_abi("${CMAKE_BINARY_DIR}/../unit/test_contracts/get_code_hash_read.abi"); }

static std::vector<uint8_t> name_pk_tests_wasm() { return read_wasm("${CMAKE_BINARY_DIR}/../unit/test_contracts/name_pk_tests.wasm"); }
static std::vector<char> name_pk_tests_abi() { return read_abi("${CMAKE_BINARY_DIR}/../unit/test_contracts/name_pk_tests.abi"); }
};
} //ns eosio::testing
32 changes: 32 additions & 0 deletions tests/integration/name_pk_tests.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#include <boost/test/unit_test.hpp>
#include <eosio/testing/tester.hpp>
#include <eosio/chain/abi_serializer.hpp>

#include <Runtime/Runtime.h>

#include <fc/variant_object.hpp>

#include <contracts.hpp>

using namespace eosio;
using namespace eosio::testing;
using namespace eosio::chain;
using namespace fc;

using mvo = fc::mutable_variant_object;

BOOST_AUTO_TEST_SUITE(name_pk_tests_suite)

BOOST_FIXTURE_TEST_CASE( name_pk_tests, tester ) try {
create_accounts( { "test"_n } );
produce_block();

set_code( "test"_n, contracts::name_pk_tests_wasm() );
set_abi( "test"_n, contracts::name_pk_tests_abi().data() );

produce_blocks();
push_action("test"_n, "write"_n, "test"_n, mvo());
push_action("test"_n, "read"_n, "test"_n, mvo());
} FC_LOG_AND_RETHROW()

BOOST_AUTO_TEST_SUITE_END()
1 change: 1 addition & 0 deletions tests/unit/test_contracts/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ add_contract(minimal_tests minimal_tests minimal_tests.cpp)
add_contract(crypto_primitives_tests crypto_primitives_tests crypto_primitives_tests.cpp)
add_contract(get_code_hash_tests get_code_hash_write get_code_hash_write.cpp)
add_contract(get_code_hash_tests get_code_hash_read get_code_hash_read.cpp)
add_contract(name_pk_tests name_pk_tests name_pk_tests.cpp)
add_contract(capi_tests capi_tests capi/capi.c capi/action.c capi/chain.c capi/crypto.c capi/db.c capi/permission.c
capi/print.c capi/privileged.c capi/system.c capi/transaction.c)

Expand Down
35 changes: 35 additions & 0 deletions tests/unit/test_contracts/name_pk_tests.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// Verifies that a table with name-typed primary key works

#include <eosio/multi_index.hpp>
#include <eosio/contract.hpp>

struct [[eosio::table]] name_table {
eosio::name pk;
int num;

auto primary_key() const { return pk; }
};
using name_table_idx = eosio::multi_index<"name.pk"_n, name_table>;

class [[eosio::contract]] name_pk_tests : public eosio::contract {
public:
using eosio::contract::contract;

[[eosio::action]] void write() {
name_table_idx table(get_self(), 0);
table.emplace(get_self(), [](auto& row) {
row.pk = "alice"_n;
row.num = 2;
});
table.emplace(get_self(), [](auto& row) {
row.pk = "bob"_n;
row.num = 1;
});
}

[[eosio::action]] void read() {
name_table_idx table(get_self(), 0);
eosio::check(table.get("alice"_n).num == 2, "num mismatch");
eosio::check(table.get("bob"_n).num == 1, "num mismatch");
}
};

0 comments on commit faa9747

Please sign in to comment.