LeechCraft  0.6.70-15082-g543737046d
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 
15 {
16  lco::PKey<int> ID_;
17  QString Value_;
18 
19  static QString ClassName ()
20  {
21  return "AutogenPKeyRecord";
22  }
23 
24  auto AsTuple () const
25  {
26  return std::tie (ID_, Value_);
27  }
28 };
29 
31  ID_,
32  Value_)
33 
35 
36 struct NoPKeyRecord
37 {
38  int ID_;
39  QString Value_;
40 
41  static QString ClassName ()
42  {
43  return "NoPKeyRecord";
44  }
45 
46  auto AsTuple () const
47  {
48  return std::tie (ID_, Value_);
49  }
50 };
51 
53  ID_,
54  Value_)
55 
56 TOSTRING (NoPKeyRecord)
57 
58 struct NonInPlaceConstructibleRecord
59 {
60  int ID_;
61  QString Value_;
62 
63  NonInPlaceConstructibleRecord () = default;
64 
65  NonInPlaceConstructibleRecord (int id, const QString& value, double someExtraArgument)
66  : ID_ { id }
67  , Value_ { value }
68  {
69  Q_UNUSED (someExtraArgument)
70  }
71 
72  static QString ClassName ()
73  {
74  return "NonInPlaceConstructibleRecord";
75  }
76 
77  auto AsTuple () const
78  {
79  return std::tie (ID_, Value_);
80  }
81 };
82 
83 BOOST_FUSION_ADAPT_STRUCT (NonInPlaceConstructibleRecord,
84  ID_,
85  Value_)
86 
87 TOSTRING (NonInPlaceConstructibleRecord)
88 
89 struct ComplexConstraintsRecord
90 {
91  int ID_;
92  QString Value_;
93  int Age_;
94  int Weight_;
95 
96  static QString ClassName ()
97  {
98  return "ComplexConstraintsRecord";
99  }
100 
101  auto AsTuple () const
102  {
103  return std::tie (ID_, Value_, Age_, Weight_);
104  }
105 
109  >;
110 };
111 
112 BOOST_FUSION_ADAPT_STRUCT (ComplexConstraintsRecord,
113  ID_,
114  Value_,
115  Age_,
116  Weight_)
117 
118 TOSTRING (ComplexConstraintsRecord)
119 
120 namespace LC
121 {
122 namespace Util
123 {
124  namespace sph = oral::sph;
125 
126  void OralTest::testAutoPKeyRecordInsertSelect ()
127  {
128  auto adapted = PrepareRecords<AutogenPKeyRecord> (MakeDatabase ());
129  const auto& list = adapted->Select ();
130  QCOMPARE (list, (QList<AutogenPKeyRecord> { { 1, "0" }, { 2, "1" }, { 3, "2" } }));
131  }
132 
133  void OralTest::testAutoPKeyRecordInsertRvalueReturnsPKey ()
134  {
135  auto adapted = Util::oral::AdaptPtr<AutogenPKeyRecord, OralFactory> (MakeDatabase ());
136 
137  QList<int> ids;
138  for (int i = 0; i < 3; ++i)
139  ids << adapted->Insert ({ 0, QString::number (i) });
140 
141  QCOMPARE (ids, (QList<int> { 1, 2, 3 }));
142  }
143 
144  void OralTest::testAutoPKeyRecordInsertConstLvalueReturnsPKey ()
145  {
146  auto adapted = Util::oral::AdaptPtr<AutogenPKeyRecord, OralFactory> (MakeDatabase ());
147 
148  QList<AutogenPKeyRecord> records;
149  for (int i = 0; i < 3; ++i)
150  records.push_back ({ 0, QString::number (i) });
151 
152  QList<int> ids;
153  for (const auto& record : records)
154  ids << adapted->Insert (record);
155 
156  QCOMPARE (ids, (QList<int> { 1, 2, 3 }));
157  }
158 
159  void OralTest::testAutoPKeyRecordInsertSetsPKey ()
160  {
161  auto adapted = Util::oral::AdaptPtr<AutogenPKeyRecord, OralFactory> (MakeDatabase ());
162 
163  QList<AutogenPKeyRecord> records;
164  for (int i = 0; i < 3; ++i)
165  records.push_back ({ 0, QString::number (i) });
166 
167  for (auto& record : records)
168  adapted->Insert (record);
169 
170  QCOMPARE (records, (QList<AutogenPKeyRecord> { { 1, "0" }, { 2, "1" }, { 3, "2" } }));
171  }
172 
173  void OralTest::testNoPKeyRecordInsertSelect ()
174  {
175  auto adapted = PrepareRecords<NoPKeyRecord> (MakeDatabase ());
176  const auto& list = adapted->Select ();
177  QCOMPARE (list, (QList<NoPKeyRecord> { { 0, "0" }, { 1, "1" }, { 2, "2" } }));
178  }
179 
180  void OralTest::testNonInPlaceConstructibleRecordInsertSelect ()
181  {
182  auto adapted = Util::oral::AdaptPtr<NonInPlaceConstructibleRecord, OralFactory> (MakeDatabase ());
183  for (int i = 0; i < 3; ++i)
184  adapted->Insert ({ i, QString::number (i), 0 });
185 
186  const auto& list = adapted->Select ();
187  QCOMPARE (list, (QList<NonInPlaceConstructibleRecord> { { 0, "0", 0 }, { 1, "1", 0 }, { 2, "2", 0 } }));
188  }
189 
190  namespace
191  {
192  template<typename Ex, typename F>
193  void ShallThrow (F&& f)
194  {
195  bool failed = false;
196  try
197  {
198  f ();
199  }
200  catch (const Ex&)
201  {
202  failed = true;
203  }
204 
205  QCOMPARE (failed, true);
206  }
207  }
208 
209  void OralTest::testComplexConstraintsRecordInsertSelectDefault ()
210  {
211  auto adapted = Util::oral::AdaptPtr<ComplexConstraintsRecord, OralFactory> (MakeDatabase ());
212 
213  adapted->Insert ({ 0, "first", 1, 2 });
214  ShallThrow<oral::QueryException> ([&] { adapted->Insert ({ 0, "second", 1, 2 }); });
215  ShallThrow<oral::QueryException> ([&] { adapted->Insert ({ 0, "first", 1, 3 }); });
216  adapted->Insert ({ 0, "second", 1, 3 });
217  ShallThrow<oral::QueryException> ([&] { adapted->Insert ({ 0, "first", 1, 3 }); });
218 
219  const auto& list = adapted->Select ();
220  QCOMPARE (list, (QList<ComplexConstraintsRecord> { { 0, "first", 1, 2 }, { 0, "second", 1, 3 } }));
221  }
222 
223  void OralTest::testComplexConstraintsRecordInsertSelectIgnore ()
224  {
225  auto adapted = Util::oral::AdaptPtr<ComplexConstraintsRecord, OralFactory> (MakeDatabase ());
226 
227  adapted->Insert ({ 0, "first", 1, 2 }, lco::InsertAction::Ignore);
228  adapted->Insert ({ 0, "second", 1, 2 }, lco::InsertAction::Ignore);
229  adapted->Insert ({ 0, "first", 1, 3 }, lco::InsertAction::Ignore);
230  adapted->Insert ({ 0, "second", 1, 3 }, lco::InsertAction::Ignore);
231  adapted->Insert ({ 0, "first", 1, 3 }, lco::InsertAction::Ignore);
232 
233  const auto& list = adapted->Select ();
234  QCOMPARE (list, (QList<ComplexConstraintsRecord> { { 0, "first", 1, 2 }, { 0, "second", 1, 3 } }));
235  }
236 
237  void OralTest::testComplexConstraintsRecordInsertSelectReplace ()
238  {
239  auto adapted = Util::oral::AdaptPtr<ComplexConstraintsRecord, OralFactory> (MakeDatabase ());
240 
241  const auto idValueFields = lco::InsertAction::Replace::Fields<
242  &ComplexConstraintsRecord::ID_,
244  >;
245  const auto weightAgeFields = lco::InsertAction::Replace::Fields<
246  &ComplexConstraintsRecord::Weight_,
247  &ComplexConstraintsRecord::Age_
248  >;
249  adapted->Insert ({ 0, "first", 1, 2 }, idValueFields);
250  adapted->Insert ({ 0, "second", 1, 2 }, weightAgeFields);
251  adapted->Insert ({ 0, "first", 1, 3 }, idValueFields);
252  adapted->Insert ({ 0, "third", 1, 3 }, weightAgeFields);
253  adapted->Insert ({ 0, "first", 1, 3 }, weightAgeFields);
254 
255  const auto& list = adapted->Select ();
256  QCOMPARE (list, (QList<ComplexConstraintsRecord> { {0, "second", 1, 2 }, { 0, "first", 1, 3 } }));
257  }
258 }
259 }
AutogenPKeyRecord
Definition: oraltest.cpp:14
QList
Definition: ianrulesstorage.h:14
LC::Util::MakeDatabase
QSqlDatabase MakeDatabase(const QString &name=":memory:")
Definition: common.h:73
common.h
LC::Util::oral::PrimaryKey
Definition: oraltypes.h:155
BOOST_FUSION_ADAPT_STRUCT
BOOST_FUSION_ADAPT_STRUCT(AutogenPKeyRecord, ID_, Value_) struct NoPKeyRecord
Definition: oraltest.cpp:30
LC::Util::oral::UniqueSubset
Definition: oraltypes.h:158
TOSTRING
#define TOSTRING(n)
Definition: common.h:52
LC::Util::Typelist
Definition: typelist.h:30
Value_
const QVariant Value_
Definition: plotitem.cpp:80
LC::Util::oral::PKey< int >
LC::Util::oral::Constraints
Typelist< Args... > Constraints
Definition: oraltypes.h:161
LC::Util::oral::InsertAction::Ignore
static struct LC::Util::oral::InsertAction::IgnoreTag Ignore
oraltest.h
LC
Definition: constants.h:14
LC::Util::OralTest
Definition: oraltest.h:29
LC::Util::oral::sph::f
constexpr detail::ExprTree< detail::ExprType::LeafStaticPlaceholder, detail::MemberPtrs< Ptr > > f
Definition: oral.h:952