-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathtestbase.cpp
213 lines (181 loc) · 6.18 KB
/
testbase.cpp
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
#include "testbase.h"
#include "testschema.h"
#include "SqlExceptions.h"
#include "SqlQueryCache.h"
#include "SqlGlobal.h"
#include "SqlCreateTable.h"
#include "Sql.h"
#include <QtTest/QTest>
#include <QFileInfo>
#include <QMetaObject>
#include <QString>
#include <QSqlDatabase>
#include <QSqlError>
#include <QSqlQuery>
#include <QDir>
QString TestBase::databaseName() const
{
const QString dbName = QLatin1String( qgetenv("SQLATE_DATABASE_NAME").constData() );
if (dbName.isEmpty()) {
return QLatin1String("sqlate_test");
}
return dbName.toLower();
}
void TestBase::openDbTest()
{
QString dbName = databaseName();
QSqlDatabase db = QSqlDatabase::addDatabase(QLatin1String( "QPSQL" ));
db.setDatabaseName( dbName );
// Allow override by env settings
// No reason to restrict as only setting where the app connects to
QString dbHost = QLatin1String( qgetenv("SQLATE_DATABASE_HOST").constData() );
if (dbHost.isEmpty()) {
dbHost = QLatin1String("127.0.0.1");
}
QString dbUser = QLatin1String( qgetenv("SQLATE_DATABASE_USER").constData() );
if (dbUser.isEmpty()) {
dbUser = QLatin1String( "sqlate.user" );
}
QString dbPassword = QLatin1String( qgetenv("SQLATE_DATABASE_PASSWORD").constData() );
if (dbPassword.isEmpty()) {
dbPassword = QLatin1String( "sql123456" );
}
int dbPort = getDbPort();
qDebug() << "Using database: " << dbName << " on host " << dbHost << " port: " << dbPort;
db.setHostName( dbHost );
db.setPort( dbPort );
db.setUserName( dbUser );
db.setPassword( dbPassword );
if ( !db.open() ) {
//try to create the database
db.close();
QSqlDatabase db2 = QSqlDatabase::database( );
db2.setDatabaseName( QLatin1String("template1") );
db2.setHostName(dbHost);
db2.setPort(dbPort);
db2.setUserName(dbUser);
db2.setPassword(dbPassword);
if ( !db2.open() ) {
qFatal("Failed to connect: %s", db2.lastError().text().toLatin1().constData());
}
QString s = QString( QLatin1String("CREATE DATABASE %1 ENCODING 'UTF8' LC_COLLATE='en_US.UTF-8' LC_CTYPE='en_US.UTF-8' TEMPLATE template0") ).arg( databaseName() );
QSqlQuery query( db2 );
if ( !query.exec(s) ) {
qFatal("Failed to create the %s database: %s", databaseName().toLatin1().constData(), query.lastError().text().toLatin1().constData());
}
db2.close();
//reconnect again
db.close();
db = QSqlDatabase::database( );
db.setDatabaseName(databaseName());
db.setHostName(dbHost);
db.setPort(dbPort);
db.setUserName(dbUser);
db.setPassword(dbPassword);
if ( !db.open() ) {
qFatal("Failed to connect: %s", db.lastError().text().toLatin1().constData());
}
}
qDebug() << db;
/* else {
qFatal("Failed to connect: %s", db.lastError().text().toLatin1().constData());
}*/
}
bool TestBase::executePreCreateScripts(const QString &path)
{
const QString proceduresFilesPath = QString::fromLatin1("%1/procedures/pre-create").arg(path);
QFileInfoList preCreateFiles;
// Make list of sql files to process
if ( path.length() ) {
QDir preCreateFilesDir(proceduresFilesPath, QLatin1String( "*.tsp" ));
preCreateFilesDir.setSorting( QDir::Name );
preCreateFiles.append(preCreateFilesDir.entryInfoList(QDir::Files));
} else {
message( tr( "Cannot find static data and procedures in %1").arg(path));
return false;
}
QFileInfoList::const_iterator sqlfileslist_it;
for (sqlfileslist_it=preCreateFiles.constBegin(); sqlfileslist_it!=preCreateFiles.constEnd(); ++sqlfileslist_it) {
bool result = processSqlFile( *sqlfileslist_it );
if (!result) {
return false;
}
}
return true;
}
bool TestBase::processSqlFile(const QFileInfo ¤tFileInfo)
{
message(tr( " Processing file: %1").arg(currentFileInfo.absoluteFilePath()));
QFile file( currentFileInfo.absoluteFilePath() );
if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
message( tr( " could not open file: %1").arg(currentFileInfo.absoluteFilePath()));
return false;
}
else {
message( tr( "... OK"));
}
QTextStream in(&file);
QString sql = in.readAll();
SqlUtils::stripComments(sql);
if ( sql.isEmpty() )
return true;
SqlQuery query;
query.exec( sql );
return true;
}
void TestBase::createEmptyDb()
{
try {
SqlQuery q;
q.exec(QLatin1String("SET client_min_messages TO WARNING;"));
} catch ( const SqlException &e ) {
qDebug() << tr("Log messages cannot be turned off: %1").arg( e.error().text() );
}
dropTables();
executePreCreateScripts(QLatin1String(":/sqlate"));
// create tables
try {
Sql::createMissingTables<Sql::SQLateTestSchema>();
} catch ( const SqlException &e ) {
message( tr("Error while creating the tables: %1").arg(e.error().text()) );
return;
}
try {
Sql::createRulesPermissionsAndTriggers<Sql::SQLateTestSchema>();
} catch ( const SqlException &e ) {
message( tr("Error while creating the table rules, triggers and setting the permissions: %1").arg(e.error().text()) );
return;
}
}
bool TestBase::dropTables()
{
message(tr("Dropping all tables..."));
foreach ( const QString &table, QSqlDatabase::database().tables() ) {
try {
SqlQuery q;
q.exec(QString::fromLatin1( "DROP TABLE IF EXISTS %1 CASCADE" ).arg( table ));
} catch ( const SqlException &e ) {
message( tr("Dropping table %1 failed: %2").arg( table ).arg( e.error().text() ) );
return false;
}
}
message(tr("All tables dropped."));
return true;
}
void TestBase::message(const QString& str)
{
SQLDEBUG << str;
}
void TestBase::cleanupTestCase()
{
SqlQueryCache::clear();
}
int TestBase::getDbPort()
{
int dbPort = QString::fromLatin1( qgetenv("SQL_DATABASE_HOST_PORT").constData() ).toInt();
if (dbPort <= 0 ) {
dbPort = SQLATE_DEFAULT_SERVER_PORT;
}
return dbPort;
}
#include "moc_testbase.cpp"