LeechCraft 0.6.70-14794-g33744ae6ce
Modular cross-platform feature rich live environment.
Loading...
Searching...
No Matches
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
12QTEST_GUILESS_MAIN (LC::Util::OralTest)
13
15{
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
36struct 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
56TOSTRING (NoPKeyRecord)
57
58struct 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
83BOOST_FUSION_ADAPT_STRUCT (NonInPlaceConstructibleRecord,
84 ID_,
85 Value_)
86
87TOSTRING (NonInPlaceConstructibleRecord)
88
89struct 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
106 using Constraints = lco::Constraints<
109 >;
110};
111
112BOOST_FUSION_ADAPT_STRUCT (ComplexConstraintsRecord,
113 ID_,
114 Value_,
115 Age_,
116 Weight_)
117
118TOSTRING (ComplexConstraintsRecord)
119
120namespace LC
121{
122namespace 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
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
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_,
243 &ComplexConstraintsRecord::Value_
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}
#define TOSTRING(n)
Definition: common.h:52
QSqlDatabase MakeDatabase(const QString &name=":memory:")
Definition: common.h:73
Definition: constants.h:15
BOOST_FUSION_ADAPT_STRUCT(AutogenPKeyRecord, ID_, Value_) struct NoPKeyRecord
Definition: oraltest.cpp:30
const QVariant Value_
Definition: plotitem.cpp:74
auto AsTuple() const
Definition: oraltest.cpp:24
lco::PKey< int > ID_
Definition: oraltest.cpp:16
static QString ClassName()
Definition: oraltest.cpp:19