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,39 @@ +/* This file is part of the KDE project + (C) Copyright (C) 2017 Nitish Kumar Dwivedi + + + 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 deleteNonExistingRecordTest(); + void deleteRecordWithTwoConstraintsTest(); + void deleteRecordWithThreeConstraintsTest(); + void deleteRecordsAllTest(); +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,155 @@ +/* This file is part of the KDE project + Copyright (C) 2017 Nitish Kumar Dwivedi + + 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. +*/ + +#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; +#include +#include +#include +#include +#include "../tests/features/tables_test_p.h" + +#define ll long long + +QTEST_GUILESS_MAIN(DeleteItemsTest) + +void DeleteItemsTest::initTestCase() +{ + QVERIFY(utils.testCreateDbWithTables("KDbTest")); + +//! @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(utils.connection.data(), "persons", "id", 2)); + QVERIFY(KDb::deleteRecords(utils.connection.data(), "persons" , "name", "Jaroslaw")); + QVERIFY(KDb::deleteRecords(utils.connection.data(), "persons", "surname", "Nitish")); + QVERIFY(KDb::deleteRecords(utils.connection.data(), "persons", "age", 45)); +//NON existing ROW -- should return false; + QVERIFY(KDb::deleteRecords(utils.connection.data(), "persons", "age", "Nitish")); + QVERIFY(utils.testDisconnectAndDropDb()); +} + +//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::deleteNonExistingRecordTest() +{ + QVERIFY(utils.testCreateDbWithTables("KDbTest")); + QVERIFY(KDb::deleteRecords(utils.connection.data(), "persons" , "id", 400)); + QVERIFY(KDb::deleteRecords(utils.connection.data(), "persons" , "name", "Nitish")); + + QVERIFY(utils.testDisconnectAndDropDb()); +} + +/* +//! Deletes records with two generic criterias. + KDB_EXPORT bool deleteRecords(KDbConnection* conn, const QString &tableName, + const QString &keyname1, KDbField::Type keytype1, const QVariant& keyval1, + const QString &keyname2, KDbField::Type keytype2, const QVariant& keyval2); + +*/ +void DeleteItemsTest::deleteRecordWithTwoConstraintsTest() +{ + QVERIFY(utils.testCreateDbWithTables("KDbTest")); +//Both Arguments as INTEGER + QVERIFY(KDb::deleteRecords(utils.connection.data() , "persons", "id" ,KDbField::Integer,2, "age",KDbField::Integer,60)); + +//One argument is Integer and another one in Text--- SHOULD RETURN TRUE; + QVERIFY(KDb::deleteRecords(utils.connection.data() , "persons" , "age" ,KDbField::Integer,20,"name",KDbField::Text,"Jaroslaw")); + +//using wrong type of data-- SHOULD RETURN FALSE; + //THIS FUNCTION IS RETURNING AND WHILE EXECUTING SQL QUERY, QUERY RETURNS ERROR. SOMETHING FISHY!!!! + //mysql> delete from kdbtestteam where age = 20 and fname = 56; + //ERROR 1292 (22007): Truncated incorrect DOUBLE value: 'Khushboo' +/* +bool KDb::deleteRecords(KDbConnection* conn, const QString &tableName, + const QString &keyname1, KDbField::Type keytype1, const QVariant& keyval1, + const QString &keyname2, KDbField::Type keytype2, const QVariant& keyval2) +{ + Q_ASSERT(conn); + return conn->executeVoidSQL(KDbEscapedString("DELETE FROM %1 WHERE %2=%3 AND %4=%5") + .arg(conn->escapeIdentifier(tableName)) + .arg(conn->escapeIdentifier(keyname1)) + .arg(conn->driver()->valueToSQL(keytype1, keyval1)) + .arg(conn->escapeIdentifier(keyname2)) + .arg(conn->driver()->valueToSQL(keytype2, keyval2))); +} + +*/ + + //here in the declaration , the keys are parsed i guess. thus preventing error in case below. SHOULD HAVE A CHECK HERE. + + QVERIFY(!KDb::deleteRecords(utils.connection.data() , "persons","age",KDbField::Integer,20, "name",KDbField::Text,56)); + +//passing both wrond type of arguments -- SHOULD RETURN FALSE; + QVERIFY(!KDb::deleteRecords(utils.connection.data() , "persons", "age",KDbField::Integer,"TRAP", "name",KDbField::Text,56)); + QVERIFY(utils.testDisconnectAndDropDb()); + +} + +////! Deletes records with three generic criterias. +/* KDB_EXPORT bool deleteRecords(KDbConnection* conn, const QString &tableName, + const QString &keyname1, KDbField::Type keytype1, const QVariant& keyval1, + const QString &keyname2, KDbField::Type keytype2, const QVariant& keyval2, + const QString &keyname3, KDbField::Type keytype3, const QVariant& keyval3); +*/ + +void DeleteItemsTest::deleteRecordWithThreeConstraintsTest() +{ + QVERIFY(utils.testCreateDbWithTables("KDbTest")); + QVERIFY(KDb::deleteRecords(utils.connection.data() ,"persons","age",KDbField::Integer,27, + "name",KDbField::Text,"Jaraslaw","id",KDbField::Integer,1)); + QVERIFY(KDb::deleteRecords(utils.connection.data() ,"persons","age",KDbField::Integer,60, + "name",KDbField::Text,"Lech","id",KDbField::Integer,2)); + QVERIFY(utils.testDisconnectAndDropDb()); +} + +//! Deletes all records from table @a tableName. +/* KDB_EXPORT bool deleteAllRecords(KDbConnection* conn, const QString &tableName); + //! @overload bool deleteAllRecords(KDbConnection*, const QString&); + inline bool deleteAllRecords(KDbConnection* conn, const KDbTableSchema &table) +*/ + +void DeleteItemsTest::deleteRecordsAllTest() +{ + QVERIFY(utils.testCreateDbWithTables("KDbTest")); + QVERIFY(KDb::deleteAllRecords(utils.connection.data() , "persons")); + //Test below throws a warning because table does not exist. + QVERIFY(!KDb::deleteAllRecords(utils.connection.data() , "NonExistingTable")); + QVERIFY(utils.testDisconnectAndDropDb()); +}