Skip to content

Commit

Permalink
Fix commodity in/out to use namespace & mnemonic instead of GUID.
Browse files Browse the repository at this point in the history
  • Loading branch information
jralls committed Jul 11, 2021
1 parent 4b997cd commit 98ca190
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 30 deletions.
16 changes: 14 additions & 2 deletions libgnucash/app-utils/gnc-option.cpp
Expand Up @@ -113,7 +113,7 @@ GncOptionDateValue::get_value() const
m_period == RelativeDatePeriod::END_PREV_YEAR ||
m_period == RelativeDatePeriod::START_PREV_QUARTER ||
m_period == RelativeDatePeriod::END_PREV_QUARTER;

if (period.tm_mon == now.tm_mon && period.tm_mday == now.tm_mday)
{
//No set accounting period, use the calendar year
Expand Down Expand Up @@ -217,13 +217,14 @@ GncOptionDateValue::in_stream(std::istream& iss)
QofInstance*
qof_instance_from_string(const std::string& str, GncOptionUIType type)
{
auto guid{static_cast<GncGUID>(gnc::GUID::from_string(str))};
QofIdType qof_type;
bool commodity_type{false};
switch(type)
{
case GncOptionUIType::CURRENCY:
case GncOptionUIType::COMMODITY:
qof_type = "Commodity";
commodity_type = true;
break;
case GncOptionUIType::BUDGET:
qof_type = "Budget";
Expand Down Expand Up @@ -256,6 +257,17 @@ qof_instance_from_string(const std::string& str, GncOptionUIType type)
break;
}
auto book{gnc_get_current_book()};
if (commodity_type)
{
auto sep{str.find(":")};
auto name_space{str.substr(0, sep)};
auto mnemonic{str.substr(sep + 1, -1)};
auto table = gnc_commodity_table_get_table(book);
return QOF_INSTANCE(gnc_commodity_table_lookup(table,
name_space.c_str(),
mnemonic.c_str()));
}
auto guid{static_cast<GncGUID>(gnc::GUID::from_string(str))};
auto col{qof_book_get_collection(book, qof_type)};
return QOF_INSTANCE(qof_collection_lookup_entity(col, &guid));
}
Expand Down
3 changes: 3 additions & 0 deletions libgnucash/app-utils/gnc-optiondb.i
Expand Up @@ -184,6 +184,9 @@ using Account = struct account_s;
%ignore GncOptionDateValue(GncOptionDateValue&&);
%ignore GncOptionDateValue::operator=(const GncOptionDateValue&);
%ignore GncOptionDateValue::operator=(GncOptionDateValue&&);
%ignore operator<<(std::ostream&, const GncOption&);
%ignore operator>>(std::istream&, GncOption&);

%rename(absolute) RelativeDatePeriod::ABSOLUTE;
%rename(today) RelativeDatePeriod::TODAY;
%rename(start_this_month) RelativeDatePeriod::START_THIS_MONTH;
Expand Down
96 changes: 68 additions & 28 deletions libgnucash/app-utils/test/gtest-gnc-option.cpp
Expand Up @@ -260,50 +260,90 @@ TEST_F(GncOptionCommodityTest, test_currency_validator)
EXPECT_FALSE(option.validate(QOF_INSTANCE(m_aapl)));
}

static inline std::string make_currency_str(gnc_commodity* cur)
{
std::string cur_str{gnc_commodity_get_mnemonic(cur)};
return cur_str;
}

static inline std::string make_commodity_str(gnc_commodity* com)
{
std::string com_str{gnc_commodity_get_namespace(com)};
com_str += " ";
com_str += gnc_commodity_get_mnemonic(com);
return com_str;
}

TEST_F(GncOptionTest, test_qofinstance_out)
static inline std::string make_currency_SCM_str(gnc_commodity* cur)
{
auto table = gnc_commodity_table_new();
qof_book_set_data(m_book, GNC_COMMODITY_TABLE, table);
auto eur = gnc_commodity_new(m_book, "Euro", "ISO4217", "EUR", NULL, 100);
auto option = make_currency_option("foo", "bar", "baz", "Phony Option", eur, true);
std::string cur_str{gnc_commodity_get_mnemonic(cur)};
cur_str.insert(0, "\"");
cur_str += "\"";
return cur_str;
}

static inline std::string make_commodity_SCM_str(gnc_commodity* com)
{
std::string com_str{commodity_scm_intro};
com_str += "\"";
com_str += gnc_commodity_get_namespace(com);
com_str += "\" \"";
com_str += gnc_commodity_get_mnemonic(com);
com_str += "\")";
return com_str;
}

std::string eur_guid{gnc::GUID{*qof_instance_get_guid(eur)}.to_string()};
TEST_F(GncOptionCommodityTest, test_currency_out)
{
auto option = make_currency_option("foo", "bar", "baz", "Phony Option",
m_eur, true);

std::string eur_str{make_currency_str(m_eur)};
std::ostringstream oss;
oss << option;
EXPECT_EQ(eur_guid, oss.str());
gnc_commodity_destroy(eur);
qof_book_set_data(m_book, GNC_COMMODITY_TABLE, nullptr);
gnc_commodity_table_destroy(table);
EXPECT_EQ(eur_str, oss.str());
}

TEST_F(GncOptionTest, test_qofinstance_in)
TEST_F(GncOptionCommodityTest, test_commodity_out)
{
auto table = gnc_commodity_table_new();
qof_book_set_data(m_book, GNC_COMMODITY_TABLE, table);
auto eur = gnc_commodity_new(m_book, "Euro", "ISO4217", "EUR", NULL, 100);
auto usd = gnc_commodity_new(m_book, "United States Dollar",
"CURRENCY", "USD", NULL, 100);
auto hpe = gnc_commodity_new(m_book, "Hewlett Packard Enterprise, Inc.",
"NYSE", "HPE", NULL, 1);
auto option = make_currency_option("foo", "bar", "baz", "Phony Option", eur, true);
GncOption option{"foo", "bar", "baz", "Phony Option", QOF_INSTANCE(m_hpe),
GncOptionUIType::COMMODITY};
std::string hpe_str{make_commodity_str(m_hpe)};
std::ostringstream oss;
oss << option;
EXPECT_EQ(hpe_str, oss.str());
}

TEST_F(GncOptionCommodityTest, test_currency_in)
{

auto option = make_currency_option("foo", "bar", "baz", "Phony Option",
m_eur, true);

EXPECT_THROW({
std::string hpe_guid{gnc::GUID{*qof_instance_get_guid(hpe)}.to_string()};
std::istringstream iss{hpe_guid};
std::string hpe_str{make_commodity_str(m_hpe)};
std::istringstream iss{hpe_str};
iss >> option;
}, std::invalid_argument);
EXPECT_NO_THROW({
std::string usd_guid{gnc::GUID{*qof_instance_get_guid(usd)}.to_string()};
std::istringstream iss{usd_guid};
std::string usd_str{make_currency_str(m_usd)};
std::istringstream iss{usd_str};
iss >> option;
EXPECT_EQ(QOF_INSTANCE(usd), option.get_value<QofInstance*>());
EXPECT_EQ(QOF_INSTANCE(m_usd), option.get_value<QofInstance*>());
});
gnc_commodity_destroy(eur);
gnc_commodity_destroy(usd);
qof_book_set_data(m_book, GNC_COMMODITY_TABLE, nullptr);
gnc_commodity_table_destroy(table);
}

TEST_F(GncOptionCommodityTest, test_commodity_in)
{
GncOption option{"foo", "bar", "baz", "Phony Option", QOF_INSTANCE(m_aapl),
GncOptionUIType::COMMODITY};

std::string hpe_str{make_commodity_str(m_hpe)};
std::istringstream iss{hpe_str};
iss >> option;
EXPECT_EQ(QOF_INSTANCE(m_hpe), option.get_value<QofInstance*>());
}

}

class GncUIItem
Expand Down

0 comments on commit 98ca190

Please sign in to comment.