File: example.cpp

package info (click to toggle)
mysql-connector-c%2B%2B 1.1.0~r814-1
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 4,388 kB
  • ctags: 3,838
  • sloc: cpp: 39,820; ansic: 2,114; perl: 936; php: 528; xml: 399; sql: 399; sh: 85; makefile: 47
file content (235 lines) | stat: -rw-r--r-- 6,844 bytes parent folder | download
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
/*
   Copyright 2008 - 2010 Sun Microsystems, Inc.  All rights reserved.

   The MySQL Connector/C++ is licensed under the terms of the GPL
   <http://www.gnu.org/licenses/old-licenses/gpl-2.0.html>, like most
   MySQL Connectors. There are special exceptions to the terms and
   conditions of the GPL as it is applied to this software, see the
   FLOSS License Exception
   <http://www.mysql.com/about/legal/licensing/foss-exception.html>.
 */

#include "example.h"

namespace testsuite
{
namespace example
{

void example_test_class::test_hello_world1()
{
  logMsg("Hello world using framework magic");
  try
  {
    /*
     By default the framework will establish a connection in setUp() and connect
     to the configured MySQL Server and select the configured schema.
     No other members will be initialized, however, there are several
     auto-ptr members which you can make use of: this->stmt, this->pstmt, this->res.
     The good thing about the auto-ptr members is that you don't need to care much
     about them. tearDown() will reset them and auto-ptr will ensure proper
     memory management.
     */
    stmt.reset(con->createStatement());

    /* Running a SELECT and storing the returned result set in this->res */
    res.reset(stmt->executeQuery("SELECT 'Hello world!'"));

    /* Move result set cursor to first rowm, fetch result, write result to log  */
    res->next();
    logMsg(res->getString(1));

  }
  catch (sql::SQLException &e)
  {
    /* If anything goes wrong, write some info to the log... */
    logErr(e.what());
    logErr("SQLState: " + std::string(e.getSQLState()));

    /*
     ... and let the test fail. FAIL() is a macro.
     FAIL calls fail(const char* reason, const char* file, int line)
     */
    fail(e.what(), __FILE__, __LINE__);
  }
  /* If all goes fine, there is no need to call PASS() or something.	 */
}

void example_test_class::test_hello_world2()
{
  logMsg("Hello world without framework magic");
  try
  {
    /*
     Connection, Statement and ResultSet are typedefs from unit_fixture.h:

     typedef std::auto_ptr<sql::Connection> Connection;
     typedef std::auto_ptr<sql::PreparedStatement> PreparedStatement;
     typedef std::auto_ptr<sql::ParameterMetaData> ParameterMetaData;
     typedef std::auto_ptr<sql::Statement> Statement;
     typedef std::auto_ptr<sql::Savepoint> Savepoint;
     typedef std::auto_ptr<sql::ResultSet> ResultSet;
     typedef sql::Driver Driver;
     typedef std::auto_ptr<sql::ResultSetMetaData> ResultSetMetaData;
     typedef std::auto_ptr<sql::DatabaseMetaData> DatabaseMetaData;

     Do yourself a favour and use auto_ptr in tests!
     */
    Connection my_con(getConnection());
    Statement my_stmt(my_con->createStatement());

    /*
     The framework defines a couple of public members (from unit_fixture.h):

     Connection con;
     PreparedStatement pstmt;
     Statement stmt;
     ResultSet res;

     The members are managed in unit_fixture.cpp setUp() and tearDown().
     You don't need to clean up them as long as setUp() and tearDown()
     are called and are not overwritten by your own method.

     However note, if you declare a local variable of the same type
     and name of any of the above members, for example ResultSet res, some
     compilers will spawn warnings that you are hiding members.
     */
    res.reset(my_stmt->executeQuery("SELECT 'Hello world!' AS _world"));

    res->next();
    logMsg(res->getString("_world"));
    res->close();

    ResultSet res2(my_stmt->executeQuery("SELECT 'What a boring example' AS _complain"));
    res2->next();
    logMsg(res2->getString("_complain"));
    res2->close();
  }
  catch (sql::SQLException &e)
  {
    logErr(e.what());
    logErr("SQLState: " + std::string(e.getSQLState()));
    fail(e.what(), __FILE__, __LINE__);
  }
}

void example_test_class::test_assert_equals()
{
  logMsg("ASSERT_EQUALS() macro demo");
  /*
   Be careful: ASSERT_EQUALS() is not available for each and every type!
   You can find more details in test_asserts.h.
   */
  int a=-1;
  ASSERT_EQUALS(a, a);

  unsigned int h=1;
  ASSERT_EQUALS(h, h);

  int64_t i=-10;
  ASSERT_EQUALS(i, i);

  uint64_t j=10;
  ASSERT_EQUALS(j, j);

  bool b=false;
  ASSERT_EQUALS(b, b);

  float c= -1.23f;
  ASSERT_EQUALS(c, c);

  double d=1.23;
  ASSERT_EQUALS(d, d);

  /*
   For fuzzy comparisons e.g. 1.01 vs. 1.01 fetched from MySQL
   */
  double k = 1.01;
  double l = 1.0999992;
  fuzzyEquals(k, l, 0.001);

  long double e=1.23e12;
  ASSERT_EQUALS(e, e);

  const char f[]="foo";
  ASSERT_EQUALS(f, f);

  std::string g("good idea");
  ASSERT_EQUALS(g, g);
}

void example_test_class::test_assert_lessthan()
{
  logMsg("ASSERT_LT() macro demo");
  /*
   Be careful: ASSERT_LT(expected, value) is not available for each and every type!
   In fact at the time of writing its only available for int and unsigned int
   */
  int a=1, b=2;

  ASSERT_LT(b, a);
  ASSERT_LT((unsigned int) b, (unsigned int) a);

  /* There is no "less or equal than", use LT instead.
  Example: 1 <= 2 */
  ASSERT_LT(3, 1);
}

void example_test_class::test_assert_greaterthan()
{
  logMsg("ASSERT_GT() macro demo");
  /*
   Be careful: ASSERT_GT(expected, value) is not available for each and every type!
   In fact at the time of writing its only available for int and unsigned int
   */
  int a=1, b=2;

  ASSERT_GT(a, b);
  ASSERT_GT((unsigned int) a, (unsigned int) b);

  /* There is no "greater or equal than", use GT instead.
  Example: 2 >= 1 */
  ASSERT_GT(0, 2);
}

void example_test_class::test_assert_equals_fail()
{
  logMsg("ASSERT_EQUALS failure - EXPECTED failure");
  ASSERT_EQUALS(true, false);
}

void example_test_class::test_assert_message()
{
  logMsg("ASSERT_MESSAGE example");
  /*
   Wrapper of
  void assertTrue(const char * msg, bool expression
                , const char* file, int line);
   */
  ASSERT_MESSAGE(1 < 2, "ASSERT_MESSAGE example (true)");
  ASSERT_MESSAGE(1 > 2, "ASSERT_MESSAGE example (fail) - EXPECTED failure");
}

void example_test_class::test_skip()
{
  logMsg("SKIP() example");
  /* Test if MySQL has a certain feature, e.g. if Procedures are supported */
  if (true)
    SKIP("EXAMPLE: the server does not support stored procedures");
  /*
   If MySQL does not suppoer feature xyz, call SKIP() and test execution ends.
   You get here only if you feature is available
   */
  logMsg("Feature is available, use it.");
}

void example_test_class::test_todo()
{
  logMsg("TODO() example");
  /* Test if MySQL has a certain feature, e.g. if Procedures are supported */
  TODO("If you know that a test mail fail and you intentionally want to let it fail temporarily, for example, as long as a bug has not been fixed, mark it as TODO");
  //FAIL("Expected failure, known failure - its on your TODO");
}

} /* namespace example */
} /* namespace testsuite */