-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathschemaupdatetest.cpp
127 lines (102 loc) · 3.88 KB
/
schemaupdatetest.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
#include <QtTest/QtTest> //Windows needs it as the first include
#include "SchemaUpdater.h" //must be the first include to avoid ambigous operator overload warnings
#include "testschema.h"
#include "testbase.h"
#include "OldSchema.h"
namespace OldSql {
DEFINE_SCHEMA( OLDSCHEMA )
}
#include "NewSchema.h"
namespace NewSql {
DEFINE_SCHEMA( NEWSCHEMA )
}
#include "SqlSchema.h"
#include "SqlQuery.h"
#include "SqlSelect.h"
#include "SqlInsertQueryBuilder.h"
#include "SqlUtils.h"
#include "SqlExceptions.h"
#include <QObject>
#include <QSqlDatabase>
#include <QSqlDriver>
#include <QString>
using namespace Sql;
class SchemaUpdateTest : public TestBase
{
Q_OBJECT
private:
int currentVersion()
{
SqlQuery q = select( Version.version ).from( Version );
q.exec();
q.next();
return q.value(0).toInt();
}
bool containsColumn( const QVector<QPair<QString, QVariant::Type> > & cols, const char* name, QVariant::Type type )
{
typedef QPair<QString, QVariant::Type> StringTypePair;
foreach ( const StringTypePair &c, cols ) {
if ( c.first == QLatin1String(name) && c.second == type )
return true;
}
return false;
}
private slots:
void initTestCase()
{
openDbTest();
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(":/sql"));
try {
foreach (const QString &stmt, Sql::createTableStatements<OldSql::OldSchema>()) {
SqlQuery q;
q.exec( stmt );
}
} catch ( const SqlException& e ) {
qDebug() << "SQL Error on table creation: " << e.error().text();
exit(1);
}
SqlInsertQueryBuilder qb;
qb.setTable( OldSql::Version );
qb.addColumnValue( OldSql::Version.version, 1 );
qb.exec();
try {
SqlQuery q;
q.exec(QLatin1String("SET client_min_messages TO NOTICE;"));
} catch ( const SqlException &e ) {
qDebug() << tr("Log messages cannot be turned off: %1").arg( e.error().text() );
}
}
void testSchemaUpdate()
{
QCOMPARE( currentVersion(), 1 );
const QSqlDatabase db = QSqlDatabase::database();
// already correct version
SchemaUpdater<OldSql::OldSchema, VersionType> updater1(1);
QVERIFY(!updater1.needsUpdate());
updater1.execUpdates(false);
QCOMPARE( currentVersion(), 1 );
QCOMPARE( db.tables().size(), 2 );
SchemaUpdater<NewSql::NewSchema, VersionType> updater2(3);
QVERIFY(updater2.needsUpdate());
updater2.execUpdates(false);
QCOMPARE( currentVersion(), 3 );
QCOMPARE( db.tables().size(), 3 );
QVector<QPair<QString, QVariant::Type> > cols = SqlUtils::columnsOfTable( OldSql::Existing, db );
QCOMPARE( cols.size(), 3 );
QVERIFY( containsColumn( cols, "id", QVariant::String ) );
QVERIFY( containsColumn( cols, "column1", QVariant::String ) );
QVERIFY( containsColumn( cols, "column2", QVariant::Int ) );
cols = SqlUtils::columnsOfTable( NewSql::New );
QCOMPARE( cols.size(), 2 );
QVERIFY( containsColumn( cols, "column1", QVariant::String ) );
}
};
QTEST_MAIN( SchemaUpdateTest )
#include "schemaupdatetest.moc"