##// END OF EJS Templates
Unit tests cases
Alexandre Leroux -
r397:195581ceb2d2
parent child
Show More
@@ -0,0 +1,6
1 #Sampling Time : 60
2 #Time Format : YYYY-MM-DDThh:mm:ss.mls
3 #imf(0) - Type : Local Parameter @ CDPP/AMDA - Name : bx_gse - Units : nT - Size : 1 - Frame : GSE - Mission : ACE - Instrument : MFI - Dataset : mfi_final-prelim
4 2013-09-23T09:00:30.000 NaN
5 2013-09-23T09:01:30.000 -2.71850
6 2013-09-23T09:02:30.000 -2.52150 No newline at end of file
@@ -0,0 +1,2
1 #Sampling Time : 60
2 #Time Format : YYYY-MM-DDThh:mm:ss.mls No newline at end of file
@@ -0,0 +1,6
1 #Sampling Time : 60
2 #Time Format : YYYY-MM-DDThh:mm:ss.mls
3 #imf(0) - Type : Local Parameter @ CDPP/AMDA - Name : bx_gse - Units : nT - Size : 1 - Frame : GSE - Mission : ACE - Instrument : MFI - Dataset : mfi_final-prelim
4 2013-09-23T09:00:30.000 -2.83950 1.05141 3.01547
5 2013-09-23T09:01:30.000 -2.71850
6 2013-09-23T09:02:30.000 -2.52150 No newline at end of file
@@ -0,0 +1,13
1 #Sampling Time : 60
2 #Time Format : YYYY-MM-DDThh:mm:ss.mls
3 #imf(0) - Type : Local Parameter @ CDPP/AMDA - Name : bx_gse - Units : nT - Size : 1 - Frame : GSE - Mission : ACE - Instrument : MFI - Dataset : mfi_final-prelim
4 2013-09-23T09:00:30.000 -2.83950
5 2013-09-23T09:01:30.000 -2.71850
6 2013-09-23T09:02:30.000 -2.52150
7 2013-09-23T09:03:30.000 -2.57633
8 2013-09-23T09:04:30.000 -2.58050
9 2013-09-23T09:05:30.000 -2.48325
10 2013-09-23T09:06:30.000 -2.63025
11 2013-09-23T09:07:30.000 -2.55800
12 2013-09-23T09:08:30.000 -2.43250
13 2013-09-23T09:09:30.000 -2.42200 No newline at end of file
@@ -0,0 +1,6
1 #Sampling Time : 60
2 #Time Format : YYYY-MM-DDThh:mm:ss.mls
3 #imf(0) - Type : Local Parameter @ CDPP/AMDA - Name : bx_gse - Units : nT - Size : 1 - Frame : GSE - Mission : ACE - Instrument : MFI - Dataset : mfi_final-prelim
4 23/09/2013 07:50:30 -2.83950
5 2013-09-23T09:01:30.000 -2.71850
6 2013-09-23T09:02:30.000 -2.52150 No newline at end of file
@@ -0,0 +1,6
1 #Sampling Time : 60
2 #Time Format : YYYY-MM-DDThh:mm:ss.mls
3 #Wrong unit comment
4 2013-09-23T09:00:30.000 -2.83950
5 2013-09-23T09:01:30.000 -2.71850
6 2013-09-23T09:02:30.000 -2.52150 No newline at end of file
@@ -0,0 +1,6
1 #Sampling Time : 60
2 #Time Format : YYYY-MM-DDThh:mm:ss.mls
3 #imf(0) - Type : Local Parameter @ CDPP/AMDA - Name : bx_gse - Units : nT - Size : 1 - Frame : GSE - Mission : ACE - Instrument : MFI - Dataset : mfi_final-prelim
4 2013-09-23T09:00:30.000 abc
5 2013-09-23T09:01:30.000 -2.71850
6 2013-09-23T09:02:30.000 -2.52150 No newline at end of file
@@ -1,113 +1,179
1 #include "AmdaResultParser.h"
1 #include "AmdaResultParser.h"
2
2
3 #include <Data/ScalarSeries.h>
4
3 #include <QObject>
5 #include <QObject>
4 #include <QtTest>
6 #include <QtTest>
5
7
6 namespace {
8 namespace {
7
9
8 /// Path for the tests
10 /// Path for the tests
9 const auto TESTS_RESOURCES_PATH
11 const auto TESTS_RESOURCES_PATH
10 = QFileInfo{QString{AMDA_TESTS_RESOURCES_DIR}, "TestAmdaResultParser"}.absoluteFilePath();
12 = QFileInfo{QString{AMDA_TESTS_RESOURCES_DIR}, "TestAmdaResultParser"}.absoluteFilePath();
11
13
12 QString inputFilePath(const QString &inputFileName)
14 QString inputFilePath(const QString &inputFileName)
13 {
15 {
14 return QFileInfo{TESTS_RESOURCES_PATH, inputFileName}.absoluteFilePath();
16 return QFileInfo{TESTS_RESOURCES_PATH, inputFileName}.absoluteFilePath();
15 }
17 }
16
18
17 struct ExpectedResults {
19 struct ExpectedResults {
18 explicit ExpectedResults() = default;
20 explicit ExpectedResults() = default;
19
21
20 /// Ctor with QVector<QDateTime> as x-axis data. Datetimes are converted to doubles
22 /// Ctor with QVector<QDateTime> as x-axis data. Datetimes are converted to doubles
21 explicit ExpectedResults(Unit xAxisUnit, Unit valuesUnit, const QVector<QDateTime> &xAxisData,
23 explicit ExpectedResults(Unit xAxisUnit, Unit valuesUnit, const QVector<QDateTime> &xAxisData,
22 QVector<double> valuesData)
24 QVector<double> valuesData)
23 : m_ParsingOK{true},
25 : m_ParsingOK{true},
24 m_XAxisUnit{xAxisUnit},
26 m_XAxisUnit{xAxisUnit},
25 m_ValuesUnit{valuesUnit},
27 m_ValuesUnit{valuesUnit},
26 m_XAxisData{},
28 m_XAxisData{},
27 m_ValuesData{std::move(valuesData)}
29 m_ValuesData{std::move(valuesData)}
28 {
30 {
29 // Converts QVector<QDateTime> to QVector<double>
31 // Converts QVector<QDateTime> to QVector<double>
30 std::transform(xAxisData.cbegin(), xAxisData.cend(), std::back_inserter(m_XAxisData),
32 std::transform(xAxisData.cbegin(), xAxisData.cend(), std::back_inserter(m_XAxisData),
31 [](const auto &dateTime) { return dateTime.toMSecsSinceEpoch() / 1000.; });
33 [](const auto &dateTime) { return dateTime.toMSecsSinceEpoch() / 1000.; });
32 }
34 }
33
35
34 /**
36 /**
35 * Validates a DataSeries compared to the expected results
37 * Validates a DataSeries compared to the expected results
36 * @param results the DataSeries to validate
38 * @param results the DataSeries to validate
37 */
39 */
38 void validate(std::shared_ptr<IDataSeries> results)
40 void validate(std::shared_ptr<IDataSeries> results)
39 {
41 {
40 if (m_ParsingOK) {
42 if (m_ParsingOK) {
41 auto scalarSeries = dynamic_cast<ScalarSeries *>(results.get());
43 auto scalarSeries = dynamic_cast<ScalarSeries *>(results.get());
42 QVERIFY(scalarSeries != nullptr);
44 QVERIFY(scalarSeries != nullptr);
43
45
44 // Checks units
46 // Checks units
45 QVERIFY(scalarSeries->xAxisUnit() == m_XAxisUnit);
47 QVERIFY(scalarSeries->xAxisUnit() == m_XAxisUnit);
46 QVERIFY(scalarSeries->valuesUnit() == m_ValuesUnit);
48 QVERIFY(scalarSeries->valuesUnit() == m_ValuesUnit);
47
49
48 // Checks values
50 // Checks values
49 QVERIFY(scalarSeries->xAxisData()->data() == m_XAxisData);
51 QVERIFY(scalarSeries->xAxisData()->data() == m_XAxisData);
50 QVERIFY(scalarSeries->valuesData()->data() == m_ValuesData);
52 QVERIFY(scalarSeries->valuesData()->data() == m_ValuesData);
51 }
53 }
52 else {
54 else {
53 QVERIFY(results == nullptr);
55 QVERIFY(results == nullptr);
54 }
56 }
55 }
57 }
56
58
57 // Parsing was successfully completed
59 // Parsing was successfully completed
58 bool m_ParsingOK{false};
60 bool m_ParsingOK{false};
59 // Expected x-axis unit
61 // Expected x-axis unit
60 Unit m_XAxisUnit{};
62 Unit m_XAxisUnit{};
61 // Expected values unit
63 // Expected values unit
62 Unit m_ValuesUnit{};
64 Unit m_ValuesUnit{};
63 // Expected x-axis data
65 // Expected x-axis data
64 QVector<double> m_XAxisData{};
66 QVector<double> m_XAxisData{};
65 // Expected values data
67 // Expected values data
66 QVector<double> m_ValuesData{};
68 QVector<double> m_ValuesData{};
67 };
69 };
68
70
69 } // namespace
71 } // namespace
70
72
71 Q_DECLARE_METATYPE(ExpectedResults)
73 Q_DECLARE_METATYPE(ExpectedResults)
72
74
73 class TestAmdaResultParser : public QObject {
75 class TestAmdaResultParser : public QObject {
74 Q_OBJECT
76 Q_OBJECT
75 private slots:
77 private slots:
76 /// Input test data
78 /// Input test data
77 /// @sa testTxtJson()
79 /// @sa testTxtJson()
78 void testReadTxt_data();
80 void testReadTxt_data();
79
81
80 /// Tests parsing of a TXT file
82 /// Tests parsing of a TXT file
81 void testReadTxt();
83 void testReadTxt();
82 };
84 };
83
85
84 void TestAmdaResultParser::testReadTxt_data()
86 void TestAmdaResultParser::testReadTxt_data()
85 {
87 {
86 // ////////////// //
88 // ////////////// //
87 // Test structure //
89 // Test structure //
88 // ////////////// //
90 // ////////////// //
89
91
90 // Name of TXT file to read
92 // Name of TXT file to read
91 QTest::addColumn<QString>("inputFileName");
93 QTest::addColumn<QString>("inputFileName");
92 // Expected results
94 // Expected results
93 QTest::addColumn<ExpectedResults>("expectedResults");
95 QTest::addColumn<ExpectedResults>("expectedResults");
94
96
97 // ////////// //
98 // Test cases //
99 // ////////// //
100
101 auto dateTime = [](int year, int month, int day, int hours, int minutes, int seconds) {
102 return QDateTime{{year, month, day}, {hours, minutes, seconds}};
103 };
104
105 // Valid file
106 QTest::newRow("Valid file")
107 << QStringLiteral("ValidScalar1.txt")
108 << ExpectedResults{
109 Unit{QStringLiteral("nT"), true}, Unit{},
110 QVector<QDateTime>{dateTime(2013, 9, 23, 9, 0, 30), dateTime(2013, 9, 23, 9, 1, 30),
111 dateTime(2013, 9, 23, 9, 2, 30), dateTime(2013, 9, 23, 9, 3, 30),
112 dateTime(2013, 9, 23, 9, 4, 30), dateTime(2013, 9, 23, 9, 5, 30),
113 dateTime(2013, 9, 23, 9, 6, 30), dateTime(2013, 9, 23, 9, 7, 30),
114 dateTime(2013, 9, 23, 9, 8, 30), dateTime(2013, 9, 23, 9, 9, 30)},
115 QVector<double>{-2.83950, -2.71850, -2.52150, -2.57633, -2.58050, -2.48325, -2.63025,
116 -2.55800, -2.43250, -2.42200}};
117
118 // Valid files but with some invalid lines (wrong unit, wrong values, etc.)
119 QTest::newRow("No unit file") << QStringLiteral("NoUnit.txt")
120 << ExpectedResults{Unit{QStringLiteral(""), true}, Unit{},
121 QVector<QDateTime>{}, QVector<double>{}};
122 QTest::newRow("Wrong unit file")
123 << QStringLiteral("WrongUnit.txt")
124 << ExpectedResults{Unit{QStringLiteral(""), true}, Unit{},
125 QVector<QDateTime>{dateTime(2013, 9, 23, 9, 0, 30),
126 dateTime(2013, 9, 23, 9, 1, 30),
127 dateTime(2013, 9, 23, 9, 2, 30)},
128 QVector<double>{-2.83950, -2.71850, -2.52150}};
129
130 QTest::newRow("Wrong results file (date of first line is invalid")
131 << QStringLiteral("WrongDate.txt")
132 << ExpectedResults{
133 Unit{QStringLiteral("nT"), true}, Unit{},
134 QVector<QDateTime>{dateTime(2013, 9, 23, 9, 1, 30), dateTime(2013, 9, 23, 9, 2, 30)},
135 QVector<double>{-2.71850, -2.52150}};
136
137 QTest::newRow("Wrong results file (too many values for first line")
138 << QStringLiteral("TooManyValues.txt")
139 << ExpectedResults{
140 Unit{QStringLiteral("nT"), true}, Unit{},
141 QVector<QDateTime>{dateTime(2013, 9, 23, 9, 1, 30), dateTime(2013, 9, 23, 9, 2, 30)},
142 QVector<double>{-2.71850, -2.52150}};
143
144 QTest::newRow("Wrong results file (value of first line is invalid")
145 << QStringLiteral("WrongValue.txt")
146 << ExpectedResults{
147 Unit{QStringLiteral("nT"), true}, Unit{},
148 QVector<QDateTime>{dateTime(2013, 9, 23, 9, 1, 30), dateTime(2013, 9, 23, 9, 2, 30)},
149 QVector<double>{-2.71850, -2.52150}};
150
151 QTest::newRow("Wrong results file (value of first line is NaN")
152 << QStringLiteral("NaNValue.txt")
153 << ExpectedResults{
154 Unit{QStringLiteral("nT"), true}, Unit{},
155 QVector<QDateTime>{dateTime(2013, 9, 23, 9, 1, 30), dateTime(2013, 9, 23, 9, 2, 30)},
156 QVector<double>{-2.71850, -2.52150}};
157
158 // Invalid file
159 QTest::newRow("Invalid file (unexisting file)")
160 << QStringLiteral("UnexistingFile.txt") << ExpectedResults{};
95 }
161 }
96
162
97 void TestAmdaResultParser::testReadTxt()
163 void TestAmdaResultParser::testReadTxt()
98 {
164 {
99 QFETCH(QString, inputFileName);
165 QFETCH(QString, inputFileName);
100 QFETCH(ExpectedResults, expectedResults);
166 QFETCH(ExpectedResults, expectedResults);
101
167
102 // Parses file
168 // Parses file
103 auto filePath = inputFilePath(inputFileName);
169 auto filePath = inputFilePath(inputFileName);
104 auto results = AmdaResultParser::readTxt(filePath);
170 auto results = AmdaResultParser::readTxt(filePath);
105
171
106 // ///////////////// //
172 // ///////////////// //
107 // Validates results //
173 // Validates results //
108 // ///////////////// //
174 // ///////////////// //
109 expectedResults.validate(results);
175 expectedResults.validate(results);
110 }
176 }
111
177
112 QTEST_MAIN(TestAmdaResultParser)
178 QTEST_MAIN(TestAmdaResultParser)
113 #include "TestAmdaResultParser.moc"
179 #include "TestAmdaResultParser.moc"
General Comments 0
You need to be logged in to leave comments. Login now