##// END OF EJS Templates
Implements validation of variable's data (2)...
Alexandre Leroux -
r1199:a199540869ec
parent child
Show More
@@ -1,141 +1,159
1 #include "FuzzingValidators.h"
1 #include "FuzzingValidators.h"
2 #include "FuzzingDefs.h"
3
4 #include <Data/DataSeries.h>
2 #include <Variable/Variable.h>
5 #include <Variable/Variable.h>
3
6
4 #include <QTest>
7 #include <QTest>
5
8
6 #include <functional>
9 #include <functional>
7
10
8 Q_LOGGING_CATEGORY(LOG_FuzzingValidators, "FuzzingValidators")
11 Q_LOGGING_CATEGORY(LOG_FuzzingValidators, "FuzzingValidators")
9
12
10 namespace {
13 namespace {
11
14
12 // ////////////// //
15 // ////////////// //
13 // DATA VALIDATOR //
16 // DATA VALIDATOR //
14 // ////////////// //
17 // ////////////// //
15
18
16 /// Singleton used to validate data of a variable
19 /// Singleton used to validate data of a variable
17 class DataValidatorHelper {
20 class DataValidatorHelper {
18 public:
21 public:
19 /// @return the single instance of the helper
22 /// @return the single instance of the helper
20 static DataValidatorHelper &instance();
23 static DataValidatorHelper &instance();
21 virtual ~DataValidatorHelper() noexcept = default;
24 virtual ~DataValidatorHelper() noexcept = default;
22
25
23 virtual void validate(const VariableState &variableState) const = 0;
26 virtual void validate(const VariableState &variableState) const = 0;
24 };
27 };
25
28
26 /**
29 /**
27 * Default implementation of @sa DataValidatorHelper
30 * Default implementation of @sa DataValidatorHelper
28 */
31 */
29 class DefaultDataValidatorHelper : public DataValidatorHelper {
32 class DefaultDataValidatorHelper : public DataValidatorHelper {
30 public:
33 public:
31 void validate(const VariableState &variableState) const override
34 void validate(const VariableState &variableState) const override
32 {
35 {
33 Q_UNUSED(variableState);
36 Q_UNUSED(variableState);
34 qCWarning(LOG_FuzzingValidators()).noquote() << "Checking variable's data... WARN: no data "
37 qCWarning(LOG_FuzzingValidators()).noquote() << "Checking variable's data... WARN: no data "
35 "verification is available for this server";
38 "verification is available for this server";
36 }
39 }
37 };
40 };
38
41
39 /**
42 /**
40 * Implementation of @sa DataValidatorHelper for the local AMDA server
43 * Implementation of @sa DataValidatorHelper for the local AMDA server
41 */
44 */
42 class LocalhostServerDataValidatorHelper : public DataValidatorHelper {
45 class LocalhostServerDataValidatorHelper : public DataValidatorHelper {
43 public:
46 public:
44 void validate(const VariableState &variableState) const override
47 void validate(const VariableState &variableState) const override
45 {
48 {
46 /// @todo: complete
49 // Don't check data for null variable
50 if (!variableState.m_Variable || variableState.m_Range == INVALID_RANGE) {
51 return;
52 }
53
54 auto message = "Checking variable's data...";
55 auto toDateString = [](double value) { return DateUtils::dateTime(value).toString(); };
56
57 // Checks that data are defined
58 auto variableDataSeries = variableState.m_Variable->dataSeries();
59 if (variableDataSeries == nullptr && variableState.m_Range != INVALID_RANGE) {
60 qCInfo(LOG_FuzzingValidators()).noquote()
61 << message << "FAIL: the variable has no data while a range is defined";
62 QFAIL("");
63 }
64
47 }
65 }
48 };
66 };
49
67
50 /// Creates the @sa DataValidatorHelper according to the server passed in parameter
68 /// Creates the @sa DataValidatorHelper according to the server passed in parameter
51 std::unique_ptr<DataValidatorHelper> createDataValidatorInstance(const QString &server)
69 std::unique_ptr<DataValidatorHelper> createDataValidatorInstance(const QString &server)
52 {
70 {
53 if (server == QString{"localhost"}) {
71 if (server == QString{"localhost"}) {
54 return std::make_unique<LocalhostServerDataValidatorHelper>();
72 return std::make_unique<LocalhostServerDataValidatorHelper>();
55 }
73 }
56 else {
74 else {
57 return std::make_unique<DefaultDataValidatorHelper>();
75 return std::make_unique<DefaultDataValidatorHelper>();
58 }
76 }
59 }
77 }
60
78
61 DataValidatorHelper &DataValidatorHelper::instance()
79 DataValidatorHelper &DataValidatorHelper::instance()
62 {
80 {
63 // Creates instance depending on the SCIQLOP_AMDA_SERVER value at compile time
81 // Creates instance depending on the SCIQLOP_AMDA_SERVER value at compile time
64 static auto instance = createDataValidatorInstance(SCIQLOP_AMDA_SERVER);
82 static auto instance = createDataValidatorInstance(SCIQLOP_AMDA_SERVER);
65 return *instance;
83 return *instance;
66 }
84 }
67
85
68 // /////////////// //
86 // /////////////// //
69 // RANGE VALIDATOR //
87 // RANGE VALIDATOR //
70 // /////////////// //
88 // /////////////// //
71
89
72 /**
90 /**
73 * Checks that a range of a variable matches the expected range passed as a parameter
91 * Checks that a range of a variable matches the expected range passed as a parameter
74 * @param variable the variable for which to check the range
92 * @param variable the variable for which to check the range
75 * @param expectedRange the expected range
93 * @param expectedRange the expected range
76 * @param getVariableRangeFun the function to retrieve the range from the variable
94 * @param getVariableRangeFun the function to retrieve the range from the variable
77 * @remarks if the variable is null, checks that the expected range is the invalid range
95 * @remarks if the variable is null, checks that the expected range is the invalid range
78 */
96 */
79 void validateRange(std::shared_ptr<Variable> variable, const SqpRange &expectedRange,
97 void validateRange(std::shared_ptr<Variable> variable, const SqpRange &expectedRange,
80 std::function<SqpRange(const Variable &)> getVariableRangeFun)
98 std::function<SqpRange(const Variable &)> getVariableRangeFun)
81 {
99 {
82 auto compare = [](const auto &range, const auto &expectedRange, const auto &message) {
100 auto compare = [](const auto &range, const auto &expectedRange, const auto &message) {
83 if (range == expectedRange) {
101 if (range == expectedRange) {
84 qCInfo(LOG_FuzzingValidators()).noquote() << message << "OK";
102 qCInfo(LOG_FuzzingValidators()).noquote() << message << "OK";
85 }
103 }
86 else {
104 else {
87 qCInfo(LOG_FuzzingValidators()).noquote()
105 qCInfo(LOG_FuzzingValidators()).noquote()
88 << message << "FAIL (current range:" << range
106 << message << "FAIL (current range:" << range
89 << ", expected range:" << expectedRange << ")";
107 << ", expected range:" << expectedRange << ")";
90 QFAIL("");
108 QFAIL("");
91 }
109 }
92 };
110 };
93
111
94 if (variable) {
112 if (variable) {
95 compare(getVariableRangeFun(*variable), expectedRange, "Checking variable's range...");
113 compare(getVariableRangeFun(*variable), expectedRange, "Checking variable's range...");
96 }
114 }
97 else {
115 else {
98 compare(INVALID_RANGE, expectedRange, "Checking that there is no range set...");
116 compare(INVALID_RANGE, expectedRange, "Checking that there is no range set...");
99 }
117 }
100 }
118 }
101
119
102 /**
120 /**
103 * Default implementation of @sa IFuzzingValidator. This validator takes as parameter of its
121 * Default implementation of @sa IFuzzingValidator. This validator takes as parameter of its
104 * construction a function of validation which is called in the validate() method
122 * construction a function of validation which is called in the validate() method
105 */
123 */
106 class FuzzingValidator : public IFuzzingValidator {
124 class FuzzingValidator : public IFuzzingValidator {
107 public:
125 public:
108 /// Signature of a validation function
126 /// Signature of a validation function
109 using ValidationFunction = std::function<void(const VariableState &variableState)>;
127 using ValidationFunction = std::function<void(const VariableState &variableState)>;
110
128
111 explicit FuzzingValidator(ValidationFunction fun) : m_Fun(std::move(fun)) {}
129 explicit FuzzingValidator(ValidationFunction fun) : m_Fun(std::move(fun)) {}
112
130
113 void validate(const VariableState &variableState) const override { m_Fun(variableState); }
131 void validate(const VariableState &variableState) const override { m_Fun(variableState); }
114
132
115 private:
133 private:
116 ValidationFunction m_Fun;
134 ValidationFunction m_Fun;
117 };
135 };
118
136
119 } // namespace
137 } // namespace
120
138
121 std::unique_ptr<IFuzzingValidator> FuzzingValidatorFactory::create(FuzzingValidatorType type)
139 std::unique_ptr<IFuzzingValidator> FuzzingValidatorFactory::create(FuzzingValidatorType type)
122 {
140 {
123 switch (type) {
141 switch (type) {
124 case FuzzingValidatorType::DATA:
142 case FuzzingValidatorType::DATA:
125 return std::make_unique<FuzzingValidator>([](const VariableState &variableState) {
143 return std::make_unique<FuzzingValidator>([](const VariableState &variableState) {
126 DataValidatorHelper::instance().validate(variableState);
144 DataValidatorHelper::instance().validate(variableState);
127 });
145 });
128 case FuzzingValidatorType::RANGE:
146 case FuzzingValidatorType::RANGE:
129 return std::make_unique<FuzzingValidator>([](const VariableState &variableState) {
147 return std::make_unique<FuzzingValidator>([](const VariableState &variableState) {
130 auto getVariableRange = [](const Variable &variable) { return variable.range(); };
148 auto getVariableRange = [](const Variable &variable) { return variable.range(); };
131 validateRange(variableState.m_Variable, variableState.m_Range, getVariableRange);
149 validateRange(variableState.m_Variable, variableState.m_Range, getVariableRange);
132 });
150 });
133 default:
151 default:
134 // Default case returns invalid validator
152 // Default case returns invalid validator
135 break;
153 break;
136 }
154 }
137
155
138 // Invalid validator
156 // Invalid validator
139 return std::make_unique<FuzzingValidator>(
157 return std::make_unique<FuzzingValidator>(
140 [](const VariableState &) { QFAIL("Invalid validator"); });
158 [](const VariableState &) { QFAIL("Invalid validator"); });
141 }
159 }
General Comments 0
You need to be logged in to leave comments. Login now