File: myx_recordset_test.cpp

package info (click to toggle)
mysql-query-browser 1.2.5beta-3
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 63,792 kB
  • ctags: 46,485
  • sloc: pascal: 249,299; ansic: 80,111; cpp: 72,467; sh: 25,271; objc: 20,015; yacc: 10,755; java: 9,917; xml: 4,580; php: 2,806; python: 1,566; sql: 1,563; makefile: 1,452; perl: 3
file content (256 lines) | stat: -rw-r--r-- 8,329 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

// This file contains common TUT test cases for myx_recordet.c
//
//

//----------------------------------------------------------------------------------------------------------------------

#include "test.h"
#include "myx_public_interface.h"
#include "myx_library.h"


#include <m_ctype.h> 
// Private test data.
BEGIN_TEST_DATA_CLASS(module6_recordset_test)
protected:
  Test_connection* connection;
END_TEST_DATA_CLASS

//----------------------------------------------------------------------------------------------------------------------

TEST_MODULE(module6_recordset_test, "Recordset test, base library");


//----------------------------------------------------------------------------------------------------------------------

TEST_FUNCTION(5)
{
  connection= test_group_singleton.get_connection();

  ensure("Valid server connection", connection != NULL);
}

//----------------------------------------------------------------------------------------------------------------------

static const char *big_rs_query= "select * from sakila.payment, sakila.staff_list";
static const char *not_so_big_rs_query= "select * from sakila.film";

// bug #11070
// memory usage limit handler
TEST_FUNCTION(10)
{
  unsigned int old_limit= myx_mysql_get_resultset_size_limit(connection->get_mysql());
  MYX_RESULTSET *rset;
  MYX_LIB_ERROR err;

  myx_mysql_limit_resultset_size(connection->get_mysql(), 1); // 1MB

  // check if it aborts the query
  rset= myx_query_execute(connection->get_mysql(),
                          big_rs_query,
                          0,
                          NULL,
                          &err, NULL,
                          NULL,
                          NULL,
                          NULL);

  ensure_equals("big query with memory limit", err, MYX_MEMORY_LIMIT_EXCEEDED);
  ensure("big query with memory limit", rset != NULL);
  myx_query_free_resultset(rset);

  // check if a smaller resultset runs ok
  rset= myx_query_execute(connection->get_mysql(),
                          not_so_big_rs_query, 
                          0,
                          NULL,
                          &err, NULL,
                          NULL,
                          NULL,
                          NULL);

  ensure_equals("small query with memory limit", err, MYX_NO_ERROR);
  ensure("small query with memory limit", rset != NULL);
  puts(mysql_error(connection->get_mysql()));
  myx_query_free_resultset(rset);

  myx_mysql_limit_resultset_size(connection->get_mysql(), old_limit);

  // check if it runs ok without the limit
  rset= myx_query_execute(connection->get_mysql(),
                          big_rs_query, 
                          0,
                          NULL,
                          &err, NULL,
                          NULL,
                          NULL,
                          NULL);

  ensure_equals("big query without memory limit", err, MYX_NO_ERROR);
  ensure("big query without memory limit", rset != NULL);
  myx_query_free_resultset(rset);
}

//----------------------------------------------------------------------------------------------------------------------

TEST_FUNCTION(15)
{
  ubigint result= myx_bit_to_int(NULL);
  ensure_equals("Convert BIT to integer", result, -1);

  result= myx_bit_to_int("");
  ensure_equals("Convert BIT to integer", result, -1);

  result= myx_bit_to_int("31");
  ensure_equals("Convert BIT to integer", result, 31);

  result= myx_bit_to_int("b'11100'");
  ensure_equals("Convert BIT to integer", result, 28);

  result= myx_bit_to_int("0xFFFE");
  ensure_equals("Convert BIT to integer", result, 65534);

  result= myx_bit_to_int("0xabcde");
  ensure_equals("Convert BIT to integer", result, 703710);

  result= myx_bit_to_int("010");
  ensure_equals("Convert BIT to integer", result, 10);

  result= myx_bit_to_int("0x");
  ensure_equals("Convert BIT to integer", result, 0);

  result= myx_bit_to_int("0xFFFF0000AAAA0000");
  ensure_equals("Convert BIT to integer", result, 18446462601596108800ULL);

}

//----------------------------------------------------------------------------------------------------------------------

TEST_FUNCTION(20)
{
  // Integer to BIT string conversion.
  // The conversion is based on baseconv from the utilities lib, which has been tested completely in the
  // util functions test module. Hence we only test what is added by myx_int_to_bit.
  char* result= myx_int_to_bit(0);
  ensure_equals("Convert integer to BIT", result, std::string("b'0'"));
  g_free(result);

  result= myx_int_to_bit(1020389);
  ensure_equals("Convert integer to BIT", result, std::string("b'11111001000111100101'"));
  g_free(result);
                                                                            
}

//----------------------------------------------------------------------------------------------------------------------

// bug #19238
// the problem with wrong UPDATE statement generated was because of
// incorrectly determined PK columns is the preceeding SELECT.
// This test checks the correctness of SELECT resultset 
TEST_FUNCTION(25)
{
  static const char *init_query=
    "DROP TABLE IF EXISTS test.test_19238;"
    "CREATE TABLE test.test_19238 (id INT PRIMARY KEY, d VARCHAR(255)) ENGINE=MYISAM;"
  ;

  static const char *cleanup_query=
    "DROP TABLE IF EXISTS test.test_19238"
  ;

  static const char *test_query[]= {
    "SELECT * FROM test.test_19238",
    "SELECT * \nFROM test.test_19238",
    "SELECT * \r\nFROM test.test_19238",
    "SELECT \r\n* \r\nFROM test.test_19238",
    "SELECT \n* \nFROM test.test_19238",
    NULL
  };

  MYX_RESULTSET *rset;
  MYX_LIB_ERROR err;

  Auto_release ar;
  ar.add_sql(connection, cleanup_query);

  connection->multi_query(init_query);

  for(int i= 0; test_query[i] != NULL; i++)
  {
    rset= ar.add(myx_query_execute(connection->get_mysql(),
                                  test_query[i], 1,
                                  NULL, &err, NULL,
                                  NULL, NULL, NULL));

    ensure("Resultset is NULL", rset);
    ensure("Invalid column number in resultset", rset->columns_num == 2);
    ensure("PK not marked", rset->columns[0].table_column->is_pk == 1);
    ensure("Invalid PK flag", rset->columns[1].table_column->is_pk == 0);
  }
}

//----------------------------------------------------------------------------------------------------------------------

// test for #19000 - PK field(s) is always present in HTML export
// test that output html file doesnt contain PK fields if they where not
// present in original query

TEST_FUNCTION(30)
{
  static const char *init_query=
    "DROP TABLE IF EXISTS test.test_19000;"
    "CREATE TABLE test.test_19000 (id INT PRIMARY KEY, data VARCHAR(255)) ENGINE=MYISAM;"
    "INSERT INTO test.test_19000 VALUES (1, 'data1'), (2, 'data2');"
  ;

  static const char *cleanup_query=
    "DROP TABLE IF EXISTS test.test_19000"
  ;

  static const char *test_query=
      "SELECT data FROM test.test_19000"
  ;

  MYX_RESULTSET *rset;
  MYX_LIB_ERROR err;

  Auto_release ar;
  ar.add_sql(connection, cleanup_query);

  connection->multi_query(init_query);

  MYX_TABLE_EXPORTER_INFO *exp_info= myx_get_table_exporter_info("HTML");

  ensure("Export info for HTML format == NULL", exp_info != NULL);

  rset= ar.add(myx_query_execute(connection->get_mysql(),
                                test_query, 1,
                                NULL, &err, NULL,
                                NULL, NULL, NULL));

  myx_export_resultset(connection->get_mysql(), exp_info,
                         "../test-data/test_19000.html",
                         "Query $QUERY$, $DATE$",
                         rset,
                         "");

  // need to close the file 
  myx_free_table_exporter_info(exp_info);

  // file is very small
  std::fstream f("../test-data/test_19000.html", std::ios::in | std::ios::binary);

  char *data1= ar.add_delete(new char[200000]);
  f.read(data1, 200000-1);
  size_t count= (size_t)f.gcount();
  f.close();
  remove("../test-data/test_19000.html");

  data1[count]= '\0';
  ensure("Output file contains redundant PK data", strstr(data1, "id") == NULL);
}

//----------------------------------------------------------------------------------------------------------------------

END_TESTS;