unit testing a c database application with unit testing a
play

Unit Testing a C++ Database Application with Unit Testing a C++ - PowerPoint PPT Presentation

Unit Testing a C++ Database Application with Unit Testing a C++ Database Application with Unit Testing a C++ Database Application with Unit Testing a C++ Database Application with Mock Objects Mock Objects Mock Objects Mock Objects Ray


  1. Unit Testing a C++ Database Application with Unit Testing a C++ Database Application with Unit Testing a C++ Database Application with Unit Testing a C++ Database Application with Mock Objects Mock Objects Mock Objects Mock Objects Ray Lischner Ray Lischner Ray Lischner Ray Lischner PNSQC PNSQC PNSQC PNSQC October 11, 2011 October 11, 2011 October 11, 2011 October 11, 2011 ___________________________________________________________________________________________________________________________ Copies may not be made or distributed for commercial use Excerpt from PNSQC 2011 PNSQC.ORG 1

  2. Problem Problem  Write a library to support simple applications  Write a library to support simple applications  Write a library to support simple applications  Write a library to support simple applications that need to access a SQL database that need to access a SQL database that need to access a SQL database that need to access a SQL database  Must support unit-testing of applications  Must support unit-testing of applications  Must support unit-testing of applications  Must support unit-testing of applications  Must have a C++ API  Must have a C++ API  Must have a C++ API  Must have a C++ API  Must be easy to write, support  Must be easy to write, support  Must be easy to write, support  Must be easy to write, support ___________________________________________________________________________________________________________________________ Copies may not be made or distributed for commercial use Excerpt from PNSQC 2011 PNSQC.ORG 2

  3. Typical Usage (Simplified) Typical Usage (Simplified) struct Name { std::string first, last }; struct Name { std::string first, last }; struct Name { std::string first, last }; struct Name { std::string first, last }; typedef std::vector<Name> Names; typedef std::vector<Name> Names; typedef std::vector<Name> Names; typedef std::vector<Name> Names; class DbApp class DbApp class DbApp class DbApp { { { { public: public: public: public: DbApp(db::sql* sql); DbApp(db::sql* sql); DbApp(db::sql* sql); DbApp(db::sql* sql); void get_names(std::string last_name, Names& name); void get_names(std::string last_name, Names& name); void get_names(std::string last_name, Names& name); void get_names(std::string last_name, Names& name); private: private: private: private: static std::string query_; static std::string query_; static std::string query_; static std::string query_; std::auto_ptr<db::sql> sql_; std::auto_ptr<db::sql> sql_; std::auto_ptr<db::sql> sql_; std::auto_ptr<db::sql> sql_; std::auto_ptr<db::statement> stmt_; std::auto_ptr<db::statement> stmt_; std::auto_ptr<db::statement> stmt_; std::auto_ptr<db::statement> stmt_; } } } } ___________________________________________________________________________________________________________________________ Copies may not be made or distributed for commercial use Excerpt from PNSQC 2011 PNSQC.ORG 3

  4. Typical Usage (Simplified) Typical Usage (Simplified) std::string DbApp::query_( std::string DbApp::query_( std::string DbApp::query_( std::string DbApp::query_( "SELECT * FROM names WHERE last LIKE ?"); "SELECT * FROM names WHERE last LIKE ?"); "SELECT * FROM names WHERE last LIKE ?"); "SELECT * FROM names WHERE last LIKE ?"); DbApp::DbApp(db::sql* sql): sql_(sql), DbApp::DbApp(db::sql* sql): sql_(sql), DbApp::DbApp(db::sql* sql): sql_(sql), DbApp::DbApp(db::sql* sql): sql_(sql), stmt_(sql->prepare(query_)) stmt_(sql->prepare(query_)) stmt_(sql->prepare(query_)) stmt_(sql->prepare(query_)) {} {} {} {} void DbApp::get_names(std::string last_name, Names& names) { void DbApp::get_names(std::string last_name, Names& names) { void DbApp::get_names(std::string last_name, Names& names) { void DbApp::get_names(std::string last_name, Names& names) { stmt_->bind_param(last_name); stmt_->bind_param(last_name); stmt_->bind_param(last_name); stmt_->bind_param(last_name); std::string first, last; std::string first, last; std::string first, last; std::string first, last; stmt_->bind_result(first, last); stmt_->bind_result(first, last); stmt_->bind_result(first, last); stmt_->bind_result(first, last); stmt_->execute(); stmt_->execute(); stmt_->execute(); stmt_->execute(); while (stmt_->fetch()) while (stmt_->fetch()) while (stmt_->fetch()) while (stmt_->fetch()) names.push_back(Name(first, last)); names.push_back(Name(first, last)); names.push_back(Name(first, last)); names.push_back(Name(first, last)); } } } } ___________________________________________________________________________________________________________________________ Copies may not be made or distributed for commercial use Excerpt from PNSQC 2011 PNSQC.ORG 4

  5. Typical Unit Test Typical Unit Test db::mock::sql sql; db::mock::sql sql; db::mock::sql sql; db::mock::sql sql; s.store_result(DbApp::query_, "Ray", "Lischner"); s.store_result(DbApp::query_, "Ray", "Lischner"); s.store_result(DbApp::query_, "Ray", "Lischner"); s.store_result(DbApp::query_, "Ray", "Lischner"); s.store_result(DbApp::query_, "Cheryl", "Klipp"); s.store_result(DbApp::query_, "Cheryl", "Klipp"); s.store_result(DbApp::query_, "Cheryl", "Klipp"); s.store_result(DbApp::query_, "Cheryl", "Klipp"); DbApp dbapp(&sql); DbApp dbapp(&sql); DbApp dbapp(&sql); DbApp dbapp(&sql); Names names; Names names; Names names; Names names; dbapp.get_names("name", names); dbapp.get_names("name", names); dbapp.get_names("name", names); dbapp.get_names("name", names); BOOST_CHECK_EQUAL(std::size_t(2), names); BOOST_CHECK_EQUAL(std::size_t(2), names); BOOST_CHECK_EQUAL(std::size_t(2), names); BOOST_CHECK_EQUAL(std::size_t(2), names); BOOST_CHECK_EQUAL(std::string("Ray"), names.at(0).first); BOOST_CHECK_EQUAL(std::string("Ray"), names.at(0).first); BOOST_CHECK_EQUAL(std::string("Ray"), names.at(0).first); BOOST_CHECK_EQUAL(std::string("Ray"), names.at(0).first); BOOST_CHECK_EQUAL(std::string("Cheryl"), BOOST_CHECK_EQUAL(std::string("Cheryl"), BOOST_CHECK_EQUAL(std::string("Cheryl"), BOOST_CHECK_EQUAL(std::string("Cheryl"), names.at(1).first); names.at(1).first); names.at(1).first); names.at(1).first); BOOST_CHECK_EQUAL(std::string("Lischner"), BOOST_CHECK_EQUAL(std::string("Lischner"), BOOST_CHECK_EQUAL(std::string("Lischner"), BOOST_CHECK_EQUAL(std::string("Lischner"), names.at(0).last); names.at(0).last); names.at(0).last); names.at(0).last); BOOST_CHECK_EQUAL(std::string("Klipp"), names.at(1).last); BOOST_CHECK_EQUAL(std::string("Klipp"), names.at(1).last); BOOST_CHECK_EQUAL(std::string("Klipp"), names.at(1).last); BOOST_CHECK_EQUAL(std::string("Klipp"), names.at(1).last); ___________________________________________________________________________________________________________________________ Copies may not be made or distributed for commercial use Excerpt from PNSQC 2011 PNSQC.ORG 5

  6. Unit Test Error Cases Unit Test Error Cases db::mock::sql sql; db::mock::sql sql; db::mock::sql sql; db::mock::sql sql; sql.store_result(DbApp::query_, db::mock::error); sql.store_result(DbApp::query_, db::mock::error); sql.store_result(DbApp::query_, db::mock::error); sql.store_result(DbApp::query_, db::mock::error); DbApp dbapp(&sql); DbApp dbapp(&sql); DbApp dbapp(&sql); DbApp dbapp(&sql); Names names; Names names; Names names; Names names; BOOST_CHECK_THROW(dbapp.get_names("name", names), BOOST_CHECK_THROW(dbapp.get_names("name", names), BOOST_CHECK_THROW(dbapp.get_names("name", names), BOOST_CHECK_THROW(dbapp.get_names("name", names), db::exception); db::exception); db::exception); db::exception); ___________________________________________________________________________________________________________________________ Copies may not be made or distributed for commercial use Excerpt from PNSQC 2011 PNSQC.ORG 6

  7. API API  sql  sql  sql  sql void connect(connection parameters) void connect(connection parameters) void connect(connection parameters) void connect(connection parameters)     void execute(query) void execute(query) void execute(query) void execute(query)     statement* prepare(query) statement* prepare(query) statement* prepare(query) statement* prepare(query)      statement  statement  statement  statement void bind_param(...) void bind_param(...) void bind_param(...) void bind_param(...)     void bind_result(...) void bind_result(...) void bind_result(...) void bind_result(...)     void execute() void execute() void execute() void execute()     bool fetch() bool fetch() bool fetch() bool fetch()     ___________________________________________________________________________________________________________________________ Copies may not be made or distributed for commercial use Excerpt from PNSQC 2011 PNSQC.ORG 7

Recommend


More recommend