Skip to content
Snippets Groups Projects
Verified Commit 0f1fb0fb authored by Timm Fitschen's avatar Timm Fitschen
Browse files

TST: more tests for select queries

parent a0c74115
No related branches found
No related tags found
2 merge requests!28Release 0.2.2,!27F grpc select
Pipeline #30636 failed
...@@ -26,7 +26,7 @@ ...@@ -26,7 +26,7 @@
#include "caosdb/transaction.h" // for Transaction, ResultTable #include "caosdb/transaction.h" // for Transaction, ResultTable
#include "caosdb/value.h" // for Value #include "caosdb/value.h" // for Value
#include <gtest/gtest-message.h> // for Message #include <gtest/gtest-message.h> // for Message
#include <gtest/gtest-spi.h> // for EXPECT_NONFATAL_FA... #include <gtest/gtest-spi.h> // for EXPECT_NONFATAL_FA...
#include <gtest/gtest-test-part.h> // for SuiteApiResolver, TestFactoryImpl #include <gtest/gtest-test-part.h> // for SuiteApiResolver, TestFactoryImpl
#include <gtest/gtest_pred_impl.h> // for Test, TestInfo, EXPECT_EQ, TEST #include <gtest/gtest_pred_impl.h> // for Test, TestInfo, EXPECT_EQ, TEST
#include <iostream> // for operator<<, basic_ostream::operat... #include <iostream> // for operator<<, basic_ostream::operat...
...@@ -34,8 +34,8 @@ ...@@ -34,8 +34,8 @@
#include <string> // for string #include <string> // for string
namespace caosdb::transaction { namespace caosdb::transaction {
using caosdb::entity::DataType;
using caosdb::entity::AtomicDataType; using caosdb::entity::AtomicDataType;
using caosdb::entity::DataType;
using caosdb::entity::Entity; using caosdb::entity::Entity;
using caosdb::entity::Parent; using caosdb::entity::Parent;
using caosdb::entity::Property; using caosdb::entity::Property;
...@@ -56,11 +56,8 @@ public: ...@@ -56,11 +56,8 @@ public:
const auto &connection = caosdb::connection::ConnectionManager::GetDefaultConnection(); const auto &connection = caosdb::connection::ConnectionManager::GetDefaultConnection();
auto query_transaction(connection->CreateTransaction()); auto query_transaction(connection->CreateTransaction());
query_transaction->Query("FIND Entity"); query_transaction->Query("FIND Entity");
// query_transaction->Query("FIND Test*");
query_transaction->Execute(); query_transaction->Execute();
if (query_transaction->GetResultSet().size() > 0) { if (query_transaction->GetResultSet().size() > 0) {
std::cout << "Cleanup: Deleting " << query_transaction->GetResultSet().size() << " entities."
<< std::endl;
auto delete_transaction(connection->CreateTransaction()); auto delete_transaction(connection->CreateTransaction());
for (const Entity &entity : query_transaction->GetResultSet()) { for (const Entity &entity : query_transaction->GetResultSet()) {
delete_transaction->DeleteById(entity.GetId()); delete_transaction->DeleteById(entity.GetId());
...@@ -69,7 +66,8 @@ public: ...@@ -69,7 +66,8 @@ public:
} }
} }
static auto CreateTestProp(const std::string &name, AtomicDataType data_type, bool isList = false) -> Entity { static auto CreateTestProp(const std::string &name, AtomicDataType data_type, bool isList = false)
-> Entity {
Entity entity; Entity entity;
entity.SetRole(Role::PROPERTY); entity.SetRole(Role::PROPERTY);
entity.SetName(name); entity.SetName(name);
...@@ -86,7 +84,8 @@ public: ...@@ -86,7 +84,8 @@ public:
return entity; return entity;
} }
static auto CreateTestProp(const std::string &name, const std::string &data_type, bool isList = false) -> Entity { static auto CreateTestProp(const std::string &name, const std::string &data_type,
bool isList = false) -> Entity {
Entity entity; Entity entity;
entity.SetRole(Role::PROPERTY); entity.SetRole(Role::PROPERTY);
entity.SetName(name); entity.SetName(name);
...@@ -146,9 +145,11 @@ protected: ...@@ -146,9 +145,11 @@ protected:
p = CreateTestProp("TestPropBool", AtomicDataType::BOOLEAN); p = CreateTestProp("TestPropBool", AtomicDataType::BOOLEAN);
insert_transaction->InsertEntity(&p); insert_transaction->InsertEntity(&p);
// 3. A List Property // 3. List Properties
p = CreateTestProp("TestListTextProp", AtomicDataType::TEXT, true); p = CreateTestProp("TestListTextProp", AtomicDataType::TEXT, true);
insert_transaction->InsertEntity(&p); insert_transaction->InsertEntity(&p);
p = CreateTestProp("TestListReferenceProp", "TestRT2", true);
insert_transaction->InsertEntity(&p);
// 4. A Record which can be references by others // 4. A Record which can be references by others
auto reference_entity = test_select::CreateRecord("TestProp3", Value("val3")); auto reference_entity = test_select::CreateRecord("TestProp3", Value("val3"));
...@@ -211,6 +212,27 @@ TEST_F(test_select, test_select_name) { ...@@ -211,6 +212,27 @@ TEST_F(test_select, test_select_name) {
} }
} }
/*
* Test select description query on record type.
*/
TEST_F(test_select, test_select_property_description) {
const auto &connection = caosdb::connection::ConnectionManager::GetDefaultConnection();
auto query_transaction(connection->CreateTransaction());
query_transaction->Query("SELECT TestPropDouble.description FROM Record TestRT2");
query_transaction->Execute();
EXPECT_EQ(query_transaction->GetResultTable().GetHeader().size(), 1);
for (const auto &column : query_transaction->GetResultTable().GetHeader()) {
EXPECT_EQ(column.GetName(), "TestPropDouble.description");
}
EXPECT_EQ(query_transaction->GetResultTable().size(), 1);
for (const auto &row : query_transaction->GetResultTable().GetRows()) {
EXPECT_EQ(row.GetValue("TestPropDouble.description").GetAsString(),
"Prop Description TestPropDouble");
}
}
/* /*
* Test select description query on record type. * Test select description query on record type.
*/ */
...@@ -276,18 +298,60 @@ TEST_F(test_select, test_select_double) { ...@@ -276,18 +298,60 @@ TEST_F(test_select, test_select_double) {
} }
} }
/*
* Test select double value query on record.
*/
TEST_F(test_select, test_select_value) {
const auto &connection = caosdb::connection::ConnectionManager::GetDefaultConnection();
auto entity = test_select::CreateRecord("TestPropDouble", Value(2.123));
test_select::InsertEntity(&entity);
auto query_transaction(connection->CreateTransaction());
query_transaction->Query("SELECT TestPropDouble.value FROM Record TestRT");
query_transaction->Execute();
EXPECT_EQ(query_transaction->GetResultTable().GetHeader().size(), 1);
for (const auto &column : query_transaction->GetResultTable().GetHeader()) {
EXPECT_EQ(column.GetName(), "TestPropDouble.value");
}
EXPECT_EQ(query_transaction->GetResultTable().size(), 1);
for (const auto &row : query_transaction->GetResultTable().GetRows()) {
EXPECT_TRUE(row.GetValue("TestPropDouble.value").IsDouble());
EXPECT_EQ(row.GetValue("TestPropDouble.value").GetAsDouble(), 2.123);
}
}
/* /*
* Test select unit value query on record. * Test select unit value query on record.
*/ */
TEST_F(test_select, test_select_unit) { TEST_F(test_select, test_select_unit) {
const auto &connection = caosdb::connection::ConnectionManager::GetDefaultConnection(); const auto &connection = caosdb::connection::ConnectionManager::GetDefaultConnection();
auto query_transaction(connection->CreateTransaction());
query_transaction->Query("SELECT unit FROM PROPERTY TestPropDouble");
query_transaction->Execute();
EXPECT_EQ(query_transaction->GetResultTable().GetHeader().size(), 1);
for (const auto &column : query_transaction->GetResultTable().GetHeader()) {
EXPECT_EQ(column.GetName(), "unit");
}
EXPECT_EQ(query_transaction->GetResultTable().size(), 1);
for (const auto &row : query_transaction->GetResultTable().GetRows()) {
EXPECT_EQ(row.GetValue("unit").GetAsString(), "m");
}
}
/*
* Test select unit value query on record.
*/
TEST_F(test_select, test_select_property_unit) {
const auto &connection = caosdb::connection::ConnectionManager::GetDefaultConnection();
auto entity = test_select::CreateRecord("TestPropDouble", Value(2.123)); auto entity = test_select::CreateRecord("TestPropDouble", Value(2.123));
test_select::InsertEntity(&entity); test_select::InsertEntity(&entity);
auto check(connection->CreateTransaction()); auto check(connection->CreateTransaction());
check->Query("FIND Record TestRT"); check->Query("FIND Record TestRT");
check->Execute(); check->Execute();
std::cout << check->GetResultSet().at(0).ToString() << std::endl;
auto query_transaction(connection->CreateTransaction()); auto query_transaction(connection->CreateTransaction());
query_transaction->Query("SELECT TestPropDouble.unit FROM Record TestRT"); query_transaction->Query("SELECT TestPropDouble.unit FROM Record TestRT");
...@@ -301,7 +365,8 @@ TEST_F(test_select, test_select_unit) { ...@@ -301,7 +365,8 @@ TEST_F(test_select, test_select_unit) {
for (const auto &row : query_transaction->GetResultTable().GetRows()) { for (const auto &row : query_transaction->GetResultTable().GetRows()) {
// TODO(tf): create bug report: this is an insertion/update problem // TODO(tf): create bug report: this is an insertion/update problem
EXPECT_NONFATAL_FAILURE({ EXPECT_EQ(row.GetValue("TestPropDouble.unit").GetAsString(), "m"); }, "TestPropDouble.unit"); EXPECT_NONFATAL_FAILURE({ EXPECT_EQ(row.GetValue("TestPropDouble.unit").GetAsString(), "m"); },
"TestPropDouble.unit");
} }
} }
...@@ -328,7 +393,8 @@ TEST_F(test_select, test_select_int) { ...@@ -328,7 +393,8 @@ TEST_F(test_select, test_select_int) {
} }
} }
/* Test select boolean value query on record. */ TEST_F(test_select, test_select_boolean) { /* Test select boolean value query on record. */
TEST_F(test_select, test_select_boolean) {
const auto &connection = caosdb::connection::ConnectionManager::GetDefaultConnection(); const auto &connection = caosdb::connection::ConnectionManager::GetDefaultConnection();
auto entity = test_select::CreateRecord("TestPropBool", Value(true)); auto entity = test_select::CreateRecord("TestPropBool", Value(true));
test_select::InsertEntity(&entity); test_select::InsertEntity(&entity);
...@@ -356,7 +422,7 @@ TEST_F(test_select, test_select_reference) { ...@@ -356,7 +422,7 @@ TEST_F(test_select, test_select_reference) {
auto get_id_of_ref_rec(connection->CreateTransaction()); auto get_id_of_ref_rec(connection->CreateTransaction());
get_id_of_ref_rec->Query("SELECT id FROM RECORD TestRT2"); get_id_of_ref_rec->Query("SELECT id FROM RECORD TestRT2");
get_id_of_ref_rec->Execute(); get_id_of_ref_rec->Execute();
auto id = (* get_id_of_ref_rec->GetResultTable().GetRows().begin()).GetValue("id").GetAsString(); auto id = (*get_id_of_ref_rec->GetResultTable().GetRows().begin()).GetValue("id").GetAsString();
auto entity = test_select::CreateRecord("TestRT2", Value(id)); auto entity = test_select::CreateRecord("TestRT2", Value(id));
test_select::InsertEntity(&entity); test_select::InsertEntity(&entity);
...@@ -375,6 +441,35 @@ TEST_F(test_select, test_select_reference) { ...@@ -375,6 +441,35 @@ TEST_F(test_select, test_select_reference) {
} }
} }
/*
* Test select the referenced id.
*/
TEST_F(test_select, test_select_reference_value) {
const auto &connection = caosdb::connection::ConnectionManager::GetDefaultConnection();
auto get_id_of_ref_rec(connection->CreateTransaction());
get_id_of_ref_rec->Query("SELECT id, version FROM RECORD TestRT2");
get_id_of_ref_rec->Execute();
auto id = (*get_id_of_ref_rec->GetResultTable().GetRows().begin()).GetValue("id").GetAsString();
auto version =
(*get_id_of_ref_rec->GetResultTable().GetRows().begin()).GetValue("version").GetAsString();
auto entity = test_select::CreateRecord("TestRT2", Value(id));
test_select::InsertEntity(&entity);
auto query_transaction(connection->CreateTransaction());
query_transaction->Query("SELECT TestRT2.value FROM Record TestRT");
query_transaction->Execute();
EXPECT_EQ(query_transaction->GetResultTable().GetHeader().size(), 1);
for (const auto &column : query_transaction->GetResultTable().GetHeader()) {
EXPECT_EQ(column.GetName(), "TestRT2.value");
}
EXPECT_EQ(query_transaction->GetResultTable().size(), 1);
for (const auto &row : query_transaction->GetResultTable().GetRows()) {
EXPECT_EQ(row.GetValue("TestRT2.value").GetAsString(), id + "@" + version);
}
}
/* /*
* Test select the referenced entity's property. * Test select the referenced entity's property.
*/ */
...@@ -383,7 +478,7 @@ TEST_F(test_select, test_select_reference_entitys_property) { ...@@ -383,7 +478,7 @@ TEST_F(test_select, test_select_reference_entitys_property) {
auto get_id_of_ref_rec(connection->CreateTransaction()); auto get_id_of_ref_rec(connection->CreateTransaction());
get_id_of_ref_rec->Query("SELECT id FROM RECORD TestRT2"); get_id_of_ref_rec->Query("SELECT id FROM RECORD TestRT2");
get_id_of_ref_rec->Execute(); get_id_of_ref_rec->Execute();
auto id = (* get_id_of_ref_rec->GetResultTable().GetRows().begin()).GetValue("id").GetAsString(); auto id = (*get_id_of_ref_rec->GetResultTable().GetRows().begin()).GetValue("id").GetAsString();
auto entity = test_select::CreateRecord("TestRT2", Value(id)); auto entity = test_select::CreateRecord("TestRT2", Value(id));
test_select::InsertEntity(&entity); test_select::InsertEntity(&entity);
...@@ -410,7 +505,7 @@ TEST_F(test_select, test_select_reference_entitys_propertys_unit) { ...@@ -410,7 +505,7 @@ TEST_F(test_select, test_select_reference_entitys_propertys_unit) {
auto get_id_of_ref_rec(connection->CreateTransaction()); auto get_id_of_ref_rec(connection->CreateTransaction());
get_id_of_ref_rec->Query("SELECT id FROM RECORD TestRT2"); get_id_of_ref_rec->Query("SELECT id FROM RECORD TestRT2");
get_id_of_ref_rec->Execute(); get_id_of_ref_rec->Execute();
auto id = (* get_id_of_ref_rec->GetResultTable().GetRows().begin()).GetValue("id").GetAsString(); auto id = (*get_id_of_ref_rec->GetResultTable().GetRows().begin()).GetValue("id").GetAsString();
auto entity = test_select::CreateRecord("TestRT2", Value(id)); auto entity = test_select::CreateRecord("TestRT2", Value(id));
test_select::InsertEntity(&entity); test_select::InsertEntity(&entity);
...@@ -437,7 +532,7 @@ TEST_F(test_select, test_select_reference_entitys_propertys_description) { ...@@ -437,7 +532,7 @@ TEST_F(test_select, test_select_reference_entitys_propertys_description) {
auto get_id_of_ref_rec(connection->CreateTransaction()); auto get_id_of_ref_rec(connection->CreateTransaction());
get_id_of_ref_rec->Query("SELECT id FROM RECORD TestRT2"); get_id_of_ref_rec->Query("SELECT id FROM RECORD TestRT2");
get_id_of_ref_rec->Execute(); get_id_of_ref_rec->Execute();
auto id = (* get_id_of_ref_rec->GetResultTable().GetRows().begin()).GetValue("id").GetAsString(); auto id = (*get_id_of_ref_rec->GetResultTable().GetRows().begin()).GetValue("id").GetAsString();
auto entity = test_select::CreateRecord("TestRT2", Value(id)); auto entity = test_select::CreateRecord("TestRT2", Value(id));
test_select::InsertEntity(&entity); test_select::InsertEntity(&entity);
...@@ -452,7 +547,8 @@ TEST_F(test_select, test_select_reference_entitys_propertys_description) { ...@@ -452,7 +547,8 @@ TEST_F(test_select, test_select_reference_entitys_propertys_description) {
EXPECT_EQ(query_transaction->GetResultTable().size(), 1); EXPECT_EQ(query_transaction->GetResultTable().size(), 1);
for (const auto &row : query_transaction->GetResultTable().GetRows()) { for (const auto &row : query_transaction->GetResultTable().GetRows()) {
EXPECT_EQ(row.GetValue("TestRT2.TestProp3.description").GetAsString(), "Prop Description TestProp3"); EXPECT_EQ(row.GetValue("TestRT2.TestProp3.description").GetAsString(),
"Prop Description TestProp3");
} }
} }
...@@ -464,7 +560,7 @@ TEST_F(test_select, test_select_reference_entitys_propertys_name) { ...@@ -464,7 +560,7 @@ TEST_F(test_select, test_select_reference_entitys_propertys_name) {
auto get_id_of_ref_rec(connection->CreateTransaction()); auto get_id_of_ref_rec(connection->CreateTransaction());
get_id_of_ref_rec->Query("SELECT id FROM RECORD TestRT2"); get_id_of_ref_rec->Query("SELECT id FROM RECORD TestRT2");
get_id_of_ref_rec->Execute(); get_id_of_ref_rec->Execute();
auto id = (* get_id_of_ref_rec->GetResultTable().GetRows().begin()).GetValue("id").GetAsString(); auto id = (*get_id_of_ref_rec->GetResultTable().GetRows().begin()).GetValue("id").GetAsString();
auto entity = test_select::CreateRecord("TestRT2", Value(id)); auto entity = test_select::CreateRecord("TestRT2", Value(id));
test_select::InsertEntity(&entity); test_select::InsertEntity(&entity);
...@@ -491,12 +587,12 @@ TEST_F(test_select, test_select_reference_entitys_propertys_id) { ...@@ -491,12 +587,12 @@ TEST_F(test_select, test_select_reference_entitys_propertys_id) {
auto get_id_of_ref_rec(connection->CreateTransaction()); auto get_id_of_ref_rec(connection->CreateTransaction());
get_id_of_ref_rec->Query("SELECT id FROM RECORD TestRT2"); get_id_of_ref_rec->Query("SELECT id FROM RECORD TestRT2");
get_id_of_ref_rec->Execute(); get_id_of_ref_rec->Execute();
auto id = (* get_id_of_ref_rec->GetResultTable().GetRows().begin()).GetValue("id").GetAsString(); auto id = (*get_id_of_ref_rec->GetResultTable().GetRows().begin()).GetValue("id").GetAsString();
auto get_id_of_prop(connection->CreateTransaction()); auto get_id_of_prop(connection->CreateTransaction());
get_id_of_prop->Query("SELECT id FROM PROPERTY TestProp3"); get_id_of_prop->Query("SELECT id FROM PROPERTY TestProp3");
get_id_of_prop->Execute(); get_id_of_prop->Execute();
auto pid = (* get_id_of_prop->GetResultTable().GetRows().begin()).GetValue("id").GetAsString(); auto pid = (*get_id_of_prop->GetResultTable().GetRows().begin()).GetValue("id").GetAsString();
auto entity = test_select::CreateRecord("TestRT2", Value(id)); auto entity = test_select::CreateRecord("TestRT2", Value(id));
test_select::InsertEntity(&entity); test_select::InsertEntity(&entity);
...@@ -516,9 +612,9 @@ TEST_F(test_select, test_select_reference_entitys_propertys_id) { ...@@ -516,9 +612,9 @@ TEST_F(test_select, test_select_reference_entitys_propertys_id) {
} }
/* /*
* Test select a list. * Test select a list (TEXT).
*/ */
TEST_F(test_select, test_select_list) { TEST_F(test_select, test_select_list_of_text) {
const auto &connection = caosdb::connection::ConnectionManager::GetDefaultConnection(); const auto &connection = caosdb::connection::ConnectionManager::GetDefaultConnection();
auto entity = test_select::CreateRecord("TestRT2", Value()); auto entity = test_select::CreateRecord("TestRT2", Value());
...@@ -544,6 +640,38 @@ TEST_F(test_select, test_select_list) { ...@@ -544,6 +640,38 @@ TEST_F(test_select, test_select_list) {
} }
} }
/*
* Test select a list (REFERENCE).
*/
TEST_F(test_select, test_select_list_of_reference) {
const auto &connection = caosdb::connection::ConnectionManager::GetDefaultConnection();
auto get_id_of_ref_rec(connection->CreateTransaction());
get_id_of_ref_rec->Query("SELECT id FROM RECORD TestRT2");
get_id_of_ref_rec->Execute();
auto id = (*get_id_of_ref_rec->GetResultTable().GetRows().begin()).GetValue("id").GetAsString();
auto entity = test_select::CreateRecord("TestRT2", Value());
Property listp;
listp.SetName("TestListReferenceProp");
listp.SetValue(std::vector<std::string>{id, id, id});
entity.AppendProperty(listp);
test_select::InsertEntity(&entity);
auto query_transaction(connection->CreateTransaction());
query_transaction->Query("SELECT TestListReferenceProp FROM Record TestRT");
query_transaction->Execute();
EXPECT_EQ(query_transaction->GetResultTable().GetHeader().size(), 1);
for (const auto &column : query_transaction->GetResultTable().GetHeader()) {
EXPECT_EQ(column.GetName(), "TestListReferenceProp");
}
EXPECT_EQ(query_transaction->GetResultTable().size(), 1);
for (const auto &row : query_transaction->GetResultTable().GetRows()) {
EXPECT_EQ(row.GetValue("TestListReferenceProp").GetAsVector().at(0).GetAsString(), id);
EXPECT_EQ(row.GetValue("TestListReferenceProp").GetAsVector().at(1).GetAsString(), id);
EXPECT_EQ(row.GetValue("TestListReferenceProp").GetAsVector().at(2).GetAsString(), id);
}
}
} // namespace caosdb::transaction } // namespace caosdb::transaction
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment