diff options
author | Dan Goodliffe <dan@randomdan.homeip.net> | 2018-04-09 13:01:40 +0100 |
---|---|---|
committer | Dan Goodliffe <dan@randomdan.homeip.net> | 2018-04-09 13:01:40 +0100 |
commit | d8aab9afcee2e897bbd12dc5af1e7e303a9d8ed9 (patch) | |
tree | f1b8627bea842b1ab99b48a85e4f841e3ffde3f5 | |
parent | C++17 (diff) | |
download | slicer-d8aab9afcee2e897bbd12dc5af1e7e303a9d8ed9.tar.bz2 slicer-d8aab9afcee2e897bbd12dc5af1e7e303a9d8ed9.tar.xz slicer-d8aab9afcee2e897bbd12dc5af1e7e303a9d8ed9.zip |
DB Test tidy
Move standard mock database definition into common library.
Move DB connection in a fixture.
-rw-r--r-- | slicer/db/Jamfile.jam | 6 | ||||
-rw-r--r-- | slicer/db/testInsert.cpp | 22 | ||||
-rw-r--r-- | slicer/db/testMockCommon.cpp | 14 | ||||
-rw-r--r-- | slicer/db/testMockCommon.h | 21 | ||||
-rw-r--r-- | slicer/db/testPatch.cpp | 16 | ||||
-rw-r--r-- | slicer/db/testSelect.cpp | 36 | ||||
-rw-r--r-- | slicer/db/testUpdate.cpp | 20 |
7 files changed, 60 insertions, 75 deletions
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 ] : <dependency>../tool//slicer <slicer>yes <slicerbin>../tool//slicer <implicit-dependency>../test//types <library>../test//types + <library>../test//common + <library>dbpp-postgresql + <library>dbppcore + <library>adhocutil : : <library>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 <boost/test/unit_test.hpp> #include <boost/date_time/posix_time/posix_time_io.hpp> -#include <pq-mock.h> +#include "testMockCommon.h" #include <slicer/slicer.h> -#include <definedDirs.h> #include "sqlInsertSerializer.h" #include "sqlSelectDeserializer.h" #include <types.h> @@ -25,19 +24,12 @@ namespace std { } } -class StandardMockDatabase : public DB::PluginMock<PQ::Mock> { - public: - StandardMockDatabase() : DB::PluginMock<PQ::Mock>("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<TestModule::BuiltIns>(true, 4, 16, 64, 128, 1.2, 3.4, "text"); Slicer::SerializeAny<Slicer::SqlInsertSerializer>(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<TestModule::BuiltIns>(true, 5, 17, 65, 129, 2.3, 4.5, "more text"), std::make_shared<TestModule::BuiltIns>(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<TestModule::BuiltIns>(true, 5, 17, 0, 129, 2.3, 4.5, "more text"), std::make_shared<TestModule::BuiltIns>(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<TestModule::BuiltIns>(true, 5, 17, 0, 129, 2.3, 4.5, "more text"), std::make_shared<TestModule::BuiltIns>(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<TestDatabase::BuiltIns>(true, IceUtil::None, 17, 0, 129, 2.3, 4.5, "more text"s), std::make_shared<TestDatabase::BuiltIns>(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<TestDatabase::SpecificTypes>( 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<Slicer::SqlInsertSerializer>(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 <definedDirs.h> + +StandardMockDatabase::StandardMockDatabase() : +DB::PluginMock<PQ::Mock>("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 <mockDatabase.h> +#include <pq-mock.h> +#include <visibility.h> + +class DLL_PUBLIC StandardMockDatabase : public DB::PluginMock<PQ::Mock> { + 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 <boost/test/unit_test.hpp> #include <boost/date_time/posix_time/posix_time_io.hpp> -#include <pq-mock.h> +#include "testMockCommon.h" #include <slicer/slicer.h> -#include <definedDirs.h> #include "sqlTablePatchSerializer.h" #include "sqlSelectDeserializer.h" #include <types.h> @@ -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<PQ::Mock> { - public: - StandardMockDatabase() : DB::PluginMock<PQ::Mock>("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 <boost/test/unit_test.hpp> #include <boost/date_time/posix_time/posix_time_io.hpp> -#include <pq-mock.h> +#include "testMockCommon.h" #include <slicer/slicer.h> -#include <definedDirs.h> #include "sqlSelectDeserializer.h" #include <types.h> #include <common.h> @@ -12,19 +11,12 @@ using namespace std::literals; -class StandardMockDatabase : public DB::PluginMock<PQ::Mock> { - public: - StandardMockDatabase() : DB::PluginMock<PQ::Mock>("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<Slicer::SqlSelectDeserializer, Ice::Int>(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<Slicer::SqlSelectDeserializer, Ice::Double>(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<Slicer::SqlSelectDeserializer, std::string>(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<Slicer::SqlSelectDeserializer, bool>(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<Slicer::SqlSelectDeserializer, bool>(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<TestModule::ClassMap>(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<Ice::Int>(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<Ice::Int>(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<Slicer::SqlSelectDeserializer, IceUtil::Optional<Ice::Int>>(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<IceUtil::Optional<Ice::Int>>(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<Slicer::SqlSelectDeserializer, IceUtil::Optional<Ice::Int>>(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<TestModule::BuiltInsPtr>(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<TestModule::BuiltInsPtr>(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<Slicer::SqlSelectDeserializer, TestModule::BaseSeq>(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 <boost/test/unit_test.hpp> #include <boost/date_time/posix_time/posix_time_io.hpp> -#include <pq-mock.h> +#include "testMockCommon.h" #include <slicer/slicer.h> -#include <definedDirs.h> #include "sqlInsertSerializer.h" #include "sqlSelectDeserializer.h" #include "sqlUpdateSerializer.h" @@ -14,26 +13,18 @@ using namespace std::literals; -class StandardMockDatabase : public DB::PluginMock<PQ::Mock> { - public: - StandardMockDatabase() : DB::PluginMock<PQ::Mock>("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<TestModule::BuiltIns>(false, 5, 17, 64, 129, -1.2, -1.4, "string"); BOOST_REQUIRE_THROW(Slicer::SerializeAny<Slicer::SqlUpdateSerializer>(ubi, db, "builtins"), Slicer::NoRowsFound); } BOOST_AUTO_TEST_CASE( update_builtins ) { - auto db = DB::MockDatabase::openConnectionTo("pqmock"); TestModule::BuiltInsPtr bi1 = std::make_shared<TestModule::BuiltIns>(true, 4, 16, 64, 128, 1.2, 3.4, "text1"); TestModule::BuiltInsPtr bi2 = std::make_shared<TestModule::BuiltIns>(true, 3, 15, 63, 127, 5.2, 5.4, "text2"); Slicer::SerializeAny<Slicer::SqlInsertSerializer>(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<TestModule::BuiltIns>(false, 5, 17, 64, 128, -1.2, -1.4, "string")), TestModule::BuiltInsPtr(std::make_shared<TestModule::BuiltIns>(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<Slicer::SqlSelectDeserializer, TestDatabase::BuiltInSeq>(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<Slicer::SqlUpdateSerializer>(cm, db, "converted"), Slicer::UnsupportedModelType); } +BOOST_AUTO_TEST_SUITE_END(); + |