Skip to content

Commit

Permalink
Merge Christian Gruber's 'test_import_backend' into maint.
Browse files Browse the repository at this point in the history
  • Loading branch information
jralls committed Nov 8, 2020
2 parents 26fa239 + 6394b64 commit e09a26b
Show file tree
Hide file tree
Showing 18 changed files with 622 additions and 347 deletions.
2 changes: 1 addition & 1 deletion gnucash/import-export/test/CMakeLists.txt
Expand Up @@ -68,7 +68,7 @@ set(gtest_import_backend_SOURCES
${CMAKE_SOURCE_DIR}/libgnucash/engine/mocks/gmock-Account.cpp
${CMAKE_SOURCE_DIR}/libgnucash/engine/mocks/gmock-Transaction.cpp
${CMAKE_SOURCE_DIR}/libgnucash/engine/mocks/gmock-Split.cpp
${CMAKE_SOURCE_DIR}/libgnucash/engine/mocks/gmock-qofquery.cpp
${CMAKE_SOURCE_DIR}/libgnucash/engine/mocks/fake-qofquery.cpp
${CMAKE_SOURCE_DIR}/libgnucash/engine/gnc-numeric.cpp
${CMAKE_SOURCE_DIR}/libgnucash/engine/gnc-rational.cpp
${CMAKE_SOURCE_DIR}/libgnucash/engine/gnc-int128.cpp
Expand Down
48 changes: 27 additions & 21 deletions gnucash/import-export/test/gtest-import-backend.cpp
Expand Up @@ -43,23 +43,26 @@ testing::Environment* const env = testing::AddGlobalTestEnvironment(new TestEnvi



/* mock functions, which can not be mocked by mock classes */
/* required fake functions from engine sources, which should not be linked to the test application */

// fake function from qofutil.cpp
gint
safe_strcasecmp (const gchar * da, const gchar * db)
{
// use simplified case-sensitive string comparison as mock up
return g_strcmp0(da, db);
}

// fake function from qoflog.cpp
const char *
qof_log_prettify (const char *name)
{
// do nothing
return name;
}

// this is a slightly modified version of the function from engine-helpers.c
// fake function from engine-helpers.c
// this is a slightly modified version of the original function
const char *
gnc_get_num_action (const Transaction *trans, const Split *split)
{
Expand All @@ -79,6 +82,10 @@ gnc_get_num_action (const Transaction *trans, const Split *split)
else return NULL;
}


/* required fake functions from app-utils sources, which should not be linked to the test application */

// fake function from gnc-ui-util.c
QofBook *
gnc_get_current_book (void)
{
Expand Down Expand Up @@ -114,8 +121,7 @@ class ImportBackendTest : public testing::Test
protected:
void SetUp()
{
m_prefs = MockPrefsBackend::getInstance();
ASSERT_NE(m_prefs, nullptr);
gmock_gnc_prefs_set_backend(&m_prefs);
m_import_acc = new MockAccount();
m_dest_acc = new MockAccount();
m_trans = new MockTransaction();
Expand All @@ -126,7 +132,7 @@ class ImportBackendTest : public testing::Test
using namespace testing;

// define behaviour of m_import_acc
ON_CALL(*m_import_acc, getBook())
ON_CALL(*m_import_acc, get_book())
.WillByDefault(Return(((TestEnvironment*)env)->m_book));
}

Expand All @@ -138,7 +144,7 @@ class ImportBackendTest : public testing::Test
m_split->free();
}

MockPrefsBackend* m_prefs;
MockPrefsBackend m_prefs;
MockAccount* m_import_acc;
MockAccount* m_dest_acc;
MockTransaction* m_trans;
Expand All @@ -161,16 +167,16 @@ TEST_F(ImportBackendTest, CreateTransInfo)
//qof_instance_get (QOF_INSTANCE (split), "online-id", &online_id, NULL);

// Define first split
ON_CALL(*m_trans, getSplit(0))
ON_CALL(*m_trans, get_split(0))
.WillByDefault(Return(m_split));
ON_CALL(*m_trans, getSplitList())
ON_CALL(*m_trans, get_split_list())
.WillByDefault(Return(m_splitList));
// define description of the transaction
ON_CALL(*m_trans, getDescription())
ON_CALL(*m_trans, get_description())
.WillByDefault(Return("This is the description"));

// function gnc_import_TransInfo_new() should try to find account using the description from the transaction
EXPECT_CALL(imap, findAccount(_, StrEq("This is the description")))
EXPECT_CALL(imap, find_account(_, StrEq("This is the description")))
.WillOnce(Return(m_dest_acc));

// call function to be tested
Expand All @@ -181,7 +187,7 @@ TEST_F(ImportBackendTest, CreateTransInfo)
EXPECT_EQ(gnc_import_TransInfo_get_destacc(trans_info), m_dest_acc);

// transaction is not open anymore
ON_CALL(*m_trans, isOpen())
ON_CALL(*m_trans, is_open())
.WillByDefault(Return(false));

// delete transaction info
Expand All @@ -201,7 +207,7 @@ class ImportBackendBayesTest : public ImportBackendTest
using namespace testing;

// set bayesian import matching in preferences
ON_CALL(*m_prefs, getBool(StrEq(GNC_PREFS_GROUP_IMPORT), StrEq(GNC_PREF_USE_BAYES)))
ON_CALL(m_prefs, get_bool(StrEq(GNC_PREFS_GROUP_IMPORT), StrEq(GNC_PREF_USE_BAYES)))
.WillByDefault(Return(true));
}

Expand Down Expand Up @@ -231,12 +237,12 @@ TEST_F(ImportBackendBayesTest, CreateTransInfo)
gnc_tm_free(tm_struct);

// Define first split
ON_CALL(*m_trans, getSplit(0))
ON_CALL(*m_trans, get_split(0))
.WillByDefault(Return(m_split));
ON_CALL(*m_trans, getSplitList())
ON_CALL(*m_trans, get_split_list())
.WillByDefault(Return(m_splitList));
// Transaction has no further splits
ON_CALL(*m_trans, getSplit(Gt(0)))
ON_CALL(*m_trans, get_split(Gt(0)))
.WillByDefault(Return(nullptr));
// Define description and memo of first split
// This transaction is used for testing tokenization of its content.
Expand All @@ -245,17 +251,17 @@ TEST_F(ImportBackendBayesTest, CreateTransInfo)
// * separators at the beginning and end of string
// * duplicated tokens within and between description text end memo
// The token separator is space.
ON_CALL(*m_trans, getDescription())
ON_CALL(*m_trans, get_description())
.WillByDefault(Return(" test tokens within description tokens "));
ON_CALL(*m_split, getMemo())
ON_CALL(*m_split, get_memo())
.WillByDefault(Return(" test the memo test "));
// Define transaction date
ON_CALL(*m_trans, getDate())
ON_CALL(*m_trans, get_date())
.WillByDefault(Return(date));

// check tokens created from transaction
EXPECT_CALL(imap, findAccountBayes(AllOf(
Each(Not(IsEmpty())), // tokens must not be empty strings
EXPECT_CALL(imap, find_account_bayes(AllOf(
Each(Not(StrEq(""))), // tokens must not be empty strings
Each(Not(HasSubstr(" "))), // tokens must not contain separator
Not(HasDuplicates()), // tokens must be unique
Contains(StrEq(local_day_of_week)), // tokens must contain local day of week
Expand All @@ -272,7 +278,7 @@ TEST_F(ImportBackendBayesTest, CreateTransInfo)
EXPECT_EQ(gnc_import_TransInfo_get_destacc(trans_info), m_dest_acc);

// transaction is not open anymore
ON_CALL(*m_trans, isOpen())
ON_CALL(*m_trans, is_open())
.WillByDefault(Return(false));

// delete transaction info
Expand Down
38 changes: 30 additions & 8 deletions libgnucash/app-utils/mocks/gmock-gnc-prefs.cpp
Expand Up @@ -2,46 +2,68 @@

#include "gmock-gnc-prefs.h"

PrefsBackend* prefsbackend = NULL;
static MockPrefsBackend* prefsbackend = nullptr;

void
gmock_gnc_prefs_set_backend(MockPrefsBackend *backend)
{
prefsbackend = backend;
}

extern "C"
{
gboolean
gnc_prefs_get_bool (const gchar *group, const gchar *pref_name)
{
return ((MockPrefsBackend*)prefsbackend)->getBool(group, pref_name);
EXPECT_NE(prefsbackend, nullptr);
return prefsbackend ? prefsbackend->get_bool(group, pref_name) : FALSE;
}

gint
gnc_prefs_get_int (const gchar *group, const gchar *pref_name)
{
return ((MockPrefsBackend*)prefsbackend)->getInt(group, pref_name);
EXPECT_NE(prefsbackend, nullptr);
return prefsbackend ? prefsbackend->get_int(group, pref_name) : 0;
}

gint64
gnc_prefs_get_int64 (const gchar *group, const gchar *pref_name)
{
return ((MockPrefsBackend*)prefsbackend)->getInt64(group, pref_name);
EXPECT_NE(prefsbackend, nullptr);
return prefsbackend ? prefsbackend->get_int64(group, pref_name) : 0;
}

gdouble
gnc_prefs_get_float (const gchar *group, const gchar *pref_name)
{
return ((MockPrefsBackend*)prefsbackend)->getFloat(group, pref_name);
EXPECT_NE(prefsbackend, nullptr);
return prefsbackend ? prefsbackend->get_float(group, pref_name) : 0.0;
}

gchar *
gnc_prefs_get_string (const gchar *group, const gchar *pref_name)
{
return ((MockPrefsBackend*)prefsbackend)->getString(group, pref_name);
EXPECT_NE(prefsbackend, nullptr);
return prefsbackend ? prefsbackend->get_string(group, pref_name) : NULL;
}

gint
gnc_prefs_get_enum (const gchar *group, const gchar *pref_name)
{
return ((MockPrefsBackend*)prefsbackend)->getEnum(group, pref_name);
EXPECT_NE(prefsbackend, nullptr);
return prefsbackend ? prefsbackend->get_enum(group, pref_name) : 0;
}

void
gnc_prefs_get_coords (const gchar *group, const gchar *pref_name, gdouble *x, gdouble *y)
{
((MockPrefsBackend*)prefsbackend)->getCoords(group, pref_name, x, y);
EXPECT_NE(prefsbackend, nullptr);

*x = 0.0;
*y = 0.0;

if (prefsbackend != nullptr)
prefsbackend->get_coords(group, pref_name, x, y);
}

} // extern "C"
46 changes: 15 additions & 31 deletions libgnucash/app-utils/mocks/gmock-gnc-prefs.h
Expand Up @@ -6,42 +6,26 @@
extern "C"
{
#include <gnc-prefs.h>
#include <gnc-prefs-p.h>
}


// mock up for PrefsBackend (singleton class)
class MockPrefsBackend : PrefsBackend
// mock up class implementing preferences backend (see struct PrefBackend in gnc-prefs-p.h)
class MockPrefsBackend
{
public:
MockPrefsBackend(MockPrefsBackend const&) = delete;
MockPrefsBackend& operator=(MockPrefsBackend const&) = delete;

static MockPrefsBackend* getInstance()
{
static MockPrefsBackend prefs; // preferences object

// register preferences object
if (prefsbackend == NULL)
prefsbackend = (PrefsBackend*)&prefs;

// check that preferences object is correctly registered
EXPECT_EQ((MockPrefsBackend*)prefsbackend, &prefs);

return &prefs;
}

MOCK_METHOD2(getBool, gboolean(const gchar *, const gchar *));
MOCK_METHOD2(getInt, gint(const gchar *, const gchar *));
MOCK_METHOD2(getInt64, gint64(const gchar *, const gchar *));
MOCK_METHOD2(getFloat, gdouble(const gchar *, const gchar *));
MOCK_METHOD2(getString, gchar*(const gchar *, const gchar *));
MOCK_METHOD2(getEnum, gint(const gchar *, const gchar *));
MOCK_METHOD4(getCoords, void(const gchar *, const gchar *, gdouble *, gdouble *));

private:
MockPrefsBackend() {}
~MockPrefsBackend() {}
MOCK_METHOD2(get_bool, gboolean(const gchar *, const gchar *));
MOCK_METHOD2(get_int, gint(const gchar *, const gchar *));
MOCK_METHOD2(get_int64, gint64(const gchar *, const gchar *));
MOCK_METHOD2(get_float, gdouble(const gchar *, const gchar *));
MOCK_METHOD2(get_string, gchar*(const gchar *, const gchar *));
MOCK_METHOD2(get_enum, gint(const gchar *, const gchar *));
MOCK_METHOD4(get_coords, void(const gchar *, const gchar *, gdouble *, gdouble *));
};

/** Define a preferences backend.
*
* \attention Each call to this function overwrites a previously set backend.
*/
void gmock_gnc_prefs_set_backend(MockPrefsBackend *backend);

#endif

0 comments on commit e09a26b

Please sign in to comment.