File: test_mysql_sql_facade.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 (227 lines) | stat: -rw-r--r-- 8,754 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
/* 
 * 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
 */

#ifndef _WIN32
#include <sstream>
#endif

#include "grt_test_utility.h"
#include "testgrt.h"
#include "grtsqlparser/sql_facade.h"
#include "wb_helpers.h"

BEGIN_TEST_DATA_CLASS(mysql_sql_facade)
public:
  WBTester wbt;
  SqlFacade::Ref sql_facade;
  db_mgmt_RdbmsRef rdbms;
  DictRef options;
END_TEST_DATA_CLASS


TEST_MODULE(mysql_sql_facade, "SQL Parser FE (MySQL)");

// Creates the needed structure for the testing...
TEST_FUNCTION(1)
{
  sql_facade = NULL;
  wbt.create_new_document();
  GRT *grt= wbt.grt;

  ensure_equals("loaded physycal model count", wbt.wb->get_document()->physicalModels().count(), 1U);

  options= DictRef(grt);
  options.set("gen_fk_names_when_empty", IntegerRef(0));

  rdbms= wbt.wb->get_document()->physicalModels().get(0)->rdbms();

  sql_facade= SqlFacade::instance_for_rdbms(rdbms);
  ensure("failed to get sqlparser module", (NULL != sql_facade));
}

// Pretty simple parsing sample
TEST_FUNCTION(2)
{
  ensure("failed to get sqlparser module", (NULL != sql_facade));
  
  std::string schema_name;
  std::string table_name;
  SqlFacade::String_tuple_list columns;
  
  std::string query = "select first_name, last_name from sakila.customer;";

  ensure("Unexpexted failure parsing test", sql_facade->parseSelectStatementForEdit(query, schema_name, table_name, columns));
  ensure_equals("Unexpected Schema Name", schema_name, "sakila");
  ensure_equals("Unexpected Table Name", table_name, "customer");
  ensure_equals("Unexpected Column Count", columns.size(), 2U);
  ensure("Unexpected Column Name", columns.front().first == "first_name");
  ensure("Unexpected Column Alias", columns.front().second == "first_name");
  columns.pop_front();
  ensure("Unexpected Column Name", columns.front().first == "last_name");
  ensure("Unexpected Column Alias", columns.front().second == "last_name");
  columns.pop_front();
}

// Pretty simple parsing sample using aliases for the columns
TEST_FUNCTION(3)
{
  ensure("failed to get sqlparser module", (NULL != sql_facade));

  std::string schema_name;
  std::string table_name;
  SqlFacade::String_tuple_list columns;
  
  std::string query = "select first_name as 'First Name', last_name as 'Last Name' from sakila.customer;";

  ensure("Unexpexted failure parsing test", sql_facade->parseSelectStatementForEdit(query, schema_name, table_name, columns));
  ensure_equals("Unexpected Schema Name", schema_name, "sakila");
  ensure_equals("Unexpected Table Name", table_name, "customer");
  ensure_equals("Unexpected Column Count", columns.size(), 2U);
  ensure("Unexpected Column Name", columns.front().first == "first_name");
  ensure("Unexpected Column Alias", columns.front().second == "First Name");
  columns.pop_front();
  ensure("Unexpected Column Name", columns.front().first == "last_name");
  ensure("Unexpected Column Alias", columns.front().second == "Last Name");
  columns.pop_front();
}

// Using numeric literals as columns will have the function failing
TEST_FUNCTION(5)
{
  ensure("failed to get sqlparser module", (NULL != sql_facade));

  std::string schema_name;
  std::string table_name;
  SqlFacade::String_tuple_list columns;
  
  std::string query = "select customer_id, 10 as 'years' from sakila.customer;";

  ensure("Unexpexted success parsing test", !sql_facade->parseSelectStatementForEdit(query, schema_name, table_name, columns));
  ensure_equals("Unexpected Schema Name", schema_name, "");
  ensure_equals("Unexpected Table Name", table_name, "");
  ensure_equals("Unexpected Column Count", columns.size(), 0U);
}

// Using text literals as columns have the function to fail
TEST_FUNCTION(6)
{
  ensure("failed to get sqlparser module", (NULL != sql_facade));
  std::string schema_name;
  std::string table_name;
  SqlFacade::String_tuple_list columns;
  
  std::string query = "select 'Dear' as Greeting, first_name, last_name from sakila.customer;";

  ensure("Unexpexted success parsing test", !sql_facade->parseSelectStatementForEdit(query, schema_name, table_name, columns));
  ensure_equals("Unexpected Schema Name", schema_name, "");
  ensure_equals("Unexpected Table Name", table_name, "");
  ensure_equals("Unexpected Column Count", columns.size(), 0U);
}

// Using SELECT *
TEST_FUNCTION(7)
{
  ensure("failed to get sqlparser module", (NULL != sql_facade));
  std::string schema_name;
  std::string table_name;
  SqlFacade::String_tuple_list columns;
  
  std::string query = "select * from `sakila`.`address`";

  ensure("Unexpected failure parsing test", sql_facade->parseSelectStatementForEdit(query, schema_name, table_name, columns));
  ensure_equals("Unexpected Schema Name", schema_name, "sakila");
  ensure_equals("Unexpected Table Name", table_name, "address");
  ensure_equals("Unexpected Column Count", columns.size(), 1U);
  ensure("Unexpected Column Name", columns.front().first == "*");
  ensure("Unexpected Column Alias", columns.front().second == "*");
  columns.pop_front();
}

// Using the WHERE clause doesn't impact the parsing as long as the information is being
// retrieved from a single table
TEST_FUNCTION(8)
{
  ensure("failed to get sqlparser module", (NULL != sql_facade));
  std::string schema_name;
  std::string table_name;
  SqlFacade::String_tuple_list columns;
  
  std::string query = "select address, phone as Phone from `sakila`.`address` where district = 'Adana'";

  ensure("Unexpexted failure parsing test", sql_facade->parseSelectStatementForEdit(query, schema_name, table_name, columns));
  ensure_equals("Unexpected Schema Name", schema_name, "sakila");
  ensure_equals("Unexpected Table Name", table_name, "address");
  ensure_equals("Unexpected Column Count", columns.size(), 2U);
  ensure("Unexpected Column Name", columns.front().first == "address");
  ensure("Unexpected Column Alias", columns.front().second == "address");
  columns.pop_front();
  ensure("Unexpected Column Name", columns.front().first == "phone");
  ensure("Unexpected Column Alias", columns.front().second == "Phone");
  columns.pop_front();
}

// Using many tables to pull the information causes failure, as expected
TEST_FUNCTION(9)
{
  ensure("failed to get sqlparser module", (NULL != sql_facade));
  std::string schema_name;
  std::string table_name;
  SqlFacade::String_tuple_list columns;
  
  std::string query = "SELECT customer.first_name, customer.last_name, address.address FROM sakila.customer, sakila.address where customer.address_id = address.address_id;";

  ensure("Unexpexted success parsing test", !sql_facade->parseSelectStatementForEdit(query, schema_name, table_name, columns));
  ensure_equals("Unexpected Schema Name", schema_name, "");
  ensure_equals("Unexpected Table Name", table_name, "");
  ensure_equals("Unexpected Column Count", columns.size(), 0U);
}

// Should fail if multiple select statements are issued
TEST_FUNCTION(10)
{
  ensure("failed to get sqlparser module", (NULL != sql_facade));
  std::string schema_name;
  std::string table_name;
  SqlFacade::String_tuple_list columns;
  
  std::string query = "SELECT * FROM sakila.customer; SELECT * FROM sakila.address;";

  ensure("Unexpexted success parsing test", !sql_facade->parseSelectStatementForEdit(query, schema_name, table_name, columns));
  ensure_equals("Unexpected Schema Name", schema_name, "");
  ensure_equals("Unexpected Table Name", table_name, "");
  ensure_equals("Unexpected Column Count", columns.size(), 0U);
}

// Should fail if multiple functions as columns are used
TEST_FUNCTION(11)
{
  ensure("failed to get sqlparser module", (NULL != sql_facade));
  std::string schema_name;
  std::string table_name;
  SqlFacade::String_tuple_list columns;
  
  std::string query = "SELECT count(*) FROM sakila.customer";

  ensure("Unexpexted success parsing test", !sql_facade->parseSelectStatementForEdit(query, schema_name, table_name, columns));
  ensure_equals("Unexpected Schema Name", schema_name, "");
  ensure_equals("Unexpected Table Name", table_name, "");
  ensure_equals("Unexpected Column Count", columns.size(), 0U);
}

END_TESTS