File: mysql_routinegroup_editor_test.cpp

package info (click to toggle)
mysql-workbench 6.3.8%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 113,932 kB
  • ctags: 87,814
  • sloc: ansic: 955,521; cpp: 427,465; python: 59,728; yacc: 59,129; xml: 54,204; sql: 7,091; objc: 965; makefile: 638; sh: 613; java: 237; perl: 30; ruby: 6; php: 1
file content (236 lines) | stat: -rw-r--r-- 8,238 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
236
/* 
 * Copyright (c) 2011, 2015, 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 as
 * published by the Free Software Foundation; version 2 of the
 * License.
 * 
 * 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 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
 */

#include "../../plugins/db.mysql.editors/backend/mysql_routinegroup_editor.h"
#include "synthetic_mysql_model.h"

using namespace grt;
using namespace bec;
using namespace tut;

BEGIN_TEST_DATA_CLASS(mysql_routinegroup_editor_test)
public:
  WBTester wbt;
  GRT* grt;

TEST_DATA_CONSTRUCTOR(mysql_routinegroup_editor_test)
{
  grt = wbt.grt;
}

END_TEST_DATA_CLASS


TEST_MODULE(mysql_routinegroup_editor_test, "mysql_routinegroup_editor_test");

TEST_FUNCTION(10) 
{
const char* routine_sql = "";

  SynteticMySQLModel model(grt);
  size_t count = model.routineGroup->routines().count();
  ensure("Invalid number of routines", count == 1);

  model.schema->name("test_schema");
  model.routineGroup->name("rg");
  MySQLRoutineGroupEditorBE rg(wbt.wb->get_grt_manager(), model.routineGroup);

  // Parse SQL without any routine definition. That should not affect the routine's existence.
  rg.set_sql(routine_sql);

  count = model.routineGroup->routines().count();
  ensure("Routine disappeard", count == 1);
}


TEST_FUNCTION(20) 
{
#ifndef NL
#define NL "\n"
#endif

const char* routine_sql =
  "DELIMITER //" NL
  "CREATE FUNCTION get_count(less_than INT, greather_than INT) RETURNS INT" NL
  "    DETERMINISTIC" NL
  "    READS SQL DATA" NL
  "BEGIN" NL
  "       #OK, here some comment" NL
  "  DECLARE res INTEGER; #FEES PAID TO RENT THE VIDEOS INITIALLY" NL
  "  SELECT count(*) INTO res" NL
  "    FROM t1" NL
  "    WHERE id > less_than AND id < greather_than;" NL
  "  RETURN res;" NL
  "END //" NL
  "CREATE FUNCTION get_count1(less_than INT, greather_than INT) RETURNS INT" NL
  "    DETERMINISTIC" NL
  "    READS SQL DATA" NL
  "BEGIN" NL
  "       #OK, here some comment" NL
  "  DECLARE res INTEGER; #FEES PAID TO RENT THE VIDEOS INITIALLY" NL
  "  SELECT count(*) INTO res" NL
  "    FROM t1" NL
  "    WHERE id > less_than AND id < greather_than;" NL
  "  RETURN res;" NL
  "END //" NL
  "CREATE FUNCTION get_count2(less_than INT, greather_than INT) RETURNS INT" NL
  "    DETERMINISTIC" NL
  "    READS SQL DATA" NL
  "BEGIN" NL
  "       #OK, here some comment" NL
  "  DECLARE res INTEGER; #FEES PAID TO RENT THE VIDEOS INITIALLY" NL
  "  SELECT count(*) INTO res" NL
  "    FROM t1" NL
  "    WHERE id > less_than AND id < greather_than;" NL
  "  RETURN res;" NL
  "END //" NL
  "DELIMITER ;";

  SynteticMySQLModel model(grt);
  model.schema->name("test_schema");
  model.routineGroup->name("rg");
  MySQLRoutineGroupEditorBE rg(wbt.wb->get_grt_manager(), model.routineGroup);

  // Note: use_sql is a special function only for tests like this. The normal access function
  //       set_sql() is interacting with the associated code editor. We don't have a working editor
  //       in unit tests, however.
  rg.use_sql(routine_sql);

  std::string names[] = {"get_count", "get_count1", "get_count2"};
  assure_equal(model.routineGroup->routines().count(), sizeof(names) / sizeof(names[0]));
  for (size_t i = 0, size = model.routineGroup->routines().count(); i < size; i++)
  {
    db_RoutineRef r = model.routineGroup->routines().get(i);
    std::string name = r->name();
    assure_equal(names[i], name);
  }

  // Read back the sql from the group to see how it changed.
  std::string processed_sql = rg.get_sql();

  std::vector<std::string> processed_routines = base::split(processed_sql, "\n\n");
  ensure_equals("Lines unintentionally removed", 5U, processed_routines.size());

  // Do the same steps from above again with the processed sql.
  // There shouldn't be any change.
  rg.use_sql(processed_sql);

  assure_equal(model.routineGroup->routines().count(), sizeof(names) / sizeof(names[0]));
  for (size_t i = 0, size = model.routineGroup->routines().count(); i < size; i++)
  {
    db_RoutineRef r = model.routineGroup->routines().get(i);
    std::string name = r->name();
    assure_equal(names[i], name);
  }

  std::string twice_processed_sql = rg.get_sql();
  std::vector<std::string> twice_processed_routines = base::split(twice_processed_sql, "\n\n");
  ensure_equals("Lines unintentionally removed", 5U, twice_processed_routines.size());

  // Now compares each routine to discard any difference
  for(size_t index = 0; index < processed_routines.size(); index++)
    ensure_equals("Routine unintentionally changed", processed_routines[index], twice_processed_routines[index]);
}

/**
 *	Same test as case 20, but this time with syntax errors.
 */
TEST_FUNCTION(30) 
{
const char* routine_sql =
  "DELIMITER //" NL
  "CR!!! FUNCTION get_count(less_than INT, greather_than INT) RETURNS INT" NL
  "    DETERMINISTIC" NL
  "    READS SQL DATA" NL
  "BEGIN" NL
  "       #OK, here some comment" NL
  "  DECLARE res INTEGER; #FEES PAID TO RENT THE VIDEOS INITIALLY" NL
  "  SELECT count(*) INTO res" NL
  "    FROM t1" NL
  "    WHERE id > less_than AND id < greather_than;" NL
  "  RETURN res;" NL
  "END //" NL
  "CREATE FUNCTION get_count1(less_than INT, greather_than INT) RETURNS INT" NL
  "    DETERMINISTIC" NL
  "    READS SQL DATA" NL
  "BEGIN" NL
  "       #OK, here some comment" NL
  "  DECLARE res INTEGER; #FEES PAID TO RENT THE VIDEOS INITIALLY" NL
  "  SELECT count(*) INTO res" NL
  "    FROM t1" NL
  "    WHERE id > less_than AND id < greather_than;" NL
  "  RETURN res;" NL
  "END //" NL
  "CREATE FUNCTION get_count2(less_than INT, greather_than INT) RETURNS INT" NL
  "    DETERMINISTIC" NL
  "    READS SQL DATA" NL
  "-- BEGIN" NL
  "       #OK, here some comment" NL
  "  DECLARE res INTEGER; #FEES PAID TO RENT THE VIDEOS INITIALLY" NL
  "  SELECT count(*) INTO res" NL
  "    FROM t1" NL
  "    WHERE id > less_than AND id < greather_than;" NL
  "  RETURN res;" NL
  "END //" NL
  "DELIMITER ;";

  SynteticMySQLModel model(grt);
  model.schema->name("test_schema");
  model.routineGroup->name("rg");
  MySQLRoutineGroupEditorBE rg(wbt.wb->get_grt_manager(), model.routineGroup);

  rg.use_sql(routine_sql);

  std::string names[] = { "rg_SYNTAX_ERROR_1", "get_count1", "get_count2_SYNTAX_ERROR" };
  assure_equal(model.routineGroup->routines().count(), sizeof(names) / sizeof(names[0]));
  for (size_t i = 0, size = model.routineGroup->routines().count(); i < size; i++)
  {
    db_RoutineRef r= model.routineGroup->routines().get(i);
    std::string name= r->name();
    assure_equal(names[i], name);
  }

  // Read back the sql from the group to see how it changed.
  std::string processed_sql = rg.get_sql();

  std::vector<std::string> processed_routines = base::split(processed_sql, "\n\n");
  ensure_equals("Lines unintentionally removed", 5U, processed_routines.size());

  // Do the same steps from above again with the processed sql.
  // There shouldn't be any change.
  rg.use_sql(processed_sql);

  assure_equal(model.routineGroup->routines().count(), sizeof(names) / sizeof(names[0]));
  for (size_t i = 0, size = model.routineGroup->routines().count(); i < size; i++)
  {
    db_RoutineRef r = model.routineGroup->routines().get(i);
    std::string name = r->name();
    assure_equal(names[i], name);
  }

  std::string twice_processed_sql = rg.get_sql();
  std::vector<std::string> twice_processed_routines = base::split(twice_processed_sql, "\n\n");
  ensure_equals("Lines unintentionally removed", 5U, twice_processed_routines.size());

  // Now compares each routine to discard any difference
  for (size_t index = 0; index < processed_routines.size(); index++)
    ensure_equals("Routine unintentionally changed", processed_routines[index], twice_processed_routines[index]);
}

END_TESTS