LeechCraft  0.6.70-16373-g319c272718
Modular cross-platform feature rich live environment.
oraltest.cpp
Go to the documentation of this file.
1 /**********************************************************************
2  * LeechCraft - modular cross-platform feature rich internet client.
3  * Copyright (C) 2006-2014 Georg Rudoy
4  *
5  * Distributed under the Boost Software License, Version 1.0.
6  * (See accompanying file LICENSE or copy at https://www.boost.org/LICENSE_1_0.txt)
7  **********************************************************************/
8 
9 #include "oraltest.h"
10 #include "common.h"
11 
12 QTEST_GUILESS_MAIN (LC::Util::OralTest)
13 
14 using LC::operator""_ct;
15 
17 {
19  QString Value_;
20 
21  constexpr static auto ClassName = "AutogenPKeyRecord"_ct;
22 
23  auto AsTuple () const
24  {
25  return std::tie (ID_, Value_);
26  }
27 };
28 
30  ID_,
31  Value_)
32 
34 
35 struct NoPKeyRecord
36 {
37  int ID_;
38  QString Value_;
39 
40  constexpr static auto ClassName = "NoPKeyRecord"_ct;
41 
42  auto AsTuple () const
43  {
44  return std::tie (ID_, Value_);
45  }
46 };
47 
48 ORAL_ADAPT_STRUCT (NoPKeyRecord,
49  ID_,
50  Value_)
51 
52 TOSTRING (NoPKeyRecord)
53 
54 struct NonInPlaceConstructibleRecord
55 {
56  int ID_;
57  QString Value_;
58 
59  NonInPlaceConstructibleRecord () = default;
60 
61  NonInPlaceConstructibleRecord (int id, const QString& value, double someExtraArgument)
62  : ID_ { id }
63  , Value_ { value }
64  {
65  Q_UNUSED (someExtraArgument)
66  }
67 
68  constexpr static auto ClassName = "NonInPlaceConstructibleRecord"_ct;
69 
70  auto AsTuple () const
71  {
72  return std::tie (ID_, Value_);
73  }
74 };
75 
76 ORAL_ADAPT_STRUCT (NonInPlaceConstructibleRecord,
77  ID_,
78  Value_)
79 
80 TOSTRING (NonInPlaceConstructibleRecord)
81 
82 struct ComplexConstraintsRecord
83 {
84  int ID_;
85  QString Value_;
86  int Age_;
87  int Weight_;
88 
89  constexpr static auto ClassName = "ComplexConstraintsRecord"_ct;
90 
91  auto AsTuple () const
92  {
93  return std::tie (ID_, Value_, Age_, Weight_);
94  }
95 
99  >;
100 };
101 
102 ORAL_ADAPT_STRUCT (ComplexConstraintsRecord,
103  ID_,
104  Value_,
105  Age_,
106  Weight_)
107 
108 TOSTRING (ComplexConstraintsRecord)
109 
110 template<typename... Args>
111 QDebug operator<< (QDebug dbg, const std::tuple<Args...>& tup)
112 {
113  return std::apply ([&] (auto&&... args) { return ((dbg.nospace () << args << ' '), ...); }, tup);
114 }
115 
116 namespace LC
117 {
118 namespace Util
119 {
120  namespace sph = oral::sph;
121 
122  void OralTest::testAutoPKeyRecordInsertSelect ()
123  {
124  qDebug () << oral::detail::FieldNames<AutogenPKeyRecord>;
125  auto adapted = PrepareRecords<AutogenPKeyRecord> (MakeDatabase ());
126  const auto& list = adapted->Select ();
127  QCOMPARE (list, (QList<AutogenPKeyRecord> { { 1, "0" }, { 2, "1" }, { 3, "2" } }));
128  }
129 
130  void OralTest::testAutoPKeyRecordInsertRvalueReturnsPKey ()
131  {
132  auto adapted = Util::oral::AdaptPtr<AutogenPKeyRecord, OralFactory> (MakeDatabase ());
133 
134  QList<int> ids;
135  for (int i = 0; i < 3; ++i)
136  ids << adapted->Insert ({ 0, QString::number (i) });
137 
138  QCOMPARE (ids, (QList<int> { 1, 2, 3 }));
139  }
140 
141  void OralTest::testAutoPKeyRecordInsertConstLvalueReturnsPKey ()
142  {
143  auto adapted = Util::oral::AdaptPtr<AutogenPKeyRecord, OralFactory> (MakeDatabase ());
144 
145  QList<AutogenPKeyRecord> records;
146  for (int i = 0; i < 3; ++i)
147  records.push_back ({ 0, QString::number (i) });
148 
149  QList<int> ids;
150  for (const auto& record : records)
151  ids << adapted->Insert (record);
152 
153  QCOMPARE (ids, (QList<int> { 1, 2, 3 }));
154  }
155 
156  void OralTest::testAutoPKeyRecordInsertSetsPKey ()
157  {
158  auto adapted = Util::oral::AdaptPtr<AutogenPKeyRecord, OralFactory> (MakeDatabase ());
159 
160  QList<AutogenPKeyRecord> records;
161  for (int i = 0; i < 3; ++i)
162  records.push_back ({ 0, QString::number (i) });
163 
164  for (auto& record : records)
165  adapted->Insert (record);
166 
167  QCOMPARE (records, (QList<AutogenPKeyRecord> { { 1, "0" }, { 2, "1" }, { 3, "2" } }));
168  }
169 
170  void OralTest::testNoPKeyRecordInsertSelect ()
171  {
172  auto adapted = PrepareRecords<NoPKeyRecord> (MakeDatabase ());
173  const auto& list = adapted->Select ();
174  QCOMPARE (list, (QList<NoPKeyRecord> { { 0, "0" }, { 1, "1" }, { 2, "2" } }));
175  }
176 
177  void OralTest::testNonInPlaceConstructibleRecordInsertSelect ()
178  {
179  auto adapted = Util::oral::AdaptPtr<NonInPlaceConstructibleRecord, OralFactory> (MakeDatabase ());
180  for (int i = 0; i < 3; ++i)
181  adapted->Insert ({ i, QString::number (i), 0 });
182 
183  const auto& list = adapted->Select ();
184  QCOMPARE (list, (QList<NonInPlaceConstructibleRecord> { { 0, "0", 0 }, { 1, "1", 0 }, { 2, "2", 0 } }));
185  }
186 
187  namespace
188  {
189  template<typename Ex, typename F>
190  void ShallThrow (F&& f)
191  {
192  bool failed = false;
193  try
194  {
195  f ();
196  }
197  catch (const Ex&)
198  {
199  failed = true;
200  }
201 
202  QCOMPARE (failed, true);
203  }
204  }
205 
206  void OralTest::testComplexConstraintsRecordInsertSelectDefault ()
207  {
208  auto adapted = Util::oral::AdaptPtr<ComplexConstraintsRecord, OralFactory> (MakeDatabase ());
209 
210  adapted->Insert ({ 0, "first", 1, 2 });
211  ShallThrow<oral::QueryException> ([&] { adapted->Insert ({ 0, "second", 1, 2 }); });
212  ShallThrow<oral::QueryException> ([&] { adapted->Insert ({ 0, "first", 1, 3 }); });
213  adapted->Insert ({ 0, "second", 1, 3 });
214  ShallThrow<oral::QueryException> ([&] { adapted->Insert ({ 0, "first", 1, 3 }); });
215 
216  const auto& list = adapted->Select ();
217  QCOMPARE (list, (QList<ComplexConstraintsRecord> { { 0, "first", 1, 2 }, { 0, "second", 1, 3 } }));
218  }
219 
220  void OralTest::testComplexConstraintsRecordInsertSelectIgnore ()
221  {
222  auto adapted = Util::oral::AdaptPtr<ComplexConstraintsRecord, OralFactory> (MakeDatabase ());
223 
224  adapted->Insert ({ 0, "first", 1, 2 }, lco::InsertAction::Ignore);
225  adapted->Insert ({ 0, "second", 1, 2 }, lco::InsertAction::Ignore);
226  adapted->Insert ({ 0, "first", 1, 3 }, lco::InsertAction::Ignore);
227  adapted->Insert ({ 0, "second", 1, 3 }, lco::InsertAction::Ignore);
228  adapted->Insert ({ 0, "first", 1, 3 }, lco::InsertAction::Ignore);
229 
230  const auto& list = adapted->Select ();
231  QCOMPARE (list, (QList<ComplexConstraintsRecord> { { 0, "first", 1, 2 }, { 0, "second", 1, 3 } }));
232  }
233 
234  void OralTest::testComplexConstraintsRecordInsertSelectReplace ()
235  {
236  auto adapted = Util::oral::AdaptPtr<ComplexConstraintsRecord, OralFactory> (MakeDatabase ());
237 
238  const auto idValueFields = lco::InsertAction::Replace::Fields<
239  &ComplexConstraintsRecord::ID_,
241  >;
242  const auto weightAgeFields = lco::InsertAction::Replace::Fields<
243  &ComplexConstraintsRecord::Weight_,
244  &ComplexConstraintsRecord::Age_
245  >;
246  adapted->Insert ({ 0, "first", 1, 2 }, idValueFields);
247  adapted->Insert ({ 0, "second", 1, 2 }, weightAgeFields);
248  adapted->Insert ({ 0, "first", 1, 3 }, idValueFields);
249  adapted->Insert ({ 0, "third", 1, 3 }, weightAgeFields);
250  adapted->Insert ({ 0, "first", 1, 3 }, weightAgeFields);
251 
252  const auto& list = adapted->Select ();
253  QCOMPARE (list, (QList<ComplexConstraintsRecord> { {0, "second", 1, 2 }, { 0, "first", 1, 3 } }));
254  }
255 }
256 }
auto AsTuple() const
Definition: oraltest.cpp:23
static constexpr struct LC::Util::oral::InsertAction::IgnoreTag Ignore
const std::tuple< Args... > & tup
Definition: oraltest.cpp:112
QSqlDatabase MakeDatabase(const QString &name=":memory:")
Definition: common.h:73
ORAL_ADAPT_STRUCT(AutogenPKeyRecord, ID_, Value_) struct NoPKeyRecord
Definition: oraltest.cpp:29
constexpr detail::ExprTree< detail::ExprType::LeafStaticPlaceholder, detail::MemberPtrs< Ptr > > f
Definition: oral.h:951
static constexpr auto ClassName
Definition: oraltest.cpp:21
QDBusArgument & operator<<(QDBusArgument &out, const IconFrame &frame)
Typelist< Args... > Constraints
Definition: oraltypes.h:139
const QVariant Value_
Definition: plotitem.cpp:74
#define TOSTRING(n)
Definition: common.h:52
lco::PKey< int > ID_
Definition: oraltest.cpp:18