Added test for and corrected get_bayes_info

This commit is contained in:
lmat 2017-11-03 21:27:48 -04:00
parent 9d7ec35ce5
commit 2cda65e012
4 changed files with 65 additions and 55 deletions

View File

@ -5432,60 +5432,39 @@ build_non_bayes (const char *key, const GValue *value, gpointer user_data)
g_free (guid_string); g_free (guid_string);
} }
static void static std::tuple<std::string, std::string, std::string>
build_bayes_layer_two (const char *key, KvpValue * val, imap_info imapInfo) parse_bayes_imap_info (std::string const & imap_bayes_entry)
{ {
QofBook *book; auto header_length = strlen (IMAP_FRAME_BAYES);
Account *map_account = NULL; std::string header {imap_bayes_entry.substr (0, header_length)};
GncGUID *guid; auto guid_start = imap_bayes_entry.size() - GUID_ENCODING_LENGTH;
gchar *kvp_path; std::string keyword {imap_bayes_entry.substr (header_length + 1, guid_start - header_length - 2)};
gchar *count; std::string account_guid {imap_bayes_entry.substr (guid_start)};
struct imap_info *imapInfo_node; return {header, keyword, account_guid};
// Get the book
book = qof_instance_get_book (imapInfo.source_account);
if (val->get_type() == KvpValue::Type::INT64)
{
PINFO("build_bayes_layer_two: account '%s', token_count: '%" G_GINT64_FORMAT "'",
key, val->get<int64_t>());
count = g_strdup_printf ("%" G_GINT64_FORMAT, val->get<int64_t>());
}
else
count = g_strdup ("0");
kvp_path = g_strconcat (imapInfo.category_head, "/", key, NULL);
PINFO("build_bayes_layer_two: kvp_path is '%s'", kvp_path);
guid = g_new (GncGUID, 1);
if (string_to_guid (key, guid))
map_account = xaccAccountLookup (guid, book);
g_free (guid);
imapInfo_node = static_cast <imap_info*> (g_malloc(sizeof(*imapInfo_node)));
imapInfo_node->source_account = imapInfo.source_account;
imapInfo_node->map_account = map_account;
imapInfo_node->full_category = g_strdup (kvp_path);
imapInfo_node->match_string = g_strdup (imapInfo.match_string);
imapInfo_node->category_head = g_strdup (imapInfo.category_head);
imapInfo_node->count = g_strdup (count);
imapInfo.list = g_list_append (imapInfo.list, imapInfo_node);
g_free (kvp_path);
g_free (count);
} }
static void static void
build_bayes (const char *key, KvpValue * value, imap_info & imapInfo) build_bayes (const char *key, KvpValue * value, imap_info & imapInfo)
{ {
struct imap_info imapInfol2; auto slots = qof_instance_get_slots_prefix (QOF_INSTANCE (imapInfo.source_account), IMAP_FRAME_BAYES);
PINFO("build_bayes: match string '%s'", (char*)key); if (!slots.size()) return;
for (auto const & entry : slots)
std::string prefix {g_strdup_printf (IMAP_FRAME_BAYES "-%s", key)}; {
PINFO("build_bayes: prefix is '%s', key '%s'", prefix.c_str(), key); auto parsed_key = parse_bayes_imap_info (entry.first);
imapInfol2.source_account = imapInfo.source_account; auto temp_guid = gnc::GUID::from_string (std::get <2> (parsed_key));
imapInfol2.match_string = g_strdup (key); GncGUID guid = temp_guid;
imapInfol2.category_head = g_strdup (prefix.c_str()); auto map_account = xaccAccountLookup (&guid, gnc_account_get_book (imapInfo.source_account));
imapInfol2.list = imapInfo.list; std::string category_head {std::get <0> (parsed_key) + "-" + std::get <1> (parsed_key)};
qof_instance_foreach_slot_prefix (QOF_INSTANCE(imapInfo.source_account), prefix, auto imap_node = static_cast <imap_info*> (g_malloc (sizeof (imap_info)));
build_bayes_layer_two, imapInfol2); auto count = entry.second->get <int64_t> ();
imapInfo.list = imapInfol2.list; imap_node->source_account = imapInfo.source_account;
g_free (imapInfol2.match_string); imap_node->map_account = map_account;
g_free (imapInfol2.category_head); imap_node->full_category = g_strdup (key);
imap_node->match_string = g_strdup (std::get <1> (parsed_key).c_str ());
imap_node->category_head = g_strdup (category_head.c_str ());
imap_node->count = g_strdup_printf ("%" G_GINT64_FORMAT, count);
imapInfo.list = g_list_append (imapInfo.list, imap_node);
};
} }
GList * GList *
@ -5498,7 +5477,6 @@ gnc_account_imap_get_info_bayes (Account *acc)
return imapInfo.list; return imapInfo.list;
} }
GList * GList *
gnc_account_imap_get_info (Account *acc, const char *category) gnc_account_imap_get_info (Account *acc, const char *category)
{ {

View File

@ -173,8 +173,9 @@ bool qof_instance_has_path_slot (QofInstance const *, std::vector<std::string> c
void qof_instance_slot_path_delete (QofInstance const *, std::vector<std::string> const &); void qof_instance_slot_path_delete (QofInstance const *, std::vector<std::string> const &);
void qof_instance_slot_path_delete_if_empty (QofInstance const *, std::vector<std::string> const &); void qof_instance_slot_path_delete_if_empty (QofInstance const *, std::vector<std::string> const &);
/** Returns all keys that match the given prefix and their corresponding values.*/ /** Returns all keys that match the given prefix and their corresponding values.*/
std::map<std::string, KvpValue*> std::vector <std::pair <std::string, KvpValue*>>
qof_instance_get_slots_prefix (QofInstance const *, std::string const & prefix); qof_instance_get_slots_prefix (QofInstance const *, std::string const & prefix);
/* Don't pass nullptr as the function */ /* Don't pass nullptr as the function */

View File

@ -1353,6 +1353,17 @@ qof_instance_slot_delete_if_empty (const QofInstance *inst, const char *path)
} }
} }
std::vector <std::pair <std::string, KvpValue*>>
qof_instance_get_slots_prefix (QofInstance const * inst, std::string const & prefix)
{
std::vector <std::pair <std::string, KvpValue*>> ret;
inst->kvp_data->for_each_slot_temp ([&prefix, &ret] (std::string const & key, KvpValue * val) {
if (key.find (prefix) == 0)
ret.emplace_back (key, val);
});
return ret;
}
namespace { namespace {
struct wrap_param struct wrap_param
{ {
@ -1360,6 +1371,7 @@ struct wrap_param
void *user_data; void *user_data;
}; };
} }
static void static void
wrap_gvalue_function (const char* key, KvpValue *val, wrap_param & param) wrap_gvalue_function (const char* key, KvpValue *val, wrap_param & param)
{ {

View File

@ -381,11 +381,30 @@ TEST_F(ImapBayesTest, ConvertAccountBayes)
TEST_F (ImapBayesTest, import_map_with_delimiters) TEST_F (ImapBayesTest, import_map_with_delimiters)
{ {
GList * tokens {nullptr}; GList * tokens {nullptr};
tokens = g_list_prepend(tokens, const_cast<char*>("one/two/three")); tokens = g_list_prepend (tokens, const_cast<char*> ("one/two/three"));
gnc_account_imap_add_account_bayes(t_imap, tokens, t_expense_account1); gnc_account_imap_add_account_bayes (t_imap, tokens, t_expense_account1);
gnc_account_imap_add_account_bayes(t_imap, tokens, t_expense_account1); gnc_account_imap_add_account_bayes (t_imap, tokens, t_expense_account1);
gnc_account_imap_add_account_bayes(t_imap, tokens, t_expense_account1); gnc_account_imap_add_account_bayes (t_imap, tokens, t_expense_account1);
auto account = gnc_account_imap_find_account_bayes (t_imap, tokens); auto account = gnc_account_imap_find_account_bayes (t_imap, tokens);
EXPECT_EQ (account, t_expense_account1); EXPECT_EQ (account, t_expense_account1);
} }
TEST_F (ImapBayesTest, get_bayes_info)
{
GList * tokens {nullptr};
tokens = g_list_prepend (tokens, const_cast <char*> ("one/two/three"));
gnc_account_imap_add_account_bayes(t_imap, tokens, t_expense_account1);
auto account = gnc_account_imap_find_account_bayes (t_imap, tokens);
EXPECT_EQ (account, t_expense_account1);
auto infos = gnc_account_imap_get_info_bayes (t_bank_account);
EXPECT_EQ (g_list_first (infos), g_list_last (infos));
auto info = static_cast <imap_info*> (g_list_first (infos)->data);
EXPECT_EQ (info->source_account, t_bank_account);
EXPECT_EQ (info->map_account, t_expense_account1);
auto acct1_guid = guid_to_string (xaccAccountGetGUID(t_expense_account1)); //Food
EXPECT_STREQ (info->full_category, (std::string {IMAP_FRAME_BAYES} + "-one-two-three-" + acct1_guid).c_str ());
EXPECT_STREQ (info->match_string, "one-two-three");
EXPECT_STREQ (info->category_head, (std::string {IMAP_FRAME_BAYES} + "-one-two-three").c_str ());
EXPECT_STREQ (info->count, "1");
}