libdballe  9.14
db/tests.h
1 #include <dballe/core/data.h>
2 #include <dballe/db/db.h>
3 #include <dballe/db/fwd.h>
4 #include <dballe/db/summary.h>
5 #include <dballe/db/v7/fwd.h>
6 #include <dballe/msg/tests.h>
7 #include <dballe/sql/fwd.h>
8 #include <dballe/values.h>
9 #include <functional>
10 #include <utility>
11 
12 namespace dballe {
13 namespace tests {
14 
15 impl::Messages messages_from_db(std::shared_ptr<db::Transaction> tr,
16  const dballe::Query& query);
17 impl::Messages messages_from_db(std::shared_ptr<db::Transaction> tr,
18  const char* query);
19 
22 {
24  std::map<std::string, core::Data> stations;
26  std::map<std::string, core::Data> data;
27 
28  TestDataSet() {}
29  virtual ~TestDataSet() {}
30 
31  void populate_db(dballe::DB& db);
32  virtual void populate_transaction(Transaction& tr);
33 };
34 
36 {
37  void populate_db();
38 };
39 
42 {
44 };
45 
46 bool has_driver(const std::string& backend);
47 
48 struct V7DB
49 {
50  typedef db::v7::DB DB;
51  typedef db::v7::Transaction TR;
52  static const auto format = db::Format::V7;
53  static std::shared_ptr<DB> create_db(const std::string& backend, bool wipe);
54 };
55 
56 template <typename DB> struct BaseDBFixture : public Fixture
57 {
58  std::string backend;
59  std::shared_ptr<typename DB::DB> db;
60  bool destroys_db = false;
61 
62  BaseDBFixture(const char* backend);
63  virtual ~BaseDBFixture();
64 
65  void test_setup();
66  void test_teardown();
67  virtual void create_db();
68  bool has_driver();
69 };
70 
71 template <typename DB> struct EmptyTransactionFixture : public BaseDBFixture<DB>
72 {
74  std::shared_ptr<typename DB::TR> tr;
75 
76  void test_setup();
77  void test_teardown();
78  void populate(TestDataSet& data_set);
79 };
80 
81 template <typename DB, typename TestData>
83 {
85 
86  TestData test_data;
87 
88  void create_db() override
89  {
91  wassert(test_data.populate_db(*this->db));
92  }
93 };
94 
95 template <typename DB> struct DBFixture : public BaseDBFixture<DB>
96 {
98 
99  void test_setup();
100  void populate_database(TestDataSet& data_set);
101 };
102 
103 struct ActualCursor : public Actual<std::shared_ptr<dballe::Cursor>>
104 {
105  using Actual::Actual;
106 
108  void station_keys_match(const DBStation& expected);
109 
111  void data_context_matches(const Data& expected);
112 
114  void data_var_matches(const Data& expected, wreport::Varcode code)
115  {
116  data_var_matches(core::Data::downcast(expected).values.var(code));
117  }
119  void data_var_matches(const DBValues& expected)
120  {
121  if (auto c =
122  std::dynamic_pointer_cast<dballe::db::CursorStation>(_actual))
123  {
124  DBValues actual_values = c->get_values();
125  if (!actual_values.vars_equal(expected))
126  // Quick hack to get proper formatting of mismatch
127  wassert(actual(c->get_values()) == expected);
128  }
129  else if (auto c = std::dynamic_pointer_cast<dballe::CursorStationData>(
130  _actual))
131  data_var_matches(expected.var(c->get_varcode()));
132  else if (auto c =
133  std::dynamic_pointer_cast<dballe::CursorData>(_actual))
134  data_var_matches(expected.var(c->get_varcode()));
135  else
137  "cannot call data_var_matches on this kind of cursor");
138  }
140  void data_var_matches(const Values& expected, wreport::Varcode code)
141  {
142  data_var_matches(expected.var(code));
143  }
145  void data_var_matches(const wreport::Var& expected);
146 
148  void data_matches(const Data& ds)
149  {
150  if (auto c =
151  std::dynamic_pointer_cast<dballe::CursorStationData>(_actual))
152  data_matches(ds, c->get_varcode());
153  else if (auto c =
154  std::dynamic_pointer_cast<dballe::CursorData>(_actual))
155  data_matches(ds, c->get_varcode());
156  else
158  "cannot call data_matches on this kind of cursor");
159  }
161  void data_matches(const Data& ds, wreport::Varcode code);
162 };
163 
164 typedef std::function<void(const db::DBSummary&)> result_checker;
165 
166 template <typename DB> struct ActualDB : public Actual<std::shared_ptr<DB>>
167 {
169 
171  void try_data_query(const std::string& query, unsigned expected);
172 
174  void try_data_query(const Query& query, unsigned expected);
175 
177  void try_station_query(const std::string& query, unsigned expected);
178 
180  void try_summary_query(const std::string& query, unsigned expected,
181  result_checker checker = nullptr);
182 };
183 
184 inline ActualCursor actual(std::shared_ptr<dballe::Cursor>& actual)
185 {
186  return ActualCursor(actual);
187 }
188 inline ActualCursor actual(std::shared_ptr<dballe::CursorStation>& actual)
189 {
190  return ActualCursor(actual);
191 }
192 inline ActualCursor actual(std::shared_ptr<dballe::CursorStationData>& actual)
193 {
194  return ActualCursor(actual);
195 }
196 inline ActualCursor actual(std::shared_ptr<dballe::CursorData>& actual)
197 {
198  return ActualCursor(actual);
199 }
200 inline ActualCursor actual(std::shared_ptr<dballe::CursorSummary>& actual)
201 {
202  return ActualCursor(actual);
203 }
204 inline ActualDB<dballe::DB> actual(std::shared_ptr<dballe::DB> actual)
205 {
206  return ActualDB<dballe::DB>(actual);
207 }
208 ActualDB<dballe::DB> actual(std::shared_ptr<dballe::db::v7::DB> actual);
209 inline ActualDB<dballe::db::Transaction>
210 actual(std::shared_ptr<dballe::db::Transaction> actual)
211 {
212  return ActualDB<dballe::db::Transaction>(actual);
213 }
214 ActualDB<dballe::db::Transaction>
215 actual(std::shared_ptr<dballe::db::v7::Transaction> actual);
216 
217 extern template class BaseDBFixture<V7DB>;
218 extern template class DBFixture<V7DB>;
219 extern template class EmptyTransactionFixture<V7DB>;
220 extern template class ActualDB<dballe::DB>;
221 extern template class ActualDB<dballe::db::Transaction>;
222 
223 } // namespace tests
224 } // namespace dballe
void try_station_query(const std::string &query, unsigned expected)
Check results of a station query.
Forward declarations for public dballe/sql names.
Definition: db.h:149
const wreport::Var & var(wreport::Varcode code) const
Lookup a wreport::Var, throwing an exception if not found.
void data_context_matches(const Data &expected)
Check cursor data context after a query_data.
Definition: db/tests.h:48
void data_var_matches(const DBValues &expected)
Check cursor data variable(s) after a query_data.
Definition: db/tests.h:119
void try_summary_query(const std::string &query, unsigned expected, result_checker checker=nullptr)
Check results of a summary query.
Functions used to connect to DB-All.e and insert, query and delete data.
static const Data & downcast(const dballe::Data &data)
Return a reference to record downcasted as core::Record.
void data_var_matches(const Data &expected, wreport::Varcode code)
Check cursor data variable after a query_data.
Definition: db/tests.h:114
Definition: cmdline.h:18
Collection of DBValue objects, indexed by wreport::Varcode.
Definition: values.h:229
bool vars_equal(const DBValues &o) const
Check if the variables are the same, regardless of the data_id.
Definition: db/tests.h:35
Definition: db/tests.h:95
void data_var_matches(const Values &expected, wreport::Varcode code)
Check cursor data variable after a query_data.
Definition: db/tests.h:140
Definition: db/tests.h:56
Key/value store where keys are strings and values are wreport variables.
Definition: data.h:17
Definition: transaction.h:15
void try_data_query(const std::string &query, unsigned expected)
Check cursor data context anda variable after a query_data.
Definition: db.h:307
Test fixture used by old DB-All.e db tests.
Definition: db/tests.h:41
std::map< std::string, core::Data > data
Arbitrarily named data values.
Definition: db/tests.h:26
DB-ALLe database connection for database format V7.
Definition: db/v7/db.h:20
void data_matches(const Data &ds)
Check cursor data context and variable after a query_data.
Definition: db/tests.h:148
std::map< std::string, core::Data > stations
Arbitrarily named station values.
Definition: db/tests.h:24
Definition: db/tests.h:82
Definition: types.h:939
Definition: db/tests.h:166
Structures used as input to database insert functions.
Query used to filter DB-All.e data.
Definition: query.h:16
Base for datasets used to populate test databases.
Definition: db/tests.h:21
Collection of Value objects, indexed by wreport::Varcode.
Definition: values.h:215
Definition: db/tests.h:103
void station_keys_match(const DBStation &expected)
Check cursor context after a query_stations.