mirror of
https://gitlab.com/spaceti-app/integrations/common/server-sdk.git
synced 2026-07-12 23:00:46 +02:00
110 lines
2.1 KiB
C++
110 lines
2.1 KiB
C++
#include <QtTest>
|
|
|
|
#include "serversdk/databasemanager.h"
|
|
#include "serversdk/databaseutils.h"
|
|
#include "serversdk/core.h"
|
|
// add necessary includes here
|
|
|
|
|
|
class Names : public serversdk::DatabaseTable {
|
|
Q_OBJECT
|
|
TABLE_PROPERTY(int, id, set_id, 0);
|
|
TABLE_PROPERTY(QString, name, set_name, 0);
|
|
};
|
|
|
|
|
|
class SqliteTest : public QObject
|
|
{
|
|
Q_OBJECT
|
|
|
|
public:
|
|
SqliteTest();
|
|
~SqliteTest();
|
|
|
|
private slots:
|
|
void createDb_test();
|
|
void createTable_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));
|
|
}
|
|
|
|
void SqliteTest::createTable_test()
|
|
{
|
|
// Prepare temporary path for database
|
|
QTemporaryDir dir;
|
|
QString dbpath = dir.path() + "/testdb.sqlite";
|
|
serversdk::DatabaseManager db;
|
|
db.openSQLite(dbpath);
|
|
Q_ASSERT(db.isOpen());
|
|
|
|
|
|
|
|
// Not correct way
|
|
auto names = std::make_unique<Names>();
|
|
|
|
|
|
// Creating table
|
|
serversdk::DatabaseUtils utils(&db);
|
|
QSqlQuery query = utils.createTable(names.get(), "names");
|
|
// Q_ASSERT(query.isValid());
|
|
|
|
// Inserting into table
|
|
names->set_id(2);
|
|
names->set_name("Testname");
|
|
|
|
utils.insertIntoTable(names.get(), "names");
|
|
QSqlQuery insertQuery = utils.insertIntoTable(names.get(), "names");
|
|
//Q_ASSERT(insertQuery.isValid());
|
|
|
|
|
|
|
|
// Select from table
|
|
QSqlQuery selectSQL("SELECT * FROM names");
|
|
// Q_ASSERT(db.executeQuery(selectSQL));
|
|
while (selectSQL.next()) {
|
|
QString value = selectSQL.value(1).toString();
|
|
qDebug() << "value is" << value;
|
|
}
|
|
|
|
//db.create
|
|
}
|
|
|
|
QTEST_APPLESS_MAIN(SqliteTest)
|
|
|
|
#include "tst_sqlite.moc"
|