#define BOOST_TEST_MODULE db_select #include #include #include #include #include #include "sqlSelectDeserializer.h" #include #include "exceptions.h" class StandardMockDatabase : public PQ::Mock { public: StandardMockDatabase() : PQ::Mock("user=postgres dbname=postgres", "pqmock", { rootDir / "slicer.sql" }) { } }; BOOST_GLOBAL_FIXTURE( StandardMockDatabase ); typedef boost::shared_ptr DBPtr; typedef boost::shared_ptr SelectPtr; BOOST_AUTO_TEST_CASE( select_simple_int ) { auto db = DBPtr(DB::MockDatabase::openConnectionTo("pqmock")); auto sel = SelectPtr(db->newSelectCommand("SELECT MAX(id) FROM test")); auto bi = Slicer::DeserializeAny(*sel); BOOST_REQUIRE_EQUAL(4, bi); } BOOST_AUTO_TEST_CASE( select_simple_double ) { auto db = DBPtr(DB::MockDatabase::openConnectionTo("pqmock")); auto sel = SelectPtr(db->newSelectCommand("SELECT MAX(fl) FROM test")); auto bi = Slicer::DeserializeAny(*sel); BOOST_REQUIRE_CLOSE(1234.1234, bi, 0.0001); } BOOST_AUTO_TEST_CASE( select_simple_string ) { auto db = DBPtr(DB::MockDatabase::openConnectionTo("pqmock")); auto sel = SelectPtr(db->newSelectCommand("SELECT MAX(string) FROM test")); auto bi = Slicer::DeserializeAny(*sel); BOOST_REQUIRE_EQUAL("text two", bi); } BOOST_AUTO_TEST_CASE( select_simple_true ) { auto db = DBPtr(DB::MockDatabase::openConnectionTo("pqmock")); auto sel = SelectPtr(db->newSelectCommand("SELECT true")); auto bi = Slicer::DeserializeAny(*sel); BOOST_REQUIRE_EQUAL(true, bi); } BOOST_AUTO_TEST_CASE( select_simple_false ) { auto db = DBPtr(DB::MockDatabase::openConnectionTo("pqmock")); auto sel = SelectPtr(db->newSelectCommand("SELECT NOT(true)")); auto bi = Slicer::DeserializeAny(*sel); BOOST_REQUIRE_EQUAL(false, bi); } BOOST_AUTO_TEST_CASE( select_single ) { auto db = DBPtr(DB::MockDatabase::openConnectionTo("pqmock")); auto sel = SelectPtr(db->newSelectCommand( "SELECT boolean mbool, \ id mbyte, id mshort, id mint, id mlong, \ fl mdouble, fl mfloat, \ string mstring \ FROM test \ ORDER BY id \ LIMIT 1")); auto bi = Slicer::DeserializeAny(*sel); BOOST_REQUIRE(bi); BOOST_REQUIRE_EQUAL(true, bi->mbool); BOOST_REQUIRE_EQUAL(1, bi->mbyte); BOOST_REQUIRE_EQUAL(1, bi->mshort); BOOST_REQUIRE_EQUAL(1, bi->mint); BOOST_REQUIRE_EQUAL(1, bi->mlong); BOOST_REQUIRE_CLOSE(1.1, bi->mfloat, 0.0001); BOOST_REQUIRE_CLOSE(1.1, bi->mdouble, 0.0001); BOOST_REQUIRE_EQUAL("text one", bi->mstring); } BOOST_AUTO_TEST_CASE( select_inherit_single ) { auto db = DBPtr(DB::MockDatabase::openConnectionTo("pqmock")); auto sel = SelectPtr(db->newSelectCommand( "SELECT id a, '::TestModule::D' || CAST(id AS TEXT) tc, 200 b, 300 c, 400 d \ FROM test \ WHERE id = 2")); auto bi = Slicer::DeserializeAny(*sel, "tc"); BOOST_REQUIRE(bi); auto d2 = TestModule::D2Ptr::dynamicCast(bi); BOOST_REQUIRE(d2); BOOST_REQUIRE_EQUAL(2, d2->a); BOOST_REQUIRE_EQUAL(300, d2->c); } BOOST_AUTO_TEST_CASE( select_inherit_sequence ) { auto db = DBPtr(DB::MockDatabase::openConnectionTo("pqmock")); auto sel = SelectPtr(db->newSelectCommand( "SELECT id a, '::TestModule::D' || CAST(id AS TEXT) tc, 200 b, 300 c, 400 d \ FROM test \ WHERE id < 4 \ ORDER BY id DESC")); auto bi = Slicer::DeserializeAny(*sel, "tc"); BOOST_REQUIRE_EQUAL(3, bi.size()); auto d3 = TestModule::D3Ptr::dynamicCast(bi[0]); auto d2 = TestModule::D2Ptr::dynamicCast(bi[1]); auto d1 = TestModule::D1Ptr::dynamicCast(bi[2]); BOOST_REQUIRE(d3); BOOST_REQUIRE(d2); BOOST_REQUIRE(d1); BOOST_REQUIRE_EQUAL(3, d3->a); BOOST_REQUIRE_EQUAL(300, d3->c); BOOST_REQUIRE_EQUAL(400, d3->d); BOOST_REQUIRE_EQUAL(2, d2->a); BOOST_REQUIRE_EQUAL(300, d2->c); BOOST_REQUIRE_EQUAL(1, d1->a); BOOST_REQUIRE_EQUAL(200, d1->b); } BOOST_AUTO_TEST_CASE( select_inherit_datetime ) { auto db = DBPtr(DB::MockDatabase::openConnectionTo("pqmock")); auto sel = SelectPtr(db->newSelectCommand( "SELECT dt, to_char(dt, 'YYYY-MM-DD') date, ts \ FROM test \ WHERE id = 3")); DB::SpecificTypesPtr bi = Slicer::DeserializeAny(*sel); BOOST_REQUIRE_EQUAL(2015, bi->dt.year); BOOST_REQUIRE_EQUAL(3, bi->dt.month); BOOST_REQUIRE_EQUAL(27, bi->dt.day); BOOST_REQUIRE_EQUAL(23, bi->dt.hour); BOOST_REQUIRE_EQUAL(6, bi->dt.minute); BOOST_REQUIRE_EQUAL(3, bi->dt.second); BOOST_REQUIRE_EQUAL(2015, bi->date.year); BOOST_REQUIRE_EQUAL(3, bi->date.month); BOOST_REQUIRE_EQUAL(27, bi->date.day); BOOST_REQUIRE_EQUAL(1, bi->ts.days); BOOST_REQUIRE_EQUAL(13, bi->ts.hours); BOOST_REQUIRE_EQUAL(13, bi->ts.minutes); BOOST_REQUIRE_EQUAL(12, bi->ts.seconds); } template T BoostThrowWrapperHelper(P && ... p) { return Slicer::DeserializeAny(p...); } BOOST_AUTO_TEST_CASE( select_inherit_unsupportedModel ) { auto db = DBPtr(DB::MockDatabase::openConnectionTo("pqmock")); auto sel = SelectPtr(db->newSelectCommand("SELECT id FROM test")); BOOST_REQUIRE_THROW(BoostThrowWrapperHelper(*sel), Slicer::UnsupportedModelType); } BOOST_AUTO_TEST_CASE( select_inherit_tooManyRowsSimple ) { auto db = DBPtr(DB::MockDatabase::openConnectionTo("pqmock")); auto sel = SelectPtr(db->newSelectCommand("SELECT id FROM test")); BOOST_REQUIRE_THROW(BoostThrowWrapperHelper(*sel), Slicer::TooManyRowsReturned); } BOOST_AUTO_TEST_CASE( select_inherit_noRowsSimple ) { auto db = DBPtr(DB::MockDatabase::openConnectionTo("pqmock")); auto sel = SelectPtr(db->newSelectCommand("SELECT id FROM test WHERE false")); BOOST_REQUIRE_THROW(BoostThrowWrapperHelper(*sel), Slicer::NoRowsReturned); } BOOST_AUTO_TEST_CASE( select_inherit_tooManyRowsComplex ) { auto db = DBPtr(DB::MockDatabase::openConnectionTo("pqmock")); auto sel = SelectPtr(db->newSelectCommand("SELECT id FROM test")); BOOST_REQUIRE_THROW(BoostThrowWrapperHelper(*sel), Slicer::TooManyRowsReturned); } BOOST_AUTO_TEST_CASE( select_inherit_noRowsComplex ) { auto db = DBPtr(DB::MockDatabase::openConnectionTo("pqmock")); auto sel = SelectPtr(db->newSelectCommand("SELECT id FROM test WHERE false")); BOOST_REQUIRE_THROW(BoostThrowWrapperHelper(*sel), Slicer::NoRowsReturned); } BOOST_AUTO_TEST_CASE( select_inherit_emptySequence ) { auto db = DBPtr(DB::MockDatabase::openConnectionTo("pqmock")); auto sel = SelectPtr(db->newSelectCommand("SELECT id FROM test WHERE false")); auto bi = Slicer::DeserializeAny(*sel); BOOST_REQUIRE_EQUAL(0, bi.size()); } BOOST_AUTO_TEST_CASE( select_null ) { auto db = DBPtr(DB::MockDatabase::openConnectionTo("pqmock")); db->execute("INSERT INTO test(id) VALUES(NULL)"); auto sel = SelectPtr(db->newSelectCommand("SELECT id optSimple FROM test WHERE id IS NULL")); auto oi = Slicer::DeserializeAny(*sel); BOOST_REQUIRE(!oi->optSimple); sel = SelectPtr(db->newSelectCommand("SELECT MAX(id) optSimple FROM test WHERE id IS NOT NULL")); oi = Slicer::DeserializeAny(*sel); BOOST_REQUIRE(oi->optSimple); BOOST_REQUIRE_EQUAL(oi->optSimple.get(), 4); }