changeset 1872:566476eeeb80 csv-import-headers

Support headers in actual import step, + test
author Chris Cannam
date Thu, 18 Jun 2020 11:55:28 +0100
parents bed42ce4d3ab
children 1d44fdc8196c
files data/fileio/CSVFileReader.cpp data/fileio/test/CSVReaderTest.h
diffstat 2 files changed, 122 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- a/data/fileio/CSVFileReader.cpp	Thu Jun 18 11:55:17 2020 +0100
+++ b/data/fileio/CSVFileReader.cpp	Thu Jun 18 11:55:28 2020 +0100
@@ -265,6 +265,7 @@
 
     map<QString, int> labelCountMap;
 
+    bool atStart = true;
     bool abandoned = false;
     
     while (!in.atEnd() && !abandoned) {
@@ -304,10 +305,16 @@
         }
         
         for (int li = 0; li < lines.size(); ++li) {
+            
+            QString line = lines[li];
+            if (line.startsWith("#")) continue;
 
-            QString line = lines[li];
-            
-            if (line.startsWith("#")) continue;
+            if (atStart) {
+                atStart = false;
+                if (m_format.getHeaderStatus() == CSVFormat::HeaderPresent) {
+                    continue;
+                }
+            }
 
             QStringList list = StringBits::split(line, separator, allowQuoting);
             if (!model) {
--- a/data/fileio/test/CSVReaderTest.h	Thu Jun 18 11:55:17 2020 +0100
+++ b/data/fileio/test/CSVReaderTest.h	Thu Jun 18 11:55:28 2020 +0100
@@ -81,6 +81,16 @@
         delete model;
     }
 
+    void modelType1DSamplesWithHeader() {
+        Model *model = nullptr;
+        loadFrom("model-type-1d-samples-header.csv", model);
+        auto actual = qobject_cast<SparseOneDimensionalModel *>(model);
+        QVERIFY(actual);
+        QCOMPARE(actual->getAllEvents().size(), 5);
+        //!!! + the actual contents
+        delete model;
+    }
+
     void modelType1DSeconds() {
         Model *model = nullptr;
         loadFrom("model-type-1d-seconds.csv", model);
@@ -90,6 +100,15 @@
         delete model;
     }
 
+    void modelType1DSecondsWithHeader() {
+        Model *model = nullptr;
+        loadFrom("model-type-1d-seconds-header.csv", model);
+        auto actual = qobject_cast<SparseOneDimensionalModel *>(model);
+        QVERIFY(actual);
+        QCOMPARE(actual->getAllEvents().size(), 5);
+        delete model;
+    }
+
     void modelType2DDurationSamples() {
         Model *model = nullptr;
         loadFrom("model-type-2d-duration-samples.csv", model);
@@ -98,6 +117,15 @@
         QCOMPARE(actual->getAllEvents().size(), 5);
         delete model;
     }
+
+    void modelType2DDurationSamplesWithHeader() {
+        Model *model = nullptr;
+        loadFrom("model-type-2d-duration-samples-header.csv", model);
+        auto actual = qobject_cast<RegionModel *>(model);
+        QVERIFY(actual);
+        QCOMPARE(actual->getAllEvents().size(), 5);
+        delete model;
+    }
     
     void modelType2DDurationSeconds() {
         Model *model = nullptr;
@@ -108,6 +136,15 @@
         delete model;
     }
     
+    void modelType2DDurationSecondsWithHeader() {
+        Model *model = nullptr;
+        loadFrom("model-type-2d-duration-seconds-header.csv", model);
+        auto actual = qobject_cast<RegionModel *>(model);
+        QVERIFY(actual);
+        QCOMPARE(actual->getAllEvents().size(), 5);
+        delete model;
+    }
+    
     void badNegativeDuration() {
         Model *model = nullptr;
         loadFrom("bad-negative-duration.csv", model);
@@ -127,6 +164,15 @@
         delete model;
     }
     
+    void modelType2DEndTimeSamplesWithHeader() {
+        Model *model = nullptr;
+        loadFrom("model-type-2d-endtime-samples-header.csv", model);
+        auto actual = qobject_cast<RegionModel *>(model);
+        QVERIFY(actual);
+        QCOMPARE(actual->getAllEvents().size(), 5);
+        delete model;
+    }
+    
     void modelType2DEndTimeSeconds() {
         Model *model = nullptr;
         loadFrom("model-type-2d-endtime-seconds.csv", model);
@@ -136,6 +182,15 @@
         delete model;
     }
     
+    void modelType2DEndTimeSecondsWithHeader() {
+        Model *model = nullptr;
+        loadFrom("model-type-2d-endtime-seconds-header.csv", model);
+        auto actual = qobject_cast<RegionModel *>(model);
+        QVERIFY(actual);
+        QCOMPARE(actual->getAllEvents().size(), 5);
+        delete model;
+    }
+    
     void modelType2DImplicit() {
         Model *model = nullptr;
         loadFrom("model-type-2d-implicit.csv", model);
@@ -145,6 +200,15 @@
         delete model;
     }
     
+    void modelType2DImplicitWithHeader() {
+        Model *model = nullptr;
+        loadFrom("model-type-2d-implicit-header.csv", model);
+        auto actual = qobject_cast<SparseTimeValueModel *>(model);
+        QVERIFY(actual);
+        QCOMPARE(actual->getAllEvents().size(), 5);
+        delete model;
+    }
+    
     void modelType2DSamples() {
         Model *model = nullptr;
         loadFrom("model-type-2d-samples.csv", model);
@@ -154,6 +218,15 @@
         delete model;
     }
     
+    void modelType2DSamplesWithHeader() {
+        Model *model = nullptr;
+        loadFrom("model-type-2d-samples-header.csv", model);
+        auto actual = qobject_cast<SparseTimeValueModel *>(model);
+        QVERIFY(actual);
+        QCOMPARE(actual->getAllEvents().size(), 5);
+        delete model;
+    }
+    
     void modelType2DSeconds() {
         Model *model = nullptr;
         loadFrom("model-type-2d-seconds.csv", model);
@@ -163,6 +236,15 @@
         delete model;
     }
     
+    void modelType2DSecondsWithHeader() {
+        Model *model = nullptr;
+        loadFrom("model-type-2d-seconds-header.csv", model);
+        auto actual = qobject_cast<SparseTimeValueModel *>(model);
+        QVERIFY(actual);
+        QCOMPARE(actual->getAllEvents().size(), 5);
+        delete model;
+    }
+    
     void modelType3DImplicit() {
         Model *model = nullptr;
         loadFrom("model-type-3d-implicit.csv", model);
@@ -173,6 +255,16 @@
         delete model;
     }
     
+    void modelType3DImplicitWithHeader() {
+        Model *model = nullptr;
+        loadFrom("model-type-3d-implicit-header.csv", model);
+        auto actual = qobject_cast<EditableDenseThreeDimensionalModel *>(model);
+        QVERIFY(actual);
+        QCOMPARE(actual->getWidth(), 6);
+        QCOMPARE(actual->getHeight(), 6);
+        delete model;
+    }
+    
     void modelType3DSamples() {
         Model *model = nullptr;
         loadFrom("model-type-3d-samples.csv", model);
@@ -183,6 +275,16 @@
         delete model;
     }
     
+    void modelType3DSamplesWithHeader() {
+        Model *model = nullptr;
+        loadFrom("model-type-3d-samples-header.csv", model);
+        auto actual = qobject_cast<EditableDenseThreeDimensionalModel *>(model);
+        QVERIFY(actual);
+        QCOMPARE(actual->getWidth(), 6);
+        QCOMPARE(actual->getHeight(), 6);
+        delete model;
+    }
+    
     void modelType3DSeconds() {
         Model *model = nullptr;
         loadFrom("model-type-3d-seconds.csv", model);
@@ -193,6 +295,16 @@
         delete model;
     }
     
+    void modelType3DSecondsWithHeader() {
+        Model *model = nullptr;
+        loadFrom("model-type-3d-seconds-header.csv", model);
+        auto actual = qobject_cast<EditableDenseThreeDimensionalModel *>(model);
+        QVERIFY(actual);
+        QCOMPARE(actual->getWidth(), 6);
+        QCOMPARE(actual->getHeight(), 6);
+        delete model;
+    }
+    
     void withBlankLines1D() {
         Model *model = nullptr;
         loadFrom("with-blank-lines-1d.csv", model);