summaryrefslogtreecommitdiff
path: root/slicer/db/testSelect.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'slicer/db/testSelect.cpp')
-rw-r--r--slicer/db/testSelect.cpp203
1 files changed, 203 insertions, 0 deletions
diff --git a/slicer/db/testSelect.cpp b/slicer/db/testSelect.cpp
new file mode 100644
index 0000000..29ea5bc
--- /dev/null
+++ b/slicer/db/testSelect.cpp
@@ -0,0 +1,203 @@
+#define BOOST_TEST_MODULE db_select
+#include <boost/test/unit_test.hpp>
+#include <boost/date_time/posix_time/posix_time_io.hpp>
+#include <mock.h>
+#include <slicer/slicer.h>
+#include <definedDirs.h>
+#include "sqlSelectDeserializer.h"
+#include <types.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<DB::Connection> DBPtr;
+typedef boost::shared_ptr<DB::SelectCommand> SelectPtr;
+
+boost::posix_time::ptime
+mkDateTime(short y, short m, short d, short h, short M, short s)
+{
+ return boost::posix_time::ptime(boost::gregorian::date(y, m, d), boost::posix_time::time_duration(h, M, s));
+}
+
+boost::posix_time::time_duration
+mkTimespan(short d, short h, short M, short s)
+{
+ return boost::posix_time::time_duration((d * 24) + h, M, s);
+}
+
+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<Slicer::SqlSelectDeserializer, Ice::Int>(*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<Slicer::SqlSelectDeserializer, Ice::Double>(*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<Slicer::SqlSelectDeserializer, std::string>(*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<Slicer::SqlSelectDeserializer, bool>(*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<Slicer::SqlSelectDeserializer, bool>(*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<Slicer::SqlSelectDeserializer, TestModule::BuiltInsPtr>(*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<Slicer::SqlSelectDeserializer, TestModule::BasePtr>(*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<Slicer::SqlSelectDeserializer, TestModule::BaseSeq>(*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<Slicer::SqlSelectDeserializer, DB::SpecificTypesPtr>(*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 <typename T, typename ... P>
+T
+BoostThrowWrapperHelper(P & ... p)
+{
+ return Slicer::DeserializeAny<Slicer::SqlSelectDeserializer, T>(p...);
+}
+
+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<Ice::Int>(*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<Ice::Int>(*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<TestModule::BuiltInsPtr>(*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<TestModule::BuiltInsPtr>(*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<Slicer::SqlSelectDeserializer, TestModule::BaseSeq>(*sel);
+ BOOST_REQUIRE_EQUAL(0, bi.size());
+}
+