##// END OF EJS Templates
Makes test cases
Alexandre Leroux -
r359:751f52f38d58
parent child
Show More
@@ -0,0 +1,4
1 "data": [
2 "root1": "root",
3 "root2": "root"
4 ] No newline at end of file
@@ -0,0 +1,63
1 {
2 "dataCenter": {
3 "name": "AMDA",
4 "desc": "AMDA_Internal_Data_Base",
5 "xml:id": "myLocalData-treeRootNode",
6 "mission": {
7 "att": "",
8 "name": "ICE@Giacobini-Zinner",
9 "rank": "93",
10 "xml:id": "ICE@Giacobini-Zinner",
11 "desc": "International Cometary Explorer",
12 "target": "Comet",
13 "available": "1",
14 "instrument": {
15 "att": "",
16 "name": "MAG",
17 "xml:id": "ICE@Giacobini-Zinner:MAG",
18 "desc": "Vector Helium Magnetometer",
19 "restricted": "",
20 "dataset": {
21 "att": "",
22 "restricted": "",
23 "name": "Magnetic Field",
24 "xml:id": "ice:mag:p21",
25 "sampling": "0.3s",
26 "maxSampling": "",
27 "dataStart": "1985/09/10",
28 "dataStop": "1985/09/14",
29 "dataSource": "PDS",
30 "target": "",
31 "parameter": [
32 {
33 "name": "B_cse",
34 "units": "nT",
35 "display_type": "",
36 "xml:id": "ice_b_cse",
37 "component": [
38 {
39 "name": "Bx",
40 "xml:id": "ice_b_cse(0)"
41 },
42 {
43 "name": "By",
44 "xml:id": "ice_b_cse(1)"
45 },
46 {
47 "name": "Bz",
48 "xml:id": "ice_b_cse(2)"
49 }
50 ]
51 },
52 {
53 "name": "|B|",
54 "display_type": "",
55 "units": "nT",
56 "xml:id": "ice_b_tot"
57 }
58 ]
59 }
60 }
61 }
62 }
63 } No newline at end of file
@@ -0,0 +1,5
1 {
2 "wrongRoot": {
3 "name": "AMDA"
4 }
5 } No newline at end of file
@@ -0,0 +1,3
1 {
2 "dataCenter": "invalidType"
3 } No newline at end of file
@@ -1,82 +1,176
1 1 #include "AmdaParser.h"
2 2
3 3 #include <DataSource/DataSourceItem.h>
4 4
5 5 #include <QObject>
6 6 #include <QtTest>
7 7
8 8 #include <QString>
9 9
10 10 namespace {
11 11
12 12 /// Path for the tests
13 13 const auto TESTS_RESOURCES_PATH
14 14 = QFileInfo{QString{AMDA_TESTS_RESOURCES_DIR}, "TestAmdaParser"}.absoluteFilePath();
15 15
16 16 QString inputFilePath(const QString &inputFileName)
17 17 {
18 18 return QFileInfo{TESTS_RESOURCES_PATH, inputFileName}.absoluteFilePath();
19 19 }
20 20
21 21 struct ExpectedResults {
22 22 explicit ExpectedResults() = default;
23 23 explicit ExpectedResults(std::shared_ptr<DataSourceItem> item)
24 24 : m_ParsingOK{true}, m_Item{std::move(item)}
25 25 {
26 26 }
27 27
28 28 // Parsing was successfully completed
29 29 bool m_ParsingOK{false};
30 30 // Expected item after parsing
31 31 std::shared_ptr<DataSourceItem> m_Item{nullptr};
32 32 };
33 33
34 // ///////////////////////////////// //
35 // Set of expected results for tests //
36 // ///////////////////////////////// //
37
38 ExpectedResults validResults1()
39 {
40 auto component1 = std::make_unique<DataSourceItem>(
41 DataSourceItemType::COMPONENT,
42 QHash<QString, QVariant>{{"name", "Bx"}, {"xml:id", "ice_b_cse(0)"}});
43 auto component2 = std::make_unique<DataSourceItem>(
44 DataSourceItemType::COMPONENT,
45 QHash<QString, QVariant>{{"name", "By"}, {"xml:id", "ice_b_cse(1)"}});
46 auto component3 = std::make_unique<DataSourceItem>(
47 DataSourceItemType::COMPONENT,
48 QHash<QString, QVariant>{{"name", "Bz"}, {"xml:id", "ice_b_cse(2)"}});
49 auto parameter1 = std::make_unique<DataSourceItem>(
50 DataSourceItemType::PRODUCT,
51 QHash<QString, QVariant>{
52 {"name", "B_cse"}, {"units", "nT"}, {"display_type", ""}, {"xml:id", "ice_b_cse"}});
53 parameter1->appendChild(std::move(component1));
54 parameter1->appendChild(std::move(component2));
55 parameter1->appendChild(std::move(component3));
56
57 auto parameter2 = std::make_unique<DataSourceItem>(
58 DataSourceItemType::PRODUCT,
59 QHash<QString, QVariant>{
60 {"name", "|B|"}, {"units", "nT"}, {"display_type", ""}, {"xml:id", "ice_b_tot"}});
61
62 auto dataset = std::make_unique<DataSourceItem>(
63 DataSourceItemType::NODE, QHash<QString, QVariant>{{"att", ""},
64 {"restricted", ""},
65 {"name", "Magnetic Field"},
66 {"xml:id", "ice:mag:p21"},
67 {"sampling", "0.3s"},
68 {"maxSampling", ""},
69 {"dataStart", "1985/09/10"},
70 {"dataStop", "1985/09/14"},
71 {"dataSource", "PDS"},
72 {"target", ""}});
73 dataset->appendChild(std::move(parameter1));
74 dataset->appendChild(std::move(parameter2));
75
76 auto instrument = std::make_unique<DataSourceItem>(
77 DataSourceItemType::NODE, QHash<QString, QVariant>{{"att", ""},
78 {"name", "MAG"},
79 {"xml:id", "ICE@Giacobini-Zinner:MAG"},
80 {"desc", "Vector Helium Magnetometer"},
81 {"restricted", ""}});
82 instrument->appendChild(std::move(dataset));
83
84 auto mission = std::make_unique<DataSourceItem>(
85 DataSourceItemType::NODE,
86 QHash<QString, QVariant>{{"att", ""},
87 {"name", "ICE@Giacobini-Zinner"},
88 {"rank", "93"},
89 {"xml:id", "ICE@Giacobini-Zinner"},
90 {"desc", "International Cometary Explorer"},
91 {"target", "Comet"},
92 {"available", "1"}});
93 mission->appendChild(std::move(instrument));
94
95 auto item = std::make_shared<DataSourceItem>(DataSourceItemType::NODE,
96 QHash<QString, QVariant>{
97 {"name", "AMDA"},
98 {"desc", "AMDA_Internal_Data_Base"},
99 {"xml:id", "myLocalData-treeRootNode"},
100 });
101 item->appendChild(std::move(mission));
102
103 return ExpectedResults{item};
104 }
105
106 ExpectedResults invalidResults()
107 {
108 return ExpectedResults{};
109 }
110
34 111 } // namespace
35 112
36 113 Q_DECLARE_METATYPE(ExpectedResults)
37 114
38 115 class TestAmdaParser : public QObject {
39 116 Q_OBJECT
40 117 private slots:
41 118 /// Input test data
42 119 /// @sa testReadJson()
43 120 void testReadJson_data();
44 121
45 122 /// Tests parsing of a JSON file
46 123 void testReadJson();
47 124 };
48 125
49 126 void TestAmdaParser::testReadJson_data()
50 127 {
51 128 // ////////////// //
52 129 // Test structure //
53 130 // ////////////// //
54 131
55 132 // Name of JSON file to read
56 133 QTest::addColumn<QString>("inputFileName");
57 134 // Expected results
58 135 QTest::addColumn<ExpectedResults>("expectedResults");
136
137 // ////////// //
138 // Test cases //
139 // ////////// //
140
141 // Valid file
142 QTest::newRow("Valid file") << QStringLiteral("ValidFile1.json") << validResults1();
143
144 // Invalid files
145 QTest::newRow("Invalid file (unexisting file)")
146 << QStringLiteral("UnexistingFile.json") << invalidResults();
147 QTest::newRow("Invalid file (two root objects)")
148 << QStringLiteral("TwoRootsFile.json") << invalidResults();
149 QTest::newRow("Invalid file (wrong root key)")
150 << QStringLiteral("WrongRootKey.json") << invalidResults();
151 QTest::newRow("Invalid file (wrong root type)")
152 << QStringLiteral("WrongRootType.json") << invalidResults();
59 153 }
60 154
61 155 void TestAmdaParser::testReadJson()
62 156 {
63 157 QFETCH(QString, inputFileName);
64 158 QFETCH(ExpectedResults, expectedResults);
65 159
66 160 // Parses file
67 161 auto filePath = inputFilePath(inputFileName);
68 162 auto item = AmdaParser::readJson(filePath);
69 163
70 164 // Validates results
71 165 if (expectedResults.m_ParsingOK) {
72 166 QVERIFY(item != nullptr);
73 167 QVERIFY(expectedResults.m_Item != nullptr);
74 168 QVERIFY(*item == *expectedResults.m_Item);
75 169 }
76 170 else {
77 171 QVERIFY(item == nullptr);
78 172 }
79 173 }
80 174
81 175 QTEST_MAIN(TestAmdaParser)
82 176 #include "TestAmdaParser.moc"
General Comments 0
You need to be logged in to leave comments. Login now