From d8aab9afcee2e897bbd12dc5af1e7e303a9d8ed9 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Mon, 9 Apr 2018 13:01:40 +0100 Subject: DB Test tidy Move standard mock database definition into common library. Move DB connection in a fixture. --- slicer/db/Jamfile.jam | 6 +++++- slicer/db/testInsert.cpp | 22 +++++----------------- slicer/db/testMockCommon.cpp | 14 ++++++++++++++ slicer/db/testMockCommon.h | 21 +++++++++++++++++++++ slicer/db/testPatch.cpp | 16 +++++----------- slicer/db/testSelect.cpp | 36 +++++------------------------------- slicer/db/testUpdate.cpp | 20 +++++--------------- 7 files changed, 60 insertions(+), 75 deletions(-) create mode 100644 slicer/db/testMockCommon.cpp create mode 100644 slicer/db/testMockCommon.h diff --git a/slicer/db/Jamfile.jam b/slicer/db/Jamfile.jam index 8efce48..47cf092 100644 --- a/slicer/db/Jamfile.jam +++ b/slicer/db/Jamfile.jam @@ -29,13 +29,17 @@ lib slicer-db : path-constant me : . ; lib testCommon : - [ glob testConversions.cpp test*.ice ] + [ glob testConversions.cpp testMockCommon.cpp test*.ice ] : ../tool//slicer yes ../tool//slicer ../test//types ../test//types + ../test//common + dbpp-postgresql + dbppcore + adhocutil : : adhocutil ; diff --git a/slicer/db/testInsert.cpp b/slicer/db/testInsert.cpp index 01a6246..57716e3 100644 --- a/slicer/db/testInsert.cpp +++ b/slicer/db/testInsert.cpp @@ -1,9 +1,8 @@ #define BOOST_TEST_MODULE db_insert #include #include -#include +#include "testMockCommon.h" #include -#include #include "sqlInsertSerializer.h" #include "sqlSelectDeserializer.h" #include @@ -25,19 +24,12 @@ namespace std { } } -class StandardMockDatabase : public DB::PluginMock { - public: - StandardMockDatabase() : DB::PluginMock("user=postgres dbname=postgres", "pqmock", { - rootDir.parent_path() / "db" / "slicer.sql" }) - { - } -}; - BOOST_GLOBAL_FIXTURE( StandardMockDatabase ); +BOOST_FIXTURE_TEST_SUITE(db, ConnectionFixture); + BOOST_AUTO_TEST_CASE( insert_builtins ) { - auto db = DB::MockDatabase::openConnectionTo("pqmock"); TestModule::BuiltInsPtr bi = std::make_shared(true, 4, 16, 64, 128, 1.2, 3.4, "text"); Slicer::SerializeAny(bi, db, "builtins"); auto sel = db->select("SELECT * FROM builtins"); @@ -54,7 +46,6 @@ BOOST_AUTO_TEST_CASE( insert_builtins ) BOOST_AUTO_TEST_CASE( insert_seq_builtins ) { - auto db = DB::MockDatabase::openConnectionTo("pqmock"); TestModule::BuiltInSeq bis = { std::make_shared(true, 5, 17, 65, 129, 2.3, 4.5, "more text"), std::make_shared(true, 6, 18, 66, 130, 3.4, 5.6, "even more text") @@ -75,7 +66,6 @@ BOOST_AUTO_TEST_CASE( insert_seq_builtins ) BOOST_AUTO_TEST_CASE( autoinsert_seq_builtins ) { - auto db = DB::MockDatabase::openConnectionTo("pqmock"); TestModule::BuiltInSeq bis = { std::make_shared(true, 5, 17, 0, 129, 2.3, 4.5, "more text"), std::make_shared(true, 6, 18, 0, 130, 3.4, 5.6, "even more text") @@ -99,7 +89,6 @@ BOOST_AUTO_TEST_CASE( autoinsert_seq_builtins ) BOOST_AUTO_TEST_CASE( fetchinsert_seq_builtins ) { - auto db = DB::MockDatabase::openConnectionTo("pqmock"); TestModule::BuiltInSeq bis = { std::make_shared(true, 5, 17, 0, 129, 2.3, 4.5, "more text"), std::make_shared(true, 6, 18, 0, 130, 3.4, 5.6, "even more text") @@ -123,7 +112,6 @@ BOOST_AUTO_TEST_CASE( fetchinsert_seq_builtins ) BOOST_AUTO_TEST_CASE( fetchinsert_seq_builtinsWithNulls ) { - auto db = DB::MockDatabase::openConnectionTo("pqmock"); TestDatabase::BuiltInSeq bis = { std::make_shared(true, IceUtil::None, 17, 0, 129, 2.3, 4.5, "more text"s), std::make_shared(true, 6, 18, 0, 130, 3.4, IceUtil::None, "even more text"s) @@ -147,7 +135,6 @@ BOOST_AUTO_TEST_CASE( fetchinsert_seq_builtinsWithNulls ) BOOST_AUTO_TEST_CASE( insert_converted ) { - auto db = DB::MockDatabase::openConnectionTo("pqmock"); TestDatabase::SpecificTypesPtr st = std::make_shared( TestModule::DateTime {2015, 10, 16, 19, 12, 34}, TestModule::IsoDate {2015, 10, 16}, @@ -166,8 +153,9 @@ BOOST_AUTO_TEST_CASE( insert_converted ) BOOST_AUTO_TEST_CASE( insert_unsupportedModel ) { - auto db = DB::MockDatabase::openConnectionTo("pqmock"); TestModule::ClassMap cm; BOOST_REQUIRE_THROW(Slicer::SerializeAny(cm, db, "converted"), Slicer::UnsupportedModelType); } +BOOST_AUTO_TEST_SUITE_END(); + diff --git a/slicer/db/testMockCommon.cpp b/slicer/db/testMockCommon.cpp new file mode 100644 index 0000000..8053b3e --- /dev/null +++ b/slicer/db/testMockCommon.cpp @@ -0,0 +1,14 @@ +#include "testMockCommon.h" +#include + +StandardMockDatabase::StandardMockDatabase() : +DB::PluginMock("user=postgres dbname=postgres", "pqmock", { + rootDir.parent_path() / "db" / "slicer.sql" }) +{ +} + +ConnectionFixture::ConnectionFixture() : + db(DB::MockDatabase::openConnectionTo("pqmock")) +{ +} + diff --git a/slicer/db/testMockCommon.h b/slicer/db/testMockCommon.h new file mode 100644 index 0000000..e55824f --- /dev/null +++ b/slicer/db/testMockCommon.h @@ -0,0 +1,21 @@ +#ifndef SLICER_DB_MOCKDB_H +#define SLICER_DB_MOCKDB_H + +#include +#include +#include + +class DLL_PUBLIC StandardMockDatabase : public DB::PluginMock { + public: + StandardMockDatabase(); +}; + +class DLL_PUBLIC ConnectionFixture { + public: + ConnectionFixture(); + + DB::ConnectionPtr db; +}; + +#endif + diff --git a/slicer/db/testPatch.cpp b/slicer/db/testPatch.cpp index ef94823..e9dda4d 100644 --- a/slicer/db/testPatch.cpp +++ b/slicer/db/testPatch.cpp @@ -1,9 +1,8 @@ #define BOOST_TEST_MODULE db_patch #include #include -#include +#include "testMockCommon.h" #include -#include #include "sqlTablePatchSerializer.h" #include "sqlSelectDeserializer.h" #include @@ -17,19 +16,12 @@ BOOST_TEST_DONT_PRINT_LOG_VALUE(TestDatabase::Timespan); BOOST_TEST_DONT_PRINT_LOG_VALUE(DB::PrimaryKey); // LCOV_EXCL_STOP -class StandardMockDatabase : public DB::PluginMock { - public: - StandardMockDatabase() : DB::PluginMock("user=postgres dbname=postgres", "pqmock", { - rootDir.parent_path() / "db" / "slicer.sql" }) - { - } -}; - BOOST_GLOBAL_FIXTURE( StandardMockDatabase ); +BOOST_FIXTURE_TEST_SUITE(db, ConnectionFixture); + BOOST_AUTO_TEST_CASE( insert_builtins ) { - auto db = DB::MockDatabase::openConnectionTo("pqmock"); TestModule::BuiltInSeq bis = { TestModule::BuiltInsPtr(new TestModule::BuiltIns(true, 5, 17, 0, 129, 2.3, 4.5, "more text")), TestModule::BuiltInsPtr(new TestModule::BuiltIns(true, 6, 18, 0, 130, 3.4, 5.6, "even more text")) @@ -49,3 +41,5 @@ BOOST_AUTO_TEST_CASE( insert_builtins ) BOOST_REQUIRE_EQUAL(cols, tp.cols); } +BOOST_AUTO_TEST_SUITE_END(); + diff --git a/slicer/db/testSelect.cpp b/slicer/db/testSelect.cpp index 9ea9139..da4b457 100644 --- a/slicer/db/testSelect.cpp +++ b/slicer/db/testSelect.cpp @@ -1,9 +1,8 @@ #define BOOST_TEST_MODULE db_select #include #include -#include +#include "testMockCommon.h" #include -#include #include "sqlSelectDeserializer.h" #include #include @@ -12,19 +11,12 @@ using namespace std::literals; -class StandardMockDatabase : public DB::PluginMock { - public: - StandardMockDatabase() : DB::PluginMock("user=postgres dbname=postgres", "pqmock", { - rootDir.parent_path() / "db" / "slicer.sql" }) - { - } -}; - BOOST_GLOBAL_FIXTURE( StandardMockDatabase ); +BOOST_FIXTURE_TEST_SUITE(db, ConnectionFixture); + BOOST_AUTO_TEST_CASE( select_simple_int ) { - auto db = DB::MockDatabase::openConnectionTo("pqmock"); auto sel = db->select("SELECT MAX(id) FROM test"); auto bi = Slicer::DeserializeAny(sel); BOOST_REQUIRE_EQUAL(4, bi); @@ -32,7 +24,6 @@ BOOST_AUTO_TEST_CASE( select_simple_int ) BOOST_AUTO_TEST_CASE( select_simple_double ) { - auto db = DB::MockDatabase::openConnectionTo("pqmock"); auto sel = db->select("SELECT MAX(fl) FROM test"); auto bi = Slicer::DeserializeAny(sel); BOOST_REQUIRE_CLOSE(1234.1234, bi, 0.0001); @@ -40,7 +31,6 @@ BOOST_AUTO_TEST_CASE( select_simple_double ) BOOST_AUTO_TEST_CASE( select_simple_string ) { - auto db = DB::MockDatabase::openConnectionTo("pqmock"); auto sel = db->select("SELECT MAX(string) FROM test"); auto bi = Slicer::DeserializeAny(sel); BOOST_REQUIRE_EQUAL("text two", bi); @@ -48,7 +38,6 @@ BOOST_AUTO_TEST_CASE( select_simple_string ) BOOST_AUTO_TEST_CASE( select_simple_true ) { - auto db = DB::MockDatabase::openConnectionTo("pqmock"); auto sel = db->select("SELECT true"); auto bi = Slicer::DeserializeAny(sel); BOOST_REQUIRE_EQUAL(true, bi); @@ -56,7 +45,6 @@ BOOST_AUTO_TEST_CASE( select_simple_true ) BOOST_AUTO_TEST_CASE( select_simple_false ) { - auto db = DB::MockDatabase::openConnectionTo("pqmock"); auto sel = db->select("SELECT NOT(true)"); auto bi = Slicer::DeserializeAny(sel); BOOST_REQUIRE_EQUAL(false, bi); @@ -64,7 +52,6 @@ BOOST_AUTO_TEST_CASE( select_simple_false ) BOOST_AUTO_TEST_CASE( select_single ) { - auto db = DB::MockDatabase::openConnectionTo("pqmock"); auto sel = db->select( "SELECT boolean mbool, \ id mbyte, id mshort, id mint, id mlong, \ @@ -87,7 +74,6 @@ BOOST_AUTO_TEST_CASE( select_single ) BOOST_AUTO_TEST_CASE( select_inherit_single ) { - auto db = DB::MockDatabase::openConnectionTo("pqmock"); auto sel = db->select( "SELECT id a, '::TestModule::D' || CAST(id AS TEXT) tc, 200 b, 300 c, 400 d \ FROM test \ @@ -102,7 +88,6 @@ BOOST_AUTO_TEST_CASE( select_inherit_single ) BOOST_AUTO_TEST_CASE( select_simple_sequence ) { - auto db = DB::MockDatabase::openConnectionTo("pqmock"); auto sel = db->select( "SELECT string \ FROM test \ @@ -117,7 +102,6 @@ BOOST_AUTO_TEST_CASE( select_simple_sequence ) BOOST_AUTO_TEST_CASE( select_inherit_sequence ) { - auto db = DB::MockDatabase::openConnectionTo("pqmock"); auto sel = db->select( "SELECT id a, '::TestModule::D' || CAST(id AS TEXT) tc, 200 b, 300 c, 400 d \ FROM test \ @@ -142,7 +126,6 @@ BOOST_AUTO_TEST_CASE( select_inherit_sequence ) BOOST_AUTO_TEST_CASE( select_inherit_datetime ) { - auto db = DB::MockDatabase::openConnectionTo("pqmock"); auto sel = db->select( "SELECT dt, to_char(dt, 'YYYY-MM-DD') date, ts \ FROM test \ @@ -172,28 +155,24 @@ BoostThrowWrapperHelper(P && ... p) BOOST_AUTO_TEST_CASE( select_unsupportedModel ) { - auto db = DB::MockDatabase::openConnectionTo("pqmock"); auto sel = db->select("SELECT id FROM test"); BOOST_REQUIRE_THROW(BoostThrowWrapperHelper(sel), Slicer::UnsupportedModelType); } BOOST_AUTO_TEST_CASE( select_tooManyRowsSimple ) { - auto db = DB::MockDatabase::openConnectionTo("pqmock"); auto sel = db->select("SELECT id FROM test"); BOOST_REQUIRE_THROW(BoostThrowWrapperHelper(sel), Slicer::TooManyRowsReturned); } BOOST_AUTO_TEST_CASE( select_noRowsSimple ) { - auto db = DB::MockDatabase::openConnectionTo("pqmock"); auto sel = db->select("SELECT id FROM test WHERE false"); BOOST_REQUIRE_THROW(BoostThrowWrapperHelper(sel), Slicer::NoRowsReturned); } BOOST_AUTO_TEST_CASE( select_noRowsSimpleOptional ) { - auto db = DB::MockDatabase::openConnectionTo("pqmock"); auto sel = db->select("SELECT id FROM test WHERE false"); auto v = Slicer::DeserializeAny>(sel); BOOST_REQUIRE(!v); @@ -201,14 +180,12 @@ BOOST_AUTO_TEST_CASE( select_noRowsSimpleOptional ) BOOST_AUTO_TEST_CASE( select_tooManyRowsSimpleOptional ) { - auto db = DB::MockDatabase::openConnectionTo("pqmock"); auto sel = db->select("SELECT id FROM test"); BOOST_REQUIRE_THROW(BoostThrowWrapperHelper>(sel), Slicer::TooManyRowsReturned); } BOOST_AUTO_TEST_CASE( select_simpleOptional ) { - auto db = DB::MockDatabase::openConnectionTo("pqmock"); auto sel = db->select("SELECT MAX(id) FROM test"); auto v = Slicer::DeserializeAny>(sel); BOOST_REQUIRE(v); @@ -217,7 +194,6 @@ BOOST_AUTO_TEST_CASE( select_simpleOptional ) BOOST_AUTO_TEST_CASE( select_noRowsComplexOptional ) { - auto db = DB::MockDatabase::openConnectionTo("pqmock"); auto sel = db->select( "SELECT boolean mbool, \ id mbyte, id mshort, id mint, id mlong, \ @@ -231,21 +207,18 @@ BOOST_AUTO_TEST_CASE( select_noRowsComplexOptional ) BOOST_AUTO_TEST_CASE( select_tooManyRowsComplex ) { - auto db = DB::MockDatabase::openConnectionTo("pqmock"); auto sel = db->select("SELECT id FROM test"); BOOST_REQUIRE_THROW(BoostThrowWrapperHelper(sel), Slicer::TooManyRowsReturned); } BOOST_AUTO_TEST_CASE( select_noRowsComplex ) { - auto db = DB::MockDatabase::openConnectionTo("pqmock"); auto sel = db->select("SELECT id FROM test WHERE false"); BOOST_REQUIRE_THROW(BoostThrowWrapperHelper(sel), Slicer::NoRowsReturned); } BOOST_AUTO_TEST_CASE( select_emptySequence ) { - auto db = DB::MockDatabase::openConnectionTo("pqmock"); auto sel = db->select("SELECT id FROM test WHERE false"); auto bi = Slicer::DeserializeAny(sel); BOOST_REQUIRE_EQUAL(0, bi.size()); @@ -253,7 +226,6 @@ BOOST_AUTO_TEST_CASE( select_emptySequence ) BOOST_AUTO_TEST_CASE( select_null ) { - auto db = DB::MockDatabase::openConnectionTo("pqmock"); db->execute("INSERT INTO test(id) VALUES(NULL)"); auto sel = db->select("SELECT id optSimple FROM test WHERE id IS NULL"); @@ -270,3 +242,5 @@ BOOST_AUTO_TEST_CASE( select_null ) BOOST_REQUIRE(!v); } +BOOST_AUTO_TEST_SUITE_END(); + diff --git a/slicer/db/testUpdate.cpp b/slicer/db/testUpdate.cpp index c33e645..2f6d821 100644 --- a/slicer/db/testUpdate.cpp +++ b/slicer/db/testUpdate.cpp @@ -1,9 +1,8 @@ #define BOOST_TEST_MODULE db_update #include #include -#include +#include "testMockCommon.h" #include -#include #include "sqlInsertSerializer.h" #include "sqlSelectDeserializer.h" #include "sqlUpdateSerializer.h" @@ -14,26 +13,18 @@ using namespace std::literals; -class StandardMockDatabase : public DB::PluginMock { - public: - StandardMockDatabase() : DB::PluginMock("user=postgres dbname=postgres", "pqmock", { - rootDir.parent_path() / "db" / "slicer.sql" }) - { - } -}; - BOOST_GLOBAL_FIXTURE( StandardMockDatabase ); +BOOST_FIXTURE_TEST_SUITE(db, ConnectionFixture); + BOOST_AUTO_TEST_CASE( update_builtinsNotFound ) { - auto db = DB::MockDatabase::openConnectionTo("pqmock"); TestModule::BuiltInsPtr ubi = std::make_shared(false, 5, 17, 64, 129, -1.2, -1.4, "string"); BOOST_REQUIRE_THROW(Slicer::SerializeAny(ubi, db, "builtins"), Slicer::NoRowsFound); } BOOST_AUTO_TEST_CASE( update_builtins ) { - auto db = DB::MockDatabase::openConnectionTo("pqmock"); TestModule::BuiltInsPtr bi1 = std::make_shared(true, 4, 16, 64, 128, 1.2, 3.4, "text1"); TestModule::BuiltInsPtr bi2 = std::make_shared(true, 3, 15, 63, 127, 5.2, 5.4, "text2"); Slicer::SerializeAny(bi1, db, "builtins"); @@ -65,7 +56,6 @@ BOOST_AUTO_TEST_CASE( update_builtins ) BOOST_AUTO_TEST_CASE( update_builtins_seq ) { - auto db = DB::MockDatabase::openConnectionTo("pqmock"); TestModule::BuiltInSeq ubis { TestModule::BuiltInsPtr(std::make_shared(false, 5, 17, 64, 128, -1.2, -1.4, "string")), TestModule::BuiltInsPtr(std::make_shared(false, 5, 21, 63, 127, -4.2, -5.4, "string updated")) @@ -95,7 +85,6 @@ BOOST_AUTO_TEST_CASE( update_builtins_seq ) BOOST_AUTO_TEST_CASE( update_withNulls ) { - auto db = DB::MockDatabase::openConnectionTo("pqmock"); auto sel = db->select("SELECT * FROM builtins ORDER BY mint"); auto bis = Slicer::DeserializeAny(sel); BOOST_REQUIRE_EQUAL(2, bis.size()); @@ -120,8 +109,9 @@ BOOST_AUTO_TEST_CASE( update_withNulls ) BOOST_AUTO_TEST_CASE( update_unsupportedModel ) { - auto db = DB::MockDatabase::openConnectionTo("pqmock"); TestModule::ClassMap cm; BOOST_REQUIRE_THROW(Slicer::SerializeAny(cm, db, "converted"), Slicer::UnsupportedModelType); } +BOOST_AUTO_TEST_SUITE_END(); + -- cgit v1.2.3