File indexing completed on 2022-11-29 19:02:33

0001 /*
0002     SPDX-FileCopyrightText: 2020 Volker Krause <vkrause@kde.org>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include <KOpeningHours/Interval>
0008 #include <KOpeningHours/IntervalModel>
0009 
0010 #include <QAbstractItemModelTester>
0011 #include <QDateTime>
0012 #include <QTest>
0013 
0014 using namespace KOpeningHours;
0015 
0016 class IntervalModelTest : public QObject
0017 {
0018     Q_OBJECT
0019 private Q_SLOTS:
0020     void testModel()
0021     {
0022         IntervalModel model;
0023         QAbstractItemModelTester modelTest(&model);
0024 
0025         OpeningHours oh("Mo,We,Fr 10:00-20:00; Su 08:00-14:00");
0026         QCOMPARE(oh.error(), OpeningHours::NoError);
0027 
0028         model.setOpeningHours(oh);
0029         model.setBeginDate({2020, 11, 2});
0030         model.setEndDate({2020, 11, 9});
0031 
0032         QCOMPARE(model.rowCount(), 7);
0033         for (int i = 0; i < model.rowCount(); ++i) {
0034             const auto idx = model.index(i, 0);
0035             const auto dt = idx.data(IntervalModel::DateRole).toDate();
0036             QCOMPARE(dt, QDate(2020, 11, 2).addDays(i));
0037             const auto intervals = idx.data(IntervalModel::IntervalsRole).value<std::vector<Interval>>();
0038             QCOMPARE(intervals.size(), (i % 2) ? 1 : 3);
0039 
0040             QCOMPARE(intervals.front().begin(), QDateTime(dt, {0, 0}));
0041             QCOMPARE(intervals.back().end(), QDateTime(dt.addDays(1), {0, 0}));
0042         }
0043     }
0044 
0045     void testModelOpenIntervals()
0046     {
0047         IntervalModel model;
0048         QAbstractItemModelTester modelTest(&model);
0049 
0050         OpeningHours oh("24/7");
0051         QCOMPARE(oh.error(), OpeningHours::NoError);
0052 
0053         model.setOpeningHours(oh);
0054         model.setBeginDate({2020, 11, 2});
0055         model.setEndDate({2020, 11, 9});
0056 
0057         QCOMPARE(model.rowCount(), 7);
0058         for (int i = 0; i < model.rowCount(); ++i) {
0059             const auto idx = model.index(i, 0);
0060             const auto dt = idx.data(IntervalModel::DateRole).toDate();
0061             QCOMPARE(dt, QDate(2020, 11, 2).addDays(i));
0062             const auto intervals = idx.data(IntervalModel::IntervalsRole).value<std::vector<Interval>>();
0063             QCOMPARE(intervals.size(), 1);
0064 
0065             QCOMPARE(intervals.front().begin(), QDateTime(dt, {0, 0}));
0066             QCOMPARE(intervals.back().end(), QDateTime(dt.addDays(1), {0, 0}));
0067         }
0068     }
0069 
0070     void testOpenEndTime()
0071     {
0072         IntervalModel model;
0073         QAbstractItemModelTester modelTest(&model);
0074 
0075         OpeningHours oh("23:00-01:00+");
0076         QCOMPARE(oh.error(), OpeningHours::NoError);
0077 
0078         model.setOpeningHours(oh);
0079         model.setBeginDate({2020, 11, 2});
0080         model.setEndDate({2020, 11, 3});
0081 
0082         QCOMPARE(model.rowCount(), 1);
0083         const auto intervals = model.index(0, 0).data(IntervalModel::IntervalsRole).value<std::vector<Interval>>();
0084         QCOMPARE(intervals.size(), 3);
0085         QVERIFY(intervals[0].hasOpenEndTime());
0086         QVERIFY(!intervals[1].hasOpenEndTime());
0087         QVERIFY(!intervals[2].hasOpenEndTime());
0088         QCOMPARE(intervals[0].estimatedEnd(), intervals[1].begin());
0089         QCOMPARE(intervals[1].end(), intervals[2].begin());
0090         QVERIFY(intervals[0].end() < intervals[0].estimatedEnd());
0091 
0092         QCOMPARE(intervals[0].state(), Interval::Open);
0093         QCOMPARE(intervals[1].state(), Interval::Closed);
0094         QCOMPARE(intervals[2].state(), Interval::Open);
0095     }
0096 };
0097 
0098 QTEST_GUILESS_MAIN(IntervalModelTest)
0099 
0100 #include "intervalmodeltest.moc"