File: test_builder.cc

package info (click to toggle)
fdb 5.19.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 78,856 kB
  • sloc: cpp: 36,785; sh: 4,380; python: 1,334; makefile: 32; ansic: 8
file content (300 lines) | stat: -rw-r--r-- 12,560 bytes parent folder | download
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
#include <memory>

#include "eckit/io/MemoryHandle.h"
#include "eckit/testing/Test.h"

#include "chunked_data_view/ChunkedDataViewBuilder.h"
#include "fdb5/api/helpers/FDBToolRequest.h"

namespace cdv = chunked_data_view;

std::unique_ptr<eckit::DataHandle> makeHandle(const std::vector<double>& values) {
    const size_t size = values.size() * sizeof(std::decay_t<decltype(values)>::value_type) + 2 * sizeof(size_t);
    const size_t bytesPerValue = 8;
    auto handle                = std::make_unique<eckit::MemoryHandle>(size);
    size_t _{};
    handle->openForWrite(_);

    size_t countValues = values.size();
    handle->write(&countValues, sizeof(size_t));
    handle->write(&bytesPerValue, sizeof(size_t));
    handle->write(values.data(), values.size() * sizeof(std::decay_t<decltype(values)>::value_type));
    handle->close();
    return handle;
};

struct MockListIterator final : public chunked_data_view::ListIteratorInterface {

    using vec2 = std::vector<std::tuple<fdb5::Key, std::vector<double>>>;
    vec2 data_;
    vec2::const_iterator iter_;

    MockListIterator(vec2 data) : data_(std::move(data)), iter_(std::begin(data_)) {};

    std::optional<std::tuple<fdb5::Key, std::unique_ptr<eckit::DataHandle>>> next() {
        iter_++;

        if (std::end(data_) == iter_) {
            return std::nullopt;
        }

        return std::make_tuple(std::get<0>(*iter_), makeHandle(std::get<1>(*iter_)));
    };
};


struct MockFdb final : public cdv::FdbInterface {
    using RetFunc = std::function<std::unique_ptr<eckit::DataHandle>(const metkit::mars::MarsRequest&)>;
    using InsFunc =
        std::function<std::unique_ptr<chunked_data_view::ListIteratorInterface>(const metkit::mars::MarsRequest&)>;

    explicit MockFdb(RetFunc retFunc, InsFunc insFunc) : retFn(std::move(retFunc)), insFn(std::move(insFunc)) {}

    std::unique_ptr<eckit::DataHandle> retrieve(const metkit::mars::MarsRequest& request) override {
        return retFn(request);
    };

    std::unique_ptr<chunked_data_view::ListIteratorInterface> inspect(
        const metkit::mars::MarsRequest& request) override {
        return insFn(request);
    }

    RetFunc retFn{};
    InsFunc insFn{};
};

struct FakeExtractor : public cdv::Extractor {
    cdv::DataLayout layout(eckit::DataHandle& handle) const override {
        cdv::DataLayout layout{};
        handle.openForRead();
        EXPECT_EQUAL(handle.read(&layout.countValues, sizeof(layout.countValues)), sizeof(layout.countValues));
        EXPECT_EQUAL(handle.read(&layout.bytesPerValue, sizeof(layout.bytesPerValue)), sizeof(layout.bytesPerValue));
        handle.close();
        return layout;
    }

    void writeInto(const metkit::mars::MarsRequest& request,
                   std::unique_ptr<chunked_data_view::ListIteratorInterface> list_iterator,
                   const std::vector<chunked_data_view::Axis>& axes, const chunked_data_view::DataLayout& layout,
                   float* ptr, size_t len, size_t expected_msg_count) const override {
        cdv::DataLayout readLayout{};
        while (auto res = list_iterator->next()) {
            if (!res) {
                break;
            }
            const auto& key   = std::get<0>(*res);
            auto& data_handle = std::get<1>(*res);
            data_handle->openForRead();

            EXPECT_EQUAL(data_handle->read(&readLayout.countValues, sizeof(layout.countValues)), 8l);
            EXPECT_EQUAL(data_handle->read(&readLayout.bytesPerValue, sizeof(layout.bytesPerValue)), 4l);
            const size_t totalBytes = layout.countValues * layout.bytesPerValue;
            EXPECT_EQUAL(data_handle->read(ptr, totalBytes), totalBytes);
        }
    };
};

CASE("ChunkedDataView | Single part - no extension axis") {
    const std::string keys{
        "type=an,"
        "domain=g,"
        "expver=0001,"
        "stream=oper,"
        "date=2020-01-01/to/2020-01-04,"
        "levtype=sfc,"
        "param=v/u,"
        "time=0/6/12/18"};

    const auto request = fdb5::FDBToolRequest::requestsFromString(keys).at(0).request();

    cdv::ChunkedDataViewBuilder(
        std::make_unique<MockFdb>(
            [](auto& _) { return makeHandle({1, 2, 3, 4, 5, 6, 7, 8, 9, 10}); },
            [](auto& _) -> std::unique_ptr<chunked_data_view::ListIteratorInterface> {
                return std::make_unique<MockListIterator>(std::vector<std::tuple<fdb5::Key, std::vector<double>>>{
                    std::make_tuple(fdb5::Key(), std::vector<double>{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}),
                });
            }))
        .addPart(keys,
                 {cdv::AxisDefinition{{"date"}, true}, cdv::AxisDefinition{{"time"}, true},
                  cdv::AxisDefinition{{"param"}, true}},
                 std::make_unique<FakeExtractor>())
        .build();
}

CASE("ChunkedDataView | Two parts - no extension axis | Expected FAILURE") {
    const std::string keys{
        "type=an,"
        "domain=g,"
        "expver=0001,"
        "stream=oper,"
        "date=2020-01-01/to/2020-01-04,"
        "levtype=sfc,"
        "param=v/u,"
        "time=0/6/12/18"};

    const auto request = fdb5::FDBToolRequest::requestsFromString(keys).at(0).request();

    EXPECT_THROWS(cdv::ChunkedDataViewBuilder(
                      std::make_unique<MockFdb>(
                          [](auto& _) { return makeHandle({1, 2, 3, 4, 5, 6, 7, 8, 9, 10}); },
                          [](auto& _) -> std::unique_ptr<chunked_data_view::ListIteratorInterface> {
                              return std::make_unique<MockListIterator>(
                                  std::vector<std::tuple<fdb5::Key, std::vector<double>>>{
                                      std::make_tuple(fdb5::Key(), std::vector<double>{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}),
                                  });
                          }))
                      .addPart(keys,
                               {cdv::AxisDefinition{{"date"}, true}, cdv::AxisDefinition{{"time"}, true},
                                cdv::AxisDefinition{{"param"}, true}},
                               std::make_unique<FakeExtractor>())
                      .addPart(keys,
                               {cdv::AxisDefinition{{"date"}, true}, cdv::AxisDefinition{{"time"}, true},
                                cdv::AxisDefinition{{"param"}, true}},
                               std::make_unique<FakeExtractor>())
                      .build());
}

CASE("ChunkedDataView | One part - extension axis set") {
    const std::string keys{
        "type=an,"
        "domain=g,"
        "expver=0001,"
        "stream=oper,"
        "date=2020-01-01/to/2020-01-04,"
        "levtype=sfc,"
        "param=v/u,"
        "time=0/6/12/18"};

    const auto request = fdb5::FDBToolRequest::requestsFromString(keys).at(0).request();

    cdv::ChunkedDataViewBuilder(
        std::make_unique<MockFdb>(
            [](auto& _) { return makeHandle({1, 2, 3, 4, 5, 6, 7, 8, 9, 10}); },
            [](auto& _) -> std::unique_ptr<chunked_data_view::ListIteratorInterface> {
                return std::make_unique<MockListIterator>(std::vector<std::tuple<fdb5::Key, std::vector<double>>>{
                    std::make_tuple(fdb5::Key(), std::vector<double>{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}),
                });
            }))
        .addPart(keys,
                 {cdv::AxisDefinition{{"date"}, true}, cdv::AxisDefinition{{"time"}, true},
                  cdv::AxisDefinition{{"param"}, true}},
                 std::make_unique<FakeExtractor>())
        .extendOnAxis(1)
        .build();
}


CASE("ChunkedDataView | One part - extension axis set | Invalid extension axis index for single part") {
    const std::string keys{
        "type=an,"
        "domain=g,"
        "expver=0001,"
        "stream=oper,"
        "date=2020-01-01/to/2020-01-04,"
        "levtype=sfc,"
        "param=v/u,"
        "time=0/6/12/18"};

    const auto request = fdb5::FDBToolRequest::requestsFromString(keys).at(0).request();

    cdv::ChunkedDataViewBuilder(
        std::make_unique<MockFdb>(
            [](auto& _) { return makeHandle({1, 2, 3, 4, 5, 6, 7, 8, 9, 10}); },
            [](auto& _) -> std::unique_ptr<chunked_data_view::ListIteratorInterface> {
                return std::make_unique<MockListIterator>(std::vector<std::tuple<fdb5::Key, std::vector<double>>>{
                    std::make_tuple(fdb5::Key(), std::vector<double>{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}),
                });
            }))
        .addPart(keys,
                 {cdv::AxisDefinition{{"date"}, true}, cdv::AxisDefinition{{"time"}, true},
                  cdv::AxisDefinition{{"param"}, true}},
                 std::make_unique<FakeExtractor>())
        .extendOnAxis(2)
        .build();
}

CASE(
    "ChunkedDataView | Two parts - extension axis set | Invalid extension axis index for multiple part | Expected "
    "FAILURE") {
    const std::string keys{
        "type=an,"
        "domain=g,"
        "expver=0001,"
        "stream=oper,"
        "date=2020-01-01/to/2020-01-04,"
        "levtype=sfc,"
        "param=v/u,"
        "time=0/6/12/18"};

    const auto request = fdb5::FDBToolRequest::requestsFromString(keys).at(0).request();

    EXPECT_THROWS(cdv::ChunkedDataViewBuilder(
                      std::make_unique<MockFdb>(
                          [](auto& _) { return makeHandle({1, 2, 3, 4, 5, 6, 7, 8, 9, 10}); },
                          [](auto& _) -> std::unique_ptr<chunked_data_view::ListIteratorInterface> {
                              return std::make_unique<MockListIterator>(
                                  std::vector<std::tuple<fdb5::Key, std::vector<double>>>{
                                      std::make_tuple(fdb5::Key(), std::vector<double>{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}),
                                  });
                          }))
                      .addPart(keys,
                               {cdv::AxisDefinition{{"date"}, true}, cdv::AxisDefinition{{"time"}, true},
                                cdv::AxisDefinition{{"param"}, true}},
                               std::make_unique<FakeExtractor>())
                      .addPart(keys,
                               {cdv::AxisDefinition{{"date"}, true}, cdv::AxisDefinition{{"time"}, true},
                                cdv::AxisDefinition{{"param"}, true}},
                               std::make_unique<FakeExtractor>())
                      .extendOnAxis(3)
                      .build());
}

CASE("ChunkedDataView | Two parts - extension axis set to 0 | Mismatching parameter dimensions | Expected FAILURE") {
    const std::string keys_part_1{
        "type=an,"
        "domain=g,"
        "expver=0001,"
        "stream=oper,"
        "date=2020-01-01/to/2020-01-04,"
        "levtype=sfc,"
        "param=v/u,"
        "time=0/6/12/18"};

    const std::string keys_part_2{
        "type=an,"
        "domain=g,"
        "expver=0001,"
        "stream=oper,"
        "date=2020-01-01/to/2020-01-04,"
        "levtype=sfc,"
        "param=v/u/w,"
        "time=0/6/12/18"};

    const auto request = fdb5::FDBToolRequest::requestsFromString(keys_part_1).at(0).request();

    EXPECT_THROWS(cdv::ChunkedDataViewBuilder(
                      std::make_unique<MockFdb>(
                          [](auto& _) { return makeHandle({1, 2, 3, 4, 5, 6, 7, 8, 9, 10}); },
                          [](auto& _) -> std::unique_ptr<chunked_data_view::ListIteratorInterface> {
                              return std::make_unique<MockListIterator>(
                                  std::vector<std::tuple<fdb5::Key, std::vector<double>>>{
                                      std::make_tuple(fdb5::Key(), std::vector<double>{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}),
                                  });
                          }))
                      .addPart(keys_part_1,
                               {cdv::AxisDefinition{{"date"}, true}, cdv::AxisDefinition{{"time"}, true},
                                cdv::AxisDefinition{{"param"}, true}},
                               std::make_unique<FakeExtractor>())
                      .addPart(keys_part_2,
                               {cdv::AxisDefinition{{"date"}, true}, cdv::AxisDefinition{{"time"}, true},
                                cdv::AxisDefinition{{"param"}, true}},
                               std::make_unique<FakeExtractor>())
                      .extendOnAxis(0)
                      .build());
}


int main(int argc, char** argv) {
    return ::eckit::testing::run_tests(argc, argv);
}