45 namespace sph = oral::sph;
47 void OralTest_SimpleRecord::testSimpleRecordInsertSelect ()
50 const auto& list = adapted->Select ();
54 void OralTest_SimpleRecord::testSimpleRecordInsertReplaceSelect ()
59 for (
int i = 0; i < 3; ++i)
62 const auto& list = adapted->Select ();
66 void OralTest_SimpleRecord::testSimpleRecordInsertIgnoreSelect ()
71 for (
int i = 0; i < 3; ++i)
74 const auto& list = adapted->Select ();
78 void OralTest_SimpleRecord::testSimpleRecordInsertSelectByPos ()
81 const auto& list = adapted->Select (sph::f<&SimpleRecord::ID_> == 1);
85 void OralTest_SimpleRecord::testSimpleRecordInsertSelectByPos2 ()
88 const auto& list = adapted->Select (sph::f<&SimpleRecord::ID_> < 2);
92 void OralTest_SimpleRecord::testSimpleRecordInsertSelectByPos3 ()
95 const auto& list = adapted->Select (sph::f<&SimpleRecord::ID_> < 2 && sph::f<&SimpleRecord::Value_> == QString {
"1" });
99 void OralTest_SimpleRecord::testSimpleRecordInsertSelectOneByPos ()
102 const auto& single = adapted->SelectOne (sph::f<&SimpleRecord::ID_> == 1);
103 QCOMPARE (single, (std::optional<SimpleRecord> { { 1,
"1" } }));
106 void OralTest_SimpleRecord::testSimpleRecordInsertSelectByFields ()
109 const auto& list = adapted->Select (sph::f<&SimpleRecord::ID_> == 1);
113 void OralTest_SimpleRecord::testSimpleRecordInsertSelectByFields2 ()
116 const auto& list = adapted->Select (sph::f<&SimpleRecord::ID_> < 2);
120 void OralTest_SimpleRecord::testSimpleRecordInsertSelectByFields3 ()
123 const auto& list = adapted->Select (sph::f<&SimpleRecord::ID_> < 2 && sph::f<&SimpleRecord::Value_> == QString {
"1" });
127 void OralTest_SimpleRecord::testSimpleRecordInsertSelectOneByFields ()
130 const auto& single = adapted->SelectOne (sph::f<&SimpleRecord::ID_> == 1);
131 QCOMPARE (single, (std::optional<SimpleRecord> { { 1,
"1" } }));
134 void OralTest_SimpleRecord::testSimpleRecordInsertSelectSingleFieldByFields ()
137 const auto& list = adapted->Select (sph::fields<&SimpleRecord::Value_>, sph::f<&SimpleRecord::ID_> < 2);
141 void OralTest_SimpleRecord::testSimpleRecordInsertSelectFieldsByFields ()
144 const auto& list = adapted->Select (sph::fields<&SimpleRecord::ID_, &SimpleRecord::Value_>, sph::f<&SimpleRecord::ID_> < 2);
145 QCOMPARE (list, (
QList<std::tuple<int, QString>> { { 0,
"0" }, { 1,
"1" } }));
148 void OralTest_SimpleRecord::testSimpleRecordInsertSelectFieldsByFieldsOrderAsc ()
151 const auto& list = adapted->Select (sph::fields<&SimpleRecord::ID_, &SimpleRecord::Value_>,
152 sph::f<&SimpleRecord::ID_> < 2,
154 QCOMPARE (list, (
QList<std::tuple<int, QString>> { { 0,
"0" }, { 1,
"1" } }));
157 void OralTest_SimpleRecord::testSimpleRecordInsertSelectFieldsByFieldsOrderDesc ()
160 const auto& list = adapted->Select (sph::fields<&SimpleRecord::ID_, &SimpleRecord::Value_>,
161 sph::f<&SimpleRecord::ID_> < 2,
163 QCOMPARE (list, (
QList<std::tuple<int, QString>> { { 1,
"1" }, { 0,
"0" } }));
166 void OralTest_SimpleRecord::testSimpleRecordInsertSelectFieldsByFieldsOrderManyAsc ()
169 const auto& list = adapted->Select (sph::fields<&SimpleRecord::ID_, &SimpleRecord::Value_>,
170 sph::f<&SimpleRecord::ID_> < 2,
171 oral::OrderBy<sph::asc<&SimpleRecord::Value_>, sph::desc<&SimpleRecord::ID_>>);
172 QCOMPARE (list, (
QList<std::tuple<int, QString>> { { 0,
"0" }, { 1,
"1" } }));
175 void OralTest_SimpleRecord::testSimpleRecordInsertSelectFieldsByFieldsOrderManyDesc ()
178 const auto& list = adapted->Select (sph::fields<&SimpleRecord::ID_, &SimpleRecord::Value_>,
179 sph::f<&SimpleRecord::ID_> < 2,
180 oral::OrderBy<sph::desc<&SimpleRecord::Value_>, sph::asc<&SimpleRecord::ID_>>);
181 QCOMPARE (list, (
QList<std::tuple<int, QString>> { { 1,
"1" }, { 0,
"0" } }));
184 void OralTest_SimpleRecord::testSimpleRecordInsertSelectNoOffsetLimit ()
187 const auto& list = adapted->Select.Build ().Limit (2) ();
200 void OralTest_SimpleRecord::testSimpleRecordInsertSelectOffsetLimit ()
203 const auto& list = adapted->Select.Build ().Offset (5).Limit (2) ();
207 void OralTest_SimpleRecord::testSimpleRecordInsertSelectBuilderAndWhere ()
210 const auto& list = adapted->Select.Build ()
211 .Where (sph::f<&SimpleRecord::ID_> < 2)
212 .AndWhere (sph::f<&SimpleRecord::Value_> == QString {
"1" })
217 void OralTest_SimpleRecord::testSimpleRecordInsertSelectBuilderAndWhereMultiple ()
220 const auto& list = adapted->Select.Build ()
221 .Where (sph::f<&SimpleRecord::ID_> < 8)
222 .AndWhere (sph::f<&SimpleRecord::ID_> >= 2)
223 .AndWhere (sph::f<&SimpleRecord::Value_> == QString {
"5" })
228 void OralTest_SimpleRecord::testSimpleRecordInsertSelectCount ()
231 const auto count = adapted->Select (sph::count<>);
235 void OralTest_SimpleRecord::testSimpleRecordInsertSelectCountByFields ()
238 const auto count = adapted->Select (sph::count<>, sph::f<&SimpleRecord::ID_> < 2);
242 void OralTest_SimpleRecord::testSimpleRecordInsertSelectMin ()
245 const auto min = adapted->Select (sph::min<&SimpleRecord::ID_>);
249 void OralTest_SimpleRecord::testSimpleRecordInsertSelectMax ()
252 const auto max = adapted->Select (sph::max<&SimpleRecord::ID_>);
256 void OralTest_SimpleRecord::testSimpleRecordInsertSelectMinPlusMax ()
259 const auto minMax = adapted->Select (sph::min<&SimpleRecord::ID_> + sph::max<&SimpleRecord::ID_>);
260 QCOMPARE (minMax, (std::tuple { 0, 2 }));
263 void OralTest_SimpleRecord::testSimpleRecordInsertSelectMinPlusMaxEmpty ()
266 const auto minMax = adapted->Select (sph::min<&SimpleRecord::ID_> + sph::max<&SimpleRecord::ID_>);
267 QCOMPARE (minMax, (std::tuple { std::nullopt, std::nullopt }));
270 void OralTest_SimpleRecord::testSimpleRecordInsertSelectValuePlusMinPlusMax ()
273 for (
int i = 0; i < 3; ++i)
274 adapted->Insert ({ i,
"0" });
275 for (
int i = 3; i < 6; ++i)
276 adapted->Insert ({ i,
"1" });
278 const auto allMinMax = adapted->Select.Build ()
279 .Select (sph::fields<&SimpleRecord::Value_> + sph::min<&SimpleRecord::ID_> + sph::max<&SimpleRecord::ID_>)
282 QCOMPARE (allMinMax, (
QList<std::tuple<QString, std::optional<int>, std::optional<int>>>
289 void OralTest_SimpleRecord::testSimpleRecordInsertSelectAllPlusMinPlusMax ()
292 const auto allMinMax = adapted->Select.Build ()
293 .Select (sph::all + sph::min<&SimpleRecord::ID_> + sph::max<&SimpleRecord::ID_>)
296 QCOMPARE (allMinMax, (
QList<std::tuple<
SimpleRecord, std::optional<int>, std::optional<int>>>
298 { { 0,
"0" }, 0, 0 }, { { 1,
"1" }, 1, 1 }
302 void OralTest_SimpleRecord::testSimpleRecordInsertSelectLike ()
304 using namespace oral::infix;
307 adapted->Insert ({ 0,
"foo" });
308 adapted->Insert ({ 1,
"bar" });
309 adapted->Insert ({ 2,
"foobar" });
310 const auto& list = adapted->Select (sph::f<&SimpleRecord::Value_> |like| QString {
"%oo%" });
314 void OralTest_SimpleRecord::testSimpleRecordUpdate ()
317 adapted->Update ({ 0,
"meh" });
318 const auto updated = adapted->Select (sph::f<&SimpleRecord::ID_> == 0);
322 void OralTest_SimpleRecord::testSimpleRecordUpdateExprTree ()
325 adapted->Update (sph::f<&SimpleRecord::Value_> = QString {
"meh" }, sph::f<&SimpleRecord::ID_> == 0);
326 const auto updated = adapted->Select (sph::f<&SimpleRecord::ID_> == 0);
330 void OralTest_SimpleRecord::testSimpleRecordUpdateMultiExprTree ()
333 adapted->Update ((sph::f<&SimpleRecord::Value_> = QString {
"meh" }, sph::f<&SimpleRecord::ID_> = 10),
334 sph::f<&SimpleRecord::ID_> == 0);
335 const auto updated = adapted->Select (sph::f<&SimpleRecord::ID_> == 10);
339 auto PrepareMessageLikeRecords (QSqlDatabase db)
343 adapted->Insert ({ {}, 300,
"c" });
344 adapted->Insert ({ {}, 400,
"d" });
345 adapted->Insert ({ {}, 500,
"e" });
346 adapted->Insert ({ {}, 100,
"a" });
347 adapted->Insert ({ {}, 200,
"b" });
351 void OralTest_SimpleRecord::testTupleCompareEq ()
354 auto adapted = PrepareMessageLikeRecords (
MakeDatabase ());
355 const auto& list = adapted->Select (sph::tuple<&Rec::TS_, &Rec::Id_> == std::tuple (300, 1));
356 QCOMPARE (list, (
QList<Rec> { { 1, 300,
"c" } }));
359 void OralTest_SimpleRecord::testTupleCompareLt ()
362 auto adapted = PrepareMessageLikeRecords (
MakeDatabase ());
363 const auto& list = adapted->Select.Build ()
364 .Where (sph::tuple<&Rec::TS_, &Rec::Id_> < std::tuple (300, 1))
365 .Order (
oral::OrderBy<sph::asc<&Rec::TS_>, sph::asc<&Rec::Id_>>)
367 QCOMPARE (list, (
QList<Rec> { { 4, 100,
"a" }, { 5, 200,
"b" } }));
370 void OralTest_SimpleRecord::testTupleCompareGt ()
373 auto adapted = PrepareMessageLikeRecords (
MakeDatabase ());
374 const auto& list = adapted->Select.Build ()
375 .Where (sph::tuple<&Rec::TS_, &Rec::Id_> > std::tuple (300, 1))
376 .Order (
oral::OrderBy<sph::asc<&Rec::TS_>, sph::asc<&Rec::Id_>>)
378 QCOMPARE (list, (
QList<Rec> { { 2, 400,
"d" }, { 3, 500,
"e" } }));
381 void OralTest_SimpleRecord::testTupleCompareLte ()
384 auto adapted = PrepareMessageLikeRecords (
MakeDatabase ());
385 const auto& list = adapted->Select.Build ()
386 .Where (sph::tuple<&Rec::TS_, &Rec::Id_> <= std::tuple (300, 1))
387 .Order (
oral::OrderBy<sph::asc<&Rec::TS_>, sph::asc<&Rec::Id_>>)
389 QCOMPARE (list, (
QList<Rec> { { 4, 100,
"a" }, { 5, 200,
"b" }, { 1, 300,
"c" } }));
392 void OralTest_SimpleRecord::testTupleCompareGte ()
395 auto adapted = PrepareMessageLikeRecords (
MakeDatabase ());
396 const auto& list = adapted->Select.Build ()
397 .Where (sph::tuple<&Rec::TS_, &Rec::Id_> >= std::tuple (300, 1))
398 .Order (
oral::OrderBy<sph::asc<&Rec::TS_>, sph::asc<&Rec::Id_>>)
400 QCOMPARE (list, (
QList<Rec> { { 1, 300,
"c" }, { 2, 400,
"d" }, { 3, 500,
"e" } }));
403 void OralTest_SimpleRecord::testTupleCompareIsNotComponentwise ()
406 auto adapted = PrepareMessageLikeRecords (
MakeDatabase ());
407 const auto& before = adapted->Select (sph::tuple<&Rec::TS_, &Rec::Id_> < std::tuple (200, 5));
408 QCOMPARE (before, (
QList<Rec> { { 4, 100,
"a" } }));
410 const auto& after = adapted->Select.Build ()
411 .Where (sph::tuple<&Rec::TS_, &Rec::Id_> > std::tuple (200, 5))
412 .Order (
oral::OrderBy<sph::asc<&Rec::TS_>, sph::asc<&Rec::Id_>>)
414 QCOMPARE (after, (
QList<Rec> { { 1, 300,
"c" }, { 2, 400,
"d" }, { 3, 500,
"e" } }));
417 void OralTest_SimpleRecord::testTupleCompareInBuilder ()
420 auto adapted = PrepareMessageLikeRecords (
MakeDatabase ());
421 auto page = adapted->Select.Build ()
422 .Where (sph::tuple<&Rec::TS_, &Rec::Id_> < std::tuple (400, 2))
423 .Order (
oral::OrderBy<sph::desc<&Rec::TS_>, sph::desc<&Rec::Id_>>)
426 QCOMPARE (page, (
QList<Rec> { { 1, 300,
"c" }, { 5, 200,
"b" } }));