diff options
author | Huynh Tran <nthuynh75@gmail.com> | 2005-06-21 22:43:23 +0000 |
---|---|---|
committer | Huynh Tran <nthuynh75@gmail.com> | 2005-06-21 22:43:23 +0000 |
commit | 460a5bb2a9f57ddf9dbbd7095452c3c4fc660d65 (patch) | |
tree | ababb79e615ec1e2fcd82a4d2ddd1520c7c9179d /src/dal | |
parent | 270163b535f64f2fd6baa32d8a76ecc1c5526539 (diff) | |
download | manaserv-460a5bb2a9f57ddf9dbbd7095452c3c4fc660d65.tar.gz manaserv-460a5bb2a9f57ddf9dbbd7095452c3c4fc660d65.tar.xz manaserv-460a5bb2a9f57ddf9dbbd7095452c3c4fc660d65.zip |
Reorganized unit tests and initial release of the unit tests for the Storage class (to be completed).
Diffstat (limited to 'src/dal')
-rw-r--r-- | src/dal/sqlitedataprovider.cpp | 4 | ||||
-rw-r--r-- | src/dal/testdataprovider.cpp | 199 | ||||
-rw-r--r-- | src/dal/testdataprovider.h | 138 | ||||
-rw-r--r-- | src/dal/testrecordset.cpp | 286 | ||||
-rw-r--r-- | src/dal/testrecordset.h | 209 |
5 files changed, 4 insertions, 832 deletions
diff --git a/src/dal/sqlitedataprovider.cpp b/src/dal/sqlitedataprovider.cpp index a9ed365..00d8298 100644 --- a/src/dal/sqlitedataprovider.cpp +++ b/src/dal/sqlitedataprovider.cpp @@ -91,6 +91,10 @@ SqLiteDataProvider::connect(const std::string& dbName, // the database after an unsuccessful call to sqlite3_open(). sqlite3_close(mDb); + // FIXME + // 21-Jun-2005: although we did invoke sqlite3_close(), there + // seems to be still a leak of 136 bytes here. + throw DbConnectionFailure(msg); } diff --git a/src/dal/testdataprovider.cpp b/src/dal/testdataprovider.cpp deleted file mode 100644 index 08b66da..0000000 --- a/src/dal/testdataprovider.cpp +++ /dev/null @@ -1,199 +0,0 @@ -/* - * The Mana World Server - * Copyright 2004 The Mana World Development Team - * - * This file is part of The Mana World. - * - * The Mana World 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 any later version. - * - * The Mana World 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 General Public License for - * more details. - * - * You should have received a copy of the GNU General Public License along - * with The Mana World; if not, write to the Free Software Foundation, Inc., - * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - * - * $Id$ - */ - - -#include "dataproviderfactory.h" -#include "testdataprovider.h" - - -// register the fixture into the 'registry' -CPPUNIT_TEST_SUITE_REGISTRATION(DataProviderTest); - - -using namespace tmwserv::dal; - - -/** - * Set up fixtures. - */ -void -DataProviderTest::setUp(void) -{ - // obtain a data provider. - try { - mDb = DataProviderFactory::createDataProvider(); - } - catch (const std::runtime_error& e) { - CPPUNIT_FAIL(e.what()); - } - - // init db info and account. -#ifdef SQLITE_SUPPORT - mDbName = "mydb.db"; -#else - mDbName = "mydb"; -#endif - mDbUser = "guest"; - mDbPassword = "guest"; - - // init SQL queries. - mSqlCreateTable = "create table employees ("; - mSqlCreateTable += " id int primary key, "; - mSqlCreateTable += " name varchar(32) not null);"; - - mSqlInsertRow = "insert into employees values (1, 'john');"; - - mSqlFetchRow = "select * from employees;"; -} - - -/** - * Tear down fixtures. - */ -void -DataProviderTest::tearDown(void) -{ - delete mDb; -} - - -/** - * Connection to an existing database. - */ -void -DataProviderTest::testConnection1(void) -{ - mDb->connect(mDbName, mDbUser, mDbPassword); - CPPUNIT_ASSERT(mDb->isConnected()); -} - - -/** - * Create a new table in the database. - */ -void -DataProviderTest::testCreateTable1(void) -{ - mDb->connect(mDbName, mDbUser, mDbPassword); - CPPUNIT_ASSERT(mDb->isConnected()); - - const RecordSet& rs = mDb->execSql(mSqlCreateTable); - CPPUNIT_ASSERT(rs.isEmpty()); - - mDb->disconnect(); - CPPUNIT_ASSERT(!mDb->isConnected()); -} - - -/** - * Create the same table one more time in the database. - */ -void -DataProviderTest::testCreateTable2(void) -{ - mDb->connect(mDbName, mDbUser, mDbPassword); - CPPUNIT_ASSERT(mDb->isConnected()); - - // this should throw tmwserv::dal::DbSqlQueryExecFailure. - const RecordSet& rs = mDb->execSql(mSqlCreateTable); -} - - -/** - * Insert a new row to the table. - */ -void -DataProviderTest::testInsert1(void) -{ - mDb->connect(mDbName, mDbUser, mDbPassword); - CPPUNIT_ASSERT(mDb->isConnected()); - - const RecordSet& rs = mDb->execSql(mSqlInsertRow); - // an insert query does not return any records - // so the recordset remains empty. - CPPUNIT_ASSERT(rs.isEmpty()); - - mDb->disconnect(); - CPPUNIT_ASSERT(!mDb->isConnected()); -} - - -/** - * Insert the same record again. - */ -void -DataProviderTest::testInsert2(void) -{ - mDb->connect(mDbName, mDbUser, mDbPassword); - CPPUNIT_ASSERT(mDb->isConnected()); - - // this should throw tmwserv::dal::DbSqlQueryExecFailure - // as we are violating the primary key uniqueness. - mDb->execSql(mSqlInsertRow); -} - - -/** - * Fetch data from the table. - */ -void -DataProviderTest::testFetch1(void) -{ - mDb->connect(mDbName, mDbUser, mDbPassword); - CPPUNIT_ASSERT(mDb->isConnected()); - - const RecordSet& rs = mDb->execSql(mSqlFetchRow); - CPPUNIT_ASSERT(!rs.isEmpty()); - - std::string id("1"); - std::string name("john"); - CPPUNIT_ASSERT_EQUAL(id, rs(0, "id")); - CPPUNIT_ASSERT_EQUAL(name, rs(0, "name")); - - mDb->disconnect(); - CPPUNIT_ASSERT(!mDb->isConnected()); -} - - -/** - * Disconnection from an open database. - */ -void -DataProviderTest::testDisconnection1(void) -{ - mDb->connect(mDbName, mDbUser, mDbPassword); - CPPUNIT_ASSERT(mDb->isConnected()); - - mDb->disconnect(); - CPPUNIT_ASSERT(!mDb->isConnected()); -} - - -/** - * Disconnection from a closed database. - */ -void -DataProviderTest::testDisconnection2(void) -{ - mDb->disconnect(); - CPPUNIT_ASSERT(!mDb->isConnected()); -} diff --git a/src/dal/testdataprovider.h b/src/dal/testdataprovider.h deleted file mode 100644 index e677840..0000000 --- a/src/dal/testdataprovider.h +++ /dev/null @@ -1,138 +0,0 @@ -/* - * The Mana World Server - * Copyright 2004 The Mana World Development Team - * - * This file is part of The Mana World. - * - * The Mana World 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 any later version. - * - * The Mana World 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 General Public License for - * more details. - * - * You should have received a copy of the GNU General Public License along - * with The Mana World; if not, write to the Free Software Foundation, Inc., - * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - * - * $Id$ - */ - - - -#ifndef _TMWSERV_TEST_DATA_PROVIDER_H_ -#define _TMWSERV_TEST_DATA_PROVIDER_H_ - - -#include <cppunit/extensions/HelperMacros.h> - -#include "dalexcept.h" - - -/** - * Unit test for the DataProvider class. - */ -class DataProviderTest: public CppUnit::TestFixture -{ - CPPUNIT_TEST_SUITE(DataProviderTest); - - // add tests to the test suite. - CPPUNIT_TEST(testConnection1); - CPPUNIT_TEST(testCreateTable1); - CPPUNIT_TEST_EXCEPTION(testCreateTable2, - tmwserv::dal::DbSqlQueryExecFailure); - CPPUNIT_TEST(testInsert1); - CPPUNIT_TEST_EXCEPTION(testInsert2, tmwserv::dal::DbSqlQueryExecFailure); - CPPUNIT_TEST(testFetch1); - CPPUNIT_TEST(testDisconnection1); - CPPUNIT_TEST(testDisconnection2); - - CPPUNIT_TEST_SUITE_END(); - - - public: - /** - * Set up fixtures. - */ - void - setUp(void); - - - /** - * Tear down fixtures. - */ - void - tearDown(void); - - - /** - * Connection to an existing database. - */ - void - testConnection1(void); - - - /** - * Create a new table in the database. - */ - void - testCreateTable1(void); - - - /** - * Create the same table one more time in the database. - */ - void - testCreateTable2(void); - - - /** - * Insert a new record into the table. - */ - void - testInsert1(void); - - - /** - * Insert the same record again. - */ - void - testInsert2(void); - - - /** - * Fetch data from the table. - */ - void - testFetch1(void); - - - /** - * Disconnection from an open database. - */ - void - testDisconnection1(void); - - - /** - * Disconnection from a closed database. - */ - void - testDisconnection2(void); - - - private: - tmwserv::dal::DataProvider* mDb; /**< the data provider */ - std::string mDbName; /**< the database name */ - std::string mDbPath; /**< the database path */ - std::string mDbUser; /**< the database user */ - std::string mDbPassword; /**< the database password */ - std::string mSqlCreateTable; /**< SQL query to create table */ - std::string mSqlInsertRow; /**< SQL query to delete table */ - std::string mSqlFetchRow; /**< SQL query to fetch data */ -}; - - -#endif // _TMWSERV_TEST_DATA_PROVIDER_H_ diff --git a/src/dal/testrecordset.cpp b/src/dal/testrecordset.cpp deleted file mode 100644 index 3d8e928..0000000 --- a/src/dal/testrecordset.cpp +++ /dev/null @@ -1,286 +0,0 @@ -/* - * The Mana World Server - * Copyright 2004 The Mana World Development Team - * - * This file is part of The Mana World. - * - * The Mana World 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 any later version. - * - * The Mana World 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 General Public License for - * more details. - * - * You should have received a copy of the GNU General Public License along - * with The Mana World; if not, write to the Free Software Foundation, Inc., - * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - * - * $Id$ - */ - - -#include <sstream> - -#include "recordset.h" -#include "testrecordset.h" - - -// register the fixture into the 'registry' -CPPUNIT_TEST_SUITE_REGISTRATION(RecordSetTest); - - -using namespace tmwserv::dal; - - -/** - * Set up fixtures. - */ -void -RecordSetTest::setUp(void) -{ - // populate mNonEmptyRs. - Row headers; - headers.push_back("id"); - headers.push_back("name"); - mNonEmptyRs.setColumnHeaders(headers); - - Row r1; - r1.push_back("1"); - r1.push_back("john"); - mNonEmptyRs.add(r1); - - Row r2; - r2.push_back("2"); - r2.push_back("mike"); - mNonEmptyRs.add(r2); -} - - -/** - * Tear down fixtures. - */ -void -RecordSetTest::tearDown(void) -{ - // NOOP -} - - -/** - * Test RecordSet::rows() on an empty RecordSet. - */ -void -RecordSetTest::testRows1(void) -{ - CPPUNIT_ASSERT_EQUAL((unsigned int) 0, mEmptyRs.rows()); -} - - -/** - * Test RecordSet::rows() on a non-empty RecordSet. - */ -void -RecordSetTest::testRows2(void) -{ - CPPUNIT_ASSERT_EQUAL((unsigned int) 2, mNonEmptyRs.rows()); -} - - -/** - * Test RecordSet::cols() on an empty RecordSet. - */ -void -RecordSetTest::testCols1(void) -{ - CPPUNIT_ASSERT_EQUAL((unsigned int) 0, mEmptyRs.cols()); -} - - -/** - * Test RecordSet::cols() on a non-empty RecordSet. - */ -void -RecordSetTest::testCols2(void) -{ - CPPUNIT_ASSERT_EQUAL((unsigned int) 2, mNonEmptyRs.cols()); -} - - -/** - * Call RecordSet::isEmpty() from an empty RecordSet. - */ -void -RecordSetTest::testIsEmpty1(void) -{ - CPPUNIT_ASSERT(mEmptyRs.isEmpty()); -} - - -/** - * Call RecordSet::isEmpty() from a non-empty RecordSet. - */ -void -RecordSetTest::testIsEmpty2(void) -{ - CPPUNIT_ASSERT(!mNonEmptyRs.isEmpty()); -} - - -/** - * Call RecordSet::operator() from an empty RecordSet. - */ -void -RecordSetTest::testOperator1(void) -{ - // this should throw std::invalid_argument. - mEmptyRs(0, 0); -} - - -/** - * Call RecordSet::operator() from a non-empty RecordSet with - * a row index and a column index as parameters. - */ -void -RecordSetTest::testOperator2(void) -{ - std::string value("mike"); - - CPPUNIT_ASSERT_EQUAL(value, mNonEmptyRs(1, 1)); -} - - -/** - * Call RecordSet::operator() from a non-empty RecordSet with - * a row index and a column index that are out of range as - * parameters. - */ -void -RecordSetTest::testOperator3(void) -{ - // this should throw std::out_of_range. - mNonEmptyRs(2, 2); -} - - -/** - * Call RecordSet::operator() from a non-empty RecordSet with - * a row index and a field name as parameters. - */ -void -RecordSetTest::testOperator4(void) -{ - std::string value("1"); - - CPPUNIT_ASSERT_EQUAL(value, mNonEmptyRs(0, "id")); -} - - -/** - * Call RecordSet::operator() from a non-empty RecordSet with - * a row index that is out of range and a field name as parameters. - */ -void -RecordSetTest::testOperator5(void) -{ - // this should throw std::out_of_range. - mNonEmptyRs(3, "id"); -} - - -/** - * Call RecordSet::operator() from a non-empty RecordSet with - * a row index and a field name that does not exist as parameters. - */ -void -RecordSetTest::testOperator6(void) -{ - // this should throw std::invalid_argument. - mNonEmptyRs(1, "noname"); -} - - -/** - * Test writing an empty RecordSet to an output stream. - */ -void -RecordSetTest::testOutputStream1(void) -{ - std::string emptyStr; - - std::ostringstream os; - os << mEmptyRs; - - CPPUNIT_ASSERT_EQUAL(emptyStr, os.str()); -} - - -/** - * Test writing a non-empty RecordSet to an output stream. - */ -void -RecordSetTest::testOutputStream2(void) -{ - std::ostringstream os1; - os1 << "|id|name|" << std::endl << std::endl - << "|1|john|" << std::endl - << "|2|mike|" << std::endl; - - std::ostringstream os2; - os2 << mNonEmptyRs; - - CPPUNIT_ASSERT_EQUAL(os1.str(), os2.str()); -} - - -/** - * Test RecordSet::add() to add a new now. - */ -void -RecordSetTest::testAdd1(void) -{ - std::string id("3"); - std::string name("elena"); - - Row r; - r.push_back(id); - r.push_back(name); - mNonEmptyRs.add(r); - - CPPUNIT_ASSERT_EQUAL((unsigned int) 3, mNonEmptyRs.rows()); - CPPUNIT_ASSERT_EQUAL((unsigned int) 2, mNonEmptyRs.cols()); - CPPUNIT_ASSERT_EQUAL(id, mNonEmptyRs(2, 0)); - CPPUNIT_ASSERT_EQUAL(name, mNonEmptyRs(2, 1)); -} - - -/** - * Test RecordSet::add() to add a new now with a different number - * of fields. - */ -void -RecordSetTest::testAdd2(void) -{ - Row r; - r.push_back("4"); - - // this should throw std::invalid_argument. - mNonEmptyRs.add(r); -} - - -/** - * Test RecordSet::add() to add a new now to a RecordSet that does - * not have column headers. - */ -void -RecordSetTest::testAdd3(void) -{ - Row r; - r.push_back("5"); - - // this should throw tmw::dal::RsColumnHeadersNotSet. - mEmptyRs.add(r); -} diff --git a/src/dal/testrecordset.h b/src/dal/testrecordset.h deleted file mode 100644 index 60b3113..0000000 --- a/src/dal/testrecordset.h +++ /dev/null @@ -1,209 +0,0 @@ -/* - * The Mana World Server - * Copyright 2004 The Mana World Development Team - * - * This file is part of The Mana World. - * - * The Mana World 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 any later version. - * - * The Mana World 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 General Public License for - * more details. - * - * You should have received a copy of the GNU General Public License along - * with The Mana World; if not, write to the Free Software Foundation, Inc., - * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - * - * $Id$ - */ - - -#ifndef _TMWSERV_TEST_RECORDSET_H_ -#define _TMWSERV_TEST_RECORDSET_H_ - - -#include <cppunit/extensions/HelperMacros.h> - -#include "dalexcept.h" - - -/** - * Unit test for the RecordSet class. - */ -class RecordSetTest: public CppUnit::TestFixture -{ - CPPUNIT_TEST_SUITE(RecordSetTest); - - // add tests to the test suite. - CPPUNIT_TEST(testRows1); - CPPUNIT_TEST(testRows2); - CPPUNIT_TEST(testCols1); - CPPUNIT_TEST(testCols2); - CPPUNIT_TEST(testIsEmpty1); - CPPUNIT_TEST(testIsEmpty2); - CPPUNIT_TEST_EXCEPTION(testOperator1, std::invalid_argument); - CPPUNIT_TEST(testOperator2); - CPPUNIT_TEST_EXCEPTION(testOperator3, std::out_of_range); - CPPUNIT_TEST(testOperator4); - CPPUNIT_TEST_EXCEPTION(testOperator5, std::out_of_range); - CPPUNIT_TEST_EXCEPTION(testOperator6, std::invalid_argument); - CPPUNIT_TEST(testOutputStream1); - CPPUNIT_TEST(testOutputStream2); - CPPUNIT_TEST(testAdd1); - CPPUNIT_TEST_EXCEPTION(testAdd2, std::invalid_argument); - CPPUNIT_TEST_EXCEPTION(testAdd3, tmwserv::dal::RsColumnHeadersNotSet); - - CPPUNIT_TEST_SUITE_END(); - - - public: - /** - * Set up fixtures. - */ - void - setUp(void); - - - /** - * Tear down fixtures. - */ - void - tearDown(void); - - - /** - * Call RecordSet::rows() from an empty RecordSet. - */ - void - testRows1(void); - - - /** - * Call RecordSet::rows() from a non-empty RecordSet. - */ - void - testRows2(void); - - - /** - * Call RecordSet::cols() from an empty RecordSet. - */ - void - testCols1(void); - - - /** - * Call RecordSet::cols() from a non-empty RecordSet. - */ - void - testCols2(void); - - - /** - * Call RecordSet::isEmpty() from an empty RecordSet. - */ - void - testIsEmpty1(void); - - - /** - * Call RecordSet::isEmpty() from a non-empty RecordSet. - */ - void - testIsEmpty2(void); - - - /** - * Call RecordSet::operator() from an empty RecordSet. - */ - void - testOperator1(void); - - - /** - * Call RecordSet::operator() from a non-empty RecordSet with - * a row index and a column index as parameters. - */ - void - testOperator2(void); - - /** - * Call RecordSet::operator() from a non-empty RecordSet with - * a row index and a column index that are out of range as - * parameters. - */ - void - testOperator3(void); - - - /** - * Call RecordSet::operator() from a non-empty RecordSet with - * a row index and a field name as parameters. - */ - void - testOperator4(void); - - - /** - * Call RecordSet::operator() from a non-empty RecordSet with - * a row index that is out of range and a field name as parameters. - */ - void - testOperator5(void); - - - /** - * Call RecordSet::operator() from a non-empty RecordSet with - * a row index and a field name that does not exist as parameters. - */ - void - testOperator6(void); - - - /** - * Test writing an empty RecordSet to an output stream. - */ - void - testOutputStream1(void); - - - /** - * Test writing a non-empty RecordSet to an output stream. - */ - void - testOutputStream2(void); - - - /** - * Call RecordSet::add() to add a new now. - */ - void - testAdd1(void); - - - /** - * Call RecordSet::add() to add a new now with a different number - * of fields. - */ - void - testAdd2(void); - - - /** - * Call RecordSet::add() to add a new now to a RecordSet that does - * not have column headers. - */ - void - testAdd3(void); - - - private: - tmwserv::dal::RecordSet mEmptyRs; /**< empty recordset */ - tmwserv::dal::RecordSet mNonEmptyRs; /**< recordset with some data */ -}; - - -#endif // _TMWSERV_TEST_RECORDSET_H_ |