mirror of
https://gitlab.com/spaceti-app/integrations/common/server-sdk.git
synced 2026-07-12 19:10:49 +02:00
Adding SQLite test ( open database )
This commit is contained in:
parent
70e389ab43
commit
b04332059f
@ -31,6 +31,7 @@ win32:CONFIG(release, debug|release): LIBS += -L$$SDKDIR/release/ -lserversdk
|
||||
else:win32:CONFIG(debug, debug|release): LIBS += -L$$SDKDIR/debug/ -lserversdk
|
||||
else:unix: LIBS += -L$$SDKDIR/ -lserversdk
|
||||
|
||||
QT += network sql
|
||||
|
||||
INCLUDEPATH += $$shell_quote($$PWD/src/)
|
||||
DEPENDPATH += $$shell_quote($$PWD/src/serversdk)
|
||||
|
||||
@ -25,7 +25,7 @@ win32:CONFIG(release, debug|release): LIBS += -L$$SDKDIR/release/ -lserversdk
|
||||
else:win32:CONFIG(debug, debug|release): LIBS += -L$$SDKDIR/debug/ -lserversdk
|
||||
else:unix: LIBS += -L$$SDKDIR/ -lserversdk -lz
|
||||
|
||||
QT += network
|
||||
QT += network sql
|
||||
INCLUDEPATH += $$PWD/src/
|
||||
DEPENDPATH += $$PWD/src/serversdk
|
||||
|
||||
|
||||
@ -1,24 +1,77 @@
|
||||
#include "databasemanager.h"
|
||||
#include "logger.h"
|
||||
|
||||
|
||||
|
||||
|
||||
namespace serversdk {
|
||||
|
||||
DatabaseManager::DatabaseManager()
|
||||
{
|
||||
}
|
||||
|
||||
DatabaseManager::DatabaseManager(const QString& host, const QString& dbName, const QString& user, const QString& password, bool dryMode)
|
||||
: mDryMode(dryMode)
|
||||
{
|
||||
db = QSqlDatabase::addDatabase("QPSQL");
|
||||
db.setDatabaseName(dbName);
|
||||
db.setHostName(host);
|
||||
db.setUserName(user); // home-office stuff -->> db.setPort(80);
|
||||
db.setPassword(password);
|
||||
if (db.open())
|
||||
mDatabase = QSqlDatabase::addDatabase("QPSQL");
|
||||
mDatabase.setDatabaseName(dbName);
|
||||
mDatabase.setHostName(host);
|
||||
mDatabase.setUserName(user); // home-office stuff -->> db.setPort(80);
|
||||
mDatabase.setPassword(password);
|
||||
if (mDatabase.open())
|
||||
{
|
||||
query = QSqlQuery("query", db);
|
||||
query = QSqlQuery("query", mDatabase);
|
||||
} else {
|
||||
log("Opening error" + db.lastError().text());
|
||||
log("Opening error" + mDatabase.lastError().text());
|
||||
emit error();
|
||||
}
|
||||
}
|
||||
|
||||
DatabaseManager::~DatabaseManager()
|
||||
{
|
||||
if (mDatabase.isValid()) {
|
||||
mDatabase.close();
|
||||
mDatabase = QSqlDatabase(); // invalidate database
|
||||
qDebug(sdkdb) << "removing database connection:" << mConnectionName;
|
||||
QSqlDatabase::removeDatabase(mConnectionName);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Creates new SQLite connection
|
||||
* @param path to database ( will be created when none )
|
||||
* @param connectionName
|
||||
* @return
|
||||
*/
|
||||
bool DatabaseManager::openSQLite(const QString &path, const QString &connectionName)
|
||||
{
|
||||
mConnectionName = connectionName;
|
||||
mDatabase = QSqlDatabase::addDatabase("QSQLITE", mConnectionName);
|
||||
mDatabase.setDatabaseName(path);
|
||||
int res = mDatabase.open();
|
||||
|
||||
if (!res) {
|
||||
qInfo(sdkdb) << "Not possible to open database from path:" << path;
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
|
||||
bool DatabaseManager::isValid() const
|
||||
{
|
||||
return mDatabase.isValid();
|
||||
}
|
||||
|
||||
bool DatabaseManager::isOpen() const
|
||||
{
|
||||
return mDatabase.isOpen();
|
||||
}
|
||||
|
||||
void DatabaseManager::close()
|
||||
{
|
||||
mDatabase.close();
|
||||
}
|
||||
|
||||
bool DatabaseManager::executeQuery(QSqlQuery &query)
|
||||
{
|
||||
if (mDryMode){
|
||||
|
||||
@ -17,7 +17,14 @@ class DatabaseManager : public QObject
|
||||
Q_OBJECT
|
||||
friend class Logger;
|
||||
public:
|
||||
DatabaseManager();
|
||||
DatabaseManager(const QString& host, const QString& dbName, const QString& user, const QString& password, bool mDryMode = false);
|
||||
~DatabaseManager();
|
||||
|
||||
bool openSQLite(const QString &path, const QString &connectionName = QStringLiteral("qt_sql_default_connection"));
|
||||
bool isValid() const;
|
||||
bool isOpen() const;
|
||||
void close();
|
||||
|
||||
private:
|
||||
bool executeQuery(QSqlQuery &query);
|
||||
@ -29,7 +36,8 @@ signals:
|
||||
void error();
|
||||
|
||||
private:
|
||||
QSqlDatabase db;
|
||||
QString mConnectionName;
|
||||
QSqlDatabase mDatabase;
|
||||
QSqlQuery query;
|
||||
bool mDryMode = false;
|
||||
};
|
||||
|
||||
@ -7,6 +7,7 @@
|
||||
Q_LOGGING_CATEGORY(sdkcore, "sdk.core");
|
||||
Q_LOGGING_CATEGORY(sdknetwork, "sdk.network");
|
||||
Q_LOGGING_CATEGORY(sdkutils, "sdk.utils");
|
||||
Q_LOGGING_CATEGORY(sdkdb, "sdk.db");
|
||||
|
||||
namespace serversdk {
|
||||
|
||||
|
||||
@ -11,11 +11,11 @@
|
||||
Q_DECLARE_LOGGING_CATEGORY(sdkcore)
|
||||
Q_DECLARE_LOGGING_CATEGORY(sdknetwork)
|
||||
Q_DECLARE_LOGGING_CATEGORY(sdkutils)
|
||||
Q_DECLARE_LOGGING_CATEGORY(sdkdb)
|
||||
|
||||
namespace serversdk {
|
||||
|
||||
/**
|
||||
* # Loggger
|
||||
/** # Loggger
|
||||
*
|
||||
* Class for logging qDebug qWarning qInfo and qCritical messages
|
||||
*
|
||||
@ -44,7 +44,21 @@ public:
|
||||
// User defined consumer ( messages will be sent there using insertMessage )
|
||||
static Logger* sConsumer;
|
||||
|
||||
/// Default constructor
|
||||
/**
|
||||
* # Default values
|
||||
*
|
||||
* * bits - Expected 32bits
|
||||
* * subjectId - must be valid ( not possible to add token withou valid subject )
|
||||
*
|
||||
* @m_class{m-block m-error}
|
||||
|
||||
@par Third-party license info
|
||||
This utility depends on the [Magnum engine](https://magnum.graphics).
|
||||
It's licensed under MIT, so all you need to do is mention it in the
|
||||
credits of your commercial app.
|
||||
*
|
||||
*
|
||||
*/
|
||||
explicit Logger(QObject *parent = nullptr);
|
||||
|
||||
|
||||
@ -85,7 +99,7 @@ signals:
|
||||
void messageReceived(const serversdk::LoggerMessage &message);
|
||||
void messageListUpdated();
|
||||
private:
|
||||
QList<LoggerMessage> mLogMessages;
|
||||
QList<LoggerMessage> mLogMessages; //!< Test
|
||||
QMutex mMutex;
|
||||
};
|
||||
|
||||
|
||||
@ -3,5 +3,6 @@ TEMPLATE = subdirs
|
||||
SUBDIRS += \
|
||||
tst_soap \
|
||||
tst_request \
|
||||
tst_database \
|
||||
tst_database/tst_database.pro \
|
||||
tst_database/tst_sqlite.pro \
|
||||
tst_utils
|
||||
|
||||
@ -7,3 +7,5 @@ CONFIG -= app_bundle
|
||||
TEMPLATE = app
|
||||
|
||||
SOURCES += tst_databasetest.cpp
|
||||
|
||||
include(../../test.pri)
|
||||
|
||||
@ -1,5 +1,6 @@
|
||||
#include <QtTest>
|
||||
|
||||
#include "serversdk/databasemanager.h"
|
||||
// add necessary includes here
|
||||
|
||||
class DatabaseTest : public QObject
|
||||
@ -11,7 +12,7 @@ public:
|
||||
~DatabaseTest();
|
||||
|
||||
private slots:
|
||||
void test_case1();
|
||||
void createDb_test();
|
||||
|
||||
};
|
||||
|
||||
@ -25,7 +26,7 @@ DatabaseTest::~DatabaseTest()
|
||||
|
||||
}
|
||||
|
||||
void DatabaseTest::test_case1()
|
||||
void DatabaseTest::createDb_test()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
57
test/auto/tst_database/tst_sqlite.cpp
Normal file
57
test/auto/tst_database/tst_sqlite.cpp
Normal file
@ -0,0 +1,57 @@
|
||||
#include <QtTest>
|
||||
|
||||
#include "serversdk/databasemanager.h"
|
||||
// add necessary includes here
|
||||
|
||||
class SqliteTest : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
SqliteTest();
|
||||
~SqliteTest();
|
||||
|
||||
private slots:
|
||||
void createDb_test();
|
||||
|
||||
};
|
||||
|
||||
SqliteTest::SqliteTest()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
SqliteTest::~SqliteTest()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
void SqliteTest::createDb_test()
|
||||
{
|
||||
// Prepare temporary path for database
|
||||
QTemporaryDir dir;
|
||||
Q_ASSERT(dir.isValid());
|
||||
QString dbpath = dir.path() + "/testdb.sqlite";
|
||||
Q_ASSERT(!dbpath.isEmpty());
|
||||
Q_ASSERT(!QFileInfo::exists(dbpath));
|
||||
|
||||
serversdk::DatabaseManager db;
|
||||
db.openSQLite(dbpath);
|
||||
Q_ASSERT(db.isValid());
|
||||
Q_ASSERT(db.isOpen());
|
||||
Q_ASSERT(QFileInfo::exists(dbpath));
|
||||
|
||||
// Closing database
|
||||
db.close();
|
||||
Q_ASSERT(!db.isOpen());
|
||||
|
||||
|
||||
// Temporary dir cleanup
|
||||
Q_ASSERT(dir.remove());
|
||||
|
||||
Q_ASSERT(!QFileInfo::exists(dbpath));
|
||||
}
|
||||
|
||||
QTEST_APPLESS_MAIN(SqliteTest)
|
||||
|
||||
#include "tst_sqlite.moc"
|
||||
11
test/auto/tst_database/tst_sqlite.pro
Normal file
11
test/auto/tst_database/tst_sqlite.pro
Normal file
@ -0,0 +1,11 @@
|
||||
QT += testlib
|
||||
QT -= gui
|
||||
|
||||
CONFIG += qt console warn_on depend_includepath testcase
|
||||
CONFIG -= app_bundle
|
||||
|
||||
TEMPLATE = app
|
||||
|
||||
SOURCES += tst_sqlite.cpp
|
||||
|
||||
include(../../test.pri)
|
||||
Loading…
x
Reference in New Issue
Block a user