GnuCash  4.8a-132-gcdaeb421d+
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 
17 static class QofFakeQueryPool
18 {
19 public:
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_queriesUsed.erase(it);
63  m_queriesConsumed.push_back(*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 
82 private:
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 
92 QofFakeQuery::QofFakeQuery(QofIdTypeConst obj_type) :
93  m_obj_type(obj_type)
94 {
95  queryPool.add_query(this);
96 }
97 
98 QofFakeQuery::~QofFakeQuery()
99 {
100  queryPool.remove_query(this);
101 }
102 
103 
104 
105 /* mock functions */
106 
107 QofQuery *
108 qof_query_create_for (QofIdTypeConst obj_type)
109 {
110  return (QofQuery*)queryPool.request_query(obj_type);
111 }
112 
113 void
114 qof_query_destroy (QofQuery *query)
115 {
116  queryPool.release_query((QofFakeQuery*)query);
117 }
118 
119 void
120 qof_query_set_book (QofQuery *query, QofBook *book)
121 {
122  ASSERT_TRUE(queryPool.query_used(query));
123  ASSERT_TRUE(QOF_IS_BOOK(book));
124  ((QofFakeQuery*)query)->set_book(book);
125 }
126 
127 extern "C"
128 {
129 void
130 xaccQueryAddDateMatchTT (
131  QofQuery *query,
132  gboolean use_start,
133  time64 stt,
134  gboolean use_end,
135  time64 ett,
136  QofQueryOp op)
137 {
138  ASSERT_TRUE(queryPool.query_used(query));
139  ((QofFakeQuery*)query)->add_date_match_tt(use_start, stt, use_end, ett, op);
140 }
141 
142 void
143 xaccQueryAddSingleAccountMatch(QofQuery *query, Account *acc, QofQueryOp op)
144 {
145  ASSERT_TRUE(queryPool.query_used(query));
146  ((QofFakeQuery*)query)->add_single_account_match(acc, op);
147 }
148 
149 } // extern "C"
150 
151 GList *
152 qof_query_run (QofQuery *query)
153 {
154  GList *matching_objects = NULL;
155  bool query_used = queryPool.query_used(query);
156 
157  EXPECT_TRUE(query_used);
158  if (query_used)
159  {
160  auto matchingObjects = ((QofFakeQuery*)query)->run();
161 
162  for (auto object : matchingObjects)
163  {
164  matching_objects = g_list_append(matching_objects, static_cast<gpointer>(object));
165  }
166  }
167 
168  return matching_objects;
169 }
const gchar * QofIdTypeConst
QofIdTypeConst declaration.
Definition: qofid.h:87
void qof_query_destroy(QofQuery *query)
Frees the resources associate with a Query object.
Mocking qof queries.
void qof_query_set_book(QofQuery *query, QofBook *book)
Set the book to be searched.
Encapsulate all the information about a dataset.
QofQueryOp
Query Term Operators, for combining Query Terms.
Definition: qofquery.h:93
GList * qof_query_run(QofQuery *query)
Perform the query, return the results.
Fake object providing functionality similar to QofQuery.
Definition: fake-qofquery.h:57
gint64 time64
Many systems, including Microsoft Windows and BSD-derived Unixes like Darwin, are retaining the int-3...
Definition: gnc-date.h:93