add items repository implementation

This commit is contained in:
Bartosz Wieczorek 2018-03-16 08:03:52 +01:00
parent 372abf5506
commit 4b64a9a86a
7 changed files with 89 additions and 8 deletions

View File

@ -72,7 +72,7 @@ int main(int argc, char ** argv) {
},
// categories factory
[db = dbIt->get()](eedb::User * owner) { //
[db = dbIt->get()](eedb::User *) { //
return std::make_unique< eedb::PgCategoriesRepository >(*db);
},

View File

@ -0,0 +1,29 @@
#pragma once
#include <eedb/db/Item.hpp>
#include <utils/spimpl.hpp>
namespace eedb::db {
class PgConnection;
}
namespace eedb {
class PgItem : public Item {
// Item interface
public:
PgItem(db::PgConnection & db, int64_t uid, item::Attributes attributes);
std::string_view displayName() const override;
Values values() const override;
CategoriesIt attachedToCategories() const override;
bool attach(const Category * category) override;
private:
struct ItemPriv;
spimpl::impl_ptr< ItemPriv > _priv;
};
} // namespace eedb

View File

@ -36,18 +36,26 @@ class CategoryMock : public ::eedb::CategoryMock {
void _initRoot() {
using namespace testing;
_root_id = _db(sqlpp::postgresql::insert_into(t_category) //
const auto & row = _db(sqlpp::postgresql::insert_into(t_category) //
.set(t_category.name = "root", //
t_category.parent_id = sqlpp::null,
t_category.owner = sqlpp::verbatim< sqlpp::integer >(" app_current_user_id() ") )
.returning(t_category.id))
.front()
.id;
t_category.owner = sqlpp::verbatim< sqlpp::integer >(" app_current_user_id() "))
.returning(t_category.id, t_category.parent_path))
.front();
_root_id = row.id;
_path = row.parent_path;
}
void _expect_call_path() {
using namespace testing;
EXPECT_CALL(*this, path()).WillRepeatedly(Return(_path));
}
private:
PgConnection & _db;
int64_t _root_id{0};
std::string _path;
};
} // namespace eedb::db::pg

View File

@ -1 +1,13 @@
#include <eedb/db/pg/PgItem.hpp>
#include <eedb/Value.hpp>
#include <eedb/db/pg/connection.hpp>
namespace eedb {
struct PgItem::ItemPriv {};
PgItem::PgItem(db::PgConnection & db, int64_t uid, item::Attributes attributes) : _priv{spimpl::make_impl< ItemPriv >()} {}
} // namespace eedb

View File

@ -4,6 +4,7 @@
#include <eedb/db/pg/config.hpp>
#include <eedb/db/pg/connection.hpp>
#include <sqlpp11/postgresql/exception.h>
#include <sqlpp11/sqlpp11.h>
#include <boost/process.hpp>
#include <nlohmann/json.hpp>
@ -127,7 +128,7 @@ class DbTestBase : public testing::Test {
return _test_db->db();
}
virtual ~DbTestBase(){
virtual ~DbTestBase() {
transaction.rollback();
}

View File

@ -0,0 +1,30 @@
#include <gmock/gmock.h>
#include <eedb/db/pg/PgItem.hpp>
#include <eedb/db/pg/model/all.hpp>
#include "DbTestBase.hpp"
#include <eedb/mock/db/pg/CategoryMock.hpp>
#include <eedb/mock/db/pg/ItemMock.hpp>
#include <eedb/mock/db/pg/ParameterMock.hpp>
#include <eedb/mock/db/pg/UserMock.hpp>
class PgItemTest : public DbTestBase< PgItemTest > {
public:
PgItemTest() : user{db()} {
using namespace testing;
user._init();
// sut = std::make_unique< eedb::PgItem >(db());
}
protected:
eedb::db::pg::UserMock user;
std::unique_ptr< eedb::PgItem > sut;
};
template <>
std::unique_ptr< PgTestDatabasePrepare > DbTestBase< PgItemTest >::_test_db = {};

View File

@ -18,6 +18,7 @@ class PgItemsRepositoryTest : public DbTestBase< PgItemsRepositoryTest > {
using namespace testing;
user._init();
category._init_simple("main");
sut = std::make_unique< eedb::PgItemsRepository >(db());
}
@ -62,6 +63,6 @@ TEST_F(PgItemsRepositoryTest, createItemDontFail) {
TEST_F(PgItemsRepositoryTest, createReturnsValidParameter) {
EXPECT_NO_THROW(sut->create(some_id(), some_attributes(), {}));
EXPECT_CALL(category, path()).WillOnce(testing::Return(std::string_view{"root.1"}));
category._expect_call_path();
auto items = sut->search(eedb::ItemQueryFilters{&category});
}