1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
|
/*
* (C) Copyright 2025- ECMWF.
*
* This software is licensed under the terms of the Apache Licence Version 2.0
* which can be obtained at http://www.apache.org/licenses/LICENSE-2.0.
* In applying this licence, ECMWF does not waive the privileges and immunities
* granted to it by virtue of its status as an intergovernmental organisation
* nor does it submit to any jurisdiction.
*/
#include "eckit/testing/Test.h"
#include "metkit/mars/MarsRequest.h"
#include "metkit/mars/Matcher.h"
using namespace eckit::testing;
using namespace metkit::mars;
namespace metkit::test {
// ----------------------------------------------------------------------------------------------------------------------
CASE("parse string with spaces") {
std::map<std::string, eckit::Regex> map =
parseMatchString("expver=(x[0-9a-z]{3}), number = (1|2) , stream= ^enfo$ ");
EXPECT(map.size() == 3);
EXPECT(map.find("expver") != map.end());
EXPECT(map.find("number") != map.end());
EXPECT(map.find("stream") != map.end());
}
CASE("parse string errors") {
// Check basic parsing
EXPECT_THROWS_AS(parseMatchString(""), eckit::BadValue);
EXPECT_THROWS_AS(parseMatchString("expver"), eckit::BadValue);
EXPECT_THROWS_AS(parseMatchString("=expver"), eckit::BadValue);
EXPECT_THROWS_AS(parseMatchString("expver="), eckit::BadValue);
EXPECT_THROWS_AS(parseMatchString("expver,number=(1|2)"), eckit::BadValue);
EXPECT_THROWS_AS(parseMatchString("number=(1|2),number=(3|4)"), eckit::BadValue);
}
CASE("match basic") {
Matcher match_any("expver=(x[0-9a-z]{3}),number=(1|2),stream=^enfo$", Matcher::Policy::Any);
Matcher match_all("expver=(x[0-9a-z]{3}),number=(1|2),stream=^enfo$", Matcher::Policy::All);
MarsRequest req("retrieve");
req.setValue("expver", "xxxx");
req.values("number", {"1", "2"});
req.setValue("stream", "enfo");
req.setValue("step ", "0"); // step is not in the matcher. This should have no effect on matching
EXPECT_EQUAL(req.count(), 2);
EXPECT_EQUAL(match_any.match(req), true);
EXPECT_EQUAL(match_all.match(req), true);
}
CASE("partially matching request") {
Matcher match_any("expver=(x[0-9a-z]{3}),number=(1|2),stream=^enfo$", Matcher::Policy::Any);
Matcher match_all("expver=(x[0-9a-z]{3}),number=(1|2),stream=^enfo$", Matcher::Policy::All);
MarsRequest req("retrieve");
req.setValue("expver", "xxxx");
req.values("number", {"1", "2", "3"}); // number=3 does not match, the others do
req.setValue("stream", "enfo");
EXPECT_EQUAL(req.count(), 3);
EXPECT_EQUAL(match_any.match(req), true);
EXPECT_EQUAL(match_all.match(req), false);
}
CASE("request entirely not matching") {
Matcher match_any("expver=(x[0-9a-z]{3}),number=(1|2),stream=^enfo$", Matcher::Policy::Any);
Matcher match_all("expver=(x[0-9a-z]{3}),number=(1|2),stream=^enfo$", Matcher::Policy::All);
MarsRequest req("retrieve");
req.setValue("expver", "yyyy"); // expver=yyyy does not match
req.values("number", {"1", "2"});
req.setValue("stream", "enfo");
EXPECT_EQUAL(req.count(), 2);
EXPECT_EQUAL(match_any.match(req), false);
EXPECT_EQUAL(match_all.match(req), false);
}
CASE("match with missing keys") {
Matcher match_any("expver=(x[0-9a-z]{3}),number=(1|2),stream=^enfo$", Matcher::Policy::Any);
Matcher match_all("expver=(x[0-9a-z]{3}),number=(1|2),stream=^enfo$", Matcher::Policy::All);
MarsRequest req("retrieve");
req.setValue("expver", "xxxx");
req.values("number", {"1", "2"});
// stream is not set: matching depends on MatchMissingPolicy
EXPECT_EQUAL(req.count(), 2);
EXPECT_EQUAL(match_any.match(req, Matcher::DontMatchOnMissing), false);
EXPECT_EQUAL(match_all.match(req, Matcher::DontMatchOnMissing), false);
EXPECT_EQUAL(match_any.match(req, Matcher::MatchOnMissing), true);
EXPECT_EQUAL(match_all.match(req, Matcher::MatchOnMissing), true);
}
CASE("match missing key and wrong values") {
// -- Combinations of the above some of the above
Matcher match_any("expver=(x[0-9a-z]{3}),number=(1|2),stream=^enfo$", Matcher::Policy::Any);
Matcher match_all("expver=(x[0-9a-z]{3}),number=(1|2),stream=^enfo$", Matcher::Policy::All);
MarsRequest req("retrieve");
req.setValue("expver", "yyyy"); // yyyy does not match
req.values("number", {"1", "2", "3"}); // number=3 does not match
EXPECT_EQUAL(req.count(), 3);
EXPECT_EQUAL(match_any.match(req, Matcher::DontMatchOnMissing), false);
EXPECT_EQUAL(match_all.match(req, Matcher::DontMatchOnMissing), false);
EXPECT_EQUAL(match_any.match(req, Matcher::MatchOnMissing), false);
EXPECT_EQUAL(match_all.match(req, Matcher::MatchOnMissing), false);
}
CASE("match missing key but some values match") {
// Missing key, but expver matches, number partially matches
Matcher match_any("expver=(x[0-9a-z]{3}),number=(1|2),stream=^enfo$", Matcher::Policy::Any);
Matcher match_all("expver=(x[0-9a-z]{3}),number=(1|2),stream=^enfo$", Matcher::Policy::All);
MarsRequest req("retrieve");
req.setValue("expver", "xxxx");
req.values("number", {"1", "2", "3"});
EXPECT_EQUAL(match_any.match(req, Matcher::DontMatchOnMissing), false);
EXPECT_EQUAL(match_all.match(req, Matcher::DontMatchOnMissing), false);
EXPECT_EQUAL(match_any.match(req, Matcher::MatchOnMissing), true);
EXPECT_EQUAL(match_all.match(req, Matcher::MatchOnMissing), false);
}
CASE("match empty request") {
// Testing that this does not raise an exception
Matcher match_any("expver=(x[0-9a-z]{3}),number=(1|2),stream=^enfo$", Matcher::Policy::Any);
Matcher match_all("expver=(x[0-9a-z]{3}),number=(1|2),stream=^enfo$", Matcher::Policy::All);
MarsRequest req("retrieve");
EXPECT_EQUAL(match_any.match(req, Matcher::DontMatchOnMissing), false);
EXPECT_EQUAL(match_all.match(req, Matcher::DontMatchOnMissing), false);
EXPECT_EQUAL(match_any.match(req, Matcher::MatchOnMissing), true);
EXPECT_EQUAL(match_all.match(req, Matcher::MatchOnMissing), true);
}
// ----------------------------------------------------------------------------------------------------------------------
} // namespace metkit::test
int main(int argc, char** argv) {
return eckit::testing::run_tests(argc, argv);
}
|