mirror of
https://github.com/getml/sqlgen.git
synced 2026-01-04 08:30:30 -06:00
Added .is_null(), .is_not_null(), .like(), .not_like() (#14)
This commit is contained in:
committed by
GitHub
parent
97cce29ae6
commit
3d8c4ecbf1
64
tests/sqlite/test_like.cpp
Normal file
64
tests/sqlite/test_like.cpp
Normal file
@@ -0,0 +1,64 @@
|
||||
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
#include <rfl.hpp>
|
||||
#include <rfl/json.hpp>
|
||||
#include <sqlgen.hpp>
|
||||
#include <sqlgen/sqlite.hpp>
|
||||
#include <vector>
|
||||
|
||||
namespace test_like {
|
||||
|
||||
struct Person {
|
||||
sqlgen::PrimaryKey<uint32_t> id;
|
||||
std::string first_name;
|
||||
std::string last_name;
|
||||
int age;
|
||||
};
|
||||
|
||||
TEST(sqlite, test_like) {
|
||||
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}});
|
||||
|
||||
using namespace sqlgen;
|
||||
|
||||
const auto conn = sqlite::connect();
|
||||
|
||||
const auto people2 =
|
||||
conn.and_then(write(std::ref(people1)))
|
||||
.and_then(sqlgen::read<std::vector<Person>> |
|
||||
where("first_name"_c.like("H%")) | order_by("age"_c))
|
||||
.value();
|
||||
|
||||
const auto people3 =
|
||||
conn.and_then(sqlgen::read<std::vector<Person>> |
|
||||
where("first_name"_c.not_like("H%")) | order_by("age"_c))
|
||||
.value();
|
||||
|
||||
const auto people4 =
|
||||
conn.and_then(sqlgen::read<std::vector<Person>> |
|
||||
where("first_name"_c.like("O'Reilly")) | order_by("age"_c))
|
||||
.value();
|
||||
|
||||
const std::string expected1 =
|
||||
R"([{"id":4,"first_name":"Hugo","last_name":"Simpson","age":10},{"id":0,"first_name":"Homer","last_name":"Simpson","age":45}])";
|
||||
|
||||
const std::string expected2 =
|
||||
R"([{"id":3,"first_name":"Maggie","last_name":"Simpson","age":0},{"id":2,"first_name":"Lisa","last_name":"Simpson","age":8},{"id":1,"first_name":"Bart","last_name":"Simpson","age":10}])";
|
||||
|
||||
const std::string expected3 = R"([])";
|
||||
|
||||
EXPECT_EQ(rfl::json::write(people2), expected1);
|
||||
EXPECT_EQ(rfl::json::write(people3), expected2);
|
||||
EXPECT_EQ(rfl::json::write(people4), expected3);
|
||||
}
|
||||
|
||||
} // namespace test_like
|
||||
|
||||
Reference in New Issue
Block a user