##// 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 1 #include "AmdaResultParser.h"
2 2
3 #include <Data/ScalarSeries.h>
4
3 5 #include <QObject>
4 6 #include <QtTest>
5 7
6 8 namespace {
7 9
8 10 /// Path for the tests
9 11 const auto TESTS_RESOURCES_PATH
10 12 = QFileInfo{QString{AMDA_TESTS_RESOURCES_DIR}, "TestAmdaResultParser"}.absoluteFilePath();
11 13
12 14 QString inputFilePath(const QString &inputFileName)
13 15 {
14 16 return QFileInfo{TESTS_RESOURCES_PATH, inputFileName}.absoluteFilePath();
15 17 }
16 18
17 19 struct ExpectedResults {
18 20 explicit ExpectedResults() = default;
19 21
20 22 /// Ctor with QVector<QDateTime> as x-axis data. Datetimes are converted to doubles
21 23 explicit ExpectedResults(Unit xAxisUnit, Unit valuesUnit, const QVector<QDateTime> &xAxisData,
22 24 QVector<double> valuesData)
23 25 : m_ParsingOK{true},
24 26 m_XAxisUnit{xAxisUnit},
25 27 m_ValuesUnit{valuesUnit},
26 28 m_XAxisData{},
27 29 m_ValuesData{std::move(valuesData)}
28 30 {
29 31 // Converts QVector<QDateTime> to QVector<double>
30 32 std::transform(xAxisData.cbegin(), xAxisData.cend(), std::back_inserter(m_XAxisData),
31 33 [](const auto &dateTime) { return dateTime.toMSecsSinceEpoch() / 1000.; });
32 34 }
33 35
34 36 /**
35 37 * Validates a DataSeries compared to the expected results
36 38 * @param results the DataSeries to validate
37 39 */
38 40 void validate(std::shared_ptr<IDataSeries> results)
39 41 {
40 42 if (m_ParsingOK) {
41 43 auto scalarSeries = dynamic_cast<ScalarSeries *>(results.get());
42 44 QVERIFY(scalarSeries != nullptr);
43 45
44 46 // Checks units
45 47 QVERIFY(scalarSeries->xAxisUnit() == m_XAxisUnit);
46 48 QVERIFY(scalarSeries->valuesUnit() == m_ValuesUnit);
47 49
48 50 // Checks values
49 51 QVERIFY(scalarSeries->xAxisData()->data() == m_XAxisData);
50 52 QVERIFY(scalarSeries->valuesData()->data() == m_ValuesData);
51 53 }
52 54 else {
53 55 QVERIFY(results == nullptr);
54 56 }
55 57 }
56 58
57 59 // Parsing was successfully completed
58 60 bool m_ParsingOK{false};
59 61 // Expected x-axis unit
60 62 Unit m_XAxisUnit{};
61 63 // Expected values unit
62 64 Unit m_ValuesUnit{};
63 65 // Expected x-axis data
64 66 QVector<double> m_XAxisData{};
65 67 // Expected values data
66 68 QVector<double> m_ValuesData{};
67 69 };
68 70
69 71 } // namespace
70 72
71 73 Q_DECLARE_METATYPE(ExpectedResults)
72 74
73 75 class TestAmdaResultParser : public QObject {
74 76 Q_OBJECT
75 77 private slots:
76 78 /// Input test data
77 79 /// @sa testTxtJson()
78 80 void testReadTxt_data();
79 81
80 82 /// Tests parsing of a TXT file
81 83 void testReadTxt();
82 84 };
83 85
84 86 void TestAmdaResultParser::testReadTxt_data()
85 87 {
86 88 // ////////////// //
87 89 // Test structure //
88 90 // ////////////// //
89 91
90 92 // Name of TXT file to read
91 93 QTest::addColumn<QString>("inputFileName");
92 94 // Expected results
93 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 163 void TestAmdaResultParser::testReadTxt()
98 164 {
99 165 QFETCH(QString, inputFileName);
100 166 QFETCH(ExpectedResults, expectedResults);
101 167
102 168 // Parses file
103 169 auto filePath = inputFilePath(inputFileName);
104 170 auto results = AmdaResultParser::readTxt(filePath);
105 171
106 172 // ///////////////// //
107 173 // Validates results //
108 174 // ///////////////// //
109 175 expectedResults.validate(results);
110 176 }
111 177
112 178 QTEST_MAIN(TestAmdaResultParser)
113 179 #include "TestAmdaResultParser.moc"
General Comments 0
You need to be logged in to leave comments. Login now