1 /********************************************************************
2  * gtest-import-account-matcher.cpp --                              *
3  *                        unit tests import-account-matcher.        *
4  * Copyright (C) 2020 John Ralls <jralls@ceridwen.us>               *
5  *                                                                  *
6  * This program is free software; you can redistribute it and/or    *
7  * modify it under the terms of the GNU General Public License as   *
8  * published by the Free Software Foundation; either version 2 of   *
9  * the License, or (at your option) any later version.              *
10  *                                                                  *
11  * This program is distributed in the hope that it will be useful,  *
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of   *
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the    *
14  * GNU General Public License for more details.                     *
15  *                                                                  *
16  * You should have received a copy of the GNU General Public License*
17  * along with this program; if not, contact:                        *
18  *                                                                  *
19  * Free Software Foundation           Voice:  +1-617-542-5942       *
20  * 51 Franklin Street, Fifth Floor    Fax:    +1-617-542-2652       *
21  * Boston, MA  02110-1301,  USA       gnu@gnu.org                   *
22  *                                                                  *
23  *******************************************************************/
24 
25 #include <gtest/gtest.h>
26 extern "C"
27 {
28 #include <config.h>
29 #include <import-account-matcher.h>
30 #include <gnc-session.h>
31 #include <qofbook.h>
32 #include <Account.h>
33 #include <gtk/gtk.h>
34 }
35 #include <vector>
36 
37 using AccountV = std::vector<const Account*>;
38 using AccountTypeV = std::vector<GNCAccountType>;
39 using AccountPair = std::pair<AccountV&,
40                               const AccountTypeV&>;
41 
42 class ImportMatcherTest : public ::testing::Test
43 {
44 protected:
ImportMatcherTest()45     ImportMatcherTest() :
46         m_book{gnc_get_current_book()}, m_root{gnc_account_create_root(m_book)}
47     {
48         auto create_account = [this](Account* parent, GNCAccountType type,
49                                      const char* name,
__anon839cc9500102(Account* parent, GNCAccountType type, const char* name, const char* online)50                                      const char* online)->Account* {
51             auto account = xaccMallocAccount(this->m_book);
52             xaccAccountBeginEdit(account);
53             xaccAccountSetType(account, type);
54             xaccAccountSetName(account, name);
55             xaccAccountBeginEdit(parent);
56             gnc_account_append_child(parent, account);
57             if (online)
58                 qof_instance_set(QOF_INSTANCE(account), "online-id", online, NULL);
59             xaccAccountCommitEdit(parent);
60             xaccAccountCommitEdit(account);
61             return account;
62         };
63         auto assets = create_account(m_root, ACCT_TYPE_ASSET,
64                                      "Assets", nullptr);
65         auto liabilities = create_account(m_root, ACCT_TYPE_LIABILITY,
66                                           "Liabilities", nullptr);
67         auto expenses = create_account(m_root, ACCT_TYPE_EXPENSE,
68                                        "Expenses", nullptr);
69         create_account(assets, ACCT_TYPE_BANK, "Bank", "Bank");
70         auto broker = create_account(assets, ACCT_TYPE_ASSET,
71                                      "Broker", "Broker");
72         auto stocks = create_account(broker, ACCT_TYPE_STOCK,
73                                      "Stocks", "BrokerStocks");
74         create_account(stocks, ACCT_TYPE_STOCK, "AAPL", "BrokerStocksAAPL");
75         create_account(stocks, ACCT_TYPE_STOCK, "MSFT", "BrokerStocksMSFT ");
76         create_account(stocks, ACCT_TYPE_STOCK, "HPE", "BrokerStocksHPE");
77         create_account(broker, ACCT_TYPE_BANK, "Cash Management",
78                        "BrokerCash Management");
79        create_account(expenses, ACCT_TYPE_EXPENSE, "Food", nullptr);
80         create_account(expenses, ACCT_TYPE_EXPENSE, "Gas", nullptr);
81         create_account(expenses, ACCT_TYPE_EXPENSE, "Rent", nullptr);
82    }
~ImportMatcherTest()83     ~ImportMatcherTest()
84     {
85         xaccAccountBeginEdit(m_root);
86         xaccAccountDestroy(m_root); //It does the commit
87         gnc_clear_current_session();
88     }
89 
90     QofBook* m_book;
91     Account* m_root;
92 };
93 
TEST_F(ImportMatcherTest,test_simple_match)94 TEST_F(ImportMatcherTest, test_simple_match)
95 {
96     auto found = gnc_import_select_account(nullptr, "Bank", FALSE, nullptr,
97                                            nullptr, ACCT_TYPE_NONE, nullptr,
98                                            nullptr);
99     ASSERT_NE(nullptr, found);
100     EXPECT_STREQ("Bank", xaccAccountGetName(found));
101 }
102 
TEST_F(ImportMatcherTest,test_noisy_match)103 TEST_F(ImportMatcherTest, test_noisy_match)
104 {
105     auto found = gnc_import_select_account(nullptr, "BankUSD", FALSE, nullptr,
106                                            nullptr, ACCT_TYPE_NONE, nullptr,
107                                            nullptr);
108     ASSERT_NE(nullptr, found);
109     EXPECT_STREQ("Bank", xaccAccountGetName(found));
110 }
111 
TEST_F(ImportMatcherTest,test_match_with_subaccounts)112 TEST_F(ImportMatcherTest, test_match_with_subaccounts)
113 {
114     auto found = gnc_import_select_account(nullptr, "BrokerStocks", FALSE,
115                                            nullptr, nullptr, ACCT_TYPE_NONE,
116                                            nullptr, nullptr);
117     ASSERT_NE(nullptr, found);
118     EXPECT_STREQ("Stocks", xaccAccountGetName(found));
119 }
120 
TEST_F(ImportMatcherTest,test_subaccount_match)121 TEST_F(ImportMatcherTest, test_subaccount_match)
122 {
123     auto found = gnc_import_select_account(nullptr, "BrokerStocksHPE", FALSE,
124                                            nullptr, nullptr, ACCT_TYPE_NONE,
125                                            nullptr, nullptr);
126     ASSERT_NE(nullptr, found);
127     EXPECT_STREQ("HPE", xaccAccountGetName(found));
128 }
129 
TEST_F(ImportMatcherTest,test_subaccount_match_trailing_noise)130 TEST_F(ImportMatcherTest, test_subaccount_match_trailing_noise)
131 {
132     auto found = gnc_import_select_account(nullptr, "BrokerStocksHPEUSD", FALSE,
133                                            nullptr, nullptr, ACCT_TYPE_NONE,
134                                            nullptr, nullptr);
135     ASSERT_NE(nullptr, found);
136     EXPECT_STREQ("HPE", xaccAccountGetName(found));
137 }
138 
TEST_F(ImportMatcherTest,test_subaccount_no_match)139 TEST_F(ImportMatcherTest, test_subaccount_no_match)
140 {
141     auto found = gnc_import_select_account(nullptr, "BrokerStocksINTC", FALSE,
142                                            nullptr, nullptr, ACCT_TYPE_STOCK,
143                                            nullptr, nullptr);
144     ASSERT_EQ(nullptr, found);
145 }
146 
TEST_F(ImportMatcherTest,test_subaccount_match_trailing_space)147 TEST_F(ImportMatcherTest, test_subaccount_match_trailing_space)
148 {
149     auto found = gnc_import_select_account(nullptr, "BrokerStocksMSFT ", FALSE,
150                                            nullptr, nullptr, ACCT_TYPE_NONE,
151                                            nullptr, nullptr);
152     ASSERT_NE(nullptr, found);
153     EXPECT_STREQ("MSFT", xaccAccountGetName(found));
154 }
155 
TEST_F(ImportMatcherTest,test_subaccount_match_trim_trailing_space)156 TEST_F(ImportMatcherTest, test_subaccount_match_trim_trailing_space)
157 {
158     auto found = gnc_import_select_account(nullptr, "BrokerStocksMSFT", FALSE,
159                                            nullptr, nullptr, ACCT_TYPE_NONE,
160                                            nullptr, nullptr);
161     ASSERT_NE(nullptr, found);
162     EXPECT_STREQ("MSFT", xaccAccountGetName(found));
163 }
164 
TEST_F(ImportMatcherTest,test_subaccount_match_internal_space)165 TEST_F(ImportMatcherTest, test_subaccount_match_internal_space)
166 {
167     auto found = gnc_import_select_account(nullptr, "BrokerCash Management",
168                                            FALSE, nullptr, nullptr,
169                                            ACCT_TYPE_NONE, nullptr, nullptr);
170     ASSERT_NE(nullptr, found);
171     EXPECT_STREQ("Cash Management", xaccAccountGetName(found));
172 }
173