libdballe  7.21
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 
8 namespace dballe {
9 struct DB;
10 
11 namespace db {
12 
13 namespace v6 {
14 struct Driver;
15 class DB;
16 }
17 
18 namespace v7 {
19 struct Driver;
20 class DB;
21 }
22 
23 }
24 
25 namespace tests {
26 
27 Messages messages_from_db(DB& db, const dballe::Query& query);
28 Messages messages_from_db(DB& db, const char* query);
29 
32 {
34  std::map<std::string, StationValues> stations;
36  std::map<std::string, DataValues> data;
37 
38  TestDataSet() {}
39  virtual ~TestDataSet() {}
40 
41  virtual void populate_db(DB& db);
42 };
43 
46 {
48 };
49 
50 
51 struct BaseDBFixture : public Fixture
52 {
53  std::string backend;
54  db::Format format;
55 
56  BaseDBFixture(const char* backend, db::Format format);
57 
58  void test_setup();
59  bool has_driver();
60  static bool has_driver(const std::string& backend);
61 };
62 
65 {
66  dballe::sql::Connection* conn = nullptr;
67  db::v6::Driver* driver = nullptr;
68 
69  DriverFixture(const char* backend, db::Format format);
70  ~DriverFixture();
71 
72  void test_setup();
73 };
74 
77 {
78  dballe::sql::Connection* conn = nullptr;
79  db::v7::Driver* driver = nullptr;
80 
81  V7DriverFixture(const char* backend, db::Format format);
82  ~V7DriverFixture();
83 
84  void test_setup();
85 };
86 
87 struct DBFixture : public BaseDBFixture
88 {
89  DB* db = nullptr;
90 
91  DBFixture(const char* backend, db::Format format);
92  ~DBFixture();
93 
95  std::unique_ptr<DB> create_db();
96 
97  void test_setup();
98 
99  template<typename DataSet>
100  void populate()
101  {
102  DataSet data_set;
103  wassert(populate_database(data_set));
104  }
105 
106  void populate_database(TestDataSet& data_set);
107 };
108 
109 template<typename FIXTURE>
110 struct DBFixtureTestCase : public FixtureTestCase<FIXTURE>
111 {
113 };
114 
115 struct ActualCursor : public Actual<dballe::db::Cursor&>
116 {
117  using Actual::Actual;
118 
120  void station_keys_match(const Station& expected);
121 
123  void station_vars_match(const StationValues& expected);
124 
126  void data_context_matches(const DataValues& expected);
127 
129  void data_var_matches(const StationValues& expected, wreport::Varcode code) {
130  data_var_matches(*expected.values[code].var);
131  }
133  void data_var_matches(const DataValues& expected, wreport::Varcode code) {
134  data_var_matches(*expected.values[code].var);
135  }
137  void data_var_matches(const DataValues& expected) {
138  if (auto c = dynamic_cast<dballe::db::CursorValue*>(&_actual))
139  data_var_matches(*expected.values[c->get_varcode()].var);
140  else
141  throw wreport::error_consistency("cannot call data_var_matches on this kind of cursor");
142  }
144  void data_var_matches(const Values& expected, wreport::Varcode code) {
145  data_var_matches(*expected[code].var);
146  }
148  void data_var_matches(const wreport::Var& expected);
149 
151  void data_matches(const DataValues& ds)
152  {
153  if (auto c = dynamic_cast<dballe::db::CursorValue*>(&_actual))
154  data_matches(ds, c->get_varcode());
155  else
156  throw wreport::error_consistency("cannot call data_matches on this kind of cursor");
157  }
159  void data_matches(const DataValues& ds, wreport::Varcode code);
160 };
161 
162 typedef std::function<void(const std::vector<core::Record>&)> result_checker;
163 
164 struct ActualDB : public Actual<dballe::DB&>
165 {
166  using Actual::Actual;
167 
169  void try_data_query(const std::string& query, unsigned expected);
170 
172  void try_data_query(const Query& query, unsigned expected);
173 
175  void try_station_query(const std::string& query, unsigned expected);
176 
178  void try_summary_query(const std::string& query, unsigned expected, result_checker checker=nullptr);
179 };
180 
181 inline ActualCursor actual(dballe::db::Cursor& actual) { return ActualCursor(actual); }
182 inline ActualCursor actual(dballe::db::CursorStation& actual) { return ActualCursor(actual); }
183 inline ActualCursor actual(dballe::db::CursorStationData& actual) { return ActualCursor(actual); }
184 inline ActualCursor actual(dballe::db::CursorData& actual) { return ActualCursor(actual); }
185 inline ActualCursor actual(dballe::db::CursorSummary& actual) { return ActualCursor(actual); }
186 inline ActualCursor actual(std::unique_ptr<dballe::db::Cursor>& actual) { return ActualCursor(*actual); }
187 inline ActualCursor actual(std::unique_ptr<dballe::db::CursorStation>& actual) { return ActualCursor(*actual); }
188 inline ActualCursor actual(std::unique_ptr<dballe::db::CursorStationData>& actual) { return ActualCursor(*actual); }
189 inline ActualCursor actual(std::unique_ptr<dballe::db::CursorData>& actual) { return ActualCursor(*actual); }
190 inline ActualCursor actual(std::unique_ptr<dballe::db::CursorSummary>& actual) { return ActualCursor(*actual); }
191 inline ActualDB actual(dballe::DB& actual) { return ActualDB(actual); }
192 inline ActualDB actual(std::unique_ptr<dballe::DB>& actual) { return ActualDB(*actual); }
193 
194 }
195 }
Cursor iterating over summary entries.
Definition: db.h:136
Cursor iterating over station data values.
Definition: db.h:118
Information about a station.
Definition: values.h:20
A set of measured values.
Definition: values.h:303
void data_var_matches(const DataValues &expected, wreport::Varcode code)
Check cursor data variable after a query_data.
Definition: db/tests.h:133
Forward declarations for public dballe/sql names.
Copyright (C) 2008–2010 ARPA-SIM urpsim@smr.arpa.emr.it
Definition: cmdline.h:17
Definition: v6/driver.h:52
std::map< std::string, StationValues > stations
Arbitrarily named station values.
Definition: db/tests.h:34
void data_matches(const DataValues &ds)
Check cursor data context and variable after a query_data.
Definition: db/tests.h:151
Common interface for all kinds of cursors.
Definition: db.h:38
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:97
Cursor iterating over data values.
Definition: db.h:123
Definition: db/tests.h:87
void data_var_matches(const Values &expected, wreport::Varcode code)
Check cursor data variable after a query_data.
Definition: db/tests.h:144
uint16_t Varcode
Definition: db/tests.h:51
Test fixture for SQL backend drivers.
Definition: db/tests.h:76
Definition: db.h:156
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:129
Test fixture used by old DB-All.e db tests.
Definition: db/tests.h:45
A set of station values.
Definition: values.h:273
Definition: db/tests.h:110
Definition: db/tests.h:164
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:137
Base for datasets used to populate test databases.
Definition: db/tests.h:31
std::map< std::string, DataValues > data
Arbitrarily named data values.
Definition: db/tests.h:36
Collection of Value objects, indexed by wreport::Varcode.
Definition: values.h:202
Test fixture for SQL backend drivers.
Definition: db/tests.h:64
Definition: v7/driver.h:26
Definition: db/tests.h:115
Implement a storage object for a group of related observation data.