use result in entity_query_builder

This commit is contained in:
Sascha Kühl
2024-04-10 16:13:37 +02:00
parent b7c12d8217
commit 25bcc362f2
9 changed files with 331 additions and 124 deletions
+3 -3
View File
@@ -21,7 +21,7 @@ TEST_CASE("Create sql query data for entity with eager belongs to", "[query][ent
auto data = eqb.build<book>(17);
REQUIRE(data.has_value());
REQUIRE(data.is_ok());
REQUIRE(data->root_table_name == "books");
REQUIRE(data->joins.size() == 1);
const std::vector<column> expected_columns {
@@ -81,7 +81,7 @@ TEST_CASE("Create sql query data for entity with eager has many belongs to", "[q
auto data = eqb.build<order>(17);
REQUIRE(data.has_value());
REQUIRE(data.is_ok());
REQUIRE(data->root_table_name == "orders");
REQUIRE(data->joins.size() == 1);
const std::vector<column> expected_columns = {
@@ -135,7 +135,7 @@ TEST_CASE("Create sql query data for entity with eager many to many", "[query][e
auto data = eqb.build<ingredient>(17);
REQUIRE(data.has_value());
REQUIRE(data.is_ok());
REQUIRE(data->root_table_name == "ingredients");
REQUIRE(data->joins.size() == 2);
const std::vector<column> expected_columns {
+45 -2
View File
@@ -20,6 +20,14 @@ utils::result<float, math_error>multiply(int x, int y) {
return utils::ok(float(x) * y);
}
utils::result<float, math_error>plus(int x, int y) {
return utils::ok(float(x) + y);
}
utils::result<float, std::string>error_to_string(math_error err) {
return utils::error(std::string("error"));
}
}
using namespace matador;
@@ -41,7 +49,42 @@ TEST_CASE("Result tests", "[result]") {
REQUIRE((res.err() == test::math_error::DIVISION_BY_ZERO));
res = test::divide(4, 2)
.and_then([](const auto &val) { return test::multiply(val, 5); })
.and_then([](const auto &val) { return test::plus(val, 10); });
REQUIRE(res);
REQUIRE(res.is_ok());
REQUIRE(!res.is_error());
REQUIRE((res.value() == 20.0));
res = test::divide(4, 0)
.and_then([](const auto &val) {
return test::multiply(val, 5);
});
REQUIRE(!res);
REQUIRE(!res.is_ok());
REQUIRE(res.is_error());
REQUIRE((res.err() == test::math_error::DIVISION_BY_ZERO));
auto res2 = test::divide(4, 0)
.or_else([](const auto &err) {
return test::error_to_string(err);
});
REQUIRE(!res2);
REQUIRE(!res2.is_ok());
REQUIRE(res2.is_error());
REQUIRE((res2.err() == "error"));
res = test::divide(4, 2)
.and_then([](const auto &val) { return test::multiply(val, 5); })
.transform([](const auto &val) { return val + 10; });
REQUIRE(res);
REQUIRE(res.is_ok());
REQUIRE(!res.is_error());
REQUIRE((res.value() == 20.0));
// res = test::divide(4, 2)
// .and_then<>()
}