IODA Bundle
TestBufrParser.h
Go to the documentation of this file.
1 /*
2  * (C) Copyright 2020 NOAA/NWS/NCEP/EMC
3  *
4  * This software is licensed under the terms of the Apache Licence Version 2.0
5  * which can be obtained at http://www.apache.org/licenses/LICENSE-2.0.
6  */
7 
8 #pragma once
9 
10 #define ECKIT_TESTING_SELF_REGISTER_CASES 0
11 
12 #include <iomanip>
13 #include <iostream>
14 #include <memory>
15 #include <string>
16 #include <vector>
17 
18 #include "eckit/config/LocalConfiguration.h"
19 #include "eckit/testing/Test.h"
20 #include "oops/runs/Test.h"
21 #include "oops/util/Expect.h"
22 #include "test/TestEnvironment.h"
23 
26 #include "BufrParser/BufrParser.h"
27 #include "BufrParser/BufrTypes.h"
28 #include "DataContainer.h"
29 #include "ParserFactory.h"
30 
33 
34 
35 namespace Ingester
36 {
37  namespace test
38  {
39  class BufrParserTestFixture : private boost::noncopyable
40  {
41  public:
42  static std::shared_ptr<Parser>& bufrParser() { return getInstance().bufrParser_; }
43 
44  private:
45  std::shared_ptr<Parser> bufrParser_;
46 
48  {
49  static BufrParserTestFixture bufrParserTestFixture;
50  return bufrParserTestFixture;
51  }
52 
54  {
56 
57  const eckit::LocalConfiguration conf(::test::TestEnvironment::config());
58 
59  if (conf.has("observations"))
60  {
61  auto obsConf = conf.getSubConfigurations("observations").front();
62 
63  if (obsConf.has("obs space") &&
64  obsConf.getSubConfiguration("obs space").has("name") &&
65  obsConf.getSubConfiguration("obs space").getString("name") == "bufr")
66  {
67  auto bufrConf = obsConf.getSubConfiguration("obs space");
69  }
70  else
71  {
72  throw eckit::BadValue(
73  "Configuration File is missing the \"bufr\" section.");
74  }
75  }
76  else
77  {
78  throw eckit::BadValue(
79  "Configuration File is missing the \"observations\" section.");
80  }
81  }
82 
84  {
85  ParserFactory::registerParser<BufrParser>("bufr");
86  }
87 
89  };
90 
91 
92  void test_constructor()
93  {
95  }
96 
98  {
99  auto data = BufrParserTestFixture::bufrParser()->parse(5);
100  auto dataObj = data->get("radiance");
101 
102  if (auto arrayDataObject = std::dynamic_pointer_cast<ArrayDataObject>(dataObj))
103  {
104  EXPECT(abs(arrayDataObject->get()(0, 0) - 248.17) < .01);
105  }
106  }
107 
109  {
110  bool endReached = false;
111  std::shared_ptr<DataContainer> data;
112  do
113  {
114  auto nextData = BufrParserTestFixture::bufrParser()->parse(10);
115 
116  if (nextData->size() > 0)
117  {
118  data = nextData;
119  }
120  else
121  {
122  endReached = true;
123  }
124  } while (!endReached);
125  }
126 
127 
128  class BufrParser : public oops::Test
129  {
130  public:
131  BufrParser() = default;
132  ~BufrParser() override = default;
133  private:
134  std::string testid() const override { return "ingester::test::BufrParser"; }
135  void register_tests() const override
136  {
137  std::vector<eckit::testing::Test>& ts = eckit::testing::specification();
138 
139  ts.emplace_back(CASE("ingester/BufrParser/testConstructor")
140  {
142  });
143  ts.emplace_back(CASE("ingester/BufrParser/testParsePartialFile")
144  {
146  });
147  ts.emplace_back(CASE("ingester/BufrParser/testParseFileIncrementally")
148  {
150  });
151  }
152 
153  void clear() const override
154  {
155  }
156  };
157  } // namespace test
158 } // namespace Ingester
static std::shared_ptr< Parser > create(const eckit::Configuration &conf)
Create a Parser.
Definition: ParserFactory.h:58
std::string testid() const override
void clear() const override
void register_tests() const override
~BufrParser() override=default
std::shared_ptr< Parser > bufrParser_
static std::shared_ptr< Parser > & bufrParser()
static BufrParserTestFixture & getInstance()
static const eckit::Configuration & config()
void test_parsePartialFile()
void test_parseFileIncrementally()
CASE("Validation")