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