Commit 3104f14c authored by Lukas Appelhans's avatar Lukas Appelhans
Browse files

Test for the SQLiteConnection

parent f3b9f473
......@@ -110,5 +110,17 @@ if("${CMAKE_BUILD_TYPE}" MATCHES "^[Dd]ebug[Ff]ull$")
target_link_libraries(akabei_polkit_archive_handler_test ${QT_QTTEST_LIBRARY} akabeicore)
add_test(akabei_polkit_archive_handler_test akabei_polkit_archive_handler_test)
#PolKitSQLiteConnection test
set(akabei_polkit_sqliteconnection_test_SRCS
akabeipolkitarchivehandlertest.cpp
)
qt4_wrap_cpp(akabei_polkit_sqliteconnection_test_MOC_SRCS akabeipolkitarchivehandlertest.h)
add_executable(akabei_polkit_sqliteconnection_test ${akabei_polkit_sqliteconnection_test_SRCS} ${akabei_polkit_sqliteconnection_test_MOC_SRCS})
target_link_libraries(akabei_polkit_sqliteconnection_test ${QT_QTTEST_LIBRARY} akabeicore)
add_test(akabei_polkit_sqliteconnection_test akabei_polkit_sqliteconnection_test)
endif("${CMAKE_BUILD_TYPE}" MATCHES "^[Dd]ebug[Ff]ull$")
/* This file is part of the Chakra project
Copyright (C) 2011 Lukas Appelhans
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; either
version 2 of the License, or (at your option) any later version.
*/
#include "akabeipolkitsqliteconnectiontest.h"
#include <QTest>
#include "PolKitSQLiteConnection.h"
#include <qtemporaryfile.h>
#include "akabeicoreconfig.h"
#include "akabeipackage.h"
#include <akabeiquery.h>
#include <QDebug>
AkabeiPolkitSQLiteConnectionTest::AkabeiPolkitSQLiteConnectionTest()
{
}
AkabeiPolkitSQLiteConnectionTest::~AkabeiPolkitSQLiteConnectionTest()
{
}
void AkabeiPolkitSQLiteConnectionTest::simpleTest()
{
QTemporaryFile file(this);
file.open();
file.close();
PolKitSQLiteConnection c(file.fileName(), false);
QFile schemaFile(DB_SCHEMA);
QVERIFY(schemaFile.open(QIODevice::ReadOnly | QIODevice::Text));
{
// Obtain the schema
QString schema = schemaFile.readAll();
// Prepare the query
QStringList queries = schema.split(';');
foreach (const QString &query, queries) {
// Check if it's valid
if (!query.contains("CREATE")) {
// Skip, it's probably a whitespaced query
continue;
}
try {
c.query(query);
} catch (SQLiteException& ex) {
std::string error = QString("An exception got thrown: %1").arg(ex.what()).toStdString();
QVERIFY2(false, error.c_str());
}
}
}
try {
c.query( Akabei::Queries::begin() );
} catch (SQLiteException& e) {
std::string error = QString("Query error: %1").arg(e.what()).toStdString();
QVERIFY2(false, error.c_str());
}
QString null = "NULL";
const QString name("foo");
const Akabei::Package::Version version("2.4.1-alpha2");
const QString filename("/tmp/foobar.tar.xz");
const QString desc("FooBar");
const int size = 2000;
const int installedSize = 1000;
const QByteArray md5sum = "balbablsdf323434523534";
const QUrl url("http://foo.com/bar.html");
QDateTime buildDate;
buildDate.addDays(100);
const QString arch("i686");
const QString packager("John Doe");
const int flags = 1;
const QUrl screenshot("http://foo.com/bar.jpg");
const Akabei::Package::InstallReason reason = Akabei::Package::ExplicitlyInstalledReason;
QDateTime installDate;
installDate.addDays(200);
try {
// That's gonna be long.
// Query first.
QString sqlQuery =
"INSERT INTO packages ('name', 'version', 'filename', 'description', 'size', 'installedsize', "
"'md5sum', 'url', 'builddate', 'arch', 'packager', "
"'flags', 'screenshoturl', 'installreason', 'installdate') "
"VALUES ("
":Name, :Version, :Filename, :Description, :Size, :InstalledSize, "
":MD5SUM, :URL, :BuildDate, :Arch, :Packager, "
":Flags, :ScreenShotURL, :InstallReason, :InstallDate)";
c.bind(":Name", name);
c.bind(":Version", version.toByteArray());
c.bind(":Filename", filename);
c.bind(":Description", desc);
c.bind(":Size", size);
c.bind(":InstalledSize", installedSize);
c.bind(":MD5SUM", md5sum);
if (url.isValid()) {
c.bind(":URL", url.toString());
} else {
c.bind(":URL", null);
}
c.bind(":BuildDate", buildDate.toTime_t());
c.bind(":Arch", arch);
c.bind(":Packager", packager);
c.bind(":Flags", flags); // TODO I still need to decide how to handle flags, bind to null
if (screenshot.isValid()) {
c.bind(":ScreenShotURL", screenshot.toString());
} else {
c.bind(":ScreenShotURL", null);
}
c.bind(":InstallReason", (int)reason);
c.bind(":InstallDate", installDate.toTime_t());
c.query(sqlQuery);
c.query("COMMIT");
c.query("VACUUM");
} catch (SQLiteException& e) {
std::string error = QString("Query error: %1").arg(e.what()).toStdString();
QVERIFY2(false, error.c_str());
}
try {
QString query = Akabei::Queries::selectPackages("name", "=", name);
QString expectedQuery = "SELECT * FROM packages WHERE name = \"" + name + "\";";
QCOMPARE(query, expectedQuery);
SQLiteResource r = c.query(query);
QCOMPARE(r.getRowsCount(), 1);
QCOMPARE(name, r.getDataAt(0, "name").toString());
QCOMPARE(version, Akabei::Package::Version(r.getDataAt(0, "version").toString().toUtf8()));
QCOMPARE(filename, r.getDataAt(0, "filename").toString());
QCOMPARE(desc, r.getDataAt(0, "description").toString());
QCOMPARE(size, r.getDataAt(0, "size").toInt());
QCOMPARE(installedSize, r.getDataAt(0, "installedsize").toInt());
QCOMPARE(md5sum, r.getDataAt(0, "md5sum").toByteArray());
QCOMPARE(url, r.getDataAt(0, "url").toUrl());
QCOMPARE(buildDate, r.getDataAt(0, "builddate").toDateTime());
QCOMPARE(arch, r.getDataAt(0, "arch").toString());
QCOMPARE(packager, r.getDataAt(0, "packager").toString());
QCOMPARE(flags, r.getDataAt(0, "flags").toInt());
QCOMPARE(screenshot, r.getDataAt(0, "screenshoturl").toUrl());
QCOMPARE((int)reason, 1);//Akabei::Package::ExplicitlyInstalledReason
QCOMPARE(installDate, r.getDataAt(0, "installdate").toDateTime());
} catch (SQLiteException& e) {
std::string error = QString("Query error: %1").arg(e.what()).toStdString();
QVERIFY2(false, error.c_str());
}
}
QTEST_MAIN(AkabeiPolkitSQLiteConnectionTest)
/* This file is part of the Chakra project
Copyright (C) 2012 Lukas Appelhans
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; either
version 2 of the License, or (at your option) any later version.
*/
#ifndef AKABEIPOLKITSQLITECONNECTIONTEST_H
#define AKABEIPOLKITSQLITECONNECTIONTEST_H
#include <QtCore/QObject>
class AkabeiPolkitSQLiteConnectionTest : public QObject
{
Q_OBJECT
public:
AkabeiPolkitSQLiteConnectionTest();
virtual ~AkabeiPolkitSQLiteConnectionTest();
private slots:
void simpleTest();
};
#endif // AKABEIHOOKTEST_H
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment