diff options
Diffstat (limited to 'libdbpp/unittests')
-rw-r--r-- | libdbpp/unittests/Jamfile.jam | 17 | ||||
-rw-r--r-- | libdbpp/unittests/patch.sql | 19 | ||||
-rw-r--r-- | libdbpp/unittests/source.dat | 4 | ||||
-rw-r--r-- | libdbpp/unittests/target.dat | 4 | ||||
-rw-r--r-- | libdbpp/unittests/testPatch.cpp | 178 |
5 files changed, 222 insertions, 0 deletions
diff --git a/libdbpp/unittests/Jamfile.jam b/libdbpp/unittests/Jamfile.jam index aa6c195..08bd027 100644 --- a/libdbpp/unittests/Jamfile.jam +++ b/libdbpp/unittests/Jamfile.jam @@ -24,6 +24,23 @@ run ; run + testPatch.cpp + : : + patch.sql + source.dat + target.dat + : + <define>ROOT=\"$(me)\" + <define>BOOST_TEST_DYN_LINK + <library>..//dbppcore + <library>..//adhocutil + <library>../../libpqpp//dbpp-postgresql + <library>boost_utf + : + testPatch + ; + +run testConnectionPool.cpp : : : <define>ROOT=\"$(me)\" diff --git a/libdbpp/unittests/patch.sql b/libdbpp/unittests/patch.sql new file mode 100644 index 0000000..8cedebb --- /dev/null +++ b/libdbpp/unittests/patch.sql @@ -0,0 +1,19 @@ +CREATE TABLE source( + a integer, + b integer, + c text, + d text, + PRIMARY KEY(a, b)); + +CREATE TABLE target( + a integer, + b integer, + c text, + d text, + deleted boolean not null default(false), + PRIMARY KEY(a, b)); + +CREATE UNIQUE INDEX u ON target(a, b) WHERE NOT deleted; +COPY source(a, b, c, d) FROM '$SCRIPTDIR/source.dat'; +COPY target(a, b, c, d) FROM '$SCRIPTDIR/target.dat'; + diff --git a/libdbpp/unittests/source.dat b/libdbpp/unittests/source.dat new file mode 100644 index 0000000..03bbdc8 --- /dev/null +++ b/libdbpp/unittests/source.dat @@ -0,0 +1,4 @@ +1 1 one one +1 2 onev2 twov2 +3 1 three one +1 3 one three diff --git a/libdbpp/unittests/target.dat b/libdbpp/unittests/target.dat new file mode 100644 index 0000000..75621f1 --- /dev/null +++ b/libdbpp/unittests/target.dat @@ -0,0 +1,4 @@ +1 1 one one +1 2 one two +2 2 two two +2 1 two one diff --git a/libdbpp/unittests/testPatch.cpp b/libdbpp/unittests/testPatch.cpp new file mode 100644 index 0000000..3295072 --- /dev/null +++ b/libdbpp/unittests/testPatch.cpp @@ -0,0 +1,178 @@ +#define BOOST_TEST_MODULE DbTablePatch +#include <boost/test/unit_test.hpp> + +#include <connection.h> +#include <definedDirs.h> +#include <mock.h> +#include <command.h> +#include <buffer.h> + +class Mock : public PQ::Mock { + public: + Mock() : + PQ::Mock("user=postgres dbname=postgres", "pqmock", { rootDir / "patch.sql" }) + { + } +}; + +class OrderByA : public DB::SqlWriter { + public: + void writeSql(AdHoc::Buffer & b) + { + b.append("a"); + } +}; + +class WhereAequals1 : public DB::SqlWriter { + public: + void writeSql(AdHoc::Buffer & b) + { + b.append("a.a = ?"); + } + void bindParams(DB::Command * cmd, unsigned int & o) + { + cmd->bindParamI(o++, 1); + } +}; + +class MarkDeleted : public DB::SqlWriter { + public: + void writeSql(AdHoc::Buffer & b) + { + b.append("deleted = ?"); + } + void bindParams(DB::Command * cmd, unsigned int & o) + { + cmd->bindParamB(o++, true); + } +}; + +BOOST_FIXTURE_TEST_SUITE(mock, Mock); + +BOOST_AUTO_TEST_CASE( sanityFail ) +{ + auto db = DB::ConnectionPtr(DB::MockDatabase::openConnectionTo("pqmock")); + BOOST_REQUIRE(db); + DB::TablePatch tp; + tp.src = "source"; + tp.dest = "target"; + tp.cols = {"a", "b", "c", "d"}; + BOOST_REQUIRE_THROW(db->patchTable(&tp), DB::PatchCheckFailure); +} + +BOOST_AUTO_TEST_CASE( noTx ) +{ + auto db = DB::ConnectionPtr(DB::MockDatabase::openConnectionTo("pqmock")); + BOOST_REQUIRE(db); + DB::TablePatch tp; + tp.src = "source"; + tp.dest = "target"; + tp.cols = {"a", "b", "c", "d"}; + tp.pk = {"a", "b"}; + BOOST_REQUIRE_THROW(db->patchTable(&tp), DB::TransactionRequired); +} + +BOOST_AUTO_TEST_SUITE_END(); + +BOOST_AUTO_TEST_CASE( testBasic ) +{ + Mock mock; + auto db = DB::ConnectionPtr(DB::MockDatabase::openConnectionTo("pqmock")); + BOOST_REQUIRE(db); + DB::TablePatch tp; + tp.src = "source"; + tp.dest = "target"; + tp.cols = {"a", "b", "c", "d"}; + tp.pk = {"a", "b"}; + db->beginTx(); + auto r = db->patchTable(&tp); + db->commitTx(); + BOOST_REQUIRE_EQUAL(2, r.deletes); + BOOST_REQUIRE_EQUAL(2, r.inserts); + BOOST_REQUIRE_EQUAL(1, r.updates); + db->beginTx(); + auto r2 = db->patchTable(&tp); + db->commitTx(); + BOOST_REQUIRE_EQUAL(0, r2.deletes); + BOOST_REQUIRE_EQUAL(0, r2.inserts); + BOOST_REQUIRE_EQUAL(0, r2.updates); +} + +BOOST_AUTO_TEST_CASE( allKeys ) +{ + Mock mock; + auto db = DB::ConnectionPtr(DB::MockDatabase::openConnectionTo("pqmock")); + BOOST_REQUIRE(db); + DB::TablePatch tp; + tp.src = "source"; + tp.dest = "target"; + tp.cols = {"a", "b"}; + tp.pk = {"a", "b"}; + db->beginTx(); + auto r = db->patchTable(&tp); + db->commitTx(); + BOOST_REQUIRE_EQUAL(2, r.deletes); + BOOST_REQUIRE_EQUAL(2, r.inserts); + BOOST_REQUIRE_EQUAL(0, r.updates); +} + +BOOST_AUTO_TEST_CASE( testOrder ) +{ + Mock mock; + auto db = DB::ConnectionPtr(DB::MockDatabase::openConnectionTo("pqmock")); + BOOST_REQUIRE(db); + DB::TablePatch tp; + OrderByA order; + tp.src = "source"; + tp.dest = "target"; + tp.cols = {"a", "b", "c", "d"}; + tp.pk = {"a", "b"}; + tp.order = ℴ + db->beginTx(); + auto r = db->patchTable(&tp); + db->commitTx(); + BOOST_REQUIRE_EQUAL(2, r.deletes); + BOOST_REQUIRE_EQUAL(2, r.inserts); + BOOST_REQUIRE_EQUAL(1, r.updates); +} + +BOOST_AUTO_TEST_CASE( testWhere ) +{ + Mock mock; + auto db = DB::ConnectionPtr(DB::MockDatabase::openConnectionTo("pqmock")); + BOOST_REQUIRE(db); + DB::TablePatch tp; + WhereAequals1 where; + tp.src = "source"; + tp.dest = "target"; + tp.cols = {"a", "b", "c", "d"}; + tp.pk = {"a", "b"}; + tp.where = &where; + db->beginTx(); + auto r = db->patchTable(&tp); + db->commitTx(); + BOOST_REQUIRE_EQUAL(0, r.deletes); + BOOST_REQUIRE_EQUAL(2, r.inserts); + BOOST_REQUIRE_EQUAL(1, r.updates); +} + +BOOST_AUTO_TEST_CASE( testInstead ) +{ + Mock mock; + auto db = DB::ConnectionPtr(DB::MockDatabase::openConnectionTo("pqmock")); + BOOST_REQUIRE(db); + DB::TablePatch tp; + MarkDeleted mark; + tp.src = "source"; + tp.dest = "target"; + tp.cols = {"a", "b", "c", "d"}; + tp.pk = {"a", "b"}; + tp.insteadOfDelete = &mark; + db->beginTx(); + auto r = db->patchTable(&tp); + db->commitTx(); + BOOST_REQUIRE_EQUAL(2, r.deletes); + BOOST_REQUIRE_EQUAL(2, r.inserts); + BOOST_REQUIRE_EQUAL(1, r.updates); +} + |