From faa9747a0906b6c36e785ade76b000f53c78069c Mon Sep 17 00:00:00 2001 From: Nathaniel Date: Sun, 19 Mar 2023 19:07:48 -0500 Subject: [PATCH] Ref #97/#98: Add unit test Create a unit test for a table with a name primary key. The new test fails to build before the commits in PReq #98, but passes when those commits are included. --- tests/integration/contracts.hpp.in | 3 ++ tests/integration/name_pk_tests.cpp | 32 +++++++++++++++++++ tests/unit/test_contracts/CMakeLists.txt | 1 + tests/unit/test_contracts/name_pk_tests.cpp | 35 +++++++++++++++++++++ 4 files changed, 71 insertions(+) create mode 100644 tests/integration/name_pk_tests.cpp create mode 100644 tests/unit/test_contracts/name_pk_tests.cpp diff --git a/tests/integration/contracts.hpp.in b/tests/integration/contracts.hpp.in index d9c40c74e1..185df34dfd 100644 --- a/tests/integration/contracts.hpp.in +++ b/tests/integration/contracts.hpp.in @@ -27,5 +27,8 @@ namespace eosio::testing { static std::vector get_code_hash_write_test_abi() { return read_abi("${CMAKE_BINARY_DIR}/../unit/test_contracts/get_code_hash_write.abi"); } static std::vector get_code_hash_read_test_wasm() { return read_wasm("${CMAKE_BINARY_DIR}/../unit/test_contracts/get_code_hash_read.wasm"); } static std::vector get_code_hash_read_test_abi() { return read_abi("${CMAKE_BINARY_DIR}/../unit/test_contracts/get_code_hash_read.abi"); } + + static std::vector name_pk_tests_wasm() { return read_wasm("${CMAKE_BINARY_DIR}/../unit/test_contracts/name_pk_tests.wasm"); } + static std::vector name_pk_tests_abi() { return read_abi("${CMAKE_BINARY_DIR}/../unit/test_contracts/name_pk_tests.abi"); } }; } //ns eosio::testing diff --git a/tests/integration/name_pk_tests.cpp b/tests/integration/name_pk_tests.cpp new file mode 100644 index 0000000000..baf6fcfeb3 --- /dev/null +++ b/tests/integration/name_pk_tests.cpp @@ -0,0 +1,32 @@ +#include +#include +#include + +#include + +#include + +#include + +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() diff --git a/tests/unit/test_contracts/CMakeLists.txt b/tests/unit/test_contracts/CMakeLists.txt index 3b1654fe9f..2139b1c7ae 100644 --- a/tests/unit/test_contracts/CMakeLists.txt +++ b/tests/unit/test_contracts/CMakeLists.txt @@ -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) diff --git a/tests/unit/test_contracts/name_pk_tests.cpp b/tests/unit/test_contracts/name_pk_tests.cpp new file mode 100644 index 0000000000..4d476db6f8 --- /dev/null +++ b/tests/unit/test_contracts/name_pk_tests.cpp @@ -0,0 +1,35 @@ +// Verifies that a table with name-typed primary key works + +#include +#include + +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"); + } +};