File: unit_fixture.h

package info (click to toggle)
mysql-connector-c%2B%2B 1.1.12-4
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye
  • size: 4,904 kB
  • sloc: cpp: 44,895; ansic: 2,114; php: 528; sql: 403; xml: 109; sh: 33; makefile: 11
file content (403 lines) | stat: -rw-r--r-- 11,372 bytes parent folder | download | duplicates (2)
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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
/*
 * Copyright (c) 2008, 2018, Oracle and/or its affiliates. All rights reserved.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License, version 2.0, as
 * published by the Free Software Foundation.
 *
 * This program is also distributed with certain software (including
 * but not limited to OpenSSL) that is licensed under separate terms,
 * as designated in a particular file or component or in included license
 * documentation.  The authors of MySQL hereby grant you an
 * additional permission to link the program and your derivative works
 * with the separately licensed software that they have included with
 * MySQL.
 *
 * Without limiting anything contained in the foregoing, this file,
 * which is part of MySQL Connector/C++, is also subject to the
 * Universal FOSS Exception, version 1.0, a copy of which can be found at
 * http://oss.oracle.com/licenses/universal-foss-exception.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU General Public License, version 2.0, for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA
 */



#ifndef _UNIT_FIXTURE_
#define _UNIT_FIXTURE_

#include <driver/mysql_public_iface.h>
#include "../framework/framework.h"
#include <vector>
#include <stdlib.h>
#include <cppconn/connection.h>

#include <boost/scoped_ptr.hpp>

namespace testsuite
{
/* TODO - document */
typedef boost::scoped_ptr<sql::Connection> Connection;
typedef boost::scoped_ptr<sql::PreparedStatement> PreparedStatement;
typedef sql::ParameterMetaData ParameterMetaData;
typedef boost::scoped_ptr<sql::Statement> Statement;
typedef boost::scoped_ptr<sql::Savepoint> Savepoint;
typedef boost::scoped_ptr<sql::ResultSet> ResultSet;
typedef sql::Driver Driver;
typedef sql::ResultSetMetaData ResultSetMetaData;
typedef sql::DatabaseMetaData DatabaseMetaData;

struct columndefinition
{
  std::string name; // TYPE_NAME
  std::string sqldef;
  int ctype; // DATA_TYPE
  std::string value;
  bool is_signed;
  unsigned int precision; // COLUMN_SIZE
  int decimal_digits; // DECIMAL_DIGITS
  bool is_nullable; // IS_NULLABLE
  int nullable; // NULLABLE
  std::string column_def; // COLUMN_DEF
  bool is_case_sensitive;
  std::string remarks; // REMARKS
  int is_searchable;
  bool fixed_prec_scale;
  bool auto_increment;
  std::string local_type_name;
  int minimum_scale;
  int maximum_scale;
  int num_prec_radix; // NUM_PREC_RADIX
  int sql_data_type; // SQL_DATA_TYPE
  int sql_datetime_sub; // SQL_DATA_TYPE_SUB
  unsigned int char_octet_length;
  std::string is_autoincrement;
  bool is_negative;
  std::string as_string; // value as it should be returns by getString()
  bool check_as_string;

  columndefinition(const std::string & _name, const std::string & _sqldef, int _ctype,
                   const std::string & _value, bool _is_signed, unsigned int _precision,
                   int _decimal_digits, bool _is_nullable, std::string _column_def,
                   unsigned int _char_octet_length, const std::string & _is_autoincrement, bool _is_negative) :
  name(_name),
  sqldef(_sqldef),
  ctype(_ctype),
  value(_value),
  is_signed(_is_signed),
  precision(_precision),
  decimal_digits(_decimal_digits),
  is_nullable(_is_nullable),
  nullable((_is_nullable) ? sql::DatabaseMetaData::columnNullable : sql::DatabaseMetaData::columnNoNulls),
  column_def(_column_def),
  is_case_sensitive(false),
  remarks(""),
  is_searchable(sql::DatabaseMetaData::typeSearchable),
  fixed_prec_scale(false),
  auto_increment(false),
  local_type_name(""),
  minimum_scale(0),
  maximum_scale(0),
  num_prec_radix(10),
  sql_data_type(0),
  sql_datetime_sub(0),
  char_octet_length(_char_octet_length),
  is_autoincrement(_is_autoincrement),
  is_negative(_is_negative),
  check_as_string(false)
  {
  }

  columndefinition(const std::string & _name, const std::string & _sqldef, int _ctype,
                   const std::string & _value, bool _is_signed, unsigned int _precision,
                   int _decimal_digits, bool _is_nullable, const std::string & _column_def,
                   int _char_octet_length, const std::string & _is_autoincrement,
                   bool _is_negative, const std::string & _as_string) :
  name(_name),
  sqldef(_sqldef),
  ctype(_ctype),
  value(_value),
  is_signed(_is_signed),
  precision(_precision),
  decimal_digits(_decimal_digits),
  is_nullable(_is_nullable),
  nullable((_is_nullable) ? sql::DatabaseMetaData::columnNullable : sql::DatabaseMetaData::columnNoNulls),
  column_def(_column_def),
  is_case_sensitive(false),
  remarks(""),
  is_searchable(sql::DatabaseMetaData::typeSearchable),
  fixed_prec_scale(false),
  auto_increment(false),
  local_type_name(""),
  minimum_scale(0),
  maximum_scale(0),
  num_prec_radix(10),
  sql_data_type(0),
  sql_datetime_sub(0),
  char_octet_length(_char_octet_length),
  is_autoincrement(_is_autoincrement),
  is_negative(_is_negative),
  as_string(_as_string),
  check_as_string(true)
  {
  }

};


struct udtattribute
{
  std::string name;
  int ctype;

  udtattribute(std::string n, int c) :
  name(n),
  ctype(c)
  {
  }

};


class unit_fixture : public TestSuite
{
private:
  typedef TestSuite super;

protected:
  /**
   * List of SQL schema objects created during the test run
   *
   * Used by tearDown() to clean up the database after the test run
   */
  List created_objects;

  /**
   * Driver to be used
   *
   * The element is managed by getConnection(). You do not need
   * to worry about the creation of a connection or a driver object,
   * use getConnection() to obtain a connection object.
   */
  static Driver *driver;

  /**
   * Copies command line connection parameter into protected slots
   *
   */
  void init();

  /**
   * Effective database connection URL
   *
   * URL refers to the conector/C++ connection URL syntax
   *
   * Fallback to default_url, see also default_url
   */
  String url;

  /**
   * Name of the schema to use (USE <db>)
   *
   * The schema will be selected during setUp() before running a test
   * Fallback to default_db, see also default_db
   */
  String db;

  /**
   * Database user for getConnection()
   *
   * Fallback to default_user, see also default_user
   */
  String user;

  /**
   * Database user password
   *
   * Fallback to default_password, see also default_password
   */
  String passwd;

  /**
   * Database connection, initiated during setUp() and cleaned up in tearDown()
   *
   */
  Connection con;

  /**
   * PreparedStatement to be used in tests, not initialized. Cleaned up in tearDown()
   */
  PreparedStatement pstmt;

  /**
   * Statement to be used in tests, not initialized. Cleaned up in tearDown()
   */
  Statement stmt;

  /**
   * ResultSet to be used in tests, not initialized. Cleaned up in tearDown()
   */
  ResultSet res;

  /**
   * List of all column types known by MySQL
   *
   */
  std::vector< columndefinition > columns;

  /**
   * List of all columns which getAttribute() should deliver
   *
   */
  std::vector< udtattribute > attributes;


  /**
   * Creates a SQL schema object
   *
   * Use this method to create arbitrary SQL object if you want the test
   * framework to clean up SQL schema objects for you at the end of the test
   * in tearDown(). The framework will register all objects created with any
   * of the createXYZ() methods and call the appropriate dropXYZ() method during
   * tearDown().
   *
   * Although this method is protected and available in the test, you should
   * prefer calling any of the specialized wrappers, for example createTable().
   *
   * @param	object_type							SQL type, e.g. TABLE, FUNCTION, ...
   * @param object_name							SQL element name, e.g. "table1"
   * @param	columns_and_other_stuff Additional SQL definitions
   * @throws SQLException &
   */

  void createSchemaObject(String object_type, String object_name,
                          String columns_and_other_stuff);

  /**
   * Drops a SQL schema object
   *
   * All SQL objects created by createSchemaObject() will be implicitly
   * dropped during tearDown().
   *
   * @param object_type			SQL type e.g. TABLE, FUNCTION, ...
   * @param object_name			SQL element name, e.g. "table1"
   * @throws SQLException &
   */
  void dropSchemaObject(String object_type, String object_name);

  /**
   * Create a SQL table of the given name and with the specified SQL definition
   *
   * @param table_name							SQL table name
   * @param	columns_and_other_stuff Additional SQL definitions
   * @throws SQLException &
   */
  void createTable(String table_name, String columns_and_other_stuff);

  /**
   * Drops a SQL table
   *
   * Note: all SQL tables created by createTable() will implicitly dropped
   * during tearDown().
   *
   * @param	table_name	SQL table name
   * @throws SQLException &
   */
  void dropTable(String table_name);

  /**
   * Returns a Connector/C++ connection object
   *
   * Note: you are responsible for handling the object, you might
   * want to use typedef boost::scoped_ptr<sql::Connection>con(getConnection())
   * or similar for convenience reasons.
   *
   * @throws SQLException &
   */
  sql::Connection * getConnection(sql::ConnectOptionsMap *additional_opts=NULL);

  /**
   * Checks if the passed SQLException is caused by the specified error
   *
   */
  std::string exceptionIsOK(sql::SQLException &e, const std::string& sql_state, int errNo);

  /**
   * Checks if the passed exception has the SQLState HY000 and the (vendor) error code 0
   */
  std::string exceptionIsOK(sql::SQLException &e);

  /**
   * Helper function to check scrolling through a result set
   */
  void checkResultSetScrolling(ResultSet &res);


  /**
   * Helper function to fetch the MySQL Server version as a classical integer in the range from 30000 - 99999
   */
  int getMySQLVersion(Connection &con);

public:

  /**
   * Creates a new BaseTestCase object.
   *
   * @param name The name of the unit test case
   */
  unit_fixture(const String & name);

  /**
   * Writes a message to the debug log
   *
   * @param message
   */
  void logDebug(const String & message);

  /**
   * Write (info) message to test protocol
   *
   * @param	message	Message to be included in the protocol
   */
  void logMsg(const String & message);

  /**
   * Write error to test protocol
   *
   * @param	message Message to be included in the protocol
   */
  void logErr(const String & message);

  /**
   * Creates resources used by all tests.
   *
   * Opens a connection and stores the object in this->con.
   * All other members (this->stmt, this->pstmt, this->res) are uninitialized!
   *
   * @throws SQLException &
   */
  virtual void setUp();

  /**
   * Destroys resources created during the test case.
   *
   * @throws SQLException &
   */
  virtual void tearDown();

};

} /* namespace testsuite */

#define EXAMPLE_TEST_FIXTURE( theFixtureClass ) typedef theFixtureClass TestSuiteClass;\
  theFixtureClass( const String & /* name */ ) \
  : unit_fixture( #theFixtureClass )

#endif // _UNIT_FIXTURE_