source: trunk/src/tests/core/sqltests/edbsettingstest.cpp @ 368

Last change on this file since 368 was 368, checked in by Joachim Langenbach, 5 years ago
  • Fixed and tested Database and DB User creation by EDBSettings widget
  • Added CMAKE Variables, to allow the specification of Database Test server and admin user with password
File size: 20.5 KB
Line 
1/*
2 *
3 *     Copyright (C) 2015 - 2016 EngSaS - Engineering Solutions and Services Langenbach. All rights reserved.
4 *
5 *     This library is free software; you can redistribute it and/or
6 *     modify it under the terms of the GNU Lesser General Public
7 *     License as published by the Free Software Foundation; either
8 *     version 2.1 of the License, or (at your option) any later version.
9 *
10 *     This library is distributed in the hope that it will be useful,
11 *     but WITHOUT ANY WARRANTY; without even the implied warranty of
12 *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13 *     Lesser General Public License for more details.
14 *
15 *     You should have received a copy of the GNU Lesser General Public
16 *     License along with this library; if not, write to the Free Software
17 *     Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
18 *     MA 02110-1301  USA.
19 *
20 */
21
22#include "edbsettingstest.h"
23#include "moc_edbsettingstest.cpp"
24
25#include "dbtestsettings.h"
26
27#include <edbsettings.h>
28#include <edbsettingswidget.h>
29#include <esetupwizarddb.h>
30#include <esqldatabase.h>
31#include <eclosemodalwidgetthread.h>
32
33#include <QTest>
34#include <QTcpSocket>
35#include <QSettings>
36#include <QComboBox>
37#include <QCheckBox>
38#include <QGroupBox>
39#include <QSqlQuery>
40#include <QSqlError>
41#include <QSignalSpy>
42
43void EDBSettingsTest::initTestCase()
44{
45        if(!QSqlDatabase::drivers().contains("QSQLITE"))
46#if QT_VERSION >= 0x050000
47        QSKIP("This test requires the SQLITE database driver");
48#else
49        QSKIP("This test requires the SQLITE database driver", SkipAll);
50#endif
51        if(!QSqlDatabase::drivers().contains("QMYSQL"))
52#if QT_VERSION >= 0x050000
53        QSKIP("This test requires the MYSQL database driver");
54#else
55        QSKIP("This test requires the MYSQL database driver", SkipAll);
56#endif
57         
58        QApplication::setOrganizationName("EngSaS");
59        QSettings settings;
60        settings.clear();
61        QCOMPARE(settings.allKeys().size(), 0);
62
63        databasePrefix = "libengsastests";
64        username = "libengsastest";
65        password = "libengsastest";
66        host = QString(DATABASE_TEST_HOST);
67       
68        ESqlDatabase db = ESqlDatabase::addDatabase("QMYSQL", "testInitTestCaseConnection");
69        db.setHostName(host);
70        db.setPort(3306);
71       
72        usernameAdmin = QString(DATABASE_TEST_ADMIN_USER);
73        passwordAdmin = QString(DATABASE_TEST_ADMIN_PASSWORD);
74        adminDatabaeConnectionName = "testAdminConnection";
75        ESqlDatabase adminDatabase = ESqlDatabase::addDatabase("QMYSQL", adminDatabaeConnectionName);
76        adminDatabase.setHostName(host);
77        adminDatabase.setUserName(usernameAdmin);
78        adminDatabase.setPassword(passwordAdmin);
79       
80        // test if server is reachable, because QSqlDatabase has a huge timeout
81        // we use 2s as timeout here
82        // Somehow, it doesn't work on windows
83        QTcpSocket *socket = new QTcpSocket(this);
84        socket->connectToHost(db.hostName(),db.port(),QIODevice::ReadOnly); 
85        if(!socket->waitForConnected(2000)){
86#if QT_VERSION >= 0x050000
87        QSKIP("This test requires a database connection. Please check DATABAE_TEST_* VARIABLES in CMake.");
88#else
89        QSKIP("This test requires a database connection. Please check DATABAE_TEST_* VARIABLES in CMake.", SkipAll);
90#endif
91        }
92        socket->close();
93}
94
95void EDBSettingsTest::checkDialog()
96{
97        EDBSettings *eDBSettings = new EDBSettings();
98        eDBSettings->show();
99
100        // test with empty drivers list
101        QStringList drivers = ESqlDatabase::drivers();
102        QComboBox *typeEdit = eDBSettings->findChild<QComboBox*>("typeEdit");
103        QVERIFY(typeEdit);
104        QCOMPARE(typeEdit->count(), drivers.count());
105        for(int i = 0; i < typeEdit->count(); i++){
106#if QT_VERSION >= 0x050000
107        QCOMPARE(drivers.contains(typeEdit->itemText(i)), true);
108#else
109        QCOMPARE(drivers.contains(typeEdit->itemText(i)), QBool(true));
110#endif
111        }
112
113        QWidget *eDBSettingsWidget = eDBSettings->findChild<QWidget*>("eDBSettingsWidget");
114        QVERIFY(eDBSettingsWidget);
115        QCOMPARE(eDBSettingsWidget->isVisible(), true);
116        QWidget *createDatabaseBox = eDBSettings->findChild<QWidget*>("createDatabaseBox");
117        QVERIFY(createDatabaseBox);
118        QCOMPARE(createDatabaseBox->isVisible(), false);
119
120        delete eDBSettings;
121       
122        drivers << "SPECIALDRIVERONLY";
123        ESetupWizardDB setupWizard("Test", NULL, drivers);
124        QList<ESetupWizardWidget*> widgets = setupWizard.wizardWidgets();
125        QCOMPARE(widgets.size(), 2);
126        eDBSettings = qobject_cast<EDBSettings*>(widgets[0]);
127        QVERIFY(eDBSettings);
128        eDBSettings->show();
129       
130        typeEdit = eDBSettings->findChild<QComboBox*>("typeEdit");
131        QVERIFY(typeEdit);
132        QCOMPARE(typeEdit->count(), drivers.count());
133        for(int i = 0; i < typeEdit->count(); i++){
134#if QT_VERSION >= 0x050000
135                QCOMPARE(drivers.contains(typeEdit->itemText(i)), true);
136#else
137                QCOMPARE(drivers.contains(typeEdit->itemText(i)), QBool(true));
138#endif
139        }
140
141        eDBSettingsWidget = eDBSettings->findChild<QWidget*>("eDBSettingsWidget");
142        QVERIFY(eDBSettingsWidget);
143        QCOMPARE(eDBSettingsWidget->isVisible(), true);
144        createDatabaseBox = eDBSettings->findChild<QWidget*>("createDatabaseBox");
145        QVERIFY(createDatabaseBox);
146        QCOMPARE(createDatabaseBox->isVisible(), true);
147}
148
149void EDBSettingsTest::connectToExistingDatabase()
150{
151        QString database = databasePrefix +"_"+ QDateTime::currentDateTime().toString("yyyyMMddhhmmsszzz");
152       
153        ESetupWizardDB setupWizard("Test", NULL);
154        QList<ESetupWizardWidget*> widgets = setupWizard.wizardWidgets();
155        QCOMPARE(widgets.size(), 2);
156        EDBSettings *eDBSettings = qobject_cast<EDBSettings*>(widgets[0]);
157        QVERIFY(eDBSettings);
158        eDBSettings->show();
159       
160        QSignalSpy errorSpy(eDBSettings, SIGNAL(error(QString)));
161       
162        // select database driver
163        QString driver = "QMYSQL";
164        selectDriver(eDBSettings, driver);
165
166        QWidget *hostEdit = eDBSettings->findChild<QWidget*>("hostEdit");
167        QVERIFY(hostEdit);
168        QTest::keyClicks(hostEdit, host);
169        QTest::keyClick(hostEdit, Qt::Key_Return);
170
171        QWidget *databaseEdit = eDBSettings->findChild<QWidget*>("databaseEdit");
172        QVERIFY(databaseEdit);
173        QTest::keyClicks(databaseEdit, database);
174        QTest::keyClick(databaseEdit, Qt::Key_Return);
175
176        QCheckBox *createDBEdit = eDBSettings->findChild<QCheckBox*>("createDBEdit");
177        QVERIFY(createDBEdit);
178        QCOMPARE(createDBEdit->isChecked(), false);
179
180        QGroupBox *createUserBox = eDBSettings->findChild<QGroupBox*>("createUserBox");
181        QVERIFY(createUserBox);
182        QCOMPARE(createUserBox->isChecked(), false);
183       
184
185        setupDatabase(database, username, password);
186
187        ECloseModalWidgetThread thread;
188        ECloseModalWidgetEvents events;
189        // the Qt::Key does not matter here, with the press of the selected key, the string is
190        // send as key content.
191        events << ECloseModalWidgetEvent("usernameEdit", new QKeyEvent(QEvent::KeyPress, Qt::Key_No, Qt::NoModifier, username));
192        events << ECloseModalWidgetEvent("usernameEdit", new QKeyEvent(QEvent::KeyPress, Qt::Key_Tab, Qt::NoModifier, ""));
193        events << ECloseModalWidgetEvent("passwordEdit", new QKeyEvent(QEvent::KeyPress, Qt::Key_No, Qt::NoModifier, password));
194        events << ECloseModalWidgetEvent("loginButton", new QKeyEvent(QEvent::KeyPress, Qt::Key_Return, Qt::NoModifier));
195        thread.setEvents(events);
196        thread.start();
197        if(!thread.waitForStart()){
198#if QT_VERSION >= 0x050000
199                QSKIP("Could not start thread");
200#else
201                QSKIP("Could not start thread", SkipSingle);
202#endif
203        }
204        bool eDBSettingsFinish = eDBSettings->finish();
205        thread.stopThread();
206        printErrors(&errorSpy);
207        QCOMPARE(eDBSettingsFinish, true);
208        QCOMPARE(errorSpy.count(), 0);
209        QCOMPARE(EDBSettingsWidget::host(), host);
210        QCOMPARE(EDBSettingsWidget::database(), database);
211        QCOMPARE(EDBSettingsWidget::driver(), driver);
212       
213        ESqlDatabase createdDatabaseConnection = ESqlDatabase::database();
214        QCOMPARE(createdDatabaseConnection.open(), true);
215        createdDatabaseConnection.close();
216        QCOMPARE(createdDatabaseConnection.isOpen(), false);
217
218        // cleanup
219        cleanupDatabase(database, username);
220
221        resetSettings();
222
223        // pass a little bit of time, to get a different database name in next test
224        QTest::qWait(100);
225}
226
227void EDBSettingsTest::createDatabaseWithUnprivilegedUser()
228{
229        QEXPECT_FAIL("", "Currently creating databases with unprivileged users is not supported!", Abort);
230        QVERIFY(0 == 1);
231
232}
233
234void EDBSettingsTest::createDatabaseWithPrivilegedUser()
235{
236        QString database = databasePrefix +"_admin_"+ QDateTime::currentDateTime().toString("yyyyMMddhhmmsszzz");
237       
238        ESetupWizardDB setupWizard("Test", NULL);
239        QList<ESetupWizardWidget*> widgets = setupWizard.wizardWidgets();
240        QCOMPARE(widgets.size(), 2);
241        EDBSettings *eDBSettings = qobject_cast<EDBSettings*>(widgets[0]);
242        QVERIFY(eDBSettings);
243        eDBSettings->show();
244       
245        QSignalSpy errorSpy(eDBSettings, SIGNAL(error(QString)));
246       
247        // select database driver
248        QString driver = "QMYSQL";
249        selectDriver(eDBSettings, driver);
250
251        QWidget *hostEdit = eDBSettings->findChild<QWidget*>("hostEdit");
252        QVERIFY(hostEdit);
253        QTest::keyClicks(hostEdit, host);
254        QTest::keyClick(hostEdit, Qt::Key_Return);
255
256        QWidget *databaseEdit = eDBSettings->findChild<QWidget*>("databaseEdit");
257        QVERIFY(databaseEdit);
258        QTest::keyClicks(databaseEdit, database);
259        QTest::keyClick(databaseEdit, Qt::Key_Return);
260
261        QCheckBox *createDBEdit = eDBSettings->findChild<QCheckBox*>("createDBEdit");
262        QVERIFY(createDBEdit);
263        createDBEdit->setChecked(true);
264        QCOMPARE(createDBEdit->isChecked(), true);
265
266        QGroupBox *createUserBox = eDBSettings->findChild<QGroupBox*>("createUserBox");
267        QVERIFY(createUserBox);
268        QCOMPARE(createUserBox->isChecked(), false);
269       
270        setupDatabase(database, username, password, true);
271       
272        // thread for privileged user login
273        ECloseModalWidgetThread thread1(5000);
274        ECloseModalWidgetEvents events;
275        // the Qt::Key does not matter here, with the press of the selected key, the string is
276        // send as key content.
277        events << ECloseModalWidgetEvent("usernameEdit", new QKeyEvent(QEvent::KeyPress, Qt::Key_No, Qt::NoModifier, usernameAdmin));
278        events << ECloseModalWidgetEvent("usernameEdit", new QKeyEvent(QEvent::KeyPress, Qt::Key_Tab, Qt::NoModifier, ""));
279        events << ECloseModalWidgetEvent("passwordEdit", new QKeyEvent(QEvent::KeyPress, Qt::Key_No, Qt::NoModifier, passwordAdmin));
280        events << ECloseModalWidgetEvent("loginButton", new QKeyEvent(QEvent::KeyPress, Qt::Key_Return, Qt::NoModifier));
281        thread1.setEvents(events);
282        thread1.start();
283        if(!thread1.waitForStart()){
284#if QT_VERSION >= 0x050000
285                QSKIP("Could not start thread1");
286#else
287                QSKIP("Could not start thread1", SkipSingle);
288#endif
289        }
290
291        // thread for unprivileged user login
292        ECloseModalWidgetThread thread2(10000);
293        events.clear();
294        // the Qt::Key does not matter here, with the press of the selected key, the string is
295        // send as key content.
296        events << ECloseModalWidgetEvent("usernameEdit", new QKeyEvent(QEvent::KeyPress, Qt::Key_No, Qt::NoModifier, username));
297        events << ECloseModalWidgetEvent("usernameEdit", new QKeyEvent(QEvent::KeyPress, Qt::Key_Tab, Qt::NoModifier, ""));
298        events << ECloseModalWidgetEvent("passwordEdit", new QKeyEvent(QEvent::KeyPress, Qt::Key_No, Qt::NoModifier, password));
299        events << ECloseModalWidgetEvent("loginButton", new QKeyEvent(QEvent::KeyPress, Qt::Key_Return, Qt::NoModifier));
300        thread2.setEvents(events);
301        thread2.start();
302        if(!thread2.waitForStart()){
303#if QT_VERSION >= 0x050000
304                QSKIP("Could not start thread2");
305#else
306                QSKIP("Could not start thread2", SkipSingle);
307#endif
308        }
309
310        bool eDBSettingsFinish = eDBSettings->finish();
311        thread1.stopThread();
312        thread2.stopThread();
313        printErrors(&errorSpy);
314        QCOMPARE(eDBSettingsFinish, true);
315        QCOMPARE(errorSpy.count(), 0);
316        QCOMPARE(EDBSettingsWidget::host(), host);
317        QCOMPARE(EDBSettingsWidget::database(), database);
318        QCOMPARE(EDBSettingsWidget::driver(), driver);
319
320        ESqlDatabase createdDatabaseConnection = ESqlDatabase::database();
321        QCOMPARE(createdDatabaseConnection.databaseName(), database);
322        QCOMPARE(createdDatabaseConnection.open(), true);
323        createdDatabaseConnection.close();
324        QCOMPARE(createdDatabaseConnection.isOpen(), false);
325
326        cleanupDatabase(database, username);
327       
328        resetSettings();
329}
330
331void EDBSettingsTest::createDatabaseAndUserWithPrivilegedUser()
332{
333        QString database = databasePrefix +"_adminanduser_"+ QDateTime::currentDateTime().toString("yyyyMMddhhmmsszzz");
334       
335        ESetupWizardDB setupWizard("Test", NULL);
336        QList<ESetupWizardWidget*> widgets = setupWizard.wizardWidgets();
337        QCOMPARE(widgets.size(), 2);
338        EDBSettings *eDBSettings = qobject_cast<EDBSettings*>(widgets[0]);
339        QVERIFY(eDBSettings);
340        eDBSettings->show();
341       
342        QSignalSpy errorSpy(eDBSettings, SIGNAL(error(QString)));
343       
344        // select database driver
345        QString driver = "QMYSQL";
346        selectDriver(eDBSettings, driver);
347
348        QWidget *hostEdit = eDBSettings->findChild<QWidget*>("hostEdit");
349        QVERIFY(hostEdit);
350        QTest::keyClicks(hostEdit, host);
351        QTest::keyClick(hostEdit, Qt::Key_Return);
352
353        QWidget *databaseEdit = eDBSettings->findChild<QWidget*>("databaseEdit");
354        QVERIFY(databaseEdit);
355        QTest::keyClicks(databaseEdit, database);
356        QTest::keyClick(databaseEdit, Qt::Key_Return);
357
358        QCheckBox *createDBEdit = eDBSettings->findChild<QCheckBox*>("createDBEdit");
359        QVERIFY(createDBEdit);
360        createDBEdit->setChecked(true);
361        QCOMPARE(createDBEdit->isChecked(), true);
362
363        QGroupBox *createUserBox = eDBSettings->findChild<QGroupBox*>("createUserBox");
364        QVERIFY(createUserBox);
365        createUserBox->setChecked(true);
366        QCOMPARE(createUserBox->isChecked(), true);
367       
368        QWidget *createUserNameEdit = eDBSettings->findChild<QWidget*>("createUserNameEdit");
369        QVERIFY(createUserNameEdit);
370        QTest::keyClicks(createUserNameEdit, username);
371        QTest::keyClick(createUserNameEdit, Qt::Key_Return);
372       
373        QWidget *createUserPasswordEdit = eDBSettings->findChild<QWidget*>("createUserPasswordEdit");
374        QVERIFY(createUserPasswordEdit);
375        QTest::keyClicks(createUserPasswordEdit, password);
376        QTest::keyClick(createUserPasswordEdit, Qt::Key_Return);
377       
378        // thread for privileged user login
379        ECloseModalWidgetThread thread1(5000);
380        ECloseModalWidgetEvents events;
381        // the Qt::Key does not matter here, with the press of the selected key, the string is
382        // send as key content.
383        events << ECloseModalWidgetEvent("usernameEdit", new QKeyEvent(QEvent::KeyPress, Qt::Key_No, Qt::NoModifier, usernameAdmin));
384        events << ECloseModalWidgetEvent("usernameEdit", new QKeyEvent(QEvent::KeyPress, Qt::Key_Tab, Qt::NoModifier, ""));
385        events << ECloseModalWidgetEvent("passwordEdit", new QKeyEvent(QEvent::KeyPress, Qt::Key_No, Qt::NoModifier, passwordAdmin));
386        events << ECloseModalWidgetEvent("loginButton", new QKeyEvent(QEvent::KeyPress, Qt::Key_Return, Qt::NoModifier));
387        thread1.setEvents(events);
388        thread1.start();
389        if(!thread1.waitForStart()){
390#if QT_VERSION >= 0x050000
391                QSKIP("Could not start thread1");
392#else
393                QSKIP("Could not start thread1", SkipSingle);
394#endif
395        }
396
397        // thread for unprivileged user login
398        ECloseModalWidgetThread thread2(10000);
399        events.clear();
400        // the Qt::Key does not matter here, with the press of the selected key, the string is
401        // send as key content.
402        events << ECloseModalWidgetEvent("usernameEdit", new QKeyEvent(QEvent::KeyPress, Qt::Key_No, Qt::NoModifier, username));
403        events << ECloseModalWidgetEvent("usernameEdit", new QKeyEvent(QEvent::KeyPress, Qt::Key_Tab, Qt::NoModifier, ""));
404        events << ECloseModalWidgetEvent("passwordEdit", new QKeyEvent(QEvent::KeyPress, Qt::Key_No, Qt::NoModifier, password));
405        events << ECloseModalWidgetEvent("loginButton", new QKeyEvent(QEvent::KeyPress, Qt::Key_Return, Qt::NoModifier));
406        thread2.setEvents(events);
407        thread2.start();
408        if(!thread2.waitForStart()){
409#if QT_VERSION >= 0x050000
410                QSKIP("Could not start thread2");
411#else
412                QSKIP("Could not start thread2", SkipSingle);
413#endif
414        }
415
416        bool eDBSettingsFinish = eDBSettings->finish();
417        thread1.stopThread();
418        thread2.stopThread();
419        printErrors(&errorSpy);
420        QCOMPARE(eDBSettingsFinish, true);
421        QCOMPARE(errorSpy.count(), 0);
422        QCOMPARE(EDBSettingsWidget::host(), host);
423        QCOMPARE(EDBSettingsWidget::database(), database);
424        QCOMPARE(EDBSettingsWidget::driver(), driver);
425
426        ESqlDatabase createdDatabaseConnection = ESqlDatabase::database();
427        QCOMPARE(createdDatabaseConnection.databaseName(), database);
428        QCOMPARE(createdDatabaseConnection.open(), true);
429        createdDatabaseConnection.close();
430        QCOMPARE(createdDatabaseConnection.isOpen(), false);
431
432        // cleanup
433        cleanupDatabase(database, username);
434       
435        resetSettings();
436}
437
438void EDBSettingsTest::selectDriver(EDBSettings *eDBSettings, const QString &driver)
439{
440        QVERIFY(eDBSettings);
441        QCOMPARE(driver.isEmpty(), false);
442
443        QComboBox *typeEdit = eDBSettings->findChild<QComboBox*>("typeEdit");
444        QVERIFY(typeEdit);
445        QVERIFY(typeEdit->findText(driver) > -1);
446        for(int i = 0; i < typeEdit->count(); i++){
447                if(typeEdit->currentText() == driver)
448                        break;
449                QTest::keyClick(typeEdit, Qt::Key_Down);
450                QTest::keyClick(typeEdit, Qt::Key_Return);
451        }
452        QCOMPARE(typeEdit->currentText(), driver);
453}
454
455void EDBSettingsTest::printErrors(const QSignalSpy* spy)
456{
457        QVERIFY(spy);
458        for(int i = 0; i < spy->count(); i++){
459                QVariantList data = spy->at(i);
460                QCOMPARE(data.size(), 1);
461                QTest::qWarn(data[0].toString().toUtf8());
462        }
463       
464        if(spy->count() > 0){
465                QTest::qWarn(QString("\n  Database Settings:\n"
466                                                                                                "    Host:     %1\n"
467                                                                                                "    Driver:   %2\n"
468                                                                                                "    Database: %3\n")
469                                                                                                .arg(EDBSettingsWidget::host())
470                                                                                                .arg(EDBSettingsWidget::driver())
471                                                                                                .arg(EDBSettingsWidget::database()).toUtf8());
472        }
473}
474
475void EDBSettingsTest::resetSettings()
476{
477        // cleanup settings
478        QSettings settings;
479        settings.clear();
480        QCOMPARE(settings.allKeys().size(), 0);
481}
482
483void EDBSettingsTest::setupDatabase(QString createDatabase, QString createUser, QString userPassword, bool manageUserOnly)
484{
485        ESqlDatabase adminDatabase = ESqlDatabase::database(adminDatabaeConnectionName);
486
487        QVERIFY(adminDatabase.open());
488        QSqlQuery query(adminDatabase);
489        QVERIFY(adminDatabase.open());
490        // create database
491        if(!createDatabase.isEmpty() && !manageUserOnly){
492                bool result = query.exec("CREATE DATABASE "+ createDatabase);
493                if(!result){
494                        adminDatabase.close();
495                        QString dbError;
496                        if(query.lastError().isValid())
497                                dbError = query.lastError().text();
498                        QTest::qWarn("Could not create new database: "+ dbError.toUtf8());
499                        QVERIFY(result);
500                }
501        }
502        // create new user
503        if(!createUser.isEmpty() && !userPassword.isEmpty()){
504                query.prepare("CREATE USER ?@? IDENTIFIED BY ?");
505                query.addBindValue(createUser);
506                query.addBindValue(host);
507                query.addBindValue(userPassword);
508                if(!query.exec()){
509                        adminDatabase.close();
510                        QString dbError;
511                        if(query.lastError().isValid())
512                                dbError = query.lastError().text();
513                        QTest::qWarn("Could not create user: "+ dbError.toUtf8());
514                        QTest::qWarn("Query: "+ query.lastQuery().toUtf8());
515                        QVERIFY(false);
516                }
517                // grant privileges
518                query.prepare("GRANT ALL PRIVILEGES ON "+ createDatabase +".* TO ?@?");
519                query.addBindValue(username);
520                query.addBindValue(host);
521                if(!query.exec()){
522                        adminDatabase.close();
523                        QString dbError;
524                        if(query.lastError().isValid())
525                                dbError = query.lastError().text();
526                        QTest::qWarn("Could not grant privileges to chosen database: "+ dbError.toUtf8());
527                        QVERIFY(false);
528                }
529               
530                if(!query.exec("FLUSH PRIVILEGES")){
531                        QString dbError;
532                        if(query.lastError().isValid())
533                                dbError = query.lastError().text();
534                        QTest::qWarn("Could not reload privileges: "+ dbError.toUtf8());
535                        QVERIFY(false);
536                }
537        }
538
539        adminDatabase.close();
540}
541
542void EDBSettingsTest::cleanupDatabase(QString dropDatabase, QString dropUser)
543{
544        // cleanup
545        ESqlDatabase adminDatabase = ESqlDatabase::database(adminDatabaeConnectionName);
546        QVERIFY(adminDatabase.open());
547        QSqlQuery query(adminDatabase);
548        QVERIFY(adminDatabase.open());
549       
550        // drop databae
551        if(!dropDatabase.isEmpty()){
552                if(!query.exec("DROP DATABASE "+ dropDatabase)){
553                        adminDatabase.close();
554                        QString dbError;
555                        if(query.lastError().isValid())
556                                dbError = query.lastError().text();
557                        QTest::qWarn("Could not drop database "+ dropDatabase.toUtf8() +": "+ dbError.toUtf8());
558                        QVERIFY(false);
559                }
560        }
561       
562        // Drop user
563        if(!dropUser.isEmpty()){
564                // drop user with all of it's hosts
565                QSqlQuery queryNotReuseable(adminDatabase);
566                queryNotReuseable.prepare("SELECT User, Host FROM mysql.user WHERE User=:User");
567                queryNotReuseable.bindValue(":User", dropUser);
568                if(!queryNotReuseable.exec()){
569                        adminDatabase.close();
570                        QString dbError;
571                        if(queryNotReuseable.lastError().isValid())
572                                dbError = queryNotReuseable.lastError().text();
573                        QTest::qWarn("Could not find user hosts for "+ dropUser.toUtf8() +": "+ dbError.toUtf8());
574                        QVERIFY(false);
575                }
576                while(queryNotReuseable.next()){
577                        query.prepare("DROP USER ?@?");
578                        query.addBindValue(dropUser);
579                        query.addBindValue(queryNotReuseable.value(1).toString());
580                        if(!query.exec()){
581                                adminDatabase.close();
582                                QString dbError;
583                                if(query.lastError().isValid())
584                                        dbError = query.lastError().text();
585                                QTest::qWarn("Could not drop user "+ dropUser.toUtf8() +": "+ dbError.toUtf8());
586                                QVERIFY(false);
587                        }
588                }
589
590                if(!query.exec("FLUSH PRIVILEGES")){
591                        QString dbError;
592                        if(query.lastError().isValid())
593                                dbError = query.lastError().text();
594                        QTest::qWarn("Could not reload privileges: "+ dbError.toUtf8());
595                        QVERIFY(false);
596                }
597        }
598       
599        adminDatabase.close();
600}
601
602QTEST_MAIN(EDBSettingsTest)
Note: See TracBrowser for help on using the repository browser.