From 3e9153eacdd5e10d40c0c6b37037d7e6daef2469 2017-09-29 09:15:41 From: Alexandre Leroux Date: 2017-09-29 09:15:41 Subject: [PATCH] Adds Variable::realRange() test --- diff --git a/core/tests/Variable/TestVariable.cpp b/core/tests/Variable/TestVariable.cpp index ce323f0..5dbb2e4 100644 --- a/core/tests/Variable/TestVariable.cpp +++ b/core/tests/Variable/TestVariable.cpp @@ -7,6 +7,32 @@ #include +namespace { + +/// Generates a date in double +auto date = [](int year, int month, int day, int hours, int minutes, int seconds) { + return DateUtils::secondsSinceEpoch( + QDateTime{{year, month, day}, {hours, minutes, seconds}, Qt::UTC}); +}; + +/// Generates a series of test data for a range +std::shared_ptr dataSeries(const SqpRange &range) +{ + auto xAxisData = std::vector{}; + auto valuesData = std::vector{}; + + auto value = 0; + for (auto x = range.m_TStart; x <= range.m_TEnd; ++x, ++value) { + xAxisData.push_back(x); + valuesData.push_back(value); + } + + return std::make_shared(std::move(xAxisData), std::move(valuesData), Unit{}, + Unit{}); +} + +} // namespace + Q_DECLARE_METATYPE(std::shared_ptr) class TestVariable : public QObject { @@ -18,6 +44,9 @@ private slots: void testNotInCacheRangeList(); void testInCacheRangeList(); + + void testRealRange_data(); + void testRealRange(); }; void TestVariable::testClone_data() @@ -36,27 +65,6 @@ void TestVariable::testClone_data() // Test cases // // ////////// // - /// Generates a date in double - auto date = [](int year, int month, int day, int hours, int minutes, int seconds) { - return DateUtils::secondsSinceEpoch( - QDateTime{{year, month, day}, {hours, minutes, seconds}, Qt::UTC}); - }; - - /// Generates a data series for a range - auto dataSeries = [](const SqpRange &range) { - auto xAxisData = std::vector{}; - auto valuesData = std::vector{}; - - auto value = 0; - for (auto x = range.m_TStart; x < range.m_TEnd; ++x, ++value) { - xAxisData.push_back(x); - valuesData.push_back(value); - } - - return std::make_shared(std::move(xAxisData), std::move(valuesData), Unit{}, - Unit{}); - }; - auto cacheRange = SqpRange{date(2017, 1, 1, 12, 0, 0), date(2017, 1, 1, 13, 0, 0)}; QTest::newRow("clone1") << QStringLiteral("var1") << QVariantHash{{"data1", 1}, {"data2", "abc"}} @@ -257,6 +265,76 @@ void TestVariable::testInCacheRangeList() QCOMPARE(notInCachRange.m_TEnd, DateUtils::secondsSinceEpoch(varCRE)); } +namespace { + +/// Struct used to represent a range operation on a variable +/// @sa TestVariable::testRealRange() +struct RangeOperation { + SqpRange m_CacheRange; /// Range to set for the variable + std::shared_ptr m_DataSeries; /// Series to merge in the variable + SqpRange m_ExpectedRealRange; /// Real Range expected after operation on the variable +}; + +using RangeOperations = std::vector; + +} // namespace + +Q_DECLARE_METATYPE(RangeOperations) + +void TestVariable::testRealRange_data() +{ + // ////////////// // + // Test structure // + // ////////////// // + + QTest::addColumn("operations"); + + // ////////// // + // Test cases // + // ////////// // + RangeOperations operations{}; + + // Inits cache range and data series (expected real range = cache range) + auto cacheRange = SqpRange{date(2017, 1, 1, 12, 0, 0), date(2017, 1, 1, 13, 0, 0)}; + operations.push_back({cacheRange, dataSeries(cacheRange), cacheRange}); + + // Changes cache range and updates data series (expected real range = cache range) + cacheRange = SqpRange{date(2017, 1, 1, 14, 0, 0), date(2017, 1, 1, 15, 0, 0)}; + operations.push_back({cacheRange, dataSeries(cacheRange), cacheRange}); + + // Changes cache range and update data series but with a lower range (expected real range = + // data series range) + cacheRange = SqpRange{date(2017, 1, 1, 12, 0, 0), date(2017, 1, 1, 16, 0, 0)}; + auto dataSeriesRange = SqpRange{date(2017, 1, 1, 14, 0, 0), date(2017, 1, 1, 15, 0, 0)}; + operations.push_back({cacheRange, dataSeries(dataSeriesRange), dataSeriesRange}); + + // Changes cache range but DON'T update data series (expected real range = cache range + // before operation) + cacheRange = SqpRange{date(2017, 1, 1, 10, 0, 0), date(2017, 1, 1, 17, 0, 0)}; + operations.push_back({cacheRange, nullptr, dataSeriesRange}); + + QTest::newRow("realRange1") << operations; +} + +void TestVariable::testRealRange() +{ + // Creates variable (real range is invalid) + Variable variable{"var"}; + QCOMPARE(variable.realRange(), INVALID_RANGE); + + QFETCH(RangeOperations, operations); + for (const auto &operation : operations) { + // Sets cache range and merge data series + variable.setCacheRange(operation.m_CacheRange); + if (operation.m_DataSeries != nullptr) { + variable.mergeDataSeries(operation.m_DataSeries); + } + + // Checks real range + QCOMPARE(variable.realRange(), operation.m_ExpectedRealRange); + } +} + QTEST_MAIN(TestVariable) #include "TestVariable.moc"