libdballe  7.29
db/tests.h
1 #include <dballe/msg/tests.h>
2 #include <dballe/core/record.h>
3 #include <dballe/core/values.h>
4 #include <dballe/db/db.h>
5 #include <dballe/sql/fwd.h>
6 #include <utility>
7 #include <functional>
8 
9 namespace dballe {
10 struct DB;
11 
12 namespace db {
13 
14 namespace v6 {
15 struct Driver;
16 class DB;
17 }
18 
19 namespace v7 {
20 struct Driver;
21 class DB;
22 }
23 
24 }
25 
26 namespace tests {
27 
28 Messages messages_from_db(DB& db, const dballe::Query& query);
29 Messages messages_from_db(DB& db, const char* query);
30 
33 {
35  std::map<std::string, StationValues> stations;
37  std::map<std::string, DataValues> data;
38 
39  TestDataSet() {}
40  virtual ~TestDataSet() {}
41 
42  virtual void populate_db(DB& db);
43 };
44 
47 {
49 };
50 
51 
52 struct BaseDBFixture : public Fixture
53 {
54  std::string backend;
55  db::Format format;
56 
57  BaseDBFixture(const char* backend, db::Format format);
58 
59  void test_setup();
60  bool has_driver();
61  static bool has_driver(const std::string& backend);
62 };
63 
66 {
67  dballe::sql::Connection* conn = nullptr;
68  db::v6::Driver* driver = nullptr;
69 
70  DriverFixture(const char* backend, db::Format format);
71  ~DriverFixture();
72 
73  void test_setup();
74 };
75 
78 {
79  dballe::sql::Connection* conn = nullptr;
80  db::v7::Driver* driver = nullptr;
81 
82  V7DriverFixture(const char* backend, db::Format format);
83  ~V7DriverFixture();
84 
85  void test_setup();
86 };
87 
88 struct DBFixture : public BaseDBFixture
89 {
90  DB* db = nullptr;
91 
92  DBFixture(const char* backend, db::Format format);
93  ~DBFixture();
94 
96  std::unique_ptr<DB> create_db();
97 
98  void test_setup();
99 
100  template<typename DataSet>
101  void populate()
102  {
103  DataSet data_set;
104  wassert(populate_database(data_set));
105  }
106 
107  void populate_database(TestDataSet& data_set);
108 };
109 
110 template<typename FIXTURE>
111 struct DBFixtureTestCase : public FixtureTestCase<FIXTURE>
112 {
114 };
115 
116 struct ActualCursor : public Actual<dballe::db::Cursor&>
117 {
118  using Actual::Actual;
119 
121  void station_keys_match(const Station& expected);
122 
124  void station_vars_match(const StationValues& expected);
125 
127  void data_context_matches(const DataValues& expected);
128 
130  void data_var_matches(const StationValues& expected, wreport::Varcode code) {
131  data_var_matches(*expected.values[code].var);
132  }
134  void data_var_matches(const DataValues& expected, wreport::Varcode code) {
135  data_var_matches(*expected.values[code].var);
136  }
138  void data_var_matches(const DataValues& expected) {
139  if (auto c = dynamic_cast<dballe::db::CursorValue*>(&_actual))
140  data_var_matches(*expected.values[c->get_varcode()].var);
141  else
142  throw wreport::error_consistency("cannot call data_var_matches on this kind of cursor");
143  }
145  void data_var_matches(const Values& expected, wreport::Varcode code) {
146  data_var_matches(*expected[code].var);
147  }
149  void data_var_matches(const wreport::Var& expected);
150 
152  void data_matches(const DataValues& ds)
153  {
154  if (auto c = dynamic_cast<dballe::db::CursorValue*>(&_actual))
155  data_matches(ds, c->get_varcode());
156  else
157  throw wreport::error_consistency("cannot call data_matches on this kind of cursor");
158  }
160  void data_matches(const DataValues& ds, wreport::Varcode code);
161 };
162 
163 typedef std::function<void(const std::vector<core::Record>&)> result_checker;
164 
165 struct ActualDB : public Actual<dballe::DB&>
166 {
167  using Actual::Actual;
168 
170  void try_data_query(const std::string& query, unsigned expected);
171 
173  void try_data_query(const Query& query, unsigned expected);
174 
176  void try_station_query(const std::string& query, unsigned expected);
177 
179  void try_summary_query(const std::string& query, unsigned expected, result_checker checker=nullptr);
180 };
181 
182 inline ActualCursor actual(dballe::db::Cursor& actual) { return ActualCursor(actual); }
183 inline ActualCursor actual(dballe::db::CursorStation& actual) { return ActualCursor(actual); }
184 inline ActualCursor actual(dballe::db::CursorStationData& actual) { return ActualCursor(actual); }
185 inline ActualCursor actual(dballe::db::CursorData& actual) { return ActualCursor(actual); }
186 inline ActualCursor actual(dballe::db::CursorSummary& actual) { return ActualCursor(actual); }
187 inline ActualCursor actual(std::unique_ptr<dballe::db::Cursor>& actual) { return ActualCursor(*actual); }
188 inline ActualCursor actual(std::unique_ptr<dballe::db::CursorStation>& actual) { return ActualCursor(*actual); }
189 inline ActualCursor actual(std::unique_ptr<dballe::db::CursorStationData>& actual) { return ActualCursor(*actual); }
190 inline ActualCursor actual(std::unique_ptr<dballe::db::CursorData>& actual) { return ActualCursor(*actual); }
191 inline ActualCursor actual(std::unique_ptr<dballe::db::CursorSummary>& actual) { return ActualCursor(*actual); }
192 inline ActualDB actual(dballe::DB& actual) { return ActualDB(actual); }
193 inline ActualDB actual(std::unique_ptr<dballe::DB>& actual) { return ActualDB(*actual); }
194 
195 }
196 }
void try_summary_query(const std::string &query, unsigned expected, result_checker checker=nullptr)
Check results of a summary query.
std::unique_ptr< DB > create_db()
Open a new DB with the backend and format specified in this fixture.
Cursor iterating over summary entries.
Definition: db.h:146
Cursor iterating over station data values.
Definition: db.h:128
Information about a station.
Definition: values.h:21
void station_keys_match(const Station &expected)
Check cursor context after a query_stations.
A set of measured values.
Definition: values.h:304
void data_var_matches(const DataValues &expected, wreport::Varcode code)
Check cursor data variable after a query_data.
Definition: db/tests.h:134
Forward declarations for public dballe/sql names.
Definition: v6/driver.h:52
std::map< std::string, StationValues > stations
Arbitrarily named station values.
Definition: db/tests.h:35
void data_matches(const DataValues &ds)
Check cursor data context and variable after a query_data.
Definition: db/tests.h:152
Common interface for all kinds of cursors.
Definition: db.h:43
Functions used to connect to DB-All.e and insert, query and delete data.
Definition: sql.h:59
Cursor iterating over stations.
Definition: db.h:107
Cursor iterating over data values.
Definition: db.h:133
Definition: db/tests.h:88
void data_context_matches(const DataValues &expected)
Check cursor data context after a query_data.
void data_var_matches(const Values &expected, wreport::Varcode code)
Check cursor data variable after a query_data.
Definition: db/tests.h:145
uint16_t Varcode
void station_vars_match(const StationValues &expected)
Check cursor context after a query_stations.
Definition: db/tests.h:52
void try_data_query(const std::string &query, unsigned expected)
Check cursor data context anda variable after a query_data.
Test fixture for SQL backend drivers.
Definition: db/tests.h:77
Definition: db.h:166
void try_station_query(const std::string &query, unsigned expected)
Check results of a station query.
Query used to filter DB-All.e data.
Definition: query.h:14
void data_var_matches(const StationValues &expected, wreport::Varcode code)
Check cursor data variable after a query_data.
Definition: db/tests.h:130
Test fixture used by old DB-All.e db tests.
Definition: db/tests.h:46
A set of station values.
Definition: values.h:274
Definition: db/tests.h:111
Definition: db/tests.h:165
Structures used as input to database insert functions.
void data_var_matches(const DataValues &expected)
Check cursor data variable after a query_data.
Definition: db/tests.h:138
Base for datasets used to populate test databases.
Definition: db/tests.h:32
std::map< std::string, DataValues > data
Arbitrarily named data values.
Definition: db/tests.h:37
Collection of Value objects, indexed by wreport::Varcode.
Definition: values.h:203
Test fixture for SQL backend drivers.
Definition: db/tests.h:65
Definition: v7/driver.h:26
Definition: db/tests.h:116
Implement a storage object for a group of related observation data.