/
fake-qofquery.cpp
165 lines (131 loc) · 3.86 KB
/
fake-qofquery.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/**
* @file fake-qofquery.cpp
*/
#include <config.h>
#include <qofbook.h>
#include <list>
#include "fake-qofquery.h"
/* class QofFakeQueryPool */
static class QofFakeQueryPool
{
public:
/* Add QofFakeQuery object to the pool */
void add_query(QofFakeQuery *query)
{
m_queriesNew.push_back(query);
}
/* Request to use a QofFakeQuery object */
QofFakeQuery* request_query(QofIdTypeConst obj_type)
{
QofFakeQuery* query = nullptr;
auto it = std::find_if(m_queriesNew.begin(), m_queriesNew.end(),
[obj_type](QofFakeQuery const* query) {
return (g_strcmp0(query->m_obj_type, obj_type) == 0);
});
if (it != m_queriesNew.end())
{
query = *it;
m_queriesNew.erase(it);
m_queriesUsed.push_back(query);
}
EXPECT_NE(query, nullptr);
return query;
}
/* Check if a QofFakeQuery object is currently used, i.e. it has been
* requested before */
bool query_used(QofQuery *query)
{
auto it = std::find(m_queriesUsed.begin(), m_queriesUsed.end(), (QofFakeQuery*)query);
return (it != m_queriesUsed.end());
}
/* Release a formerly requested QofFakeQuery object, which is not used
* anymore */
void release_query(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);
}
/* Remove a formerly added QofFakeQueryObject from the pool */
void remove_query(QofFakeQuery *query)
{
ASSERT_FALSE(query_used((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(QofIdTypeConst obj_type) :
m_obj_type(obj_type)
{
queryPool.add_query(this);
}
QofFakeQuery::~QofFakeQuery()
{
queryPool.remove_query(this);
}
/* mock functions */
QofQuery *
qof_query_create_for (QofIdTypeConst obj_type)
{
return (QofQuery*)queryPool.request_query(obj_type);
}
void
qof_query_destroy (QofQuery *query)
{
queryPool.release_query((QofFakeQuery*)query);
}
void
qof_query_set_book (QofQuery *query, QofBook *book)
{
ASSERT_TRUE(queryPool.query_used(query));
ASSERT_TRUE(QOF_IS_BOOK(book));
((QofFakeQuery*)query)->set_book(book);
}
void
xaccQueryAddDateMatchTT (
QofQuery *query,
gboolean use_start,
time64 stt,
gboolean use_end,
time64 ett,
QofQueryOp op)
{
ASSERT_TRUE(queryPool.query_used(query));
((QofFakeQuery*)query)->add_date_match_tt(use_start, stt, use_end, ett, op);
}
void
xaccQueryAddSingleAccountMatch(QofQuery *query, Account *acc, QofQueryOp op)
{
ASSERT_TRUE(queryPool.query_used(query));
((QofFakeQuery*)query)->add_single_account_match(acc, op);
}
GList *
qof_query_run (QofQuery *query)
{
GList *matching_objects = NULL;
bool query_used = queryPool.query_used(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;
}