##// END OF EJS Templates
Add correction for TestVariable
perrinel -
r825:3b8725ae29c0
parent child
Show More
@@ -1,409 +1,409
1 1 #include <Variable/Variable.h>
2 2
3 3 #include <Data/ScalarSeries.h>
4 4
5 5 #include <QObject>
6 6 #include <QtTest>
7 7
8 8 #include <memory>
9 9
10 10 namespace {
11 11
12 12 /// Generates a date in double
13 13 auto date = [](int year, int month, int day, int hours, int minutes, int seconds) {
14 14 return DateUtils::secondsSinceEpoch(
15 15 QDateTime{{year, month, day}, {hours, minutes, seconds}, Qt::UTC});
16 16 };
17 17
18 18 /// Generates a series of test data for a range
19 19 std::shared_ptr<ScalarSeries> dataSeries(const SqpRange &range)
20 20 {
21 21 auto xAxisData = std::vector<double>{};
22 22 auto valuesData = std::vector<double>{};
23 23
24 24 auto value = 0;
25 25 for (auto x = range.m_TStart; x <= range.m_TEnd; ++x, ++value) {
26 26 xAxisData.push_back(x);
27 27 valuesData.push_back(value);
28 28 }
29 29
30 30 return std::make_shared<ScalarSeries>(std::move(xAxisData), std::move(valuesData), Unit{},
31 31 Unit{});
32 32 }
33 33
34 34 } // namespace
35 35
36 36 Q_DECLARE_METATYPE(std::shared_ptr<ScalarSeries>)
37 37
38 38 class TestVariable : public QObject {
39 39 Q_OBJECT
40 40
41 41 private slots:
42 42 void testClone_data();
43 43 void testClone();
44 44
45 45 void testNotInCacheRangeList();
46 46 void testInCacheRangeList();
47 47
48 48 void testNbPoints_data();
49 49 void testNbPoints();
50 50
51 51 void testRealRange_data();
52 52 void testRealRange();
53 53 };
54 54
55 55 void TestVariable::testClone_data()
56 56 {
57 57 // ////////////// //
58 58 // Test structure //
59 59 // ////////////// //
60 60
61 61 QTest::addColumn<QString>("name");
62 62 QTest::addColumn<QVariantHash>("metadata");
63 63 QTest::addColumn<SqpRange>("range");
64 64 QTest::addColumn<SqpRange>("cacheRange");
65 65 QTest::addColumn<std::shared_ptr<ScalarSeries> >("dataSeries");
66 66
67 67 // ////////// //
68 68 // Test cases //
69 69 // ////////// //
70 70
71 71 auto cacheRange = SqpRange{date(2017, 1, 1, 12, 0, 0), date(2017, 1, 1, 13, 0, 0)};
72 72 QTest::newRow("clone1") << QStringLiteral("var1")
73 73 << QVariantHash{{"data1", 1}, {"data2", "abc"}}
74 74 << SqpRange{date(2017, 1, 1, 12, 30, 0), (date(2017, 1, 1, 12, 45, 0))}
75 75 << cacheRange << dataSeries(cacheRange);
76 76 }
77 77
78 78 void TestVariable::testClone()
79 79 {
80 80 // Creates variable
81 81 QFETCH(QString, name);
82 82 QFETCH(QVariantHash, metadata);
83 83 QFETCH(SqpRange, range);
84 84 QFETCH(SqpRange, cacheRange);
85 85 QFETCH(std::shared_ptr<ScalarSeries>, dataSeries);
86 86
87 87 Variable variable{name, metadata};
88 88 variable.setRange(range);
89 89 variable.setCacheRange(cacheRange);
90 90 variable.mergeDataSeries(dataSeries);
91 91
92 92 // Clones variable
93 93 auto clone = variable.clone();
94 94
95 95 // Checks cloned variable's state
96 96 QCOMPARE(clone->name(), name);
97 97 QCOMPARE(clone->metadata(), metadata);
98 98 QCOMPARE(clone->range(), range);
99 99 QCOMPARE(clone->cacheRange(), cacheRange);
100 100
101 101 // Compares data series
102 102 if (dataSeries != nullptr) {
103 103 QVERIFY(clone->dataSeries() != nullptr);
104 104 QVERIFY(std::equal(dataSeries->cbegin(), dataSeries->cend(), clone->dataSeries()->cbegin(),
105 105 clone->dataSeries()->cend(), [](const auto &it1, const auto &it2) {
106 106 return it1.x() == it2.x() && it1.value() == it2.value();
107 107 }));
108 108 }
109 109 else {
110 110 QVERIFY(clone->dataSeries() == nullptr);
111 111 }
112 112 }
113 113
114 114 void TestVariable::testNotInCacheRangeList()
115 115 {
116 116 auto varRS = QDateTime{QDate{2017, 01, 01}, QTime{2, 3, 20, 0}};
117 117 auto varRE = QDateTime{QDate{2017, 01, 01}, QTime{2, 3, 40, 0}};
118 118
119 119 auto sqpR = SqpRange{DateUtils::secondsSinceEpoch(varRS), DateUtils::secondsSinceEpoch(varRE)};
120 120
121 121 auto varCRS = QDateTime{QDate{2017, 01, 01}, QTime{2, 3, 0, 0}};
122 122 auto varCRE = QDateTime{QDate{2017, 01, 01}, QTime{2, 4, 0, 0}};
123 123
124 124 auto sqpCR
125 125 = SqpRange{DateUtils::secondsSinceEpoch(varCRS), DateUtils::secondsSinceEpoch(varCRE)};
126 126
127 127 Variable var{"Var test"};
128 128 var.setRange(sqpR);
129 129 var.setCacheRange(sqpCR);
130 130
131 131 // 1: [ts,te] < varTS
132 132 auto ts = QDateTime{QDate{2017, 01, 01}, QTime{2, 0, 0, 0}};
133 133 auto te = QDateTime{QDate{2017, 01, 01}, QTime{2, 1, 0, 0}};
134 134 auto sqp = SqpRange{DateUtils::secondsSinceEpoch(ts), DateUtils::secondsSinceEpoch(te)};
135 135
136 136 auto notInCach = var.provideNotInCacheRangeList(sqp);
137 137
138 138 QCOMPARE(notInCach.size(), 1);
139 139
140 140 auto notInCachRange = notInCach.first();
141 141
142 142 QCOMPARE(notInCachRange.m_TStart, DateUtils::secondsSinceEpoch(ts));
143 143 QCOMPARE(notInCachRange.m_TEnd, DateUtils::secondsSinceEpoch(te));
144 144
145 145 // 2: ts < varTS < te < varTE
146 146 ts = QDateTime{QDate{2017, 01, 01}, QTime{2, 0, 0, 0}};
147 147 te = QDateTime{QDate{2017, 01, 01}, QTime{2, 3, 30, 0}};
148 148 sqp = SqpRange{DateUtils::secondsSinceEpoch(ts), DateUtils::secondsSinceEpoch(te)};
149 149 notInCach = var.provideNotInCacheRangeList(sqp);
150 150 QCOMPARE(notInCach.size(), 1);
151 151 notInCachRange = notInCach.first();
152 152 QCOMPARE(notInCachRange.m_TStart, DateUtils::secondsSinceEpoch(ts));
153 153 QCOMPARE(notInCachRange.m_TEnd, DateUtils::secondsSinceEpoch(varCRS));
154 154
155 155 // 3: varTS < ts < te < varTE
156 156 ts = QDateTime{QDate{2017, 01, 01}, QTime{2, 3, 20, 0}};
157 157 te = QDateTime{QDate{2017, 01, 01}, QTime{2, 3, 30, 0}};
158 158 sqp = SqpRange{DateUtils::secondsSinceEpoch(ts), DateUtils::secondsSinceEpoch(te)};
159 159 notInCach = var.provideNotInCacheRangeList(sqp);
160 160 QCOMPARE(notInCach.size(), 0);
161 161
162 162
163 163 // 4: varTS < ts < varTE < te
164 164 ts = QDateTime{QDate{2017, 01, 01}, QTime{2, 3, 20, 0}};
165 165 te = QDateTime{QDate{2017, 01, 01}, QTime{2, 5, 0, 0}};
166 166 sqp = SqpRange{DateUtils::secondsSinceEpoch(ts), DateUtils::secondsSinceEpoch(te)};
167 167 notInCach = var.provideNotInCacheRangeList(sqp);
168 168 QCOMPARE(notInCach.size(), 1);
169 169 notInCachRange = notInCach.first();
170 170 QCOMPARE(notInCachRange.m_TStart, DateUtils::secondsSinceEpoch(varCRE));
171 171 QCOMPARE(notInCachRange.m_TEnd, DateUtils::secondsSinceEpoch(te));
172 172
173 173 // 5: varTS < varTE < ts < te
174 174 ts = QDateTime{QDate{2017, 01, 01}, QTime{2, 4, 20, 0}};
175 175 te = QDateTime{QDate{2017, 01, 01}, QTime{2, 5, 0, 0}};
176 176 sqp = SqpRange{DateUtils::secondsSinceEpoch(ts), DateUtils::secondsSinceEpoch(te)};
177 177 notInCach = var.provideNotInCacheRangeList(sqp);
178 178 QCOMPARE(notInCach.size(), 1);
179 179 notInCachRange = notInCach.first();
180 180 QCOMPARE(notInCachRange.m_TStart, DateUtils::secondsSinceEpoch(ts));
181 181 QCOMPARE(notInCachRange.m_TEnd, DateUtils::secondsSinceEpoch(te));
182 182
183 183 // 6: ts <varTS < varTE < te
184 184 ts = QDateTime{QDate{2017, 01, 01}, QTime{2, 1, 0, 0}};
185 185 te = QDateTime{QDate{2017, 01, 01}, QTime{2, 5, 0, 0}};
186 186 sqp = SqpRange{DateUtils::secondsSinceEpoch(ts), DateUtils::secondsSinceEpoch(te)};
187 187 notInCach = var.provideNotInCacheRangeList(sqp);
188 188 QCOMPARE(notInCach.size(), 2);
189 189 notInCachRange = notInCach.first();
190 190 QCOMPARE(notInCachRange.m_TStart, DateUtils::secondsSinceEpoch(ts));
191 191 QCOMPARE(notInCachRange.m_TEnd, DateUtils::secondsSinceEpoch(varCRS));
192 192 notInCachRange = notInCach[1];
193 193 QCOMPARE(notInCachRange.m_TStart, DateUtils::secondsSinceEpoch(varCRE));
194 194 QCOMPARE(notInCachRange.m_TEnd, DateUtils::secondsSinceEpoch(te));
195 195 }
196 196
197 197
198 198 void TestVariable::testInCacheRangeList()
199 199 {
200 200 auto varRS = QDateTime{QDate{2017, 01, 01}, QTime{2, 3, 20, 0}};
201 201 auto varRE = QDateTime{QDate{2017, 01, 01}, QTime{2, 3, 40, 0}};
202 202
203 203 auto sqpR = SqpRange{DateUtils::secondsSinceEpoch(varRS), DateUtils::secondsSinceEpoch(varRE)};
204 204
205 205 auto varCRS = QDateTime{QDate{2017, 01, 01}, QTime{2, 3, 0, 0}};
206 206 auto varCRE = QDateTime{QDate{2017, 01, 01}, QTime{2, 4, 0, 0}};
207 207 auto sqpCR
208 208 = SqpRange{DateUtils::secondsSinceEpoch(varCRS), DateUtils::secondsSinceEpoch(varCRE)};
209 209
210 210 Variable var{"Var test"};
211 211 var.setRange(sqpR);
212 212 var.setCacheRange(sqpCR);
213 213
214 214 // 1: [ts,te] < varTS
215 215 auto ts = QDateTime{QDate{2017, 01, 01}, QTime{2, 0, 0, 0}};
216 216 auto te = QDateTime{QDate{2017, 01, 01}, QTime{2, 1, 0, 0}};
217 217 auto sqp = SqpRange{DateUtils::secondsSinceEpoch(ts), DateUtils::secondsSinceEpoch(te)};
218 218
219 219 auto notInCach = var.provideInCacheRangeList(sqp);
220 220
221 221 QCOMPARE(notInCach.size(), 0);
222 222
223 223 // 2: ts < varTS < te < varTE
224 224 ts = QDateTime{QDate{2017, 01, 01}, QTime{2, 0, 0, 0}};
225 225 te = QDateTime{QDate{2017, 01, 01}, QTime{2, 3, 30, 0}};
226 226 sqp = SqpRange{DateUtils::secondsSinceEpoch(ts), DateUtils::secondsSinceEpoch(te)};
227 227 notInCach = var.provideInCacheRangeList(sqp);
228 228 QCOMPARE(notInCach.size(), 1);
229 229 auto notInCachRange = notInCach.first();
230 230 QCOMPARE(notInCachRange.m_TStart, DateUtils::secondsSinceEpoch(varCRS));
231 231 QCOMPARE(notInCachRange.m_TEnd, DateUtils::secondsSinceEpoch(te));
232 232
233 233 // 3: varTS < ts < te < varTE
234 234 ts = QDateTime{QDate{2017, 01, 01}, QTime{2, 3, 20, 0}};
235 235 te = QDateTime{QDate{2017, 01, 01}, QTime{2, 3, 30, 0}};
236 236 sqp = SqpRange{DateUtils::secondsSinceEpoch(ts), DateUtils::secondsSinceEpoch(te)};
237 237 notInCach = var.provideInCacheRangeList(sqp);
238 238 QCOMPARE(notInCach.size(), 1);
239 239 notInCachRange = notInCach.first();
240 240 QCOMPARE(notInCachRange.m_TStart, DateUtils::secondsSinceEpoch(ts));
241 241 QCOMPARE(notInCachRange.m_TEnd, DateUtils::secondsSinceEpoch(te));
242 242
243 243 // 4: varTS < ts < varTE < te
244 244 ts = QDateTime{QDate{2017, 01, 01}, QTime{2, 3, 20, 0}};
245 245 te = QDateTime{QDate{2017, 01, 01}, QTime{2, 5, 0, 0}};
246 246 sqp = SqpRange{DateUtils::secondsSinceEpoch(ts), DateUtils::secondsSinceEpoch(te)};
247 247 notInCach = var.provideInCacheRangeList(sqp);
248 248 QCOMPARE(notInCach.size(), 1);
249 249 notInCachRange = notInCach.first();
250 250 QCOMPARE(notInCachRange.m_TStart, DateUtils::secondsSinceEpoch(ts));
251 251 QCOMPARE(notInCachRange.m_TEnd, DateUtils::secondsSinceEpoch(varCRE));
252 252
253 253 // 5: varTS < varTE < ts < te
254 254 ts = QDateTime{QDate{2017, 01, 01}, QTime{2, 4, 20, 0}};
255 255 te = QDateTime{QDate{2017, 01, 01}, QTime{2, 5, 0, 0}};
256 256 sqp = SqpRange{DateUtils::secondsSinceEpoch(ts), DateUtils::secondsSinceEpoch(te)};
257 257 notInCach = var.provideInCacheRangeList(sqp);
258 258 QCOMPARE(notInCach.size(), 0);
259 259
260 260 // 6: ts <varTS < varTE < te
261 261 ts = QDateTime{QDate{2017, 01, 01}, QTime{2, 1, 0, 0}};
262 262 te = QDateTime{QDate{2017, 01, 01}, QTime{2, 5, 0, 0}};
263 263 sqp = SqpRange{DateUtils::secondsSinceEpoch(ts), DateUtils::secondsSinceEpoch(te)};
264 264 notInCach = var.provideInCacheRangeList(sqp);
265 265 QCOMPARE(notInCach.size(), 1);
266 266 notInCachRange = notInCach.first();
267 267 QCOMPARE(notInCachRange.m_TStart, DateUtils::secondsSinceEpoch(varCRS));
268 268 QCOMPARE(notInCachRange.m_TEnd, DateUtils::secondsSinceEpoch(varCRE));
269 269 }
270 270
271 271 namespace {
272 272
273 273 /// Struct used to represent an operation for @sa TestVariable::testNbPoints()
274 274 struct NbPointsOperation {
275 275 SqpRange m_CacheRange; /// Range to set for the variable
276 276 std::shared_ptr<ScalarSeries> m_DataSeries; /// Series to merge in the variable
277 277 int m_ExpectedNbPoints; /// Number of points in the variable expected after operation
278 278 };
279 279
280 280 using NbPointsOperations = std::vector<NbPointsOperation>;
281 281
282 282 } // namespace
283 283
284 284 Q_DECLARE_METATYPE(NbPointsOperations)
285 285
286 286 void TestVariable::testNbPoints_data()
287 287 {
288 288 // ////////////// //
289 289 // Test structure //
290 290 // ////////////// //
291 291
292 292 QTest::addColumn<NbPointsOperations>("operations");
293 293
294 294 // ////////// //
295 295 // Test cases //
296 296 // ////////// //
297 297 NbPointsOperations operations{};
298 298
299 299 // Sets cache range (expected nb points = series xAxis data + series values data)
300 300 auto cacheRange = SqpRange{date(2017, 1, 1, 12, 0, 0), date(2017, 1, 1, 12, 0, 9)};
301 301 operations.push_back({cacheRange, dataSeries(cacheRange), 20});
302 302
303 303 // Doubles cache but don't add data series (expected nb points don't change)
304 304 cacheRange = SqpRange{date(2017, 1, 1, 12, 0, 0), date(2017, 1, 1, 12, 0, 19)};
305 operations.push_back({cacheRange, nullptr, 20});
305 operations.push_back({cacheRange, dataSeries(INVALID_RANGE), 20});
306 306
307 307 // Doubles cache and data series (expected nb points change)
308 308 cacheRange = SqpRange{date(2017, 1, 1, 12, 0, 0), date(2017, 1, 1, 12, 0, 19)};
309 309 operations.push_back({cacheRange, dataSeries(cacheRange), 40});
310 310
311 311 // Decreases cache (expected nb points decreases as the series is purged)
312 312 cacheRange = SqpRange{date(2017, 1, 1, 12, 0, 5), date(2017, 1, 1, 12, 0, 9)};
313 operations.push_back({cacheRange, nullptr, 10});
313 operations.push_back({cacheRange, dataSeries(INVALID_RANGE), 10});
314 314
315 315 QTest::newRow("nbPoints1") << operations;
316 316 }
317 317
318 318 void TestVariable::testNbPoints()
319 319 {
320 320 // Creates variable
321 321 Variable variable{"var"};
322 322 QCOMPARE(variable.nbPoints(), 0);
323 323
324 324 QFETCH(NbPointsOperations, operations);
325 325 for (const auto &operation : operations) {
326 326 // Sets cache range and merge data series
327 327 variable.setCacheRange(operation.m_CacheRange);
328 328 if (operation.m_DataSeries != nullptr) {
329 329 variable.mergeDataSeries(operation.m_DataSeries);
330 330 }
331 331
332 332 // Checks nb points
333 333 QCOMPARE(variable.nbPoints(), operation.m_ExpectedNbPoints);
334 334 }
335 335 }
336 336
337 337 namespace {
338 338
339 339 /// Struct used to represent a range operation on a variable
340 340 /// @sa TestVariable::testRealRange()
341 341 struct RangeOperation {
342 342 SqpRange m_CacheRange; /// Range to set for the variable
343 343 std::shared_ptr<ScalarSeries> m_DataSeries; /// Series to merge in the variable
344 344 SqpRange m_ExpectedRealRange; /// Real Range expected after operation on the variable
345 345 };
346 346
347 347 using RangeOperations = std::vector<RangeOperation>;
348 348
349 349 } // namespace
350 350
351 351 Q_DECLARE_METATYPE(RangeOperations)
352 352
353 353 void TestVariable::testRealRange_data()
354 354 {
355 355 // ////////////// //
356 356 // Test structure //
357 357 // ////////////// //
358 358
359 359 QTest::addColumn<RangeOperations>("operations");
360 360
361 361 // ////////// //
362 362 // Test cases //
363 363 // ////////// //
364 364 RangeOperations operations{};
365 365
366 366 // Inits cache range and data series (expected real range = cache range)
367 367 auto cacheRange = SqpRange{date(2017, 1, 1, 12, 0, 0), date(2017, 1, 1, 13, 0, 0)};
368 368 operations.push_back({cacheRange, dataSeries(cacheRange), cacheRange});
369 369
370 370 // Changes cache range and updates data series (expected real range = cache range)
371 371 cacheRange = SqpRange{date(2017, 1, 1, 14, 0, 0), date(2017, 1, 1, 15, 0, 0)};
372 372 operations.push_back({cacheRange, dataSeries(cacheRange), cacheRange});
373 373
374 374 // Changes cache range and update data series but with a lower range (expected real range =
375 375 // data series range)
376 376 cacheRange = SqpRange{date(2017, 1, 1, 12, 0, 0), date(2017, 1, 1, 16, 0, 0)};
377 377 auto dataSeriesRange = SqpRange{date(2017, 1, 1, 14, 0, 0), date(2017, 1, 1, 15, 0, 0)};
378 378 operations.push_back({cacheRange, dataSeries(dataSeriesRange), dataSeriesRange});
379 379
380 380 // Changes cache range but DON'T update data series (expected real range = cache range
381 381 // before operation)
382 382 cacheRange = SqpRange{date(2017, 1, 1, 10, 0, 0), date(2017, 1, 1, 17, 0, 0)};
383 383 operations.push_back({cacheRange, nullptr, dataSeriesRange});
384 384
385 385 QTest::newRow("realRange1") << operations;
386 386 }
387 387
388 388 void TestVariable::testRealRange()
389 389 {
390 390 // Creates variable (real range is invalid)
391 391 Variable variable{"var"};
392 392 QCOMPARE(variable.realRange(), INVALID_RANGE);
393 393
394 394 QFETCH(RangeOperations, operations);
395 395 for (const auto &operation : operations) {
396 396 // Sets cache range and merge data series
397 397 variable.setCacheRange(operation.m_CacheRange);
398 398 if (operation.m_DataSeries != nullptr) {
399 399 variable.mergeDataSeries(operation.m_DataSeries);
400 400 }
401 401
402 402 // Checks real range
403 403 QCOMPARE(variable.realRange(), operation.m_ExpectedRealRange);
404 404 }
405 405 }
406 406
407 407
408 408 QTEST_MAIN(TestVariable)
409 409 #include "TestVariable.moc"
General Comments 3
Under Review
author

Auto status change to "Under Review"

Approved
author

Merge lasted acquisition developpement on main Sciqlop branch

You need to be logged in to leave comments. Login now