diff --git a/backends/mysql/test/CMakeLists.txt b/backends/mysql/test/CMakeLists.txt index 7aa4292..7834f2a 100644 --- a/backends/mysql/test/CMakeLists.txt +++ b/backends/mysql/test/CMakeLists.txt @@ -20,9 +20,9 @@ message(STATUS "mysql connection string: ${MYSQL_CONNECTION_STRING}") set(TEST_SOURCES ../../tests/QueryTest.cpp - ../../tests/SessionTest.cpp + ../../tests/QueryTest.cpp ../../tests/ConnectionTest.cpp - ../../tests/SessionRecordTest.cpp + ../../tests/QueryRecordTest.cpp ../../tests/StatementTest.cpp ../../tests/TypeTraitsTest.cpp ../../tests/StatementCacheTest.cpp) diff --git a/backends/postgres/test/CMakeLists.txt b/backends/postgres/test/CMakeLists.txt index 8e6323d..a07d07f 100644 --- a/backends/postgres/test/CMakeLists.txt +++ b/backends/postgres/test/CMakeLists.txt @@ -20,9 +20,9 @@ message(STATUS "postgresql connection string: ${POSTGRES_CONNECTION_STRING}") set(TEST_SOURCES ../../tests/QueryTest.cpp - ../../tests/SessionTest.cpp + ../../tests/QueryTest.cpp ../../tests/ConnectionTest.cpp - ../../tests/SessionRecordTest.cpp + ../../tests/QueryRecordTest.cpp ../../tests/StatementTest.cpp ../../tests/TypeTraitsTest.cpp ../../tests/StatementCacheTest.cpp) diff --git a/backends/sqlite/test/CMakeLists.txt b/backends/sqlite/test/CMakeLists.txt index 24666b4..8be1d13 100644 --- a/backends/sqlite/test/CMakeLists.txt +++ b/backends/sqlite/test/CMakeLists.txt @@ -20,9 +20,9 @@ message(STATUS "sqlite connection string: ${SQLITE_CONNECTION_STRING}") set(TEST_SOURCES ../../tests/QueryTest.cpp - ../../tests/SessionTest.cpp + ../../tests/QueryTest.cpp ../../tests/ConnectionTest.cpp - ../../tests/SessionRecordTest.cpp + ../../tests/QueryRecordTest.cpp ../../tests/StatementTest.cpp ../../tests/TypeTraitsTest.cpp ../../tests/StatementCacheTest.cpp) diff --git a/backends/tests/SessionRecordTest.cpp b/backends/tests/QueryRecordTest.cpp similarity index 90% rename from backends/tests/SessionRecordTest.cpp rename to backends/tests/QueryRecordTest.cpp index b88ae47..e5ca4bf 100644 --- a/backends/tests/SessionRecordTest.cpp +++ b/backends/tests/QueryRecordTest.cpp @@ -8,15 +8,15 @@ #include -class SessionRecordFixture +class QueryRecordFixture { public: - SessionRecordFixture() + QueryRecordFixture() : db(matador::test::connection::dns) { db.open(); } - ~SessionRecordFixture() { + ~QueryRecordFixture() { drop_table_if_exists("flight"); drop_table_if_exists("airplane"); drop_table_if_exists("person"); @@ -36,7 +36,7 @@ private: using namespace matador::sql; -TEST_CASE_METHOD(SessionRecordFixture, " Create and drop table statement", "[session][record]") +TEST_CASE_METHOD(QueryRecordFixture, " Create and drop table statement", "[session][record]") { REQUIRE(!db.exists("person")); db.create() @@ -56,7 +56,7 @@ TEST_CASE_METHOD(SessionRecordFixture, " Create and drop table statement", "[ses REQUIRE(!db.exists("person")); } -TEST_CASE_METHOD(SessionRecordFixture, " Create and drop table statement with foreign key", "[session][record]") +TEST_CASE_METHOD(QueryRecordFixture, " Create and drop table statement with foreign key", "[session][record]") { db.create() .table("airplane", { @@ -91,7 +91,7 @@ TEST_CASE_METHOD(SessionRecordFixture, " Create and drop table statement with fo REQUIRE(!db.exists("airplane")); } -TEST_CASE_METHOD(SessionRecordFixture, " Execute insert record statement", "[session][record]") +TEST_CASE_METHOD(QueryRecordFixture, " Execute insert record statement", "[session][record]") { db.create() .table("person", { @@ -130,7 +130,7 @@ TEST_CASE_METHOD(SessionRecordFixture, " Execute insert record statement", "[ses .execute(); } -TEST_CASE_METHOD(SessionRecordFixture, " Execute insert record statement with foreign key", "[session][record]") +TEST_CASE_METHOD(QueryRecordFixture, " Execute insert record statement with foreign key", "[session][record]") { db.create() .table("airplane", { @@ -170,7 +170,7 @@ TEST_CASE_METHOD(SessionRecordFixture, " Execute insert record statement with fo REQUIRE(!db.exists("airplane")); } -TEST_CASE_METHOD(SessionRecordFixture, " Execute update record statement", "[session][record]") +TEST_CASE_METHOD(QueryRecordFixture, " Execute update record statement", "[session][record]") { db.create() .table("person", { @@ -216,7 +216,7 @@ TEST_CASE_METHOD(SessionRecordFixture, " Execute update record statement", "[ses db.drop().table("person").execute(); } -TEST_CASE_METHOD(SessionRecordFixture, " Execute select statement", "[session][record]") +TEST_CASE_METHOD(QueryRecordFixture, " Execute select statement", "[session][record]") { db.create() .table("person", { @@ -259,7 +259,7 @@ TEST_CASE_METHOD(SessionRecordFixture, " Execute select statement", "[session][r db.drop().table("person").execute(); } -TEST_CASE_METHOD(SessionRecordFixture, " Execute select statement with order by", "[session][record]") +TEST_CASE_METHOD(QueryRecordFixture, " Execute select statement with order by", "[session][record]") { db.create() .table("person", { @@ -293,7 +293,7 @@ TEST_CASE_METHOD(SessionRecordFixture, " Execute select statement with order by" db.drop().table("person").execute(); } -TEST_CASE_METHOD(SessionRecordFixture, " Execute select statement with group by and order by", "[session][record]") +TEST_CASE_METHOD(QueryRecordFixture, " Execute select statement with group by and order by", "[session][record]") { db.create() .table("person", { @@ -332,7 +332,7 @@ TEST_CASE_METHOD(SessionRecordFixture, " Execute select statement with group by db.drop().table("person").execute(); } -TEST_CASE_METHOD(SessionRecordFixture, " Execute delete statement", "[session][record]") +TEST_CASE_METHOD(QueryRecordFixture, " Execute delete statement", "[session][record]") { db.create() .table("person", { @@ -362,7 +362,7 @@ TEST_CASE_METHOD(SessionRecordFixture, " Execute delete statement", "[session][r db.drop().table("person").execute(); } -TEST_CASE_METHOD(SessionRecordFixture, " Test quoted identifier", "[session][record]") { +TEST_CASE_METHOD(QueryRecordFixture, " Test quoted identifier", "[session][record]") { db.create() .table("quotes", { make_column("from", 255), diff --git a/backends/tests/QueryTest.cpp b/backends/tests/QueryTest.cpp index 239d635..1c20c5e 100644 --- a/backends/tests/QueryTest.cpp +++ b/backends/tests/QueryTest.cpp @@ -1,15 +1,182 @@ #include "catch2/catch_test_macros.hpp" -#include +#include "matador/sql/column.hpp" +#include "matador/sql/condition.hpp" +#include "matador/sql/session.hpp" -TEST_CASE("Query test", "[query]") { - SECTION("Create") { - REQUIRE(true); +#include "connection.hpp" + +#include "models/airplane.hpp" +#include "models/flight.hpp" +#include "models/person.hpp" + +using namespace matador::sql; +using namespace matador::test; + +class QueryFixture +{ +public: + QueryFixture() + : db(matador::test::connection::dns) + { + db.open(); } - SECTION("Insert") { - REQUIRE(true); + ~QueryFixture() { + drop_table_if_exists("flight"); + drop_table_if_exists("airplane"); + drop_table_if_exists("person"); } - SECTION("Select") { - REQUIRE(true); + +protected: + matador::sql::connection db; + +private: + void drop_table_if_exists(const std::string &table_name) { + if (db.exists(table_name)) { + db.drop().table(table_name).execute(); + } } +}; + +TEST_CASE_METHOD(QueryFixture, " Create table with foreign key relation", "[session]") { + db.create() + .table("airplane") + .execute(); + + REQUIRE(db.exists("airplane")); + + db.create() + .table("flight") + .execute(); + + REQUIRE(db.exists("flight")); + + db.drop().table("flight").execute(); + db.drop().table("airplane").execute(); + + REQUIRE(!db.exists("flight")); + REQUIRE(!db.exists("airplane")); } + +TEST_CASE_METHOD(QueryFixture, " Execute select statement with where clause", "[session]") { + db.create() + .table("person") + .execute(); + + person george{7, "george", 45}; + george.image.push_back(37); + + auto res = db.insert() + .into("person", george) + .execute(); + REQUIRE(res == 1); + + // fetch person as record + auto result_record = db.select() + .from("person") + .where("id"_col == 7) + .fetch_all(); + + for (const auto& i : result_record) { + REQUIRE(i.size() == 4); + REQUIRE(i.at(0).name() == "id"); + REQUIRE(i.at(0).type() == data_type_t::type_unsigned_long); + REQUIRE(i.at(0).as() == george.id); + REQUIRE(i.at(1).name() == "name"); + REQUIRE(i.at(1).type() == data_type_t::type_varchar); + REQUIRE(i.at(1).as() == george.name); + REQUIRE(i.at(2).name() == "age"); + REQUIRE(i.at(2).type() == matador::sql::data_type_t::type_unsigned_int); + REQUIRE(i.at(2).as() == george.age); + } + + // fetch person as person + auto result_person = db.select() + .from("person") + .where("id"_col == 7) + .fetch_all(); + + for (const auto& i : result_person) { + REQUIRE(i.id == 7); + REQUIRE(i.name == "george"); + REQUIRE(i.age == 45); + } + + db.drop().table("person").execute(); +} + +TEST_CASE_METHOD(QueryFixture, " Execute insert statement", "[session]") { + db.create() + .table("person", { + make_pk_column("id"), + make_column("name", 255), + make_column("color", 63) + }) + .execute(); + + auto res = db.insert() + .into("person", {"id", "name", "color"}) + .values({7, "george", "green"}) + .execute(); + + REQUIRE(res == 1); + + // fetch person as record + auto result_record = db.select({"id", "name", "color"}) + .from("person") + .where("id"_col == 7) + .fetch_all(); + + for (const auto& i : result_record) { + REQUIRE(i.size() == 3); + REQUIRE(i.at(0).name() == "id"); + REQUIRE(i.at(0).type() == data_type_t::type_long_long); + REQUIRE(i.at(0).as() == 7); + REQUIRE(i.at(1).name() == "name"); + REQUIRE(i.at(1).type() == data_type_t::type_varchar); + REQUIRE(i.at(1).as() == "george"); + REQUIRE(i.at(2).name() == "color"); + REQUIRE(i.at(2).type() == matador::sql::data_type_t::type_varchar); + REQUIRE(i.at(2).as() == "green"); + } + + db.drop().table("person").execute(); +} + +TEST_CASE_METHOD(QueryFixture, " Select statement with foreign key", "[session]") { + db.create() + .table("airplane") + .execute(); + + db.create() + .table("flight") + .execute(); + + std::vector> planes { + make_entity(1, "Airbus", "A380"), + make_entity(2, "Boeing", "707"), + make_entity(3, "Boeing", "747") + }; + + for (const auto &plane : planes) { + auto res = db.insert().into("airplane").values(*plane).execute(); + REQUIRE(res == 1); + } + + auto count = db.select({count_all()}).from("airplane").fetch_value(); + REQUIRE(count == 3); + + flight f4711{4, planes.at(1), "hans"}; + + auto res = db.insert().into("flight").values(f4711).execute(); + REQUIRE(res == 1); + + auto f = *db.select().from("flight").fetch_all().begin(); + REQUIRE(f.id == 4); + REQUIRE(f.pilot_name == "hans"); + REQUIRE(f.airplane.get() != nullptr); + REQUIRE(f.airplane->id == 2); + + db.drop().table("flight").execute(); + db.drop().table("airplane").execute(); +} \ No newline at end of file diff --git a/backends/tests/SessionTest.cpp b/backends/tests/SessionTest.cpp deleted file mode 100644 index 1c20c5e..0000000 --- a/backends/tests/SessionTest.cpp +++ /dev/null @@ -1,182 +0,0 @@ -#include "catch2/catch_test_macros.hpp" - -#include "matador/sql/column.hpp" -#include "matador/sql/condition.hpp" -#include "matador/sql/session.hpp" - -#include "connection.hpp" - -#include "models/airplane.hpp" -#include "models/flight.hpp" -#include "models/person.hpp" - -using namespace matador::sql; -using namespace matador::test; - -class QueryFixture -{ -public: - QueryFixture() - : db(matador::test::connection::dns) - { - db.open(); - } - ~QueryFixture() { - drop_table_if_exists("flight"); - drop_table_if_exists("airplane"); - drop_table_if_exists("person"); - } - -protected: - matador::sql::connection db; - -private: - void drop_table_if_exists(const std::string &table_name) { - if (db.exists(table_name)) { - db.drop().table(table_name).execute(); - } - } -}; - -TEST_CASE_METHOD(QueryFixture, " Create table with foreign key relation", "[session]") { - db.create() - .table("airplane") - .execute(); - - REQUIRE(db.exists("airplane")); - - db.create() - .table("flight") - .execute(); - - REQUIRE(db.exists("flight")); - - db.drop().table("flight").execute(); - db.drop().table("airplane").execute(); - - REQUIRE(!db.exists("flight")); - REQUIRE(!db.exists("airplane")); -} - -TEST_CASE_METHOD(QueryFixture, " Execute select statement with where clause", "[session]") { - db.create() - .table("person") - .execute(); - - person george{7, "george", 45}; - george.image.push_back(37); - - auto res = db.insert() - .into("person", george) - .execute(); - REQUIRE(res == 1); - - // fetch person as record - auto result_record = db.select() - .from("person") - .where("id"_col == 7) - .fetch_all(); - - for (const auto& i : result_record) { - REQUIRE(i.size() == 4); - REQUIRE(i.at(0).name() == "id"); - REQUIRE(i.at(0).type() == data_type_t::type_unsigned_long); - REQUIRE(i.at(0).as() == george.id); - REQUIRE(i.at(1).name() == "name"); - REQUIRE(i.at(1).type() == data_type_t::type_varchar); - REQUIRE(i.at(1).as() == george.name); - REQUIRE(i.at(2).name() == "age"); - REQUIRE(i.at(2).type() == matador::sql::data_type_t::type_unsigned_int); - REQUIRE(i.at(2).as() == george.age); - } - - // fetch person as person - auto result_person = db.select() - .from("person") - .where("id"_col == 7) - .fetch_all(); - - for (const auto& i : result_person) { - REQUIRE(i.id == 7); - REQUIRE(i.name == "george"); - REQUIRE(i.age == 45); - } - - db.drop().table("person").execute(); -} - -TEST_CASE_METHOD(QueryFixture, " Execute insert statement", "[session]") { - db.create() - .table("person", { - make_pk_column("id"), - make_column("name", 255), - make_column("color", 63) - }) - .execute(); - - auto res = db.insert() - .into("person", {"id", "name", "color"}) - .values({7, "george", "green"}) - .execute(); - - REQUIRE(res == 1); - - // fetch person as record - auto result_record = db.select({"id", "name", "color"}) - .from("person") - .where("id"_col == 7) - .fetch_all(); - - for (const auto& i : result_record) { - REQUIRE(i.size() == 3); - REQUIRE(i.at(0).name() == "id"); - REQUIRE(i.at(0).type() == data_type_t::type_long_long); - REQUIRE(i.at(0).as() == 7); - REQUIRE(i.at(1).name() == "name"); - REQUIRE(i.at(1).type() == data_type_t::type_varchar); - REQUIRE(i.at(1).as() == "george"); - REQUIRE(i.at(2).name() == "color"); - REQUIRE(i.at(2).type() == matador::sql::data_type_t::type_varchar); - REQUIRE(i.at(2).as() == "green"); - } - - db.drop().table("person").execute(); -} - -TEST_CASE_METHOD(QueryFixture, " Select statement with foreign key", "[session]") { - db.create() - .table("airplane") - .execute(); - - db.create() - .table("flight") - .execute(); - - std::vector> planes { - make_entity(1, "Airbus", "A380"), - make_entity(2, "Boeing", "707"), - make_entity(3, "Boeing", "747") - }; - - for (const auto &plane : planes) { - auto res = db.insert().into("airplane").values(*plane).execute(); - REQUIRE(res == 1); - } - - auto count = db.select({count_all()}).from("airplane").fetch_value(); - REQUIRE(count == 3); - - flight f4711{4, planes.at(1), "hans"}; - - auto res = db.insert().into("flight").values(f4711).execute(); - REQUIRE(res == 1); - - auto f = *db.select().from("flight").fetch_all().begin(); - REQUIRE(f.id == 4); - REQUIRE(f.pilot_name == "hans"); - REQUIRE(f.airplane.get() != nullptr); - REQUIRE(f.airplane->id == 2); - - db.drop().table("flight").execute(); - db.drop().table("airplane").execute(); -} \ No newline at end of file