LeechCraft  0.6.70-13729-g7046a9d2a7
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  * Boost Software License - Version 1.0 - August 17th, 2003
6  *
7  * Permission is hereby granted, free of charge, to any person or organization
8  * obtaining a copy of the software and accompanying documentation covered by
9  * this license (the "Software") to use, reproduce, display, distribute,
10  * execute, and transmit the Software, and to prepare derivative works of the
11  * Software, and to permit third-parties to whom the Software is furnished to
12  * do so, all subject to the following:
13  *
14  * The copyright notices in the Software and this entire statement, including
15  * the above license grant, this restriction and the following disclaimer,
16  * must be included in all copies of the Software, in whole or in part, and
17  * all derivative works of the Software, unless such copies or derivative
18  * works are solely in the form of machine-executable object code generated by
19  * a source language processor.
20  *
21  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23  * FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
24  * SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
25  * FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
26  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
27  * DEALINGS IN THE SOFTWARE.
28  **********************************************************************/
29 
30 #include "oraltest.h"
31 #include "common.h"
32 
33 QTEST_GUILESS_MAIN (LC::Util::OralTest)
34 
36 {
38  QString Value_;
39 
40  static QString ClassName ()
41  {
42  return "AutogenPKeyRecord";
43  }
44 
45  auto AsTuple () const
46  {
47  return std::tie (ID_, Value_);
48  }
49 };
50 
52  ID_,
53  Value_)
54 
56 
57 struct NoPKeyRecord
58 {
59  int ID_;
60  QString Value_;
61 
62  static QString ClassName ()
63  {
64  return "NoPKeyRecord";
65  }
66 
67  auto AsTuple () const
68  {
69  return std::tie (ID_, Value_);
70  }
71 };
72 
74  ID_,
75  Value_)
76 
77 TOSTRING (NoPKeyRecord)
78 
79 struct NonInPlaceConstructibleRecord
80 {
81  int ID_;
82  QString Value_;
83 
84  NonInPlaceConstructibleRecord () = default;
85 
86  NonInPlaceConstructibleRecord (int id, const QString& value, double someExtraArgument)
87  : ID_ { id }
88  , Value_ { value }
89  {
90  Q_UNUSED (someExtraArgument)
91  }
92 
93  static QString ClassName ()
94  {
95  return "NonInPlaceConstructibleRecord";
96  }
97 
98  auto AsTuple () const
99  {
100  return std::tie (ID_, Value_);
101  }
102 };
103 
104 BOOST_FUSION_ADAPT_STRUCT (NonInPlaceConstructibleRecord,
105  ID_,
106  Value_)
107 
108 TOSTRING (NonInPlaceConstructibleRecord)
109 
110 struct ComplexConstraintsRecord
111 {
112  int ID_;
113  QString Value_;
114  int Age_;
115  int Weight_;
116 
117  static QString ClassName ()
118  {
119  return "ComplexConstraintsRecord";
120  }
121 
122  auto AsTuple () const
123  {
124  return std::tie (ID_, Value_, Age_, Weight_);
125  }
126 
130  >;
131 };
132 
133 BOOST_FUSION_ADAPT_STRUCT (ComplexConstraintsRecord,
134  ID_,
135  Value_,
136  Age_,
137  Weight_)
138 
139 TOSTRING (ComplexConstraintsRecord)
140 
141 namespace LC
142 {
143 namespace Util
144 {
145  namespace sph = oral::sph;
146 
147  void OralTest::testAutoPKeyRecordInsertSelect ()
148  {
149  auto adapted = PrepareRecords<AutogenPKeyRecord> (MakeDatabase ());
150  const auto& list = adapted->Select ();
151  QCOMPARE (list, (QList<AutogenPKeyRecord> { { 1, "0" }, { 2, "1" }, { 3, "2" } }));
152  }
153 
154  void OralTest::testAutoPKeyRecordInsertRvalueReturnsPKey ()
155  {
156  auto adapted = Util::oral::AdaptPtr<AutogenPKeyRecord, OralFactory> (MakeDatabase ());
157 
158  QList<int> ids;
159  for (int i = 0; i < 3; ++i)
160  ids << adapted->Insert ({ 0, QString::number (i) });
161 
162  QCOMPARE (ids, (QList<int> { 1, 2, 3 }));
163  }
164 
165  void OralTest::testAutoPKeyRecordInsertConstLvalueReturnsPKey ()
166  {
167  auto adapted = Util::oral::AdaptPtr<AutogenPKeyRecord, OralFactory> (MakeDatabase ());
168 
169  QList<AutogenPKeyRecord> records;
170  for (int i = 0; i < 3; ++i)
171  records.push_back ({ 0, QString::number (i) });
172 
173  QList<int> ids;
174  for (const auto& record : records)
175  ids << adapted->Insert (record);
176 
177  QCOMPARE (ids, (QList<int> { 1, 2, 3 }));
178  }
179 
180  void OralTest::testAutoPKeyRecordInsertSetsPKey ()
181  {
182  auto adapted = Util::oral::AdaptPtr<AutogenPKeyRecord, OralFactory> (MakeDatabase ());
183 
184  QList<AutogenPKeyRecord> records;
185  for (int i = 0; i < 3; ++i)
186  records.push_back ({ 0, QString::number (i) });
187 
188  for (auto& record : records)
189  adapted->Insert (record);
190 
191  QCOMPARE (records, (QList<AutogenPKeyRecord> { { 1, "0" }, { 2, "1" }, { 3, "2" } }));
192  }
193 
194  void OralTest::testNoPKeyRecordInsertSelect ()
195  {
196  auto adapted = PrepareRecords<NoPKeyRecord> (MakeDatabase ());
197  const auto& list = adapted->Select ();
198  QCOMPARE (list, (QList<NoPKeyRecord> { { 0, "0" }, { 1, "1" }, { 2, "2" } }));
199  }
200 
201  void OralTest::testNonInPlaceConstructibleRecordInsertSelect ()
202  {
203  auto adapted = Util::oral::AdaptPtr<NonInPlaceConstructibleRecord, OralFactory> (MakeDatabase ());
204  for (int i = 0; i < 3; ++i)
205  adapted->Insert ({ i, QString::number (i), 0 });
206 
207  const auto& list = adapted->Select ();
208  QCOMPARE (list, (QList<NonInPlaceConstructibleRecord> { { 0, "0", 0 }, { 1, "1", 0 }, { 2, "2", 0 } }));
209  }
210 
211  namespace
212  {
213  template<typename Ex, typename F>
214  void ShallThrow (F&& f)
215  {
216  bool failed = false;
217  try
218  {
219  f ();
220  }
221  catch (const Ex&)
222  {
223  failed = true;
224  }
225 
226  QCOMPARE (failed, true);
227  }
228  }
229 
230  void OralTest::testComplexConstraintsRecordInsertSelectDefault ()
231  {
232  auto adapted = Util::oral::AdaptPtr<ComplexConstraintsRecord, OralFactory> (MakeDatabase ());
233 
234  adapted->Insert ({ 0, "first", 1, 2 });
235  ShallThrow<oral::QueryException> ([&] { adapted->Insert ({ 0, "second", 1, 2 }); });
236  ShallThrow<oral::QueryException> ([&] { adapted->Insert ({ 0, "first", 1, 3 }); });
237  adapted->Insert ({ 0, "second", 1, 3 });
238  ShallThrow<oral::QueryException> ([&] { adapted->Insert ({ 0, "first", 1, 3 }); });
239 
240  const auto& list = adapted->Select ();
241  QCOMPARE (list, (QList<ComplexConstraintsRecord> { { 0, "first", 1, 2 }, { 0, "second", 1, 3 } }));
242  }
243 
244  void OralTest::testComplexConstraintsRecordInsertSelectIgnore ()
245  {
246  auto adapted = Util::oral::AdaptPtr<ComplexConstraintsRecord, OralFactory> (MakeDatabase ());
247 
248  adapted->Insert ({ 0, "first", 1, 2 }, lco::InsertAction::Ignore);
249  adapted->Insert ({ 0, "second", 1, 2 }, lco::InsertAction::Ignore);
250  adapted->Insert ({ 0, "first", 1, 3 }, lco::InsertAction::Ignore);
251  adapted->Insert ({ 0, "second", 1, 3 }, lco::InsertAction::Ignore);
252  adapted->Insert ({ 0, "first", 1, 3 }, lco::InsertAction::Ignore);
253 
254  const auto& list = adapted->Select ();
255  QCOMPARE (list, (QList<ComplexConstraintsRecord> { { 0, "first", 1, 2 }, { 0, "second", 1, 3 } }));
256  }
257 
258  void OralTest::testComplexConstraintsRecordInsertSelectReplace ()
259  {
260  auto adapted = Util::oral::AdaptPtr<ComplexConstraintsRecord, OralFactory> (MakeDatabase ());
261 
262  const auto idValueFields = lco::InsertAction::Replace::Fields<
263  &ComplexConstraintsRecord::ID_,
264  &ComplexConstraintsRecord::Value_
265  >;
266  const auto weightAgeFields = lco::InsertAction::Replace::Fields<
267  &ComplexConstraintsRecord::Weight_,
268  &ComplexConstraintsRecord::Age_
269  >;
270  adapted->Insert ({ 0, "first", 1, 2 }, idValueFields);
271  adapted->Insert ({ 0, "second", 1, 2 }, weightAgeFields);
272  adapted->Insert ({ 0, "first", 1, 3 }, idValueFields);
273  adapted->Insert ({ 0, "third", 1, 3 }, weightAgeFields);
274  adapted->Insert ({ 0, "first", 1, 3 }, weightAgeFields);
275 
276  const auto& list = adapted->Select ();
277  QCOMPARE (list, (QList<ComplexConstraintsRecord> { {0, "second", 1, 2 }, { 0, "first", 1, 3 } }));
278  }
279 }
280 }
static struct LC::Util::oral::InsertAction::IgnoreTag Ignore
auto AsTuple() const
Definition: oraltest.cpp:45
QSqlDatabase MakeDatabase(const QString &name=":memory:")
Definition: common.h:112
BOOST_FUSION_ADAPT_STRUCT(AutogenPKeyRecord, ID_, Value_) struct NoPKeyRecord
Definition: oraltest.cpp:51
constexpr detail::ExprTree< detail::ExprType::LeafStaticPlaceholder, detail::MemberPtrs< Ptr > > f
Definition: oral.h:955
Typelist< Args... > Constraints
Definition: oraltypes.h:164
Definition: constants.h:35
#define TOSTRING(n)
Definition: common.h:91
static QString ClassName()
Definition: oraltest.cpp:40
lco::PKey< int > ID_
Definition: oraltest.cpp:37