File indexing completed on 2024-11-17 04:49:34

0001 /*
0002  * Copyright (c) 2019 Alexander Potashev <aspotashev@gmail.com>
0003  *
0004  * This program is free software; you can redistribute it and/or
0005  * modify it under the terms of the GNU General Public License as
0006  * published by the Free Software Foundation; either version 2 of
0007  * the License or (at your option) version 3 or any later version
0008  * accepted by the membership of KDE e.V. (or its successor approved
0009  * by the membership of KDE e.V.), which shall act as a proxy
0010  * defined in Section 14 of version 3 of the license.
0011  *
0012  * This program is distributed in the hope that it will be useful,
0013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
0014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0015  * GNU General Public License for more details.
0016  *
0017  * You should have received a copy of the GNU General Public License
0018  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
0019  */
0020 
0021 #include <QTest>
0022 
0023 #include "export/export.h"
0024 #include "export/totalsastext.h"
0025 #include "helpers.h"
0026 #include "model/task.h"
0027 #include "taskview.h"
0028 #include "widgets/taskswidget.h"
0029 
0030 class ExportCSVTest : public QObject
0031 {
0032     Q_OBJECT
0033 
0034 private:
0035     ReportCriteria createRC(ReportCriteria::REPORTTYPE type);
0036 
0037 private Q_SLOTS:
0038     void testTotalsEmpty();
0039     void testTotalsSimpleTree();
0040     void testTimesSimpleTree();
0041     void testHistorySimpleTree();
0042 };
0043 
0044 ReportCriteria ExportCSVTest::createRC(ReportCriteria::REPORTTYPE type)
0045 {
0046     ReportCriteria rc;
0047     rc.reportType = type;
0048     rc.from = QDate::currentDate();
0049     rc.to = QDate::currentDate();
0050     rc.decimalMinutes = false;
0051     rc.sessionTimes = false;
0052     rc.allTasks = true;
0053     rc.delimiter = QChar::fromLatin1(';');
0054     rc.quote = QChar::fromLatin1('"');
0055 
0056     return rc;
0057 }
0058 
0059 void ExportCSVTest::testTotalsEmpty()
0060 {
0061     QLocale::setDefault(QLocale(QLocale::C));
0062 
0063     auto *taskView = createTaskView(this, false);
0064     QVERIFY(taskView);
0065 
0066     const QString &timeString = QLocale().toString(QDateTime::currentDateTime());
0067     const QString &expected = QStringLiteral(
0068         "Task Totals\n%1\n\n"
0069         "  Time    Task\n----------------------------------------------\nNo tasks.").arg(timeString);
0070     QCOMPARE(
0071         totalsAsText(taskView->storage()->tasksModel(), taskView->tasksWidget()->currentItem(), createRC(ReportCriteria::CSVTotalsExport)),
0072         expected);
0073 }
0074 
0075 void ExportCSVTest::testTotalsSimpleTree()
0076 {
0077     QLocale::setDefault(QLocale(QLocale::C));
0078 
0079     auto *taskView = createTaskView(this, true);
0080     QVERIFY(taskView);
0081 
0082     const QString &timeString = QLocale().toString(QDateTime::currentDateTime());
0083     const QString &expected = QStringLiteral(
0084         "Task Totals\n"
0085          "%1\n"
0086          "\n"
0087          "  Time    Task\n"
0088          "----------------------------------------------\n"
0089          "  0:08    1\n"
0090          "   0:03    2\n"
0091          "  0:07    3\n"
0092          "----------------------------------------------\n"
0093          "  0:15 Total").arg(timeString);
0094     QCOMPARE(
0095         totalsAsText(taskView->storage()->tasksModel(), taskView->tasksWidget()->currentItem(), createRC(ReportCriteria::CSVTotalsExport)),
0096         expected);
0097 }
0098 
0099 void ExportCSVTest::testTimesSimpleTree()
0100 {
0101     QLocale::setDefault(QLocale(QLocale::C));
0102 
0103     auto *taskView = createTaskView(this, true);
0104     QVERIFY(taskView);
0105 
0106     const auto &rc = createRC(ReportCriteria::CSVTotalsExport);
0107     const QUrl &url = createTempFile(this);
0108     QVERIFY(!url.isEmpty());
0109     QString output = exportToString(taskView->storage()->projectModel(), taskView->tasksWidget()->currentItem(), rc);
0110     QCOMPARE(writeExport(output, url), QString());
0111 
0112     const QString &expected = QStringLiteral(
0113         "\"3\";;0:07;0:07;0:07;0:07\n"
0114         "\"1\";;0:05;0:05;0:08;0:08\n"
0115         ";\"2\";0:03;0:03;0:03;0:03\n");
0116     QCOMPARE(readTextFile(url.toLocalFile()), expected);
0117 }
0118 
0119 void ExportCSVTest::testHistorySimpleTree()
0120 {
0121     QLocale::setDefault(QLocale(QLocale::C));
0122 
0123     auto *taskView = createTaskView(this, true);
0124     QVERIFY(taskView);
0125 
0126     const auto &rc = createRC(ReportCriteria::CSVHistoryExport);
0127     const QUrl &url = createTempFile(this);
0128     QString output = exportToString(taskView->storage()->projectModel(), taskView->tasksWidget()->currentItem(), rc);
0129     QCOMPARE(writeExport(output, url), QString());
0130 
0131     const QString &expected = QStringLiteral(
0132         "\"Task name\";%1\n"
0133         "\"1\";0:05\n"
0134         "\"1->2\";0:03\n"
0135         "\"3\";0:07\n").arg(QDate::currentDate().toString());
0136     QCOMPARE(readTextFile(url.toLocalFile()), expected);
0137 }
0138 
0139 QTEST_MAIN(ExportCSVTest)
0140 
0141 #include "exportcsvtest.moc"