File indexing completed on 2024-10-13 06:30:10
0001 /* 0002 File : DatasetsTest.cpp 0003 Project : LabPlot 0004 Description : Tests for Dataset related features 0005 -------------------------------------------------------------------- 0006 SPDX-FileCopyrightText: 2019 Kovacs Ferencz <kferike98@gmail.com> 0007 SPDX-FileCopyrightText: 2023 Stefan Gerlach <stefan.gerlach@uni.kn> 0008 0009 SPDX-License-Identifier: GPL-2.0-or-later 0010 */ 0011 0012 #include "DatasetsTest.h" 0013 0014 #include "backend/datasources/DatasetHandler.h" 0015 #include "backend/datasources/filters/AsciiFilter.h" 0016 #include "backend/lib/macros.h" 0017 #include "backend/spreadsheet/Spreadsheet.h" 0018 #include "kdefrontend/DatasetModel.h" 0019 #include "kdefrontend/datasources/DatasetMetadataManagerDialog.h" 0020 #include "kdefrontend/datasources/ImportDatasetWidget.h" 0021 0022 #include <QDebug> 0023 #include <QEventLoop> 0024 #include <QFile> 0025 #include <QStandardPaths> 0026 #include <QTextStream> 0027 #include <QTimer> 0028 #include <QTreeWidgetItem> 0029 #include <QVector> 0030 0031 void DatasetsTest::initTestCase() { 0032 DEBUG(Q_FUNC_INFO) 0033 const QString currentDir = QStringLiteral(__FILE__); 0034 m_dataDir = currentDir.left(currentDir.lastIndexOf(QDir::separator())) + QDir::separator() + QLatin1String("data") + QDir::separator(); 0035 0036 const QString baseDir = QStandardPaths::standardLocations(QStandardPaths::AppDataLocation).first(); 0037 const QString containingDir = QStringLiteral("labplot_data"); 0038 m_jsonDir = baseDir + QDir::separator() + containingDir + QDir::separator(); 0039 0040 // needed in order to have the signals triggered by SignallingUndoCommand, see LabPlot.cpp 0041 // TODO: redesign/remove this 0042 0043 qRegisterMetaType<const AbstractAspect*>("const AbstractAspect*"); 0044 qRegisterMetaType<const AbstractColumn*>("const AbstractColumn*"); 0045 0046 removeFiles(); 0047 copyFiles(); 0048 } 0049 0050 void DatasetsTest::copyFiles() { 0051 DEBUG(Q_FUNC_INFO) 0052 const QString baseDir = QStandardPaths::standardLocations(QStandardPaths::AppDataLocation).first(); 0053 0054 if (!QDir(baseDir).exists()) 0055 QDir().mkdir(baseDir); 0056 0057 if (!QDir(m_jsonDir).exists()) 0058 QDir().mkdir(m_jsonDir); 0059 0060 QFile::copy(m_dataDir + QStringLiteral("Test.json"), m_jsonDir + QStringLiteral("Test.json")); 0061 QFile::copy(m_dataDir + QStringLiteral("DatasetCollections.json"), m_jsonDir + QStringLiteral("DatasetCollections.json")); 0062 } 0063 0064 // ############################################################################## 0065 // ##################### Test processing metadata files ####################### 0066 // ############################################################################## 0067 0068 void DatasetsTest::removeFiles() { 0069 DEBUG(Q_FUNC_INFO) 0070 const QString baseDir = QStandardPaths::standardLocations(QStandardPaths::AppDataLocation).first(); 0071 const QString containingDir = QStringLiteral("labplot_data"); 0072 const QString jsonDir = baseDir + QDir::separator() + containingDir + QDir::separator(); 0073 0074 QDir deleteDir(jsonDir); 0075 deleteDir.setNameFilters(QStringList() << QStringLiteral("*.*")); 0076 deleteDir.setFilter(QDir::Files); 0077 0078 for (QString dirFile : deleteDir.entryList()) { 0079 deleteDir.remove(dirFile); 0080 } 0081 } 0082 0083 void DatasetsTest::testCategories() { 0084 DEBUG(Q_FUNC_INFO) 0085 copyFiles(); 0086 // m_jsonDir = QFINDTESTDATA(QLatin1String("data/Test.json")); 0087 /* auto* importWidget = new ImportDatasetWidget(nullptr); 0088 auto* model = new DatasetModel(importWidget->getDatasetsMap()); 0089 0090 QCOMPARE(model->categories(QStringLiteral("Test")).size(), 3); 0091 QCOMPARE(model->categories(QStringLiteral("Test")).contains(QStringLiteral("Test_Cat")), true); 0092 QCOMPARE(model->categories(QStringLiteral("Test")).contains(QStringLiteral("Test_Cat_2")), true); 0093 QCOMPARE(model->categories(QStringLiteral("Test")).contains(QStringLiteral("Test_Cat_3")), true); 0094 0095 delete importWidget; 0096 delete model; 0097 */ 0098 } 0099 0100 /*void DatasetsTest::testSubcategories() { 0101 auto* importWidget = new ImportDatasetWidget(nullptr); 0102 auto* model = new DatasetModel(importWidget->getDatasetsMap()); 0103 0104 QCOMPARE(model->subcategories(QStringLiteral("Test"), QStringLiteral("Test_Cat")).size(), 4); 0105 QCOMPARE(model->subcategories(QStringLiteral("Test"), QStringLiteral("Test_Cat")).contains(QStringLiteral("Test_Subcat")), true); 0106 QCOMPARE(model->subcategories(QStringLiteral("Test"), QStringLiteral("Test_Cat")).contains(QStringLiteral("Test_Subcat2")), true); 0107 QCOMPARE(model->subcategories(QStringLiteral("Test"), QStringLiteral("Test_Cat")).contains(QStringLiteral("Test_Subcat3")), true); 0108 QCOMPARE(model->subcategories(QStringLiteral("Test"), QStringLiteral("Test_Cat")).contains(QStringLiteral("Test_Subcat4")), true); 0109 0110 delete importWidget; 0111 delete model; 0112 } 0113 0114 void DatasetsTest::testDatasets() { 0115 auto* importWidget = new ImportDatasetWidget(nullptr); 0116 auto* model = new DatasetModel(importWidget->getDatasetsMap()); 0117 0118 QCOMPARE(model->datasets(QStringLiteral("Test"), QStringLiteral("Test_Cat"), QStringLiteral("Test_Subcat")).size(), 1); 0119 QCOMPARE(model->datasets(QStringLiteral("Test"), QStringLiteral("Test_Cat"), QStringLiteral("Test_Subcat")).contains(QStringLiteral("test1")), true); 0120 QCOMPARE(model->allDatasetsList().toStringList().size(), 6); 0121 0122 delete importWidget; 0123 delete model; 0124 } 0125 0126 //############################################################################## 0127 //################### Test processing and downloading dataset ################ 0128 //############################################################################## 0129 0130 void DatasetsTest::testProcessDataset() { 0131 auto* importWidget = new ImportDatasetWidget(nullptr); 0132 0133 auto* spreadsheet = new Spreadsheet(QStringLiteral("test")); 0134 auto* datasetHandler = new DatasetHandler(spreadsheet); 0135 importWidget->setCollection(QStringLiteral("Test")); 0136 importWidget->setCategory(QStringLiteral("Test_Cat")); 0137 importWidget->setSubcategory(QStringLiteral("Test_Subcat")); 0138 importWidget->setDataset(QStringLiteral("test1")); 0139 importWidget->import(datasetHandler); 0140 0141 QTimer timer; 0142 timer.setSingleShot(true); 0143 QEventLoop loop; 0144 connect(datasetHandler, &DatasetHandler::downloadCompleted, &loop, &QEventLoop::quit); 0145 connect(&timer, &QTimer::timeout, &loop, &QEventLoop::quit); 0146 timer.start(1500); 0147 loop.exec(); 0148 0149 bool datasetFound = false; 0150 0151 if (timer.isActive()) { 0152 timer.stop(); 0153 datasetFound = true; 0154 } 0155 qDebug() << datasetFound; 0156 0157 QCOMPARE(datasetFound, true); 0158 QCOMPARE(spreadsheet->rowCount(), 60); 0159 QCOMPARE(spreadsheet->columnCount(), 4); 0160 0161 Column* firstColumn = spreadsheet->column(0); 0162 Column* secondColumn = spreadsheet->column(1); 0163 Column* thirdColumn = spreadsheet->column(2); 0164 Column* fourthColumn = spreadsheet->column(3); 0165 0166 QCOMPARE(firstColumn->valueAt(0), 1); 0167 QCOMPARE(secondColumn->textAt(3), QStringLiteral("4/86")); 0168 QCOMPARE(thirdColumn->valueAt(0), -0.061134); 0169 QCOMPARE(fourthColumn->valueAt(0), 0.03016); 0170 0171 delete importWidget; 0172 delete spreadsheet; 0173 delete datasetHandler; 0174 } 0175 0176 //############################################################################## 0177 //########### Test adding new datasets to the existing collection ############ 0178 //############################################################################## 0179 0180 void DatasetsTest::testNewCollection() { 0181 removeFiles(); 0182 copyFiles(); 0183 0184 auto* importWidget = new ImportDatasetWidget(nullptr); 0185 auto* datasetDialog = new DatasetMetadataManagerDialog(nullptr, importWidget->getDatasetsMap()); 0186 0187 delete importWidget; 0188 0189 datasetDialog->setCollection(QStringLiteral("Test2")); 0190 datasetDialog->setCategory(QStringLiteral("Test_Cat")); 0191 datasetDialog->setSubcategory(QStringLiteral("Test_Subcat")); 0192 datasetDialog->setShortName(QStringLiteral("test_new")); 0193 datasetDialog->setFullName(QStringLiteral("New test dataset")); 0194 datasetDialog->setDescription(QStringLiteral("This is a new test dataset")); 0195 datasetDialog->setURL(QStringLiteral("www.testdataset.com")); 0196 datasetDialog->updateDocument(m_jsonDir); 0197 0198 importWidget = new ImportDatasetWidget(nullptr); 0199 auto* model = new DatasetModel(importWidget->getDatasetsMap()); 0200 0201 QCOMPARE(model->collections().size(), 2); 0202 QCOMPARE(model->allDatasetsList().toStringList().size(), 7); 0203 } 0204 0205 void DatasetsTest::testNewCategory() { 0206 removeFiles(); 0207 copyFiles(); 0208 0209 auto* importWidget = new ImportDatasetWidget(nullptr); 0210 auto* datasetDialog = new DatasetMetadataManagerDialog(nullptr, importWidget->getDatasetsMap()); 0211 0212 delete importWidget; 0213 0214 datasetDialog->setCollection(QStringLiteral("Test")); 0215 datasetDialog->setCategory(QStringLiteral("Test_Cat_4")); 0216 datasetDialog->setSubcategory(QStringLiteral("Test_Subcat")); 0217 datasetDialog->setShortName(QStringLiteral("test_new")); 0218 datasetDialog->setFullName(QStringLiteral("New test dataset")); 0219 datasetDialog->setDescription(QStringLiteral("This is a new test dataset")); 0220 datasetDialog->setURL(QStringLiteral("www.testdataset.com")); 0221 datasetDialog->updateDocument(m_jsonDir); 0222 0223 importWidget = new ImportDatasetWidget(nullptr); 0224 auto* model = new DatasetModel(importWidget->getDatasetsMap()); 0225 0226 QCOMPARE(model->categories(QStringLiteral("Test")).size(), 4); 0227 QCOMPARE(model->allDatasetsList().toStringList().size(), 7); 0228 } 0229 0230 void DatasetsTest::testNewSubcategory() { 0231 removeFiles(); 0232 copyFiles(); 0233 0234 auto* importWidget = new ImportDatasetWidget(nullptr); 0235 auto* datasetDialog = new DatasetMetadataManagerDialog(nullptr, importWidget->getDatasetsMap()); 0236 0237 delete importWidget; 0238 0239 datasetDialog->setCollection(QStringLiteral("Test")); 0240 datasetDialog->setCategory(QStringLiteral("Test_Cat")); 0241 datasetDialog->setSubcategory(QStringLiteral("Test_Subcat5")); 0242 datasetDialog->setShortName(QStringLiteral("test_new")); 0243 datasetDialog->setFullName(QStringLiteral("New test dataset")); 0244 datasetDialog->setDescription(QStringLiteral("This is a new test dataset")); 0245 datasetDialog->setURL(QStringLiteral("www.testdataset.com")); 0246 datasetDialog->updateDocument(m_jsonDir); 0247 0248 importWidget = new ImportDatasetWidget(nullptr); 0249 auto* model = new DatasetModel(importWidget->getDatasetsMap()); 0250 0251 QCOMPARE(model->subcategories(QStringLiteral("Test"), QStringLiteral("Test_Cat")).size(), 5); 0252 QCOMPARE(model->allDatasetsList().toStringList().size(), 7); 0253 } 0254 0255 void DatasetsTest::testNewDataset() { 0256 removeFiles(); 0257 copyFiles(); 0258 0259 auto* importWidget = new ImportDatasetWidget(nullptr); 0260 auto* datasetDialog = new DatasetMetadataManagerDialog(nullptr, importWidget->getDatasetsMap()); 0261 0262 delete importWidget; 0263 0264 datasetDialog->setCollection(QStringLiteral("Test")); 0265 datasetDialog->setCategory(QStringLiteral("Test_Cat")); 0266 datasetDialog->setSubcategory(QStringLiteral("Test_Subcat")); 0267 datasetDialog->setShortName(QStringLiteral("test_new")); 0268 datasetDialog->setFullName(QStringLiteral("New test dataset")); 0269 datasetDialog->setDescription(QStringLiteral("This is a new test dataset")); 0270 datasetDialog->setURL(QStringLiteral("www.testdataset.com")); 0271 datasetDialog->updateDocument(m_jsonDir); 0272 0273 importWidget = new ImportDatasetWidget(nullptr); 0274 auto* model = new DatasetModel(importWidget->getDatasetsMap()); 0275 0276 QCOMPARE(model->datasets(QStringLiteral("Test"), QStringLiteral("Test_Cat"), QStringLiteral("Test_Subcat")).size(), 2); 0277 QCOMPARE(model->allDatasetsList().toStringList().size(), 7); 0278 }*/ 0279 0280 QTEST_MAIN(DatasetsTest)