LeechCraft  0.6.70-13729-g7046a9d2a7
Modular cross-platform feature rich live environment.
oraltest_simplerecord.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_simplerecord.h"
31 #include "common.h"
32 #include "simplerecord.h"
33 
34 QTEST_GUILESS_MAIN (LC::Util::OralTest_SimpleRecord)
35 
36 namespace LC
37 {
38 namespace Util
39 {
40  namespace sph = oral::sph;
41 
42  void OralTest_SimpleRecord::testSimpleRecordInsertSelect ()
43  {
44  auto adapted = PrepareRecords<SimpleRecord> (MakeDatabase ());
45  const auto& list = adapted->Select ();
46  QCOMPARE (list, (QList<SimpleRecord> { { 0, "0" }, { 1, "1" }, { 2, "2" } }));
47  }
48 
49  void OralTest_SimpleRecord::testSimpleRecordInsertReplaceSelect ()
50  {
51  auto db = MakeDatabase ();
52 
53  auto adapted = Util::oral::AdaptPtr<SimpleRecord, OralFactory> (db);
54  for (int i = 0; i < 3; ++i)
55  adapted->Insert ({ 0, QString::number (i) }, lco::InsertAction::Replace::PKey<SimpleRecord>);
56 
57  const auto& list = adapted->Select ();
58  QCOMPARE (list, (QList<SimpleRecord> { { 0, "2" } }));
59  }
60 
61  void OralTest_SimpleRecord::testSimpleRecordInsertIgnoreSelect ()
62  {
63  auto db = MakeDatabase ();
64 
65  auto adapted = Util::oral::AdaptPtr<SimpleRecord, OralFactory> (db);
66  for (int i = 0; i < 3; ++i)
67  adapted->Insert ({ 0, QString::number (i) }, lco::InsertAction::Ignore);
68 
69  const auto& list = adapted->Select ();
70  QCOMPARE (list, (QList<SimpleRecord> { { 0, "0" } }));
71  }
72 
73  void OralTest_SimpleRecord::testSimpleRecordInsertSelectByPos ()
74  {
75  auto adapted = PrepareRecords<SimpleRecord> (MakeDatabase ());
76  const auto& list = adapted->Select (sph::f<&SimpleRecord::ID_> == 1);
77  QCOMPARE (list, (QList<SimpleRecord> { { 1, "1" } }));
78  }
79 
80  void OralTest_SimpleRecord::testSimpleRecordInsertSelectByPos2 ()
81  {
82  auto adapted = PrepareRecords<SimpleRecord> (MakeDatabase ());
83  const auto& list = adapted->Select (sph::f<&SimpleRecord::ID_> < 2);
84  QCOMPARE (list, (QList<SimpleRecord> { { 0, "0" }, { 1, "1" } }));
85  }
86 
87  void OralTest_SimpleRecord::testSimpleRecordInsertSelectByPos3 ()
88  {
89  auto adapted = PrepareRecords<SimpleRecord> (MakeDatabase ());
90  const auto& list = adapted->Select (sph::f<&SimpleRecord::ID_> < 2 && sph::f<&SimpleRecord::Value_> == QString { "1" });
91  QCOMPARE (list, (QList<SimpleRecord> { { 1, "1" } }));
92  }
93 
94  void OralTest_SimpleRecord::testSimpleRecordInsertSelectOneByPos ()
95  {
96  auto adapted = PrepareRecords<SimpleRecord> (MakeDatabase ());
97  const auto& single = adapted->SelectOne (sph::f<&SimpleRecord::ID_> == 1);
98  QCOMPARE (single, (std::optional<SimpleRecord> { { 1, "1" } }));
99  }
100 
101  void OralTest_SimpleRecord::testSimpleRecordInsertSelectByFields ()
102  {
103  auto adapted = PrepareRecords<SimpleRecord> (MakeDatabase ());
104  const auto& list = adapted->Select (sph::f<&SimpleRecord::ID_> == 1);
105  QCOMPARE (list, (QList<SimpleRecord> { { 1, "1" } }));
106  }
107 
108  void OralTest_SimpleRecord::testSimpleRecordInsertSelectByFields2 ()
109  {
110  auto adapted = PrepareRecords<SimpleRecord> (MakeDatabase ());
111  const auto& list = adapted->Select (sph::f<&SimpleRecord::ID_> < 2);
112  QCOMPARE (list, (QList<SimpleRecord> { { 0, "0" }, { 1, "1" } }));
113  }
114 
115  void OralTest_SimpleRecord::testSimpleRecordInsertSelectByFields3 ()
116  {
117  auto adapted = PrepareRecords<SimpleRecord> (MakeDatabase ());
118  const auto& list = adapted->Select (sph::f<&SimpleRecord::ID_> < 2 && sph::f<&SimpleRecord::Value_> == QString { "1" });
119  QCOMPARE (list, (QList<SimpleRecord> { { 1, "1" } }));
120  }
121 
122  void OralTest_SimpleRecord::testSimpleRecordInsertSelectOneByFields ()
123  {
124  auto adapted = PrepareRecords<SimpleRecord> (MakeDatabase ());
125  const auto& single = adapted->SelectOne (sph::f<&SimpleRecord::ID_> == 1);
126  QCOMPARE (single, (std::optional<SimpleRecord> { { 1, "1" } }));
127  }
128 
129  void OralTest_SimpleRecord::testSimpleRecordInsertSelectSingleFieldByFields ()
130  {
131  auto adapted = PrepareRecords<SimpleRecord> (MakeDatabase ());
132  const auto& list = adapted->Select (sph::fields<&SimpleRecord::Value_>, sph::f<&SimpleRecord::ID_> < 2);
133  QCOMPARE (list, (QList<QString> { "0", "1" }));
134  }
135 
136  void OralTest_SimpleRecord::testSimpleRecordInsertSelectFieldsByFields ()
137  {
138  auto adapted = PrepareRecords<SimpleRecord> (MakeDatabase ());
139  const auto& list = adapted->Select (sph::fields<&SimpleRecord::ID_, &SimpleRecord::Value_>, sph::f<&SimpleRecord::ID_> < 2);
140  QCOMPARE (list, (QList<std::tuple<int, QString>> { { 0, "0" }, { 1, "1" } }));
141  }
142 
143  void OralTest_SimpleRecord::testSimpleRecordInsertSelectFieldsByFieldsOrderAsc ()
144  {
145  auto adapted = PrepareRecords<SimpleRecord> (MakeDatabase ());
146  const auto& list = adapted->Select (sph::fields<&SimpleRecord::ID_, &SimpleRecord::Value_>,
147  sph::f<&SimpleRecord::ID_> < 2,
148  oral::OrderBy<sph::asc<&SimpleRecord::Value_>>);
149  QCOMPARE (list, (QList<std::tuple<int, QString>> { { 0, "0" }, { 1, "1" } }));
150  }
151 
152  void OralTest_SimpleRecord::testSimpleRecordInsertSelectFieldsByFieldsOrderDesc ()
153  {
154  auto adapted = PrepareRecords<SimpleRecord> (MakeDatabase ());
155  const auto& list = adapted->Select (sph::fields<&SimpleRecord::ID_, &SimpleRecord::Value_>,
156  sph::f<&SimpleRecord::ID_> < 2,
157  oral::OrderBy<sph::desc<&SimpleRecord::Value_>>);
158  QCOMPARE (list, (QList<std::tuple<int, QString>> { { 1, "1" }, { 0, "0" } }));
159  }
160 
161  void OralTest_SimpleRecord::testSimpleRecordInsertSelectFieldsByFieldsOrderManyAsc ()
162  {
163  auto adapted = PrepareRecords<SimpleRecord> (MakeDatabase ());
164  const auto& list = adapted->Select (sph::fields<&SimpleRecord::ID_, &SimpleRecord::Value_>,
165  sph::f<&SimpleRecord::ID_> < 2,
166  oral::OrderBy<sph::asc<&SimpleRecord::Value_>, sph::desc<&SimpleRecord::ID_>>);
167  QCOMPARE (list, (QList<std::tuple<int, QString>> { { 0, "0" }, { 1, "1" } }));
168  }
169 
170  void OralTest_SimpleRecord::testSimpleRecordInsertSelectFieldsByFieldsOrderManyDesc ()
171  {
172  auto adapted = PrepareRecords<SimpleRecord> (MakeDatabase ());
173  const auto& list = adapted->Select (sph::fields<&SimpleRecord::ID_, &SimpleRecord::Value_>,
174  sph::f<&SimpleRecord::ID_> < 2,
175  oral::OrderBy<sph::desc<&SimpleRecord::Value_>, sph::asc<&SimpleRecord::ID_>>);
176  QCOMPARE (list, (QList<std::tuple<int, QString>> { { 1, "1" }, { 0, "0" } }));
177  }
178 
179  void OralTest_SimpleRecord::testSimpleRecordInsertSelectNoOffsetLimit ()
180  {
181  auto adapted = PrepareRecords<SimpleRecord> (MakeDatabase (), 10);
182  const auto& list = adapted->Select.Build ().Limit ({ 2 }) ();
183  QCOMPARE (list, (QList<SimpleRecord> { { 0, "0" }, { 1, "1" } }));
184  }
185 
186  void OralTest_SimpleRecord::testSimpleRecordInsertSelectOffsetNoLimit ()
187  {
188  auto adapted = PrepareRecords<SimpleRecord> (MakeDatabase (), 10);
189  const auto& list = adapted->Select.Build ().Offset ({ 8 }) ();
190  QCOMPARE (list, (QList<SimpleRecord> { { 8, "8" }, { 9, "9" } }));
191  }
192 
193  void OralTest_SimpleRecord::testSimpleRecordInsertSelectOffsetLimit ()
194  {
195  auto adapted = PrepareRecords<SimpleRecord> (MakeDatabase (), 10);
196  const auto& list = adapted->Select.Build ().Offset ({ 5 }).Limit ({ 2 }) ();
197  QCOMPARE (list, (QList<SimpleRecord> { { 5, "5" }, { 6, "6" } }));
198  }
199 
200  void OralTest_SimpleRecord::testSimpleRecordInsertSelectCount ()
201  {
202  auto adapted = PrepareRecords<SimpleRecord> (MakeDatabase ());
203  const auto count = adapted->Select (sph::count<>);
204  QCOMPARE (count, 3);
205  }
206 
207  void OralTest_SimpleRecord::testSimpleRecordInsertSelectCountByFields ()
208  {
209  auto adapted = PrepareRecords<SimpleRecord> (MakeDatabase ());
210  const auto count = adapted->Select (sph::count<>, sph::f<&SimpleRecord::ID_> < 2);
211  QCOMPARE (count, 2);
212  }
213 
214  void OralTest_SimpleRecord::testSimpleRecordInsertSelectMin ()
215  {
216  auto adapted = PrepareRecords<SimpleRecord> (MakeDatabase ());
217  const auto min = adapted->Select (sph::min<&SimpleRecord::ID_>);
218  QCOMPARE (min, 0);
219  }
220 
221  void OralTest_SimpleRecord::testSimpleRecordInsertSelectMax ()
222  {
223  auto adapted = PrepareRecords<SimpleRecord> (MakeDatabase ());
224  const auto max = adapted->Select (sph::max<&SimpleRecord::ID_>);
225  QCOMPARE (max, 2);
226  }
227 
228  void OralTest_SimpleRecord::testSimpleRecordInsertSelectMinPlusMax ()
229  {
230  auto adapted = PrepareRecords<SimpleRecord> (MakeDatabase ());
231  const auto minMax = adapted->Select (sph::min<&SimpleRecord::ID_> + sph::max<&SimpleRecord::ID_>);
232  QCOMPARE (minMax, (std::tuple { 0, 2 }));
233  }
234 
235  void OralTest_SimpleRecord::testSimpleRecordInsertSelectValuePlusMinPlusMax ()
236  {
237  auto adapted = oral::AdaptPtr<SimpleRecord, OralFactory> (MakeDatabase ());
238  for (int i = 0; i < 3; ++i)
239  adapted->Insert ({ i, "0" });
240  for (int i = 3; i < 6; ++i)
241  adapted->Insert ({ i, "1" });
242 
243  const auto allMinMax = adapted->Select.Build ()
244  .Select (sph::fields<&SimpleRecord::Value_> + sph::min<&SimpleRecord::ID_> + sph::max<&SimpleRecord::ID_>)
245  .Group (oral::GroupBy<&SimpleRecord::Value_>)
246  ();
247  QCOMPARE (allMinMax, (QList<std::tuple<QString, int, int>> { { { "0" }, 0, 2 }, { { "1" }, 3, 5 } }));
248  }
249 
250  void OralTest_SimpleRecord::testSimpleRecordInsertSelectAllPlusMinPlusMax ()
251  {
252  auto adapted = PrepareRecords<SimpleRecord> (MakeDatabase (), 2);
253  const auto allMinMax = adapted->Select.Build ()
254  .Select (sph::all + sph::min<&SimpleRecord::ID_> + sph::max<&SimpleRecord::ID_>)
256  ();
257  QCOMPARE (allMinMax, (QList<std::tuple<SimpleRecord, int, int>> { { { 0, "0" }, 0, 0 }, { { 1, "1" }, 1, 1 } }));
258  }
259 
260  void OralTest_SimpleRecord::testSimpleRecordInsertSelectLike ()
261  {
262  using namespace oral::infix;
263 
264  auto adapted = Util::oral::AdaptPtr<SimpleRecord, OralFactory> (MakeDatabase ());
265  adapted->Insert ({ 0, "foo" });
266  adapted->Insert ({ 1, "bar" });
267  adapted->Insert ({ 2, "foobar" });
268  const auto& list = adapted->Select (sph::f<&SimpleRecord::Value_> |like| QString { "%oo%" });
269  QCOMPARE (list, (QList<SimpleRecord> { { 0, "foo" }, { 2, "foobar" } }));
270  }
271 
272  void OralTest_SimpleRecord::testSimpleRecordUpdate ()
273  {
274  auto adapted = PrepareRecords<SimpleRecord> (MakeDatabase ());
275  adapted->Update ({ 0, "meh" });
276  const auto updated = adapted->Select (sph::f<&SimpleRecord::ID_> == 0);
277  QCOMPARE (updated, (QList<SimpleRecord> { { 0, "meh" } }));
278  }
279 
280  void OralTest_SimpleRecord::testSimpleRecordUpdateExprTree ()
281  {
282  auto adapted = PrepareRecords<SimpleRecord> (MakeDatabase ());
283  adapted->Update (sph::f<&SimpleRecord::Value_> = QString { "meh" }, sph::f<&SimpleRecord::ID_> == 0);
284  const auto updated = adapted->Select (sph::f<&SimpleRecord::ID_> == 0);
285  QCOMPARE (updated, (QList<SimpleRecord> { { 0, "meh" } }));
286  }
287 
288  void OralTest_SimpleRecord::testSimpleRecordUpdateMultiExprTree ()
289  {
290  auto adapted = PrepareRecords<SimpleRecord> (MakeDatabase ());
291  adapted->Update ((sph::f<&SimpleRecord::Value_> = QString { "meh" }, sph::f<&SimpleRecord::ID_> = 10),
292  sph::f<&SimpleRecord::ID_> == 0);
293  const auto updated = adapted->Select (sph::f<&SimpleRecord::ID_> == 10);
294  QCOMPARE (updated, (QList<SimpleRecord> { { 10, "meh" } }));
295  }
296 }
297 }
lco::PKey< int, lco::NoAutogen > ID_
Definition: simplerecord.h:36
static struct LC::Util::oral::InsertAction::IgnoreTag Ignore
constexpr detail::AggregateType< detail::AggregateFunction::Count, Ptr > count
Definition: oral.h:969
constexpr detail::OrderBy< Orders... > OrderBy
Definition: oral.h:979
constexpr detail::AggregateType< detail::AggregateFunction::Max, Ptr > max
Definition: oral.h:975
QString Value_
Definition: simplerecord.h:37
QSqlDatabase MakeDatabase(const QString &name=":memory:")
Definition: common.h:112
constexpr detail::InfixBinary< detail::ExprType::Like > like
Definition: oral.h:829
constexpr detail::AggregateType< detail::AggregateFunction::Min, Ptr > min
Definition: oral.h:972
constexpr detail::SelectWhole all
Definition: oral.h:960
Definition: constants.h:35