mirror of
https://github.com/Gnucash/gnucash.git
synced 2025-02-25 18:55:30 -06:00
Modify gnc_imap... functions to use KVP indirectly, provide unit tests.
This commit is contained in:
parent
ccd74059a2
commit
43e93e5fb5
@ -5029,7 +5029,6 @@ xaccAccountForEachTransaction(const Account *acc, TransactionCallback proc,
|
||||
|
||||
typedef struct _GncImportMatchMap
|
||||
{
|
||||
KvpFrame * frame;
|
||||
Account * acc;
|
||||
QofBook * book;
|
||||
} GncImportMatchMap;
|
||||
@ -5050,15 +5049,10 @@ GncImportMatchMap *
|
||||
gnc_account_create_imap (Account *acc)
|
||||
{
|
||||
GncImportMatchMap *imap;
|
||||
KvpFrame *frame;
|
||||
|
||||
if (!acc) return NULL;
|
||||
frame = qof_instance_get_slots (QOF_INSTANCE (acc));
|
||||
g_return_val_if_fail (frame != NULL, NULL);
|
||||
g_return_val_if_fail (frame != NULL, NULL);
|
||||
|
||||
imap = g_new0(GncImportMatchMap, 1);
|
||||
imap->frame = frame;
|
||||
|
||||
/* Cache the book for easy lookups; store the account/book for
|
||||
* marking dirtiness
|
||||
@ -5140,16 +5134,16 @@ struct token_accounts_info
|
||||
* \note Can always assume that keys are unique, reduces code in this function
|
||||
*/
|
||||
static void
|
||||
buildTokenInfo(const char *key, KvpValue *value, gpointer data)
|
||||
buildTokenInfo(const char *key, const GValue *value, gpointer data)
|
||||
{
|
||||
struct token_accounts_info *tokenInfo = (struct token_accounts_info*)data;
|
||||
struct account_token_count* this_account;
|
||||
|
||||
// PINFO("buildTokenInfo: account '%s', token_count: '%ld'\n", (char*)key,
|
||||
// (long)kvp_value_get_gint64(value));
|
||||
// (long)g_value_get_int64(value));
|
||||
|
||||
/* add the count to the total_count */
|
||||
tokenInfo->total_count += kvp_value_get_gint64(value);
|
||||
tokenInfo->total_count += g_value_get_int64(value);
|
||||
|
||||
/* allocate a new structure for this account and it's token count */
|
||||
this_account = (struct account_token_count*)
|
||||
@ -5157,7 +5151,7 @@ buildTokenInfo(const char *key, KvpValue *value, gpointer data)
|
||||
|
||||
/* fill in the account name and number of tokens found for this account name */
|
||||
this_account->account_name = (char*)key;
|
||||
this_account->token_count = kvp_value_get_gint64(value);
|
||||
this_account->token_count = g_value_get_int64(value);
|
||||
|
||||
/* append onto the glist a pointer to the new account_token_count structure */
|
||||
tokenInfo->accounts = g_list_prepend(tokenInfo->accounts, this_account);
|
||||
@ -5262,8 +5256,6 @@ gnc_imap_find_account_bayes (GncImportMatchMap *imap, GList *tokens)
|
||||
GHashTable *final_probabilities = g_hash_table_new(g_str_hash,
|
||||
g_str_equal);
|
||||
struct account_info account_i;
|
||||
KvpValue* value;
|
||||
KvpFrame* token_frame;
|
||||
|
||||
ENTER(" ");
|
||||
|
||||
@ -5281,40 +5273,20 @@ gnc_imap_find_account_bayes (GncImportMatchMap *imap, GList *tokens)
|
||||
for (current_token = tokens; current_token;
|
||||
current_token = current_token->next)
|
||||
{
|
||||
/* zero out the token_accounts_info structure */
|
||||
char* path = g_strdup_printf (IMAP_FRAME_BAYES "/%s",
|
||||
(char*)current_token->data);
|
||||
/* zero out the token_accounts_info structure */
|
||||
memset(&tokenInfo, 0, sizeof(struct token_accounts_info));
|
||||
|
||||
PINFO("token: '%s'", (char*)current_token->data);
|
||||
|
||||
/* find the slot for the given token off of the source account
|
||||
* for these tokens, search off of the IMAP_FRAME_BAYES path so
|
||||
* we aren't looking from the parent of the entire kvp tree
|
||||
*/
|
||||
value = kvp_frame_get_slot_path(imap->frame, IMAP_FRAME_BAYES,
|
||||
(char*)current_token->data, NULL);
|
||||
|
||||
/* if value is null we should skip over this token */
|
||||
if (!value)
|
||||
continue;
|
||||
|
||||
/* convert the slot(value) into a the frame that contains the
|
||||
* list of accounts
|
||||
*/
|
||||
token_frame = kvp_value_get_frame(value);
|
||||
|
||||
/* token_frame should NEVER be null */
|
||||
if (!token_frame)
|
||||
{
|
||||
PERR("token '%s' has no accounts", (char*)current_token->data);
|
||||
continue; /* skip over this token */
|
||||
}
|
||||
|
||||
/* process the accounts for this token, adding the account if it
|
||||
* doesn't already exist or adding to the existing accounts token
|
||||
* count if it does
|
||||
*/
|
||||
kvp_frame_for_each_slot(token_frame, buildTokenInfo, &tokenInfo);
|
||||
|
||||
qof_instance_foreach_slot(QOF_INSTANCE (imap->acc), path,
|
||||
buildTokenInfo, &tokenInfo);
|
||||
g_free (path);
|
||||
/* for each account we have just found, see if the account
|
||||
* already exists in the list of account probabilities, if not
|
||||
* add it
|
||||
@ -5477,7 +5449,8 @@ gnc_imap_add_account_bayes(GncImportMatchMap *imap,
|
||||
token_count += count;
|
||||
}
|
||||
token_count++;
|
||||
g_value_init (&value, G_TYPE_INT64);
|
||||
if (!G_IS_VALUE (&value))
|
||||
g_value_init (&value, G_TYPE_INT64);
|
||||
g_value_set_int64 (&value, token_count);
|
||||
qof_instance_set_kvp (QOF_INSTANCE (imap->acc), kvp_path, &value);
|
||||
g_free (kvp_path);
|
||||
|
@ -128,6 +128,32 @@ libutest_Trans_la_SOURCES = \
|
||||
|
||||
libutest_Trans_la_LIBADD = $(LDADD)
|
||||
|
||||
if WITH_GOOGLE_TEST
|
||||
test_import_map_SOURCES = \
|
||||
gtest-import-map.cpp
|
||||
test_import_map_LDADD = \
|
||||
${top_builddir}/src/libqof/qof/libgnc-qof.la \
|
||||
${top_builddir}/src/engine/libgncmod-engine.la \
|
||||
${GLIB_LIBS} \
|
||||
${GTEST_LIBS}
|
||||
|
||||
if !GOOGLE_TEST_LIBS
|
||||
nodist_test_import_map_SOURCES = \
|
||||
${GTEST_SRC}/src/gtest_main.cc
|
||||
test_import_map_LDADD += ${top_builddir}/src/test-core/libgtest.a
|
||||
endif
|
||||
|
||||
test_import_map_CPPFLAGS = \
|
||||
-I${GTEST_HEADERS} \
|
||||
-I${top_srcdir}/${MODULEPATH} \
|
||||
-I${top_srcdir}/src/libqof/qof \
|
||||
-I${top_srcdir}/src/core-utils \
|
||||
${GLIB_CFLAGS}
|
||||
|
||||
TEST_GROUP_1 += test-import-map
|
||||
endif
|
||||
|
||||
|
||||
|
||||
clean-local:
|
||||
rm -f translog.*
|
||||
|
288
src/engine/test/gtest-import-map.cpp
Normal file
288
src/engine/test/gtest-import-map.cpp
Normal file
@ -0,0 +1,288 @@
|
||||
/********************************************************************
|
||||
* test-import-map.cpp: Test import match maps. *
|
||||
* Copyright 2015 John Ralls <jralls@ceridwen.us> *
|
||||
* *
|
||||
* This program is free software; you can redistribute it and/or *
|
||||
* modify it under the terms of the GNU General Public License as *
|
||||
* published by the Free Software Foundation; either version 2 of *
|
||||
* the License, or (at your option) any later version. *
|
||||
* *
|
||||
* This program is distributed in the hope that it will be useful, *
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
|
||||
* GNU General Public License for more details. *
|
||||
* *
|
||||
* You should have received a copy of the GNU General Public License*
|
||||
* along with this program; if not, contact: *
|
||||
* *
|
||||
* Free Software Foundation Voice: +1-617-542-5942 *
|
||||
* 51 Franklin Street, Fifth Floor Fax: +1-617-542-2652 *
|
||||
* Boston, MA 02110-1301, USA gnu@gnu.org *
|
||||
\********************************************************************/
|
||||
|
||||
extern "C"
|
||||
{
|
||||
#include <config.h>
|
||||
#include "../Account.h"
|
||||
#include <qof.h>
|
||||
#include <qofinstance-p.h>
|
||||
#include <kvp_frame.h>
|
||||
|
||||
struct GncImportMatchMap
|
||||
{
|
||||
Account *acc;
|
||||
QofBook *book;
|
||||
};
|
||||
|
||||
extern GncImportMatchMap * gnc_account_create_imap (Account *acc);
|
||||
extern Account* gnc_imap_find_account(GncImportMatchMap *imap,
|
||||
const char* category,
|
||||
const char *key);
|
||||
extern void gnc_imap_add_account (GncImportMatchMap *imap,
|
||||
const char *category,
|
||||
const char *key, Account *acc);
|
||||
extern Account* gnc_imap_find_account_bayes (GncImportMatchMap *imap,
|
||||
GList* tokens);
|
||||
extern void gnc_imap_add_account_bayes (GncImportMatchMap *imap,
|
||||
GList* tokens,
|
||||
Account *acc);
|
||||
}
|
||||
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
class ImapTest : public testing::Test
|
||||
{
|
||||
protected:
|
||||
void SetUp() {
|
||||
QofBook *book = qof_book_new();
|
||||
Account *root = gnc_account_create_root(book);
|
||||
t_bank_account = xaccMallocAccount(book);
|
||||
t_expense_account1 = xaccMallocAccount(book);
|
||||
xaccAccountSetName(t_expense_account1, "Food");
|
||||
gnc_account_append_child(root, t_expense_account1);
|
||||
t_expense_account2 = xaccMallocAccount(book);
|
||||
xaccAccountSetName(t_expense_account2, "Drink");
|
||||
gnc_account_append_child(root, t_expense_account2);
|
||||
}
|
||||
void TearDown() {
|
||||
qof_book_destroy (gnc_account_get_book (t_bank_account));
|
||||
}
|
||||
Account *t_bank_account {};
|
||||
Account *t_expense_account1 {};
|
||||
Account *t_expense_account2 {};
|
||||
};
|
||||
|
||||
TEST_F(ImapTest, CreateImap) {
|
||||
GncImportMatchMap *imap = gnc_account_create_imap (t_bank_account);
|
||||
EXPECT_NE(nullptr, imap);
|
||||
EXPECT_EQ(t_bank_account, imap->acc);
|
||||
EXPECT_EQ(gnc_account_get_book(t_bank_account), imap->book);
|
||||
|
||||
g_free(imap);
|
||||
}
|
||||
|
||||
static const char* IMAP_FRAME = "import-map";
|
||||
static const char* IMAP_FRAME_BAYES = "import-map-bayes";
|
||||
|
||||
class ImapPlainTest : public ImapTest
|
||||
{
|
||||
protected:
|
||||
void SetUp() {
|
||||
ImapTest::SetUp();
|
||||
t_imap = gnc_account_create_imap (t_bank_account);
|
||||
}
|
||||
|
||||
void TearDown() {
|
||||
g_free(t_imap);
|
||||
ImapTest::TearDown();
|
||||
}
|
||||
|
||||
GncImportMatchMap *t_imap {};
|
||||
};
|
||||
|
||||
TEST_F(ImapPlainTest, FindAccount)
|
||||
{
|
||||
auto root = qof_instance_get_slots(QOF_INSTANCE(t_bank_account));
|
||||
auto acc1_val = kvp_value_new_guid(xaccAccountGetGUID(t_expense_account1));
|
||||
auto acc2_val = kvp_value_new_guid(xaccAccountGetGUID(t_expense_account2));
|
||||
kvp_frame_set_slot_path(root, acc1_val, IMAP_FRAME, "foo", "bar", NULL);
|
||||
kvp_frame_set_slot_path(root, acc2_val, IMAP_FRAME, "baz", "waldo", NULL);
|
||||
kvp_frame_set_slot_path(root, acc1_val, IMAP_FRAME, "pepper", NULL);
|
||||
kvp_frame_set_slot_path(root, acc2_val, IMAP_FRAME, "salt", NULL);
|
||||
kvp_value_delete(acc1_val);
|
||||
kvp_value_delete(acc2_val);
|
||||
|
||||
EXPECT_EQ(t_expense_account1, gnc_imap_find_account(t_imap, "foo", "bar"));
|
||||
EXPECT_EQ(t_expense_account2,
|
||||
gnc_imap_find_account(t_imap, "baz", "waldo"));
|
||||
EXPECT_EQ(t_expense_account1,
|
||||
gnc_imap_find_account(t_imap, NULL, "pepper"));
|
||||
EXPECT_EQ(t_expense_account2, gnc_imap_find_account(t_imap, NULL, "salt"));
|
||||
EXPECT_EQ(nullptr, gnc_imap_find_account(t_imap, "salt", NULL));
|
||||
}
|
||||
|
||||
TEST_F(ImapPlainTest, AddAccount)
|
||||
{
|
||||
// prevent the embedded beginedit/commitedit from doing anything
|
||||
qof_instance_increase_editlevel(QOF_INSTANCE(t_bank_account));
|
||||
qof_instance_mark_clean(QOF_INSTANCE(t_bank_account));
|
||||
gnc_imap_add_account(t_imap, "foo", "bar", t_expense_account1);
|
||||
gnc_imap_add_account(t_imap, "baz", "waldo", t_expense_account2);
|
||||
gnc_imap_add_account(t_imap, NULL, "pepper", t_expense_account1);
|
||||
gnc_imap_add_account(t_imap, NULL, "salt", t_expense_account2);
|
||||
EXPECT_EQ(1, qof_instance_get_editlevel(QOF_INSTANCE(t_bank_account)));
|
||||
EXPECT_TRUE(qof_instance_get_dirty_flag(QOF_INSTANCE(t_bank_account)));
|
||||
qof_instance_mark_clean(QOF_INSTANCE(t_bank_account));
|
||||
gnc_imap_add_account(t_imap, NULL, NULL, t_expense_account2);
|
||||
EXPECT_FALSE(qof_instance_get_dirty_flag(QOF_INSTANCE(t_bank_account)));
|
||||
gnc_imap_add_account(t_imap, "pork", "sausage", NULL);
|
||||
EXPECT_FALSE(qof_instance_get_dirty_flag(QOF_INSTANCE(t_bank_account)));
|
||||
qof_instance_reset_editlevel(QOF_INSTANCE(t_bank_account));
|
||||
|
||||
auto root = qof_instance_get_slots(QOF_INSTANCE(t_bank_account));
|
||||
auto value = kvp_frame_get_slot_path(root, IMAP_FRAME, "foo", "bar", NULL);
|
||||
auto check_account = [this](KvpValue* v) {
|
||||
return xaccAccountLookup(kvp_value_get_guid(v), this->t_imap->book); };
|
||||
EXPECT_EQ(t_expense_account1, check_account(value));
|
||||
value = kvp_frame_get_slot_path(root, IMAP_FRAME, "baz", "waldo", NULL);
|
||||
EXPECT_EQ(t_expense_account2, check_account(value));
|
||||
value = kvp_frame_get_slot_path(root, IMAP_FRAME, "pepper", NULL);
|
||||
EXPECT_EQ(t_expense_account1, check_account(value));
|
||||
value = kvp_frame_get_slot_path(root, IMAP_FRAME, "salt", NULL);
|
||||
EXPECT_EQ(t_expense_account2, check_account(value));
|
||||
value = kvp_frame_get_slot_path(root, IMAP_FRAME, "pork", "sausage", NULL);
|
||||
EXPECT_EQ(nullptr, value);
|
||||
}
|
||||
|
||||
static const char* foo = "foo";
|
||||
static const char* bar = "bar";
|
||||
static const char* baz = "baz";
|
||||
static const char* waldo = "waldo";
|
||||
static const char* pepper = "pepper";
|
||||
static const char* salt = "salt";
|
||||
static const char* pork = "pork";
|
||||
static const char* sausage = "sausage";
|
||||
|
||||
|
||||
|
||||
class ImapBayesTest : public ImapPlainTest
|
||||
{
|
||||
protected:
|
||||
void SetUp() {
|
||||
ImapPlainTest::SetUp();
|
||||
t_list1 = g_list_prepend(t_list1, const_cast<char*>(foo));
|
||||
t_list1 = g_list_prepend(t_list1, const_cast<char*>(bar));
|
||||
t_list2 = g_list_prepend(t_list2, const_cast<char*>(baz));
|
||||
t_list2 = g_list_prepend(t_list2, const_cast<char*>(waldo));
|
||||
t_list3 = g_list_prepend(t_list3, const_cast<char*>(pepper));
|
||||
t_list4 = g_list_prepend(t_list4, const_cast<char*>(salt));
|
||||
t_list5 = g_list_prepend(t_list5, const_cast<char*>(pork));
|
||||
t_list5 = g_list_prepend(t_list5, const_cast<char*>(sausage));
|
||||
}
|
||||
void TearDown() {
|
||||
g_list_free(t_list1);
|
||||
g_list_free(t_list2);
|
||||
g_list_free(t_list3);
|
||||
g_list_free(t_list4);
|
||||
g_list_free(t_list5);
|
||||
t_list1 = nullptr;
|
||||
t_list2 = nullptr;
|
||||
t_list3 = nullptr;
|
||||
t_list4 = nullptr;
|
||||
t_list5 = nullptr;
|
||||
ImapPlainTest::TearDown();
|
||||
}
|
||||
|
||||
GList *t_list1 {};
|
||||
GList *t_list2 {};
|
||||
GList *t_list3 {};
|
||||
GList *t_list4 {};
|
||||
GList *t_list5 {};
|
||||
};
|
||||
|
||||
TEST_F(ImapBayesTest, FindAccountBayes)
|
||||
{
|
||||
auto root = qof_instance_get_slots(QOF_INSTANCE(t_bank_account));
|
||||
auto acct1_name = gnc_account_get_full_name(t_expense_account1);
|
||||
auto acct2_name = gnc_account_get_full_name(t_expense_account2);
|
||||
auto value = kvp_value_new_gint64(42);
|
||||
|
||||
kvp_frame_set_slot_path(root, value, IMAP_FRAME_BAYES,
|
||||
foo, acct1_name, NULL);
|
||||
kvp_frame_set_slot_path(root, value, IMAP_FRAME_BAYES,
|
||||
bar, acct1_name, NULL);
|
||||
kvp_frame_set_slot_path(root, value, IMAP_FRAME_BAYES,
|
||||
baz, acct2_name, NULL);
|
||||
kvp_frame_set_slot_path(root, value, IMAP_FRAME_BAYES,
|
||||
waldo, acct2_name, NULL);
|
||||
kvp_frame_set_slot_path(root, value, IMAP_FRAME_BAYES,
|
||||
pepper, acct1_name, NULL);
|
||||
kvp_frame_set_slot_path(root, value, IMAP_FRAME_BAYES,
|
||||
salt, acct2_name, NULL);
|
||||
kvp_value_delete(value);
|
||||
|
||||
auto account = gnc_imap_find_account_bayes(t_imap, t_list1);
|
||||
EXPECT_EQ(t_expense_account1, account);
|
||||
account = gnc_imap_find_account_bayes(t_imap, t_list2);
|
||||
EXPECT_EQ(t_expense_account2, account);
|
||||
account = gnc_imap_find_account_bayes(t_imap, t_list3);
|
||||
EXPECT_EQ(t_expense_account1, account);
|
||||
account = gnc_imap_find_account_bayes(t_imap, t_list4);
|
||||
EXPECT_EQ(t_expense_account2, account);
|
||||
account = gnc_imap_find_account_bayes(t_imap, t_list5);
|
||||
EXPECT_EQ(nullptr, account);
|
||||
}
|
||||
|
||||
TEST_F(ImapBayesTest, AddAccountBayes)
|
||||
{
|
||||
// prevent the embedded beginedit/commitedit from doing anything
|
||||
qof_instance_increase_editlevel(QOF_INSTANCE(t_bank_account));
|
||||
qof_instance_mark_clean(QOF_INSTANCE(t_bank_account));
|
||||
gnc_imap_add_account_bayes(t_imap, t_list1, t_expense_account1);
|
||||
gnc_imap_add_account_bayes(t_imap, t_list2, t_expense_account2);
|
||||
gnc_imap_add_account_bayes(t_imap, t_list3, t_expense_account1);
|
||||
gnc_imap_add_account_bayes(t_imap, t_list4, t_expense_account2);
|
||||
EXPECT_EQ(1, qof_instance_get_editlevel(QOF_INSTANCE(t_bank_account)));
|
||||
EXPECT_TRUE(qof_instance_get_dirty_flag(QOF_INSTANCE(t_bank_account)));
|
||||
qof_instance_mark_clean(QOF_INSTANCE(t_bank_account));
|
||||
gnc_imap_add_account_bayes(t_imap, t_list5, NULL);
|
||||
EXPECT_FALSE(qof_instance_get_dirty_flag(QOF_INSTANCE(t_bank_account)));
|
||||
qof_instance_reset_editlevel(QOF_INSTANCE(t_bank_account));
|
||||
|
||||
auto root = qof_instance_get_slots(QOF_INSTANCE(t_bank_account));
|
||||
auto acct1_name = gnc_account_get_full_name(t_expense_account1);
|
||||
auto acct2_name = gnc_account_get_full_name(t_expense_account2);
|
||||
auto value = kvp_frame_get_slot_path(root, IMAP_FRAME_BAYES, "foo", "bar", NULL);
|
||||
auto check_account = [this](KvpValue* v) {
|
||||
return (kvp_value_get_string(v),
|
||||
this->t_imap->book); };
|
||||
value = kvp_frame_get_slot_path(root, IMAP_FRAME_BAYES, foo, acct1_name,
|
||||
NULL);
|
||||
EXPECT_EQ(1, kvp_value_get_gint64(value));
|
||||
value = kvp_frame_get_slot_path(root, IMAP_FRAME_BAYES, bar, acct1_name,
|
||||
NULL);
|
||||
EXPECT_EQ(1, kvp_value_get_gint64(value));
|
||||
value = kvp_frame_get_slot_path(root, IMAP_FRAME_BAYES, baz, acct2_name,
|
||||
NULL);
|
||||
EXPECT_EQ(1, kvp_value_get_gint64(value));
|
||||
value = kvp_frame_get_slot_path(root, IMAP_FRAME_BAYES, waldo, acct2_name,
|
||||
NULL);
|
||||
EXPECT_EQ(1, kvp_value_get_gint64(value));
|
||||
value = kvp_frame_get_slot_path(root, IMAP_FRAME_BAYES, pepper, acct1_name,
|
||||
NULL);
|
||||
EXPECT_EQ(1, kvp_value_get_gint64(value));
|
||||
value = kvp_frame_get_slot_path(root, IMAP_FRAME_BAYES, salt, acct2_name,
|
||||
NULL);
|
||||
EXPECT_EQ(1, kvp_value_get_gint64(value));
|
||||
value = kvp_frame_get_slot_path(root, IMAP_FRAME_BAYES, baz, acct1_name,
|
||||
NULL);
|
||||
EXPECT_EQ(0, kvp_value_get_gint64(value));
|
||||
|
||||
qof_instance_increase_editlevel(QOF_INSTANCE(t_bank_account));
|
||||
gnc_imap_add_account_bayes(t_imap, t_list2, t_expense_account2);
|
||||
qof_instance_mark_clean(QOF_INSTANCE(t_bank_account));
|
||||
qof_instance_reset_editlevel(QOF_INSTANCE(t_bank_account));
|
||||
value = kvp_frame_get_slot_path(root, IMAP_FRAME_BAYES, baz, acct2_name,
|
||||
NULL);
|
||||
EXPECT_EQ(2, kvp_value_get_gint64(value));
|
||||
}
|
Loading…
Reference in New Issue
Block a user