IODA Bundle
test_text_reader_select.cc
Go to the documentation of this file.
1 /*
2  * (C) Copyright 1996-2012 ECMWF.
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  * In applying this licence, ECMWF does not waive the privileges and immunities
7  * granted to it by virtue of its status as an intergovernmental organisation nor
8  * does it submit to any jurisdiction.
9  */
10 
11 #include "eckit/testing/Test.h"
12 #include "eckit/types/FloatCompare.h"
13 
14 #include "odc/Select.h"
15 #include "odc/sql/TODATable.h"
16 
17 using namespace eckit::testing;
18 
19 // ------------------------------------------------------------------------------------------------------
20 
21 CASE("Simple select on CSV (one column) data") {
22 
23  // Construct some data to use
24  std::stringstream DATA;
25  DATA << "a:REAL\n";
26  for (size_t i = 1; i <= 10; i++) {
27  DATA << i << "\n";
28  }
29 
30  SECTION("Select with where condition") {
31  odc::Select select("select * where a > 4");
32  select.database().addImplicitTable(new odc::sql::ODBCSVTable(select.database(), DATA, "input", ","));
33 
34  size_t count = 0;
35  for (odc::Select::iterator it = select.begin(); it != select.end(); ++it) {
36  count++;
37  EXPECT((*it)[0] == count+4);
38  }
39 
40  EXPECT(count == 6);
41  }
42 
43 
44  SECTION("Sum one column") {
45  odc::Select select("select sum(a), count(a), count(*);");
46  select.database().addImplicitTable(new odc::sql::ODBCSVTable(select.database(), DATA, "input", ","));
47 
48  odc::Select::iterator it = select.begin();
49 
50  EXPECT(it != select.end());
51  EXPECT((*it)[0] == 55);
52  EXPECT((*it)[1] == 10);
53  EXPECT((*it)[2] == 10);
54 
55  ++it;
56  EXPECT(it == select.end());
57  }
58 
59  SECTION("Mixed output and aggregate") {
60  odc::Select select("select a, sum(a), count(a)");
61  select.database().addImplicitTable(new odc::sql::ODBCSVTable(select.database(), DATA, "input", ","));
62 
63  odc::Select::iterator it = select.begin();
64 
65  EXPECT(it != select.end());
66 
67  // n.b. as all values of a are different, we get counts of 1
68 
69  size_t count = 0;
70  for (odc::Select::iterator it = select.begin(); it != select.end(); ++it) {
71  count++;
72  EXPECT(it->data(0) == count);
73  EXPECT(it->data(1) == count);
74  EXPECT(it->data(2) == 1);
75  }
76  EXPECT(count = 10);
77  }
78 }
79 
80 
81 CASE("Simple select on CSV (two columns) data") {
82 
83  // Construct some data to use
84  std::stringstream DATA;
85  DATA << "a:REAL,b:STRING,c:INTEGER,d:INTEGER\n";
86  for (size_t i = 1; i <= 10; i++) {
87  DATA << i << ",";
88  // length=16/17 --> tests overflowing 16 char and resizing string on last row
89  DATA << "a-long-" << i << "-str****,";
90  DATA << 11-i << ",";
91  DATA << int(i/3) << "\n";
92  }
93 
94  SECTION("Select with where condition") {
95  odc::Select select("select * where a > 4");
96  select.database().addImplicitTable(new odc::sql::ODBCSVTable(select.database(), DATA, "input", ","));
97 
98  size_t count = 0;
99  for (odc::Select::iterator it = select.begin(); it != select.end(); ++it) {
100  count++;
101  std::stringstream colb;
102  colb << "a-long-" << (count+4) << "-str****";
103  EXPECT(it->data(0) == count+4);
104  EXPECT(it->dataSizeDoubles(1) == (count == 6 ? 3 : 2));
105  EXPECT(::strncmp(colb.str().c_str(), (char*)&it->data(1), it->dataSizeDoubles(1)*sizeof(double)) == 0);
106  EXPECT(it->data(2) == 11-(count+4));
107  }
108 
109  EXPECT(count == 6);
110  }
111 
112 
113  SECTION("Sum and count") {
114  odc::Select select("select sum(a), count(a), count(*), sum(c), count(c), count(b);");
115  select.database().addImplicitTable(new odc::sql::ODBCSVTable(select.database(), DATA, "input", ","));
116 
117  odc::Select::iterator it = select.begin();
118 
119  EXPECT(it != select.end());
120  EXPECT(it->data(0) == 55);
121  EXPECT(it->data(1) == 10);
122  EXPECT(it->data(2) == 10);
123  EXPECT(it->data(3) == 55);
124  EXPECT(it->data(4) == 10);
125  EXPECT(it->data(5) == 10);
126 
127  ++it;
128  EXPECT(it == select.end());
129  }
130 
131  SECTION("Mixed output and aggregate") {
132  odc::Select select("select d, sum(a), count(c)");
133  select.database().addImplicitTable(new odc::sql::ODBCSVTable(select.database(), DATA, "input", ","));
134 
135  odc::Select::iterator it = select.begin();
136 
137  EXPECT(it != select.end());
138  bool found[4] = {false, false, false, false};
139  for (odc::Select::iterator it = select.begin(); it != select.end(); ++it) {
140  EXPECT(0 <= it->data(0) && 3 >= it->data(0));
141  found[int(it->data(0))] = true;
142  switch(int(it->data(0))) {
143  case 0:
144  EXPECT(it->data(1) == 3);
145  EXPECT(it->data(2) == 2);
146  break;
147  case 1:
148  EXPECT(it->data(1) == 12);
149  EXPECT(it->data(2) == 3);
150  break;
151  case 2:
152  EXPECT(it->data(1) == 21);
153  EXPECT(it->data(2) == 3);
154  break;
155  case 3:
156  EXPECT(it->data(1) == 19);
157  EXPECT(it->data(2) == 2);
158  break;
159  default:
160  ASSERT(false);
161  }
162  }
163  EXPECT(std::all_of(std::begin(found), std::end(found), [](bool x){return x;}));
164  }
165 }
166 
167 // ------------------------------------------------------------------------------------------------------
168 
169 int main(int argc, char* argv[]) {
170  return run_tests(argc, argv);
171 }
static void count(void *counter, const double *data, size_t n)
Definition: UnitTests.cc:531
DATA * data()
Definition: IteratorProxy.h:77
eckit::sql::SQLDatabase & database()
Definition: Select.cc:64
const iterator end()
Definition: Select.cc:77
iterator begin()
Definition: Select.cc:81
int main(int argc, char *argv[])
CASE("Simple select on CSV (one column) data")