summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDan Goodliffe <dan@randomdan.homeip.net>2015-09-24 03:02:07 +0100
committerDan Goodliffe <dan@randomdan.homeip.net>2015-09-24 03:06:06 +0100
commit0cf86f0b5592399bf7898fad5db96f023c901559 (patch)
tree194caf3c7582e88f06bad06ba40b905e6fe472af
parentAdd the core test library for testing connector implementations (diff)
downloadproject2-0cf86f0b5592399bf7898fad5db96f023c901559.tar.bz2
project2-0cf86f0b5592399bf7898fad5db96f023c901559.tar.xz
project2-0cf86f0b5592399bf7898fad5db96f023c901559.zip
PostgreSQL mocking and tests from Project2
-rw-r--r--project2/sql/sql-modPQ.cpp36
-rw-r--r--project2/sql/sql-modPQ.h20
-rw-r--r--project2/sql/unittests/pqschema.sql37
-rw-r--r--project2/sql/unittests/testpq.cpp187
4 files changed, 0 insertions, 280 deletions
diff --git a/project2/sql/sql-modPQ.cpp b/project2/sql/sql-modPQ.cpp
deleted file mode 100644
index 84a9411..0000000
--- a/project2/sql/sql-modPQ.cpp
+++ /dev/null
@@ -1,36 +0,0 @@
-#include "connectionLoader.h"
-#include "../libpqpp/connection.h"
-#include "sql-modPQ.h"
-#include <buffer.h>
-#include <scripts.h>
-#include <logger.h>
-
-typedef PQ::Connection PQConnection;
-DECLARE_GENERIC_LOADER("postgresql", ConnectionLoader, PQConnection)
-
-MockPqDatabase::MockPqDatabase(const std::string & masterdb, const std::string & name, const std::vector<boost::filesystem::path> & ss) :
- MockServerDatabase(masterdb, name, "postgresql")
-{
- CreateNewDatabase();
- PlaySchemaScripts(ss);
-}
-
-DB::Connection *
-MockPqDatabase::openConnection() const
-{
- return InstanceMap<ConnectionLoader, std::string>::Get<std::invalid_argument>("postgresql")->create(
- stringbf("user=postgres dbname=%s", testDbName));
-}
-
-MockPqDatabase::~MockPqDatabase()
-{
- DropDatabase();
-}
-
-void MockPqDatabase::DropDatabase() const
-{
- Logger()->messagebf(LOG_INFO, "Killing any active connections to database %s", testDbName);
- master->execute("SELECT pg_terminate_backend(pid) FROM pg_stat_activity WHERE datname = '" + testDbName + "'");
- MockServerDatabase::DropDatabase();
-}
-
diff --git a/project2/sql/sql-modPQ.h b/project2/sql/sql-modPQ.h
deleted file mode 100644
index 43ef1ee..0000000
--- a/project2/sql/sql-modPQ.h
+++ /dev/null
@@ -1,20 +0,0 @@
-#ifndef MOCKPQDATASOURCE_H
-#define MOCKPQDATASOURCE_H
-
-#include "mockDatabase.h"
-#include <boost/filesystem/path.hpp>
-
-class MockPqDatabase : public MockServerDatabase {
- public:
- MockPqDatabase(const std::string & master, const std::string & name, const std::vector<boost::filesystem::path> & ss);
- ~MockPqDatabase();
-
- protected:
- void DropDatabase() const override;
-
- private:
- DB::Connection * openConnection() const override;
-};
-
-#endif
-
diff --git a/project2/sql/unittests/pqschema.sql b/project2/sql/unittests/pqschema.sql
deleted file mode 100644
index 2c7e94a..0000000
--- a/project2/sql/unittests/pqschema.sql
+++ /dev/null
@@ -1,37 +0,0 @@
---
--- pg_dump style comment
--- Table: test; owner: comment: ;
---
-
-/*
- This is
- a
- multiline comment */
-
-CREATE TABLE test(
- id int,
- fl numeric(5,2),
- string text,
- boolean bool,
- dt timestamp without time zone,
- ts interval);
-
-INSERT INTO test VALUES(4, 123.45, 'some text with a ; in it and a '' too', true, '2015-04-27 23:06:03', '1 day 14:13:12');
-
-CREATE TABLE test2(
- path text not null);
-
-INSERT INTO test2 VALUES('$P2MOCKSCRIPTDIR/pqschema.sql');
-
-CREATE FUNCTION event_tsvector() RETURNS int
-LANGUAGE sql STABLE
-AS $tag$
- SELECT max(id)
- FROM test
- WHERE string != 'complex '' string;';
-$tag$;
-
-CREATE TABLE bulktest(
- id int,
- string text);
-
diff --git a/project2/sql/unittests/testpq.cpp b/project2/sql/unittests/testpq.cpp
deleted file mode 100644
index da38a34..0000000
--- a/project2/sql/unittests/testpq.cpp
+++ /dev/null
@@ -1,187 +0,0 @@
-#define BOOST_TEST_MODULE TestPQ
-#include <boost/test/unit_test.hpp>
-
-#include <mockDatasource.h>
-#include <definedDirs.h>
-#include <modifycommand.h>
-#include <selectcommand.h>
-#include <column.h>
-#include <sql-modPQ.h>
-#include "testCore.h"
-#include <sqlHandleAsVariableType.h>
-#include <fstream>
-
-class StandardMockDatabase : public MockPqDatabase {
- public:
- StandardMockDatabase() : MockPqDatabase("user=postgres dbname=postgres", "pqmock", {
- RootDir / "pqschema.sql" })
- {
- }
-};
-
-BOOST_GLOBAL_FIXTURE( StandardMockDatabase );
-
-BOOST_FIXTURE_TEST_SUITE( Core, TestCore );
-
-BOOST_AUTO_TEST_CASE( transactions )
-{
- RdbmsDataSource * ds = CommonObjects::dataSource<RdbmsDataSource>("pqmock");
- auto ro = ds->getReadonly();
-
- BOOST_REQUIRE_EQUAL(false, ro->inTx());
- ro->beginTx();
- BOOST_REQUIRE_EQUAL(true, ro->inTx());
- ro->rollbackTx();
- BOOST_REQUIRE_EQUAL(false, ro->inTx());
-
- ro->beginTx();
- BOOST_REQUIRE_EQUAL(true, ro->inTx());
- ro->commitTx();
- BOOST_REQUIRE_EQUAL(false, ro->inTx());
-
- ds->close();
-}
-
-BOOST_AUTO_TEST_CASE( bindAndSend )
-{
- RdbmsDataSource * ds = CommonObjects::dataSource<RdbmsDataSource>("pqmock");
- auto rw = ds->getWritable();
-
- auto mod = rw->newModifyCommand("INSERT INTO test VALUES(?, ?, ?, ?, ?, ?)");
- mod->bindParamI(0, testInt);
- mod->bindParamF(1, testDouble);
- mod->bindParamS(2, testString);
- mod->bindParamB(3, testBool);
- mod->bindParamT(4, testDateTime);
- mod->bindParamT(5, testInterval);
- mod->execute();
- delete mod;
- ds->commit();
- ds->close();
-}
-
-template<typename T>
-void
-assertColumnValueHelper(DB::SelectCommand & sel, const T & t)
-{
- while (sel.fetch()) {
- HandleAsVariableType h;
- sel[0].apply(h);
- BOOST_REQUIRE_EQUAL(t, h.variable.as<T>());
- }
-}
-
-template<typename T>
-void
-assertColumnValueHelper(DB::SelectCommand & sel, unsigned int col, const T & t)
-{
- HandleAsVariableType h;
- sel[col].apply(h);
- BOOST_REQUIRE_EQUAL(t, h.variable.as<T>());
-}
-
-BOOST_AUTO_TEST_CASE( bindAndSelect )
-{
- RdbmsDataSource * ds = CommonObjects::dataSource<RdbmsDataSource>("pqmock");
- auto ro = ds->getReadonly();
-
- auto select = ro->newSelectCommand("SELECT * FROM test WHERE id = ?");
- select->bindParamI(0, testInt);
- select->execute();
- int rows = 0;
- while (select->fetch()) {
- assertColumnValueHelper(*select, 0, testInt);
- assertColumnValueHelper(*select, 1, testDouble);
- assertColumnValueHelper(*select, 2, testString);
- assertColumnValueHelper(*select, 3, testBool);
- assertColumnValueHelper(*select, 4, testDateTime);
- assertColumnValueHelper(*select, 5, testInterval);
- rows += 1;
- }
- delete select;
- BOOST_REQUIRE_EQUAL(1, rows);
- ds->close();
-}
-
-BOOST_AUTO_TEST_CASE( bindAndSelectOther )
-{
- RdbmsDataSource * ds = CommonObjects::dataSource<RdbmsDataSource>("pqmock");
- auto ro = ds->getReadonly();
-
- auto select = ro->newSelectCommand("SELECT * FROM test WHERE id != ?");
- select->bindParamI(0, testInt);
- select->execute();
- int rows = 0;
- while (select->fetch()) {
- assertColumnValueHelper(*select, 0, 4);
- assertColumnValueHelper(*select, 1, 123.45);
- assertColumnValueHelper(*select, 2, std::string("some text with a ; in it and a ' too"));
- assertColumnValueHelper(*select, 3, true);
- assertColumnValueHelper(*select, 4, boost::posix_time::ptime_from_tm({ 3, 6, 23, 27, 3, 115, 0, 0, 0, 0, 0}));
- assertColumnValueHelper(*select, 5, boost::posix_time::time_duration(38, 13, 12));
- rows += 1;
- }
- delete select;
- BOOST_REQUIRE_EQUAL(1, rows);
- ds->close();
-}
-
-BOOST_AUTO_TEST_CASE( testP2MockScriptDir )
-{
- RdbmsDataSource * ds = CommonObjects::dataSource<RdbmsDataSource>("pqmock");
- auto ro = ds->getReadonly();
-
- auto select = ro->newSelectCommand("SELECT path FROM test2");
- select->execute();
- while (select->fetch()) {
- HandleAsVariableType h;
- (*select)[0].apply(h);
- BOOST_REQUIRE(boost::filesystem::exists(h.variable.as<std::string>()));
- }
- delete select;
- ds->close();
-}
-
-BOOST_AUTO_TEST_CASE( bulkload )
-{
- RdbmsDataSource * ds = CommonObjects::dataSource<RdbmsDataSource>("pqmock");
- auto ro = ds->getReadonly();
-
- auto count = ro->newSelectCommand("SELECT COUNT(*) FROM bulktest");
- // Test empty
- ro->beginBulkUpload("bulktest", "");
- ro->endBulkUpload(NULL);
- assertColumnValueHelper(*count, 0);
- // Test sample file
- ro->beginBulkUpload("bulktest", "");
- std::ifstream in((RootDir / "bulk.sample").string());
- if (!in.good()) throw std::runtime_error("Couldn't open bulk.sample");
- char buf[BUFSIZ];
- for (std::streamsize r; (r = in.readsome(buf, sizeof(buf))) > 0; ) {
- ro->bulkUploadData(buf, r);
- }
- ro->endBulkUpload(NULL);
- assertColumnValueHelper(*count, 800);
-
- delete count;
- ds->close();
-}
-
-BOOST_AUTO_TEST_CASE( bigIterate )
-{
- RdbmsDataSource * ds = CommonObjects::dataSource<RdbmsDataSource>("pqmock");
- auto ro = ds->getReadonly();
-
- auto count = ro->newSelectCommand("SELECT * FROM bulktest");
- unsigned int rows = 0;
- while (count->fetch()) {
- rows += 1;
- }
- BOOST_REQUIRE_EQUAL(800, rows);
-
- delete count;
- ds->close();
-}
-
-BOOST_AUTO_TEST_SUITE_END();
-