mirror of
https://github.com/getml/sqlgen.git
synced 2026-01-05 17:09:50 -06:00
This commit is contained in:
committed by
GitHub
parent
0ea4d461d4
commit
c8f4ad8d84
57
tests/sqlite/test_update_with_optional.cpp
Normal file
57
tests/sqlite/test_update_with_optional.cpp
Normal file
@@ -0,0 +1,57 @@
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
#include <rfl.hpp>
|
||||
#include <rfl/json.hpp>
|
||||
#include <sqlgen.hpp>
|
||||
#include <sqlgen/sqlite.hpp>
|
||||
#include <vector>
|
||||
|
||||
namespace test_update_with_optional {
|
||||
|
||||
struct Person {
|
||||
sqlgen::PrimaryKey<uint32_t> id;
|
||||
std::string first_name;
|
||||
std::string last_name;
|
||||
std::optional<int> age;
|
||||
};
|
||||
|
||||
TEST(sqlite, test_update_with_optional) {
|
||||
const auto people1 = std::vector<Person>(
|
||||
{Person{
|
||||
.id = 0, .first_name = "Homer", .last_name = "Simpson", .age = 45},
|
||||
Person{.id = 1, .first_name = "Bart", .last_name = "Simpson", .age = 10},
|
||||
Person{.id = 2, .first_name = "Lisa", .last_name = "Simpson", .age = 8},
|
||||
Person{
|
||||
.id = 3, .first_name = "Maggie", .last_name = "Simpson", .age = 0},
|
||||
Person{
|
||||
.id = 4, .first_name = "Hugo", .last_name = "Simpson", .age = 10}});
|
||||
|
||||
const auto conn = sqlgen::sqlite::connect();
|
||||
|
||||
sqlgen::write(conn, people1);
|
||||
|
||||
using namespace sqlgen;
|
||||
using namespace sqlgen::literals;
|
||||
|
||||
const auto query1 = update<Person>("first_name"_c.set("last_name"_c),
|
||||
"age"_c.set(std::nullopt)) |
|
||||
where("first_name"_c == "Hugo");
|
||||
|
||||
const auto query2 =
|
||||
update<Person>("age"_c.set(50)) | where("first_name"_c == "Homer");
|
||||
|
||||
const auto query3 = update<Person>("age"_c.set(std::optional<int>(11))) |
|
||||
where("first_name"_c == "Bart");
|
||||
|
||||
query1(conn).and_then(query2).and_then(query3).value();
|
||||
|
||||
const auto people2 =
|
||||
(sqlgen::read<std::vector<Person>> | order_by("id"_c))(conn).value();
|
||||
|
||||
const std::string expected =
|
||||
R"([{"id":0,"first_name":"Homer","last_name":"Simpson","age":50},{"id":1,"first_name":"Bart","last_name":"Simpson","age":11},{"id":2,"first_name":"Lisa","last_name":"Simpson","age":8},{"id":3,"first_name":"Maggie","last_name":"Simpson","age":0},{"id":4,"first_name":"Simpson","last_name":"Simpson"}])";
|
||||
|
||||
EXPECT_EQ(rfl::json::write(people2), expected);
|
||||
}
|
||||
|
||||
} // namespace test_update_with_optional
|
||||
Reference in New Issue
Block a user