GnuCash c935c2f+
Loading...
Searching...
No Matches
fake-qofquery.cpp
Go to the documentation of this file.
1
5#include <config.h>
6
7#include <qofbook.h>
8
9#include <list>
10
11#include "fake-qofquery.h"
12
13
14
15/* class QofFakeQueryPool */
16
17static class QofFakeQueryPool
18{
19public:
20
21 /* Add QofFakeQuery object to the pool */
22 void add_query(QofFakeQuery *query)
23 {
24 m_queriesNew.push_back(query);
25 }
26
27 /* Request to use a QofFakeQuery object */
28 QofFakeQuery* request_query(QofIdTypeConst obj_type)
29 {
30 QofFakeQuery* query = nullptr;
31
32 auto it = std::find_if(m_queriesNew.begin(), m_queriesNew.end(),
33 [obj_type](QofFakeQuery const* query) {
34 return (g_strcmp0(query->m_obj_type, obj_type) == 0);
35 });
36 if (it != m_queriesNew.end())
37 {
38 query = *it;
39 m_queriesNew.erase(it);
40 m_queriesUsed.push_back(query);
41 }
42
43 EXPECT_NE(query, nullptr);
44 return query;
45 }
46
47 /* Check if a QofFakeQuery object is currently used, i.e. it has been
48 * requested before */
49 bool query_used(QofQuery *query)
50 {
51 auto it = std::find(m_queriesUsed.begin(), m_queriesUsed.end(), (QofFakeQuery*)query);
52
53 return (it != m_queriesUsed.end());
54 }
55
56 /* Release a formerly requested QofFakeQuery object, which is not used
57 * anymore */
58 void release_query(QofFakeQuery *query)
59 {
60 ASSERT_TRUE(query_used((QofQuery*)query));
61 auto it = std::find(m_queriesUsed.begin(), m_queriesUsed.end(), query);
62 m_queriesConsumed.push_back(*it);
63 m_queriesUsed.erase(it);
64 }
65
66 /* Remove a formerly added QofFakeQueryObject from the pool */
67 void remove_query(QofFakeQuery *query)
68 {
69 ASSERT_FALSE(query_used((QofQuery*)query));
70 auto it = std::find(m_queriesConsumed.begin(), m_queriesConsumed.end(), (QofFakeQuery*)query);
71 if (it != m_queriesConsumed.end())
72 m_queriesConsumed.erase(it);
73 else
74 {
75 it = std::find(m_queriesNew.begin(), m_queriesNew.end(), (QofFakeQuery*)query);
76 bool query_found = (it != m_queriesNew.end());
77 ASSERT_TRUE(query_found);
78 m_queriesNew.erase(it);
79 }
80 }
81
82private:
83 std::list<QofFakeQuery*> m_queriesNew {};
84 std::list<QofFakeQuery*> m_queriesUsed {};
85 std::list<QofFakeQuery*> m_queriesConsumed {};
86} queryPool;
87
88
89
90/* class QofFakeQuery */
91
92QofFakeQuery::QofFakeQuery(QofIdTypeConst obj_type) :
93 m_obj_type(obj_type)
94{
95 queryPool.add_query(this);
96}
97
98QofFakeQuery::~QofFakeQuery()
99{
100 queryPool.remove_query(this);
101}
102
103
104
105/* mock functions */
106
107QofQuery *
108qof_query_create_for (QofIdTypeConst obj_type)
109{
110 return (QofQuery*)queryPool.request_query(obj_type);
111}
112
113void
115{
116 queryPool.release_query((QofFakeQuery*)query);
117}
118
119void
121{
122 ASSERT_TRUE(queryPool.query_used(query));
123 ASSERT_TRUE(QOF_IS_BOOK(book));
124 ((QofFakeQuery*)query)->set_book(book);
125}
126
127void
128xaccQueryAddDateMatchTT (
129 QofQuery *query,
130 gboolean use_start,
131 time64 stt,
132 gboolean use_end,
133 time64 ett,
134 QofQueryOp op)
135{
136 ASSERT_TRUE(queryPool.query_used(query));
137 ((QofFakeQuery*)query)->add_date_match_tt(use_start, stt, use_end, ett, op);
138}
139
140void
141xaccQueryAddSingleAccountMatch(QofQuery *query, Account *acc, QofQueryOp op)
142{
143 ASSERT_TRUE(queryPool.query_used(query));
144 ((QofFakeQuery*)query)->add_single_account_match(acc, op);
145}
146
147
148GList *
150{
151 GList *matching_objects = NULL;
152 bool query_used = queryPool.query_used(query);
153
154 EXPECT_TRUE(query_used);
155 if (query_used)
156 {
157 auto matchingObjects = ((QofFakeQuery*)query)->run();
158
159 for (auto object : matchingObjects)
160 {
161 matching_objects = g_list_append(matching_objects, static_cast<gpointer>(object));
162 }
163 }
164
165 return matching_objects;
166}
Fake object providing functionality similar to QofQuery.
Mocking qof queries.
gint64 time64
Most systems that are currently maintained, including Microsoft Windows, BSD-derived Unixes and Linux...
Definition gnc-date.h:87
const gchar * QofIdTypeConst
QofIdTypeConst declaration.
Definition qofid.h:82
void qof_query_set_book(QofQuery *query, QofBook *book)
Set the book to be searched.
QofQueryOp
Query Term Operators, for combining Query Terms.
Definition qofquery.h:93
void qof_query_destroy(QofQuery *query)
Frees the resources associate with a Query object.
GList * qof_query_run(QofQuery *query)
Perform the query, return the results.
Encapsulate all the information about a dataset.
STRUCTS.
QofBook reference.
Definition qofbook-p.hpp:47
A Query.
Definition qofquery.cpp:75