mirror of
https://github.com/Gnucash/gnucash.git
synced 2025-02-25 18:55:30 -06:00
Replace query factory by a query pool
Query factory was implemented using a public global variable qof_query_factory. Furthermore a query pool is easier to handle than a factory, since the whole management can be hidden from the user.
This commit is contained in:
parent
2639cdefdf
commit
fa82a8bcce
@ -1,48 +1,111 @@
|
||||
#include <config.h>
|
||||
|
||||
#include <list>
|
||||
|
||||
#include "fake-qofquery.h"
|
||||
#include "gmock-qofbook.h"
|
||||
|
||||
|
||||
|
||||
/* class QofFakeQueryPool */
|
||||
|
||||
static class QofFakeQueryPool
|
||||
{
|
||||
public:
|
||||
void addQuery(QofFakeQuery *query)
|
||||
{
|
||||
m_queriesNew.push_back(query);
|
||||
}
|
||||
|
||||
QofFakeQuery* requestQuery()
|
||||
{
|
||||
QofFakeQuery* query = nullptr;
|
||||
|
||||
if (!m_queriesNew.empty())
|
||||
{
|
||||
query = m_queriesNew.front();
|
||||
m_queriesNew.pop_front();
|
||||
m_queriesUsed.push_back(query);
|
||||
}
|
||||
|
||||
EXPECT_NE(query, nullptr);
|
||||
return query;
|
||||
}
|
||||
|
||||
bool queryUsed(QofQuery *query)
|
||||
{
|
||||
auto it = std::find(m_queriesUsed.begin(), m_queriesUsed.end(), (QofFakeQuery*)query);
|
||||
|
||||
return (it != m_queriesUsed.end());
|
||||
}
|
||||
|
||||
void releaseQuery(QofFakeQuery *query)
|
||||
{
|
||||
ASSERT_TRUE(query_used((QofQuery*)query));
|
||||
auto it = std::find(m_queriesUsed.begin(), m_queriesUsed.end(), query);
|
||||
m_queriesUsed.erase(it);
|
||||
m_queriesConsumed.push_back(*it);
|
||||
}
|
||||
|
||||
void removeQuery(QofFakeQuery *query)
|
||||
{
|
||||
ASSERT_FALSE(queryUsed((QofQuery*)query));
|
||||
auto it = std::find(m_queriesConsumed.begin(), m_queriesConsumed.end(), (QofFakeQuery*)query);
|
||||
if (it != m_queriesConsumed.end())
|
||||
m_queriesConsumed.erase(it);
|
||||
else
|
||||
{
|
||||
it = std::find(m_queriesNew.begin(), m_queriesNew.end(), (QofFakeQuery*)query);
|
||||
bool query_found = (it != m_queriesNew.end());
|
||||
ASSERT_TRUE(query_found);
|
||||
m_queriesNew.erase(it);
|
||||
}
|
||||
}
|
||||
|
||||
private:
|
||||
std::list<QofFakeQuery*> m_queriesNew {};
|
||||
std::list<QofFakeQuery*> m_queriesUsed {};
|
||||
std::list<QofFakeQuery*> m_queriesConsumed {};
|
||||
} queryPool;
|
||||
|
||||
|
||||
|
||||
/* class QofFakeQuery */
|
||||
|
||||
QofFakeQuery::QofFakeQuery()
|
||||
{
|
||||
queryPool.addQuery(this);
|
||||
}
|
||||
|
||||
QofFakeQuery::~QofFakeQuery()
|
||||
{
|
||||
queryPool.removeQuery(this);
|
||||
}
|
||||
|
||||
|
||||
|
||||
/* mock functions */
|
||||
|
||||
QofQuery *
|
||||
qof_query_create_for (QofIdTypeConst obj_type)
|
||||
{
|
||||
return (QofQuery*)qof_query_factory.create();
|
||||
/*
|
||||
// \todo create typed query objects
|
||||
QofQuery *ret = NULL;
|
||||
return (QofQuery*)queryPool.requestQuery();
|
||||
}
|
||||
|
||||
if (g_strcmp0(obj_type, GNC_ID_SPLIT) == 0)
|
||||
ret = (QofQuery*)qof_query_factory.createForSplit();
|
||||
// else
|
||||
// FAIL();
|
||||
|
||||
return ret;
|
||||
*/
|
||||
void
|
||||
qof_query_destroy (QofQuery *query)
|
||||
{
|
||||
queryPool.releaseQuery((QofFakeQuery*)query);
|
||||
}
|
||||
|
||||
void
|
||||
qof_query_set_book (QofQuery *query, QofBook *book)
|
||||
{
|
||||
ASSERT_TRUE(queryPool.queryUsed(query));
|
||||
ASSERT_TRUE(QOF_IS_MOCK_BOOK(book));
|
||||
((QofFakeQuery*)query)->setBook(book);
|
||||
}
|
||||
|
||||
GList *
|
||||
qof_query_run (QofQuery *query)
|
||||
{
|
||||
GList *matching_objects = NULL;
|
||||
|
||||
// \todo use typed mock objects
|
||||
auto matchingObjects = ((QofFakeQuery*)query)->run();
|
||||
|
||||
for (auto object : matchingObjects)
|
||||
{
|
||||
matching_objects = g_list_append(matching_objects, static_cast<gpointer>(object));
|
||||
}
|
||||
|
||||
return matching_objects;
|
||||
}
|
||||
|
||||
void
|
||||
xaccQueryAddDateMatchTT (
|
||||
QofQuery *query,
|
||||
@ -52,17 +115,33 @@ xaccQueryAddDateMatchTT (
|
||||
time64 ett,
|
||||
QofQueryOp op)
|
||||
{
|
||||
ASSERT_TRUE(queryPool.queryUsed(query));
|
||||
((QofFakeQuery*)query)->addDateMatchTT(use_start, stt, use_end, ett, op);
|
||||
}
|
||||
|
||||
void
|
||||
xaccQueryAddSingleAccountMatch(QofQuery *query, Account *acc, QofQueryOp op)
|
||||
{
|
||||
ASSERT_TRUE(queryPool.queryUsed(query));
|
||||
((QofFakeQuery*)query)->addSingleAccountMatch(acc, op);
|
||||
}
|
||||
|
||||
void
|
||||
qof_query_destroy (QofQuery *query)
|
||||
GList *
|
||||
qof_query_run (QofQuery *query)
|
||||
{
|
||||
((QofFakeQuery*)query)->destroy();
|
||||
GList *matching_objects = NULL;
|
||||
bool query_used = queryPool.queryUsed(query);
|
||||
|
||||
EXPECT_TRUE(query_used);
|
||||
if (query_used)
|
||||
{
|
||||
auto matchingObjects = ((QofFakeQuery*)query)->run();
|
||||
|
||||
for (auto object : matchingObjects)
|
||||
{
|
||||
matching_objects = g_list_append(matching_objects, static_cast<gpointer>(object));
|
||||
}
|
||||
}
|
||||
|
||||
return matching_objects;
|
||||
}
|
||||
|
@ -15,31 +15,14 @@ extern "C"
|
||||
class QofFakeQuery
|
||||
{
|
||||
public:
|
||||
QofFakeQuery() {};
|
||||
QofFakeQuery();
|
||||
~QofFakeQuery();
|
||||
|
||||
MOCK_METHOD1(setBook, void(QofBook*));
|
||||
MOCK_METHOD0(destroy, void());
|
||||
MOCK_METHOD5(addDateMatchTT, void(gboolean, time64, gboolean, time64, QofQueryOp));
|
||||
MOCK_METHOD2(addSingleAccountMatch, void(Account*, QofQueryOp));
|
||||
MOCK_METHOD0(run, std::vector<void*>());
|
||||
};
|
||||
|
||||
/*
|
||||
// typed mock up for QofQuery
|
||||
template <typename T>
|
||||
class MockQofQueryWithType : MockQofQuery
|
||||
{
|
||||
public:
|
||||
// \todo: write constructor
|
||||
MOCK_METHOD0_T(run, std::list<T*>());
|
||||
};
|
||||
*/
|
||||
|
||||
class QofQueryFactory
|
||||
{
|
||||
public:
|
||||
// MOCK_METHOD0(createForSplit, MockQofQueryWithType<Split>*());
|
||||
MOCK_METHOD0(create, QofFakeQuery*());
|
||||
} qof_query_factory;
|
||||
|
||||
#endif
|
||||
|
Loading…
Reference in New Issue
Block a user