Skip to content

Commit f4eae2d

Browse files
committed
test: Create test fixture for wallet
Removes all the `#ifdef ENABLE_WALLET` from `test_bitcoin` by making the wallet tests use their own fixture.
1 parent de39c95 commit f4eae2d

File tree

8 files changed

+55
-31
lines changed

8 files changed

+55
-31
lines changed

src/Makefile.test.include

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,8 @@ BITCOIN_TESTS =\
9090

9191
if ENABLE_WALLET
9292
BITCOIN_TESTS += \
93+
wallet/test/wallet_test_fixture.cpp \
94+
wallet/test/wallet_test_fixture.h \
9395
wallet/test/accounting_tests.cpp \
9496
wallet/test/wallet_tests.cpp \
9597
wallet/test/rpc_wallet_tests.cpp

src/test/test_bitcoin.cpp

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,6 @@
1919
#include "ui_interface.h"
2020
#include "rpc/server.h"
2121
#include "rpc/register.h"
22-
#ifdef ENABLE_WALLET
23-
#include "wallet/db.h"
24-
#include "wallet/wallet.h"
25-
#endif
2622

2723
#include "test/testutil.h"
2824

@@ -57,10 +53,6 @@ TestingSetup::TestingSetup(const std::string& chainName) : BasicTestingSetup(cha
5753
// Ideally we'd move all the RPC tests to the functional testing framework
5854
// instead of unit tests, but for now we need these here.
5955
RegisterAllCoreRPCCommands(tableRPC);
60-
#ifdef ENABLE_WALLET
61-
bitdb.MakeMock();
62-
RegisterWalletRPCCommands(tableRPC);
63-
#endif
6456
ClearDatadirCache();
6557
pathTemp = GetTempPath() / strprintf("test_bitcoin_%lu_%i", (unsigned long)GetTime(), (int)(GetRand(100000)));
6658
boost::filesystem::create_directories(pathTemp);
@@ -69,12 +61,6 @@ TestingSetup::TestingSetup(const std::string& chainName) : BasicTestingSetup(cha
6961
pcoinsdbview = new CCoinsViewDB(1 << 23, true);
7062
pcoinsTip = new CCoinsViewCache(pcoinsdbview);
7163
InitBlockIndex(chainparams);
72-
#ifdef ENABLE_WALLET
73-
bool fFirstRun;
74-
pwalletMain = new CWallet("wallet.dat");
75-
pwalletMain->LoadWallet(fFirstRun);
76-
RegisterValidationInterface(pwalletMain);
77-
#endif
7864
nScriptCheckThreads = 3;
7965
for (int i=0; i < nScriptCheckThreads-1; i++)
8066
threadGroup.create_thread(&ThreadScriptCheck);
@@ -86,19 +72,10 @@ TestingSetup::~TestingSetup()
8672
UnregisterNodeSignals(GetNodeSignals());
8773
threadGroup.interrupt_all();
8874
threadGroup.join_all();
89-
#ifdef ENABLE_WALLET
90-
UnregisterValidationInterface(pwalletMain);
91-
delete pwalletMain;
92-
pwalletMain = NULL;
93-
#endif
9475
UnloadBlockIndex();
9576
delete pcoinsTip;
9677
delete pcoinsdbview;
9778
delete pblocktree;
98-
#ifdef ENABLE_WALLET
99-
bitdb.Flush(true);
100-
bitdb.Reset();
101-
#endif
10279
boost::filesystem::remove_all(pathTemp);
10380
}
10481

src/test/test_bitcoin.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,7 @@ struct BasicTestingSetup {
2525
};
2626

2727
/** Testing setup that configures a complete environment.
28-
* Included are data directory, coins database, script check threads
29-
* and wallet (if enabled) setup.
28+
* Included are data directory, coins database, script check threads setup.
3029
*/
3130
struct TestingSetup: public BasicTestingSetup {
3231
CCoinsViewDB *pcoinsdbview;

src/wallet/test/accounting_tests.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
#include "wallet/wallet.h"
66
#include "wallet/walletdb.h"
77

8-
#include "test/test_bitcoin.h"
8+
#include "wallet/test/wallet_test_fixture.h"
99

1010
#include <stdint.h>
1111

@@ -14,7 +14,7 @@
1414

1515
extern CWallet* pwalletMain;
1616

17-
BOOST_FIXTURE_TEST_SUITE(accounting_tests, TestingSetup)
17+
BOOST_FIXTURE_TEST_SUITE(accounting_tests, WalletTestingSetup)
1818

1919
static void
2020
GetResults(CWalletDB& walletdb, std::map<CAmount, CAccountingEntry>& results)

src/wallet/test/rpc_wallet_tests.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
#include "main.h"
1010
#include "wallet/wallet.h"
1111

12-
#include "test/test_bitcoin.h"
12+
#include "wallet/test/wallet_test_fixture.h"
1313

1414
#include <boost/algorithm/string.hpp>
1515
#include <boost/test/unit_test.hpp>
@@ -23,7 +23,7 @@ extern UniValue CallRPC(string args);
2323

2424
extern CWallet* pwalletMain;
2525

26-
BOOST_FIXTURE_TEST_SUITE(rpc_wallet_tests, TestingSetup)
26+
BOOST_FIXTURE_TEST_SUITE(rpc_wallet_tests, WalletTestingSetup)
2727

2828
BOOST_AUTO_TEST_CASE(rpc_addmultisig)
2929
{
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#include "wallet/test/wallet_test_fixture.h"
2+
3+
#include "rpc/server.h"
4+
#include "wallet/db.h"
5+
#include "wallet/wallet.h"
6+
7+
WalletTestingSetup::WalletTestingSetup(const std::string& chainName):
8+
TestingSetup(chainName)
9+
{
10+
bitdb.MakeMock();
11+
12+
bool fFirstRun;
13+
pwalletMain = new CWallet("wallet.dat");
14+
pwalletMain->LoadWallet(fFirstRun);
15+
RegisterValidationInterface(pwalletMain);
16+
17+
RegisterWalletRPCCommands(tableRPC);
18+
}
19+
20+
WalletTestingSetup::~WalletTestingSetup()
21+
{
22+
UnregisterValidationInterface(pwalletMain);
23+
delete pwalletMain;
24+
pwalletMain = NULL;
25+
26+
bitdb.Flush(true);
27+
bitdb.Reset();
28+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// Copyright (c) 2016 The Bitcoin Core developers
2+
// Distributed under the MIT software license, see the accompanying
3+
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
4+
5+
#ifndef BITCOIN_WALLET_TEST_FIXTURE_H
6+
#define BITCOIN_WALLET_TEST_FIXTURE_H
7+
8+
#include "test/test_bitcoin.h"
9+
10+
/** Testing setup and teardown for wallet.
11+
*/
12+
struct WalletTestingSetup: public TestingSetup {
13+
WalletTestingSetup(const std::string& chainName = CBaseChainParams::MAIN);
14+
~WalletTestingSetup();
15+
};
16+
17+
#endif
18+

src/wallet/test/wallet_tests.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
#include <utility>
1010
#include <vector>
1111

12-
#include "test/test_bitcoin.h"
12+
#include "wallet/test/wallet_test_fixture.h"
1313

1414
#include <boost/foreach.hpp>
1515
#include <boost/test/unit_test.hpp>
@@ -25,7 +25,7 @@ using namespace std;
2525

2626
typedef set<pair<const CWalletTx*,unsigned int> > CoinSet;
2727

28-
BOOST_FIXTURE_TEST_SUITE(wallet_tests, TestingSetup)
28+
BOOST_FIXTURE_TEST_SUITE(wallet_tests, WalletTestingSetup)
2929

3030
static CWallet wallet;
3131
static vector<COutput> vCoins;

0 commit comments

Comments
 (0)