Skip to content

Commit

Permalink
improved unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
danovaro committed Nov 27, 2024
1 parent 804472a commit 75f43be
Show file tree
Hide file tree
Showing 2 changed files with 295 additions and 37 deletions.
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.11.22
1.11.23
330 changes: 294 additions & 36 deletions tests/test_expand.cc
Original file line number Diff line number Diff line change
Expand Up @@ -30,65 +30,183 @@ namespace test {

//-----------------------------------------------------------------------------

CASE( "test_metkit_expand_1" ) {
const char* text = "ret,date=-5/to/-1";
MarsRequest r = MarsRequest::parse(text);
r.dump(std::cout);
void expand(const MarsRequest& r, const std::string& verb, std::map<std::string, std::vector<std::string>> expected, std::vector<long> dates) {
// MarsExpension exp(false);
// MarsRequest r = exp.expand(req);
std::cout << "comparing " << r << " with " << expected << " dates " << dates << std::endl;
ASSERT(r.verb() == verb);
for(const auto& [key, vals] : expected) {
ASSERT(r.has(key));
auto vv = r.values(key);
ASSERT(vv.size() == vals.size());
for (int i=0; i<vv.size(); i++) {
ASSERT(vv.at(i) == vals.at(i));
}
}
if (dates.size() > 0) {
ASSERT(r.has("date"));
auto dd = r.values("date");
ASSERT(dd.size() == dates.size());
for (int i=0; i<dates.size(); i++) {
long d = dates.at(i);
if (d<0) {
eckit::Date day(d);
d = day.yyyymmdd();
}
ASSERT(dd.at(i) == std::to_string(d));
}
}
}


CASE( "test_metkit_expand_2" ) {
const char* text = "ret";
MarsRequest r = MarsRequest::parse(text);
r.dump(std::cout);

const std::vector< std::string >& dates = r.values("date");
EXPECT(dates.size() == 1);

eckit::Date yesterday(-1);
std::ostringstream oss;

oss << yesterday.yyyymmdd();
EXPECT(dates[0] == oss.str());

void expand(const std::string& text, const std::string& verb, std::map<std::string, std::vector<std::string>> expected, std::vector<long> dates) {
MarsRequest r = MarsRequest::parse(text, true);
expand(r, verb, expected, dates);
}

CASE( "test_metkit_expand_1" ) {
const char* text = "ret,date=-5/to/-1.";
std::map<std::string, std::vector<std::string>> expected{
{"class", {"od"}},
{"domain", {"g"}},
{"expver", {"0001"}},
{"levelist", {"1000","850","700","500","400","300"}},
{"levtype", {"pl"}},
{"param", {"129"}},
{"step", {"0"}},
{"stream", {"oper"}},
{"time", {"1200"}},
{"type", {"an"}}
};
expand(text, "retrieve", expected, {-5,-4,-3,-2,-1});
}

// CASE( "test_metkit_expand_2" ) {
// const char* text = "ret";
// std::map<std::string, std::vector<std::string>> expected{
// {"class", {"od"}},
// {"domain", {"g"}},
// {"expver", {"0001"}},
// {"levelist", {"1000","850","700","500","400","300"}},
// {"levtype", {"pl"}},
// {"param", {"129"}},
// {"step", {"0"}},
// {"stream", {"oper"}},
// {"time", {"1200"}},
// {"type", {"an"}}
// };
// expand(text, "retrieve", expected, {-1});
// }

CASE( "test_metkit_expand_3" ) {
const char* text = "ret,date=-5/to/-1,grid=n640";
MarsRequest r = MarsRequest::parse(text);
r.dump(std::cout);
std::map<std::string, std::vector<std::string>> expected{
{"class", {"od"}},
{"domain", {"g"}},
{"expver", {"0001"}},
{"levelist", {"1000","850","700","500","400","300"}},
{"levtype", {"pl"}},
{"param", {"129"}},
{"step", {"0"}},
{"stream", {"oper"}},
{"time", {"1200"}},
{"type", {"an"}},
{"grid", {"N640"}}
};
expand(text, "retrieve", expected, {-5,-4,-3,-2,-1});
}


CASE( "test_metkit_expand_4" ) {
const char* text = "ret,date=-5/to/-1,grid=o640";
MarsRequest r = MarsRequest::parse(text);
r.dump(std::cout);
std::map<std::string, std::vector<std::string>> expected{
{"class", {"od"}},
{"domain", {"g"}},
{"expver", {"0001"}},
{"levelist", {"1000","850","700","500","400","300"}},
{"levtype", {"pl"}},
{"param", {"129"}},
{"step", {"0"}},
{"stream", {"oper"}},
{"time", {"1200"}},
{"type", {"an"}},
{"grid", {"O640"}}
};
expand(text, "retrieve", expected, {-5,-4,-3,-2,-1});
}

CASE( "test_metkit_expand_5" ) {
const char* text = "retrieve,class=od,date=20050601,diagnostic=1,expver=1,iteration=0,levelist=1,levtype=ml,param=155.129,stream=sens,time=1200,type=sg";
MarsRequest r = MarsRequest::parse(text);
r.dump(std::cout);
std::map<std::string, std::vector<std::string>> expected{
{"class", {"od"}},
{"diagnostic", {"1"}},
{"domain", {"g"}},
{"expver", {"0001"}},
{"iteration", {"0"}},
{"levelist", {"1"}},
{"levtype", {"ml"}},
{"param", {"129155"}},
{"step", {"0"}},
{"stream", {"sens"}},
{"time", {"1200"}},
{"type", {"sg"}}
};
expand(text, "retrieve", expected, {20050601});
}

CASE( "test_metkit_expand_6" ) {
const char* text = "retrieve,class=rd,expver=hl1m,stream=oper,date=20000801,time=0000,domain=g,type=fc,levtype=pl,step=24,param=129,levelist=1/to/31";
MarsRequest r = MarsRequest::parse(text);
r.dump(std::cout);
std::map<std::string, std::vector<std::string>> expected{
{"class", {"rd"}},
{"expver", {"hl1m"}},
{"stream", {"oper"}},
{"time", {"0000"}},
{"domain", {"g"}},
{"type", {"fc"}},
{"levtype", {"pl"}},
{"step", {"24"}},
{"param", {"129"}}
};
std::vector<std::string> levelist;
for (int i=1; i<=31; i++) {
levelist.push_back(std::to_string(i));
}
expected["levelist"] = levelist;
expand(text, "retrieve", expected, {20000801});
}

CASE( "test_metkit_expand_7" ) {
const char* text = "retrieve,class=rd,expver=hl1m,stream=oper,date=20000801,time=0000,domain=g,type=fc,levtype=pl,step=24,param=129,levelist=0.01/0.7";
MarsRequest r = MarsRequest::parse(text);
r.dump(std::cout);
std::map<std::string, std::vector<std::string>> expected{
{"class", {"rd"}},
{"expver", {"hl1m"}},
{"stream", {"oper"}},
{"time", {"0000"}},
{"domain", {"g"}},
{"type", {"fc"}},
{"levtype", {"pl"}},
{"step", {"24"}},
{"param", {"129"}},
{"levelist", {".01", ".7"}}
};
expand(text, "retrieve", expected, {20000801});
}

CASE( "test_metkit_expand_8" ) {
const char* text = "retrieve,class=rd,expver=hl1m,stream=oper,date=20000801,time=0000,domain=g,type=fc,levtype=pl,step=24,param=129,levelist=0.1/to/0.7/by/0.2";
MarsRequest r = MarsRequest::parse(text);
r.dump(std::cout);
std::map<std::string, std::vector<std::string>> expected{
{"class", {"rd"}},
{"expver", {"hl1m"}},
{"stream", {"oper"}},
{"time", {"0000"}},
{"domain", {"g"}},
{"type", {"fc"}},
{"levtype", {"pl"}},
{"step", {"24"}},
{"param", {"129"}},
{"levelist", {".1", ".3", ".5", ".7"}}
};
expand(text, "retrieve", expected, {20000801});
}

CASE( "test_metkit_expand_9_strict" ) {
Expand Down Expand Up @@ -124,14 +242,58 @@ CASE( "test_metkit_expand_10_strict" ) {
ASSERT(v.size() == 1);
v[0].dump(std::cout);
}
{
std::istringstream in(text);
MarsParser parser(in);
MarsExpension expand(false, true);
EXPECT_THROWS(expand.expand(parser.parse()));
}
// {
// std::istringstream in(text);
// MarsParser parser(in);
// MarsExpension expand(false, true);
// EXPECT_THROWS(expand.expand(parser.parse()));
// }
}

CASE( "test_metkit_expand_multirequest-1" ) {
const std::string text = "ret,date=-5/to/-2.\nret,date=-1";
std::istringstream in(text);
std::vector<MarsRequest> reqs = MarsRequest::parse(in, true);
EXPECT_EQUAL(reqs.size(), 2);
std::map<std::string, std::vector<std::string>> expected{
{"class", {"od"}},
{"domain", {"g"}},
{"expver", {"0001"}},
{"levelist", {"1000","850","700","500","400","300"}},
{"levtype", {"pl"}},
{"param", {"129"}},
{"step", {"0"}},
{"stream", {"oper"}},
{"time", {"1200"}},
{"type", {"an"}}
};
expand(reqs.at(0), "retrieve", expected, {-5,-4,-3,-2});
expand(reqs.at(1), "retrieve", expected, {-1});
}

// CASE( "test_metkit_expand_multirequest-2" ) {
// const std::string text = "retrieve,\n type=ai,date=20230616,time=12,class=OD,stream=LWDA,\n obstype=airs,expver=0001,target=\"airs\".";
// std::istringstream in(text);
// std::vector<MarsRequest> reqs = MarsRequest::parse(in, true);
// EXPECT_EQUAL(reqs.size(), 1);
// std::map<std::string, std::vector<std::string>> expected{
// {"class", {"od"}},
// {"domain", {"g"}},
// {"expver", {"0001"}},
// {"stream", {"lwda"}},
// {"time", {"1200"}},
// {"type", {"ai"}},
// {"target", {"\"airs\""}},
// // {"repres", {"bu"}},
// {"obstype", {"57"}},
// {"duplicates", {"keep"}}
// };

// expand(reqs.at(0), "retrieve", expected, {20230616});
// }



void expandKeyThrows(const std::string& key, std::vector<std::string> values) {
DummyContext ctx;
static metkit::mars::MarsLanguage language("retrieve");
Expand Down Expand Up @@ -437,6 +599,102 @@ CASE( "test_metkit_expand_param" ) {
}
}

CASE( "test_metkit_expand_d1" ) {
{
const char* text = "retrieve,class=d1,dataset=extremes-dt,date=-1";
std::map<std::string, std::vector<std::string>> expected{
{"class", {"d1"}},
{"dataset", {"extremes-dt"}},
{"domain", {"g"}},
{"expver", {"0001"}},
{"levelist", {"1000","850","700","500","400","300"}},
{"levtype", {"pl"}},
{"param", {"129"}},
{"step", {"0"}},
{"stream", {"oper"}},
{"time", {"1200"}},
{"type", {"an"}}
};
expand(text, "retrieve", expected, {-1});
} {
const char* text = "retrieve,class=d1,dataset=extreme-dt,date=-1";
std::map<std::string, std::vector<std::string>> expected{
{"class", {"d1"}},
{"dataset", {"extremes-dt"}},
{"expver", {"0001"}},
{"levelist", {"1000","850","700","500","400","300"}},
{"levtype", {"pl"}},
{"param", {"129"}},
{"step", {"0"}},
{"stream", {"oper"}},
{"time", {"1200"}},
{"type", {"an"}}
};
expand(text, "retrieve", expected, {-1});
}
// {
// const char* text = "retrieve,class=d1,dataset=extreme-dt,date=-1";
// std::istringstream in(text);
// MarsParser parser(in);
// MarsExpension expand(false, true);
// EXPECT_THROWS(expand.expand(parser.parse()));
// }
{
const char* text = "retrieve,class=d1,dataset=climate-dt,levtype=pl,date=20000101,activity=CMIP6,experiment=hist,model=IFS-NEMO,generation=1,realization=1,resolution=high,stream=clte,type=fc,param=134/137";
std::map<std::string, std::vector<std::string>> expected{
{"class", {"d1"}},
{"dataset", {"climate-dt"}},
{"activity", {"cmip6"}},
{"experiment", {"hist"}},
{"model", {"ifs-nemo"}},
{"generation", {"1"}},
{"realization", {"1"}},
{"resolution", {"high"}},
{"expver", {"0001"}},
{"date", {"20000101"}},
{"time", {"1200"}},
{"stream", {"clte"}},
{"type", {"fc"}},
{"levtype", {"pl"}},
{"levelist", {"1000","850","700","500","400","300"}},
{"param", {"134","137"}}
};
expand(text, "retrieve", expected, {20000101});
}
}


CASE( "test_metkit_expand_ng" ) {
{
const char* text = "retrieve,class=ng,date=20000101,activity=CMIP6,experiment=hist,model=IFS-NEMO,generation=1,realization=1,resolution=high,stream=clte,type=fc,levtype=pl,param=134/137";
std::map<std::string, std::vector<std::string>> expected {
{"class", {"ng"}},
{"levtype", {"pl"}},
{"levelist", {"1000","850","700","500","400","300"}},
{"activity", {"cmip6"}},
{"experiment", {"hist"}},
{"model", {"ifs-nemo"}},
{"generation", {"1"}},
{"realization", {"1"}},
{"resolution", {"high"}},
{"expver", {"0001"}},
{"date", {"20000101"}},
{"time", {"1200"}},
{"stream", {"clte"}},
{"type", {"fc"}},
{"param", {"134","137"}}
};
expand(text, "retrieve", expected, {20000101});
}
{
const char* text = "retrieve,class=ng,dataset=climate-dt,date=20000101,activity=CMIP6,experiment=hist,model=IFS-NEMO,generation=1,realization=1,resolution=high,stream=clte,type=fc,levtype=sfc,param=134/137";
std::istringstream in(text);
MarsParser parser(in);
MarsExpension expand(false, true);
EXPECT_THROWS(expand.expand(parser.parse()));
}
}

//-----------------------------------------------------------------------------

} // namespace test
Expand Down

0 comments on commit 75f43be

Please sign in to comment.