added belongs-to-eager test
This commit is contained in:
@@ -9,6 +9,9 @@
|
||||
#include "QueryFixture.hpp"
|
||||
|
||||
#include "models/airplane.hpp"
|
||||
#include "models/author.hpp"
|
||||
#include "models/book.hpp"
|
||||
#include "models/department.hpp"
|
||||
#include "models/flight.hpp"
|
||||
#include "models/person.hpp"
|
||||
#include "models/recipe.hpp"
|
||||
@@ -577,4 +580,150 @@ TEST_CASE_METHOD(QueryFixture, "Test load entity with eager has many relation",
|
||||
REQUIRE(s.tracking_number == shipments.at(index)->tracking_number);
|
||||
REQUIRE(s.packages.size() == packages_sizes.at(index++));
|
||||
}
|
||||
}
|
||||
|
||||
TEST_CASE_METHOD(QueryFixture, "Test load entity with lazy belongs to relation", "[query][belongs_to][lazy]") {
|
||||
auto result = repo.attach<department>("departments")
|
||||
.and_then([this] { return repo.attach<employee>("employees"); })
|
||||
.and_then([this] {return repo.create(db); });
|
||||
REQUIRE(result.is_ok());
|
||||
|
||||
const std::vector deps {
|
||||
object_ptr{new department{1, "Human Resources"}},
|
||||
object_ptr{new department{2, "Invoices"}}
|
||||
};
|
||||
|
||||
const std::vector emps {
|
||||
object_ptr{new employee{3, "Hans", "Wurst", deps[0]}},
|
||||
object_ptr{new employee{4, "Steven", "Spielberg", deps[0]}},
|
||||
object_ptr{new employee{5, "Julia", "Roberts", deps[0]}},
|
||||
object_ptr{new employee{6, "Otto", "Walkes", deps[1]}},
|
||||
object_ptr{new employee{7, "Miss", "Marple", deps[1]}},
|
||||
};
|
||||
|
||||
for (const auto &dep: deps) {
|
||||
auto res = query::insert()
|
||||
.into(DEPARTMENT, {DEPARTMENT.id, DEPARTMENT.name})
|
||||
.values(*dep)
|
||||
.execute(db);
|
||||
REQUIRE(res.is_ok());
|
||||
REQUIRE(*res == 1);
|
||||
}
|
||||
|
||||
auto count = query::select({count_all()})
|
||||
.from(DEPARTMENT)
|
||||
.fetch_value<int>(db);
|
||||
REQUIRE(count.is_ok());
|
||||
REQUIRE(*count == 2);
|
||||
|
||||
for (const auto &emp: emps) {
|
||||
auto res = query::insert()
|
||||
.into(EMPLOYEE, {EMPLOYEE.id, EMPLOYEE.first_name, EMPLOYEE.last_name, EMPLOYEE.dep_id})
|
||||
.values(*emp)
|
||||
.execute(db);
|
||||
REQUIRE(res.is_ok());
|
||||
REQUIRE(*res == 1);
|
||||
}
|
||||
|
||||
count = query::select({count_all()})
|
||||
.from(EMPLOYEE)
|
||||
.fetch_value<int>(db);
|
||||
REQUIRE(count.is_ok());
|
||||
REQUIRE(*count == 5);
|
||||
|
||||
auto emps_result = query::select({EMPLOYEE.id, EMPLOYEE.first_name, EMPLOYEE.last_name, DEPARTMENT.id, DEPARTMENT.name})
|
||||
.from(EMPLOYEE)
|
||||
.join_left(DEPARTMENT)
|
||||
.on(EMPLOYEE.dep_id == DEPARTMENT.id)
|
||||
.order_by(EMPLOYEE.id).asc()
|
||||
.fetch_all<employee>(db);
|
||||
|
||||
REQUIRE(emps_result.is_ok());
|
||||
size_t index{0};
|
||||
for (const auto &e : *emps_result) {
|
||||
REQUIRE(e.id == emps.at(index)->id);
|
||||
REQUIRE(e.first_name == emps.at(index)->first_name);
|
||||
REQUIRE(e.last_name == emps.at(index)->last_name);
|
||||
REQUIRE(e.dep->id == emps.at(index)->dep->id);
|
||||
REQUIRE(e.dep->name == emps.at(index)->dep->name);
|
||||
++index;
|
||||
}
|
||||
}
|
||||
|
||||
TEST_CASE_METHOD(QueryFixture, "Test load entity with eager belongs to relation", "[query][belongs_to][eager]") {
|
||||
auto result = repo.attach<author>("authors")
|
||||
.and_then( [this] { return repo.attach<book>("books"); } )
|
||||
.and_then([this] {
|
||||
return repo.create(db);
|
||||
} );
|
||||
|
||||
const std::vector authors {
|
||||
object_ptr{new author{1, "Michael", "Crichton", "23.10.1942", 1975, true, {}}},
|
||||
object_ptr{new author{ 2, "Steven", "King", "21.9.1947", 1956, false, {}}}
|
||||
};
|
||||
|
||||
const std::vector books {
|
||||
object_ptr{new book{3, "Jurassic Park", authors[0], 1990}},
|
||||
object_ptr{new book{4, "Timeline", authors[0], 1999}},
|
||||
object_ptr{new book{5, "The Andromeda Strain", authors[0], 1969}},
|
||||
object_ptr{new book{6, "Congo", authors[0], 1980}},
|
||||
object_ptr{new book{7, "Prey", authors[0], 2002}},
|
||||
object_ptr{new book{8, "Carrie", authors[1], 1974}},
|
||||
object_ptr{new book{9, "The Shining", authors[1], 1977}},
|
||||
object_ptr{new book{10, "It", authors[1], 1986}},
|
||||
object_ptr{new book{11, "Misery", authors[1], 1987}},
|
||||
object_ptr{new book{12, "The Dark Tower: The Gunslinger", authors[1], 1982}},
|
||||
};
|
||||
|
||||
for (const auto &a: authors) {
|
||||
auto res = query::insert()
|
||||
.into(AUTHOR, {AUTHOR.id, AUTHOR.first_name, AUTHOR.last_name, AUTHOR.date_of_birth, AUTHOR.year_of_birth, AUTHOR.distinguished})
|
||||
.values(*a)
|
||||
.execute(db);
|
||||
REQUIRE(res.is_ok());
|
||||
REQUIRE(*res == 1);
|
||||
}
|
||||
|
||||
auto count = query::select({count_all()})
|
||||
.from(AUTHOR)
|
||||
.fetch_value<int>(db);
|
||||
REQUIRE(count.is_ok());
|
||||
REQUIRE(*count == 2);
|
||||
|
||||
for (const auto &b: books) {
|
||||
auto res = query::insert()
|
||||
.into(BOOK, {BOOK.id, BOOK.title, BOOK.author_id, BOOK.published_in})
|
||||
.values(*b)
|
||||
.execute(db);
|
||||
REQUIRE(res.is_ok());
|
||||
REQUIRE(*res == 1);
|
||||
}
|
||||
|
||||
count = query::select({count_all()})
|
||||
.from(BOOK)
|
||||
.fetch_value<int>(db);
|
||||
REQUIRE(count.is_ok());
|
||||
REQUIRE(*count == 10);
|
||||
|
||||
auto books_result = query::select({BOOK.id, BOOK.title, AUTHOR.id, AUTHOR.first_name, AUTHOR.last_name, AUTHOR.date_of_birth, AUTHOR.year_of_birth, AUTHOR.distinguished, BOOK.published_in})
|
||||
.from(BOOK)
|
||||
.join_left(AUTHOR)
|
||||
.on(BOOK.author_id == AUTHOR.id)
|
||||
.order_by(BOOK.id).asc()
|
||||
.fetch_all<book>(db);
|
||||
|
||||
REQUIRE(books_result.is_ok());
|
||||
size_t index{0};
|
||||
for (const auto &b : *books_result) {
|
||||
REQUIRE(b.id == books.at(index)->id);
|
||||
REQUIRE(b.title == books.at(index)->title);
|
||||
REQUIRE(b.book_author->id == books.at(index)->book_author->id);
|
||||
REQUIRE(b.book_author->first_name == books.at(index)->book_author->first_name);
|
||||
REQUIRE(b.book_author->last_name == books.at(index)->book_author->last_name);
|
||||
REQUIRE(b.book_author->date_of_birth == books.at(index)->book_author->date_of_birth);
|
||||
REQUIRE(b.book_author->year_of_birth == books.at(index)->book_author->year_of_birth);
|
||||
REQUIRE(b.book_author->distinguished == books.at(index)->book_author->distinguished);
|
||||
REQUIRE(b.published_in == books.at(index)->published_in);
|
||||
++index;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user