diff --git a/autotests/CMakeLists.txt b/autotests/CMakeLists.txt --- a/autotests/CMakeLists.txt +++ b/autotests/CMakeLists.txt @@ -43,7 +43,7 @@ DriverTest.cpp ExpressionsTest.cpp KDbTest.cpp - + DeleteItemsTest.cpp LINK_LIBRARIES kdbtestutils ) diff --git a/autotests/DeleteItemsTest.h b/autotests/DeleteItemsTest.h new file mode 100644 --- /dev/null +++ b/autotests/DeleteItemsTest.h @@ -0,0 +1,36 @@ +/* This file is part of the KDE project + Copyright (C) 2012-2016 Jarosław Staniek + + This program is free software; you can redistribute it and/or + modify it under the terms of the GNU Library General Public + License as published by the Free Software Foundation; either + version 2 of the License, or (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Library General Public License for more details. + + You should have received a copy of the GNU Library General Public License + along with this program; see the file COPYING. If not, write to + the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + * Boston, MA 02110-1301, USA. +*/ + +#ifndef KDB_DELETEITEMSTEST_H +#define KDB_DELETEITEMSTEST_H + +#include "KDbTestUtils.h" + +class DeleteItemsTest : public QObject +{ + Q_OBJECT +private Q_SLOTS: + void initTestCase(); + void deleteNonExistingrowTest(); + +private: + KDbTestUtils utils; +}; + +#endif diff --git a/autotests/DeleteItemsTest.cpp b/autotests/DeleteItemsTest.cpp new file mode 100644 --- /dev/null +++ b/autotests/DeleteItemsTest.cpp @@ -0,0 +1,122 @@ +#include "DeleteItemsTest.h" +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "KDbUtils_p.h" +#include +using namespace std; +//#define KDB_EXPORT __attribute__((visibility("true"))) +//#define KDB_EXPORT bool deleteRecords(KDbConnection* conn, const QString &tableName, +// const QString &keyname, KDbField::Type keytype, const QVariant &keyval); +#include +#include +#include +#include +#include "../tests/features/tables_test_p.h" + +#define ll long long +QByteArray prgname; +QString db_name; +QString drv_id; +QString test_name; +int cursor_options = 0; +bool db_name_required = true; +KDbConnectionData conn_data; +KDbConnection* conn = 0; +KDbDriver* driver; +QApplication *app = 0; +KDbTableSchema *kdbtest_t; + +QTEST_GUILESS_MAIN(DeleteItemsTest) + +void DeleteItemsTest::initTestCase() +{ + conn_data.setUserName("root"); + conn_data.setPassword("rememberreach"); + //conn_data.setPort(455); + KDbDriverManager manager; + const QStringList driverIds = manager.driverIds(); + qDebug() << "DRIVERS: " << driverIds; + driver = manager.driver("org.kde.kdb.mysql"); + conn_data.setDatabaseName("testdata"); + conn_data.setDatabaseName("nkdtestdb"); + + conn = driver->createConnection(conn_data); + + QVERIFY(conn || !driver->result().isError()); + qDebug() << "main: KDbConnection object created."; + QVERIFY(conn->connect()); + qDebug() << "main: KDbConnection::connect() OK."; + +// This far - i have created database connection.. + + + QVERIFY(conn->dropDatabase("nkdtestdb")); + QVERIFY(conn->createDatabase("nkdtestdb")); + if(conn->databaseExists("nkdtestdb")) + qDebug() << "DB" << " nkdtestdb " << "created"; + + +// this far, i have created database. + + QVERIFY(conn->useDatabase("nkdtestdb")); + KDbField *f; + kdbtest_t = new KDbTableSchema("kdbtestteam"); + kdbtest_t->setCaption("kdbtestteam database"); + kdbtest_t->addField(f = new KDbField("id", KDbField::Integer, KDbField::PrimaryKey | KDbField::AutoInc, KDbField::Unsigned)); + f->setCaption("ID"); + kdbtest_t->addField(f = new KDbField("age", KDbField::Integer, 0, KDbField::Unsigned)); + f->setCaption("Age"); + kdbtest_t->addField(f = new KDbField("fname", KDbField::Text)); + f->setCaption("Name"); + kdbtest_t->addField(f = new KDbField("sname", KDbField::Text)); + f->setCaption("Surname"); + QVERIFY(conn->createTable(kdbtest_t)); + qDebug() << "kdbtestteam table schema made."; + + +//this far, i have created table and schema. + + + + qDebug() << *kdbtest_t; + QVERIFY(conn->insertRecord(kdbtest_t, QVariant(1), QVariant(30), QVariant("Jaroslaw"), QVariant("Staniek"))); + QVERIFY(conn->insertRecord(kdbtest_t, QVariant(2), QVariant(20), QVariant("Nitish"), QVariant("Dwivedi"))); + QVERIFY(conn->insertRecord(kdbtest_t, QVariant(3), QVariant(20), QVariant("Khushboo"), QVariant("Gupta"))); + QVERIFY(conn->insertRecord(kdbtest_t, QVariant(4), QVariant(20), QVariant("Priyanshu"), QVariant("Jain"))); + qDebug() << "values in kdbtestteam inserted."; + +// this far, i have inserted values in table. +// Now , REAL TEST GOES!!! + + +//! @overload bool deleteRecord(KDbConnection*, const KDbTableSchema&, const QString &, KDbField::Type, const QVariant &) + /*KDB_EXPORT bool deleteRecords(KDbConnection* conn, const QString &tableName, + const QString &keyname, KDbField::Type keytype, const QVariant &keyval);*/ + //! Deletes records using one generic criteria. + + QVERIFY(KDb::deleteRecords(conn, *kdbtest_t, QLatin1String("id"), 4)); + QVERIFY(KDb::deleteRecords(conn, *kdbtest_t, QLatin1String("sname"), "Gupta")); + QVERIFY(KDb::deleteRecords(conn, *kdbtest_t, QLatin1String("fname"), "Nitish")); + QVERIFY(KDb::deleteRecords(conn, *kdbtest_t, QLatin1String("age"), 30)); +} + +//by default, if we try to delete a row that does not exist in a table, the query will execute successfully and returns +//zero rows affected. Hence when we try to delete row that does not exist, the query should complete its execution successfully. + +void DeleteItemsTest::deleteNonExistingrowTest() +{ + QVERIFY(KDb::deleteRecords(conn, *kdbtest_t, QLatin1String("id"), 400)); +} \ No newline at end of file