File indexing completed on 2024-10-13 06:30:10

0001 /*
0002     File                 : JSONFilterTest.cpp
0003     Project              : LabPlot
0004     Description          : Tests for the JSON I/O-filter.
0005     --------------------------------------------------------------------
0006     SPDX-FileCopyrightText: 2018 Andrey Cygankov <craftplace.ms@gmail.com>
0007     SPDX-License-Identifier: GPL-2.0-or-later
0008 */
0009 
0010 #include "JSONFilterTest.h"
0011 #include "backend/datasources/filters/JsonFilter.h"
0012 #include "backend/spreadsheet/Spreadsheet.h"
0013 
0014 #include <KLocalizedString>
0015 
0016 void JSONFilterTest::initTestCase() {
0017     // needed in order to have the signals triggered by SignallingUndoCommand, see LabPlot.cpp
0018     // TODO: redesign/remove this
0019     qRegisterMetaType<const AbstractAspect*>("const AbstractAspect*");
0020     qRegisterMetaType<const AbstractColumn*>("const AbstractColumn*");
0021 }
0022 
0023 void JSONFilterTest::testArrayImport() {
0024     Spreadsheet spreadsheet(QStringLiteral("test"), false);
0025     JsonFilter filter;
0026 
0027     const QString& fileName = QFINDTESTDATA(QLatin1String("data/array.json"));
0028     AbstractFileFilter::ImportMode mode = AbstractFileFilter::ImportMode::Replace;
0029     filter.setCreateIndexEnabled(true);
0030     filter.setDataRowType(QJsonValue::Array);
0031     filter.setDateTimeFormat(QLatin1String("yyyy-MM-dd"));
0032     filter.readDataFromFile(fileName, &spreadsheet, mode);
0033 
0034     QCOMPARE(spreadsheet.columnCount(), 3);
0035     QCOMPARE(spreadsheet.rowCount(), 3);
0036     QCOMPARE(spreadsheet.column(0)->columnMode(), AbstractColumn::ColumnMode::Integer);
0037     QCOMPARE(spreadsheet.column(1)->columnMode(), AbstractColumn::ColumnMode::DateTime);
0038     QCOMPARE(spreadsheet.column(2)->columnMode(), AbstractColumn::ColumnMode::Double);
0039 
0040     QCOMPARE(spreadsheet.column(0)->plotDesignation(), AbstractColumn::PlotDesignation::X);
0041     QCOMPARE(spreadsheet.column(1)->plotDesignation(), AbstractColumn::PlotDesignation::Y);
0042     QCOMPARE(spreadsheet.column(2)->plotDesignation(), AbstractColumn::PlotDesignation::Y);
0043 
0044     QCOMPARE(spreadsheet.column(0)->name(), i18n("index"));
0045     QCOMPARE(spreadsheet.column(1)->name(), QLatin1String("Column 1")); // TODO is translatable in JsonFilter
0046     QCOMPARE(spreadsheet.column(2)->name(), QLatin1String("Column 2"));
0047 
0048     QCOMPARE(spreadsheet.column(0)->integerAt(0), 1);
0049     QCOMPARE(spreadsheet.column(0)->integerAt(1), 2);
0050     QCOMPARE(spreadsheet.column(0)->integerAt(2), 3);
0051 
0052     QDateTime value = QDateTime::fromString(QLatin1String("2018-06-01"), QLatin1String("yyyy-MM-dd"));
0053     QCOMPARE(spreadsheet.column(1)->dateTimeAt(0), value);
0054     value = QDateTime::fromString(QLatin1String("2018-06-02"), QLatin1String("yyyy-MM-dd"));
0055     QCOMPARE(spreadsheet.column(1)->dateTimeAt(1), value);
0056     value = QDateTime::fromString(QLatin1String("2018-06-03"), QLatin1String("yyyy-MM-dd"));
0057     QCOMPARE(spreadsheet.column(1)->dateTimeAt(2), value);
0058 
0059     QCOMPARE(spreadsheet.column(2)->valueAt(0), 0.01);
0060     QCOMPARE(spreadsheet.column(2)->valueAt(1), 0.02);
0061     QCOMPARE(spreadsheet.column(2)->valueAt(2), 0.03);
0062 }
0063 
0064 /*!
0065  * import objects with an additional column for the index
0066  */
0067 void JSONFilterTest::testObjectImport01() {
0068     Spreadsheet spreadsheet(QStringLiteral("test"), false);
0069     JsonFilter filter;
0070 
0071     const QString& fileName = QFINDTESTDATA(QLatin1String("data/object.json"));
0072     AbstractFileFilter::ImportMode mode = AbstractFileFilter::ImportMode::Replace;
0073     filter.setCreateIndexEnabled(true);
0074     filter.setDataRowType(QJsonValue::Object);
0075     filter.readDataFromFile(fileName, &spreadsheet, mode);
0076 
0077     QCOMPARE(spreadsheet.columnCount(), 5);
0078     QCOMPARE(spreadsheet.rowCount(), 3);
0079     QCOMPARE(spreadsheet.column(0)->columnMode(), AbstractColumn::ColumnMode::Integer);
0080     QCOMPARE(spreadsheet.column(1)->columnMode(), AbstractColumn::ColumnMode::Double);
0081     QCOMPARE(spreadsheet.column(2)->columnMode(), AbstractColumn::ColumnMode::Double);
0082     QCOMPARE(spreadsheet.column(3)->columnMode(), AbstractColumn::ColumnMode::Double);
0083     QCOMPARE(spreadsheet.column(4)->columnMode(), AbstractColumn::ColumnMode::Text);
0084 
0085     QCOMPARE(spreadsheet.column(0)->plotDesignation(), AbstractColumn::PlotDesignation::X);
0086     QCOMPARE(spreadsheet.column(1)->plotDesignation(), AbstractColumn::PlotDesignation::Y);
0087     QCOMPARE(spreadsheet.column(2)->plotDesignation(), AbstractColumn::PlotDesignation::Y);
0088     QCOMPARE(spreadsheet.column(3)->plotDesignation(), AbstractColumn::PlotDesignation::Y);
0089     QCOMPARE(spreadsheet.column(4)->plotDesignation(), AbstractColumn::PlotDesignation::Y);
0090 
0091     QCOMPARE(spreadsheet.column(0)->name(), i18n("index"));
0092     QCOMPARE(spreadsheet.column(1)->name(), QLatin1String("1"));
0093     QCOMPARE(spreadsheet.column(2)->name(), QLatin1String("2"));
0094     QCOMPARE(spreadsheet.column(3)->name(), QLatin1String("3"));
0095     QCOMPARE(spreadsheet.column(4)->name(), QLatin1String("4"));
0096 
0097     QCOMPARE(spreadsheet.column(0)->integerAt(0), 1);
0098     QCOMPARE(spreadsheet.column(0)->integerAt(1), 2);
0099     QCOMPARE(spreadsheet.column(0)->integerAt(2), 3);
0100 
0101     QCOMPARE(spreadsheet.column(1)->valueAt(0), 1.234);
0102     QCOMPARE(spreadsheet.column(1)->valueAt(1), 111.);
0103     QCOMPARE(spreadsheet.column(1)->valueAt(2), 0.001);
0104 
0105     QCOMPARE(spreadsheet.column(2)->valueAt(0), 2.345);
0106     QCOMPARE(spreadsheet.column(2)->valueAt(1), 222.);
0107     QCOMPARE(spreadsheet.column(2)->valueAt(2), 0.002);
0108 
0109     QCOMPARE(spreadsheet.column(3)->valueAt(0), 3.456);
0110     QCOMPARE(spreadsheet.column(3)->valueAt(1), 333.);
0111     QCOMPARE(spreadsheet.column(3)->valueAt(2), 0.003);
0112 
0113     QCOMPARE(spreadsheet.column(4)->textAt(0), QStringLiteral("field1"));
0114     QCOMPARE(spreadsheet.column(4)->textAt(1), QStringLiteral("field2"));
0115     QCOMPARE(spreadsheet.column(4)->textAt(2), QStringLiteral("field3"));
0116 }
0117 
0118 /*!
0119  * import objects with an additional column for the object names
0120  */
0121 void JSONFilterTest::testObjectImport02() {
0122     Spreadsheet spreadsheet(QStringLiteral("test"), false);
0123     JsonFilter filter;
0124 
0125     const QString& fileName = QFINDTESTDATA(QLatin1String("data/object.json"));
0126     AbstractFileFilter::ImportMode mode = AbstractFileFilter::ImportMode::Replace;
0127     filter.setDataRowType(QJsonValue::Object);
0128     filter.setImportObjectNames(true);
0129     filter.readDataFromFile(fileName, &spreadsheet, mode);
0130 
0131     QCOMPARE(spreadsheet.columnCount(), 5);
0132     QCOMPARE(spreadsheet.rowCount(), 3);
0133     QCOMPARE(spreadsheet.column(0)->columnMode(), AbstractColumn::ColumnMode::Text);
0134     QCOMPARE(spreadsheet.column(1)->columnMode(), AbstractColumn::ColumnMode::Double);
0135     QCOMPARE(spreadsheet.column(2)->columnMode(), AbstractColumn::ColumnMode::Double);
0136     QCOMPARE(spreadsheet.column(3)->columnMode(), AbstractColumn::ColumnMode::Double);
0137     QCOMPARE(spreadsheet.column(4)->columnMode(), AbstractColumn::ColumnMode::Text);
0138 
0139     QCOMPARE(spreadsheet.column(0)->plotDesignation(), AbstractColumn::PlotDesignation::X);
0140     QCOMPARE(spreadsheet.column(1)->plotDesignation(), AbstractColumn::PlotDesignation::Y);
0141     QCOMPARE(spreadsheet.column(2)->plotDesignation(), AbstractColumn::PlotDesignation::Y);
0142     QCOMPARE(spreadsheet.column(3)->plotDesignation(), AbstractColumn::PlotDesignation::Y);
0143     QCOMPARE(spreadsheet.column(4)->plotDesignation(), AbstractColumn::PlotDesignation::Y);
0144 
0145     QCOMPARE(spreadsheet.column(0)->name(), i18n("name"));
0146     QCOMPARE(spreadsheet.column(1)->name(), QLatin1String("1"));
0147     QCOMPARE(spreadsheet.column(2)->name(), QLatin1String("2"));
0148     QCOMPARE(spreadsheet.column(3)->name(), QLatin1String("3"));
0149     QCOMPARE(spreadsheet.column(4)->name(), QLatin1String("4"));
0150 
0151     QCOMPARE(spreadsheet.column(0)->textAt(0), QStringLiteral("field1"));
0152     QCOMPARE(spreadsheet.column(0)->textAt(1), QStringLiteral("field2"));
0153     QCOMPARE(spreadsheet.column(0)->textAt(2), QStringLiteral("field3"));
0154 
0155     QCOMPARE(spreadsheet.column(1)->valueAt(0), 1.234);
0156     QCOMPARE(spreadsheet.column(1)->valueAt(1), 111.);
0157     QCOMPARE(spreadsheet.column(1)->valueAt(2), 0.001);
0158 
0159     QCOMPARE(spreadsheet.column(2)->valueAt(0), 2.345);
0160     QCOMPARE(spreadsheet.column(2)->valueAt(1), 222.);
0161     QCOMPARE(spreadsheet.column(2)->valueAt(2), 0.002);
0162 
0163     QCOMPARE(spreadsheet.column(3)->valueAt(0), 3.456);
0164     QCOMPARE(spreadsheet.column(3)->valueAt(1), 333.);
0165     QCOMPARE(spreadsheet.column(3)->valueAt(2), 0.003);
0166 
0167     QCOMPARE(spreadsheet.column(4)->textAt(0), QStringLiteral("field1"));
0168     QCOMPARE(spreadsheet.column(4)->textAt(1), QStringLiteral("field2"));
0169     QCOMPARE(spreadsheet.column(4)->textAt(2), QStringLiteral("field3"));
0170 }
0171 
0172 /*!
0173  * import objects with an additional column for the object names
0174  * with custom start and end columns
0175  */
0176 void JSONFilterTest::testObjectImport03() {
0177     Spreadsheet spreadsheet(QStringLiteral("test"), false);
0178     JsonFilter filter;
0179 
0180     const QString& fileName = QFINDTESTDATA(QLatin1String("data/object.json"));
0181     AbstractFileFilter::ImportMode mode = AbstractFileFilter::ImportMode::Replace;
0182     filter.setDataRowType(QJsonValue::Object);
0183     filter.setImportObjectNames(true);
0184     filter.setStartColumn(2);
0185     filter.setEndColumn(3);
0186     filter.readDataFromFile(fileName, &spreadsheet, mode);
0187 
0188     QCOMPARE(spreadsheet.columnCount(), 3);
0189     QCOMPARE(spreadsheet.rowCount(), 3);
0190     QCOMPARE(spreadsheet.column(0)->columnMode(), AbstractColumn::ColumnMode::Text);
0191     QCOMPARE(spreadsheet.column(1)->columnMode(), AbstractColumn::ColumnMode::Double);
0192     QCOMPARE(spreadsheet.column(2)->columnMode(), AbstractColumn::ColumnMode::Double);
0193 
0194     QCOMPARE(spreadsheet.column(0)->plotDesignation(), AbstractColumn::PlotDesignation::X);
0195     QCOMPARE(spreadsheet.column(1)->plotDesignation(), AbstractColumn::PlotDesignation::Y);
0196     QCOMPARE(spreadsheet.column(2)->plotDesignation(), AbstractColumn::PlotDesignation::Y);
0197 
0198     QCOMPARE(spreadsheet.column(0)->name(), i18n("name"));
0199     QCOMPARE(spreadsheet.column(1)->name(), QLatin1String("2"));
0200     QCOMPARE(spreadsheet.column(2)->name(), QLatin1String("3"));
0201 
0202     QCOMPARE(spreadsheet.column(0)->textAt(0), QStringLiteral("field1"));
0203     QCOMPARE(spreadsheet.column(0)->textAt(1), QStringLiteral("field2"));
0204     QCOMPARE(spreadsheet.column(0)->textAt(2), QStringLiteral("field3"));
0205 
0206     QCOMPARE(spreadsheet.column(1)->valueAt(0), 2.345);
0207     QCOMPARE(spreadsheet.column(1)->valueAt(1), 222.);
0208     QCOMPARE(spreadsheet.column(1)->valueAt(2), 0.002);
0209 
0210     QCOMPARE(spreadsheet.column(2)->valueAt(0), 3.456);
0211     QCOMPARE(spreadsheet.column(2)->valueAt(1), 333.);
0212     QCOMPARE(spreadsheet.column(2)->valueAt(2), 0.003);
0213 }
0214 
0215 /*!
0216  * import objects with an additional datetime column for the object names
0217  */
0218 void JSONFilterTest::testObjectImport04() {
0219     Spreadsheet spreadsheet(QStringLiteral("test"), false);
0220     JsonFilter filter;
0221 
0222     const QString& fileName = QFINDTESTDATA(QLatin1String("data/intraday.json"));
0223     AbstractFileFilter::ImportMode mode = AbstractFileFilter::ImportMode::Replace;
0224     QVector<int> rows{0, 1};
0225     filter.setModelRows(rows);
0226     filter.setDataRowType(QJsonValue::Object);
0227     filter.setImportObjectNames(true);
0228 
0229     QString dateTimeFormat(QStringLiteral("yyyy-MM-dd hh:mm:ss"));
0230     filter.setDateTimeFormat(dateTimeFormat);
0231     filter.setStartRow(1);
0232     filter.setEndRow(2);
0233     filter.readDataFromFile(fileName, &spreadsheet, mode);
0234 
0235     QCOMPARE(spreadsheet.columnCount(), 6);
0236     QCOMPARE(spreadsheet.rowCount(), 2);
0237     QCOMPARE(spreadsheet.column(0)->columnMode(), AbstractColumn::ColumnMode::DateTime);
0238     QCOMPARE(spreadsheet.column(1)->columnMode(), AbstractColumn::ColumnMode::Double);
0239     QCOMPARE(spreadsheet.column(2)->columnMode(), AbstractColumn::ColumnMode::Double);
0240     QCOMPARE(spreadsheet.column(3)->columnMode(), AbstractColumn::ColumnMode::Double);
0241     QCOMPARE(spreadsheet.column(4)->columnMode(), AbstractColumn::ColumnMode::Double);
0242     QCOMPARE(spreadsheet.column(5)->columnMode(), AbstractColumn::ColumnMode::Integer);
0243 
0244     QCOMPARE(spreadsheet.column(0)->plotDesignation(), AbstractColumn::PlotDesignation::X);
0245     QCOMPARE(spreadsheet.column(1)->plotDesignation(), AbstractColumn::PlotDesignation::Y);
0246     QCOMPARE(spreadsheet.column(2)->plotDesignation(), AbstractColumn::PlotDesignation::Y);
0247     QCOMPARE(spreadsheet.column(3)->plotDesignation(), AbstractColumn::PlotDesignation::Y);
0248     QCOMPARE(spreadsheet.column(4)->plotDesignation(), AbstractColumn::PlotDesignation::Y);
0249     QCOMPARE(spreadsheet.column(5)->plotDesignation(), AbstractColumn::PlotDesignation::Y);
0250 
0251     QCOMPARE(spreadsheet.column(0)->name(), i18n("timestamp"));
0252     QCOMPARE(spreadsheet.column(1)->name(), QLatin1String("1. open"));
0253     QCOMPARE(spreadsheet.column(2)->name(), QLatin1String("2. high"));
0254     QCOMPARE(spreadsheet.column(3)->name(), QLatin1String("3. low"));
0255     QCOMPARE(spreadsheet.column(4)->name(), QLatin1String("4. close"));
0256     QCOMPARE(spreadsheet.column(5)->name(), QLatin1String("5. volume"));
0257 
0258     // TODO: the values are sorted with respect to the names of the objects, i.e. to the timestamp. Why?
0259     QCOMPARE(spreadsheet.column(0)->dateTimeAt(0).toString(dateTimeFormat), QStringLiteral("2018-06-14 15:56:00"));
0260     QCOMPARE(spreadsheet.column(0)->dateTimeAt(1).toString(dateTimeFormat), QStringLiteral("2018-06-14 15:57:00"));
0261 
0262     QCOMPARE(spreadsheet.column(1)->valueAt(0), 101.2700);
0263     QCOMPARE(spreadsheet.column(1)->valueAt(1), 101.2700);
0264 
0265     QCOMPARE(spreadsheet.column(2)->valueAt(0), 101.2800);
0266     QCOMPARE(spreadsheet.column(2)->valueAt(1), 101.3350);
0267 
0268     QCOMPARE(spreadsheet.column(3)->valueAt(0), 101.2600);
0269     QCOMPARE(spreadsheet.column(3)->valueAt(1), 101.2550);
0270 
0271     QCOMPARE(spreadsheet.column(4)->valueAt(0), 101.2600);
0272     QCOMPARE(spreadsheet.column(4)->valueAt(1), 101.3300);
0273 
0274     QCOMPARE(spreadsheet.column(5)->integerAt(0), 27960);
0275     QCOMPARE(spreadsheet.column(5)->integerAt(1), 127830);
0276 }
0277 
0278 QTEST_MAIN(JSONFilterTest)