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
|
/*
* Copyright (c) 2012, 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 "connection_helpers.h"
#include "test_fixture.h"
#include "base/file_utilities.h"
#include "sqlide/wb_sql_editor_form.h"
#include "sqlide/autocomplete_object_name_cache.h"
using namespace grt;
using namespace wb;
BEGIN_TEST_DATA_CLASS(autocompletion_cache_test)
public:
GRT _grt;
base::RecMutex _connection_mutex;
sql::Dbc_connection_handler::Ref _conn;
AutoCompleteCache *_cache;
TEST_DATA_CONSTRUCTOR(autocompletion_cache_test)
: _conn(new sql::Dbc_connection_handler()), _cache(0)
{
// We want to print this out only once, not for every test, so we put it here
// as this is the first test that runs usually.
#ifdef _WIN32
TCHAR path[MAX_PATH];
GetCurrentDirectory(MAX_PATH, path);
printf("\nTests running in: %s\n\n", base::wstring_to_string(path).c_str());
#endif
_grt.scan_metaclasses_in("../../res/grt/");
_grt.end_loading_metaclasses();
// Because tests are executed in alphabetic order this is the first one.
// Hence we set up the sakila db in server here.
setup_sakila_db();
}
TEST_DATA_DESTRUCTOR(autocompletion_cache_test)
{
// cleanup_sakila_db();
_cache->shutdown();
delete _cache;
}
base::RecMutexLock get_connection(sql::Dbc_connection_handler::Ref &conn)
{
base::RecMutexLock lock(_connection_mutex);
conn = _conn;
return lock;
}
END_TEST_DATA_CLASS;
TEST_MODULE(autocompletion_cache_test, "autocompletion object name cache");
TEST_FUNCTION(2)
{
db_mgmt_ConnectionRef connectionProperties(&_grt);
setup_env(&_grt, connectionProperties);
sql::DriverManager *dm= sql::DriverManager::getDriverManager();
_conn->ref = dm->getConnection(connectionProperties);
}
TEST_FUNCTION(3)
{
base::remove("testconn.cache");
_cache = new AutoCompleteCache("testconn", boost::bind(&Test_object_base<autocompletion_cache_test>::get_connection, this, _1),
".", NULL);
// Right after creation the schema list is empty. Retrieval has just been set up.
std::vector<std::string> list = _cache->get_matching_schema_names("sakila");
ensure("Schema list is not empty", list.empty());
// After creation a first data retrieval starts automatically in the background.
// Wait a moment to have that finished.
g_usleep(2000000);
// Retrieve db objects in the sakila schema. So they are available when we ask for them
// in the following tests.
_cache->refresh_schema_cache_if_needed("sakila");
g_usleep(2000000);
}
static void ensure_list_equals(const char *what, const std::vector<std::string> &list, const char **comp)
{
std::vector<std::string>::const_iterator i;
int j = 0;
try
{
for (i = list.begin(); i != list.end() && comp[j] != NULL; ++i, ++j)
{
ensure_equals(what, *i, comp[j]);
}
ensure(what, comp[j] == NULL);
ensure(what, i == list.end());
}
catch (...)
{
// TODO: this should be part of the TUT message, otherwise we might not see it.
g_message("Result list:");
for (i = list.begin(); i != list.end(); ++i)
g_message(" %s", i->c_str());
g_message("Expected list:");
for (j = 0; comp[j]; j++)
g_message(" %s", comp[j]);
throw;
}
}
TEST_FUNCTION(10)
{
std::vector<std::string> list = _cache->get_matching_schema_names("");
int found = 0;
// This time the schema list should contain sakila and mysql.
for (std::vector<std::string>::const_iterator i = list.begin(); i != list.end(); ++i)
{
if (*i == "sakila" || *i == "mysql")
found++;
}
ensure_equals("known schema name matches", found, 2);
// match just sakila
list = _cache->get_matching_schema_names("sakila");
found = 0;
for (std::vector<std::string>::const_iterator i = list.begin(); i != list.end(); ++i)
{
if (*i == "sakila" || *i == "mysql")
found++;
}
ensure_equals("known schema name matches (sakila)", found, 1);
}
TEST_FUNCTION(12)
{
static const char *sakila_ac[] = {
"actor",
"actor_info",
NULL
};
static const char *sakila_actor_[] = {
"actor_info",
NULL
};
_cache->refresh_schema_cache_if_needed("sakila");
// Wait for the refresh to settle. The functions to check if a fetch is done
// are really unreliable and should be revised.
g_usleep(1000000);
std::vector<std::string> list = _cache->get_matching_table_names("sakila", "ac");
std::vector<std::string> list2 = _cache->get_matching_view_names("sakila", "ac");
std::copy(list2.begin(), list2.end(), std::back_inserter(list));
ensure_list_equals("tables sakila.ac*", list, sakila_ac);
list = _cache->get_matching_table_names("sakila", "actor_");
list2 = _cache->get_matching_view_names("sakila", "ac");
std::copy(list2.begin(), list2.end(), std::back_inserter(list));
ensure_list_equals("tables sakila.actor_*", list, sakila_actor_);
}
TEST_FUNCTION(14)
{
static const char *sakila_inv[] = {
"inventory_held_by_customer",
"inventory_in_stock",
NULL
};
std::vector<std::string> list = _cache->get_matching_function_names("sakila", "inv");
ensure_list_equals("functions sakila.inv*", list, sakila_inv);
}
TEST_FUNCTION(16)
{
static const char *sakila_fi[] = {
"film_in_stock",
"film_not_in_stock",
NULL
};
std::vector<std::string> list = _cache->get_matching_procedure_names("sakila", "fi");
ensure_list_equals("procedures sakila.fi*", list, sakila_fi);
}
TEST_FUNCTION(18)
{
// columns
static const char *sakila_a[] = {
"actor_id",
NULL
};
std::vector<std::string> list = _cache->get_matching_column_names("sakila", "actor", "a");
ensure_list_equals("columns sakila.actor.a*", list, sakila_a);
}
// Everything again reusing cache
TEST_FUNCTION(19)
{
_cache->shutdown();
delete _cache;
_cache = new AutoCompleteCache("testconn", boost::bind(&Test_object_base<autocompletion_cache_test>::get_connection, this, _1),
".", NULL);
_cache->refresh_schema_cache_if_needed("sakila");
g_usleep(2000000);
}
TEST_FUNCTION(20)
{
std::vector<std::string> list;
list = _cache->get_matching_schema_names("");
int found = 0;
// This time the schema list should contain sakila and mysql.
for (std::vector<std::string>::const_iterator i = list.begin(); i != list.end(); ++i)
{
if (*i == "sakila" || *i == "mysql")
found++;
}
ensure_equals("known schema name matches", found, 2);
// match just sakila
list = _cache->get_matching_schema_names("sakila");
found = 0;
for (std::vector<std::string>::const_iterator i = list.begin(); i != list.end(); ++i)
{
if (*i == "sakila" || *i == "mysql")
found++;
}
ensure_equals("known schema name matches (sakila*)", found, 1);
}
TEST_FUNCTION(22)
{
static const char *sakila_ac[] = {
"actor",
"actor_info",
NULL
};
static const char *sakila_actor_[] = {
"actor_info",
NULL
};
std::vector<std::string> list = _cache->get_matching_table_names("sakila", "ac");
std::vector<std::string> list2 = _cache->get_matching_view_names("sakila", "ac");
std::copy(list2.begin(), list2.end(), std::back_inserter(list));
ensure_list_equals("tables sakila.ac*", list, sakila_ac);
list = _cache->get_matching_table_names("sakila", "actor_");
list2 = _cache->get_matching_view_names("sakila", "actor_");
std::copy(list2.begin(), list2.end(), std::back_inserter(list));
ensure_list_equals("tables sakila.actor_*", list, sakila_actor_);
}
TEST_FUNCTION(24)
{
static const char *sakila_inv[] = {
"inventory_held_by_customer",
"inventory_in_stock",
NULL
};
std::vector<std::string> list = _cache->get_matching_function_names("sakila", "inv");
ensure_list_equals("functions sakila.inv*", list, sakila_inv);
}
TEST_FUNCTION(26)
{
static const char *sakila_fi[] = {
"film_in_stock",
"film_not_in_stock",
NULL
};
std::vector<std::string> list = _cache->get_matching_procedure_names("sakila", "fi");
ensure_list_equals("procedures sakila.fi*", list, sakila_fi);
}
TEST_FUNCTION(28)
{
static const char *sakila_a[] = {
"actor_id",
NULL
};
std::vector<std::string> list = _cache->get_matching_column_names("sakila", "actor", "a");
ensure_list_equals("columns sakila.actor.a*", list, sakila_a);
}
END_TESTS
|