Skip to content

Commit

Permalink
Add Test for ORM
Browse files Browse the repository at this point in the history
  • Loading branch information
SchaichAlonso committed Aug 29, 2023
1 parent ef38cbd commit 3ca86c7
Show file tree
Hide file tree
Showing 8 changed files with 543 additions and 2 deletions.
161 changes: 161 additions & 0 deletions BookModel.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
#include <orm/schema.hpp>

#include "BookModel.hpp"
#include "PublisherModel.hpp"

const QString BookModel::g_property_id = "id";
const QString BookModel::g_property_publisher("publisher");
const QString BookModel::g_property_author = "author";
const QString BookModel::g_property_title = "title";
const QString BookModel::g_property_releaseDate = "released";
const QString BookModel::g_property_isbn = "isbn";
const QString BookModel::g_table("books");

QStringList BookModel::u_fillable {
g_property_id,
g_property_author,
g_property_title,
g_property_releaseDate,
g_property_isbn
};

const bool BookModel::u_snakeAttributes(false);

BookModel::BookModel(QString author, QString title, QDate releaseDate, QString isbn)
: BookModel()
{
setAuthor(author);
setTitle(title);
setReleaseDate(releaseDate);
setIsbn(isbn);
}

BookModel::BookModel()
: Model()
, u_relations(relations())
, u_table(table())
{
}

BookModel::~BookModel()
{
}

BookModel::BookModel(const BookModel &other)
: Model(other)
, u_relations(relations())
, u_table(table())
{
}

BookModel::BookModel(Orm::Tiny::DontFillDefaultAttributes attributes)
: Model(attributes)
, u_relations(relations())
, u_table(table())
{
}

BookModel::BookModel(const QVector<AttributeItem> &attributes)
: Model(attributes)
, u_relations(relations())
, u_table(table())
{
}

BookModel::BookModel(std::initializer_list<AttributeItem> attributes)
: Model(attributes)
, u_relations(relations())
, u_table(table())
{
}

BookModel &BookModel::operator=(const BookModel &other)
{
Model::operator=(other);
// do not overwrite u_relations and u_table
return *this;
}

QHash<QString, BookModel::RelationVisitor> BookModel::relations() const
{
return {
{PublisherModel::g_table, [](RelationStore& visitor) { visitor(&BookModel::publisher); }}
};
}

QString BookModel::table() const
{
return g_table;
}

std::unique_ptr<BookModel::BelongsTo<BookModel, PublisherModel>>
BookModel::publisher()
{
return belongsTo<PublisherModel>();
}

void BookModel::createTableCallback(Orm::SchemaNs::Blueprint& table)
{
table.id();
table.foreignId(g_property_publisher).nullable().constrained().cascadeOnDelete().cascadeOnUpdate();
table.string(g_property_author);
table.string(g_property_title);
table.string(g_property_isbn);
table.date(g_property_releaseDate);
table.timestamps();
}

void BookModel::createTable()
{
Orm::Schema::create(g_table, BookModel::createTableCallback);
}

void BookModel::dropTable()
{
Orm::Schema::drop(g_table);
}

qlonglong BookModel::id() const
{
return getAttribute(g_property_id).toLongLong();
}
void BookModel::setId(qlonglong id)
{
setAttribute(g_property_id, id);
}

QString BookModel::author() const
{
return getAttribute(g_property_author).toString();
}
void BookModel::setAuthor(QString author)
{
setAttribute(g_property_author, author);
}

QString BookModel::title() const
{
return getAttribute(g_property_title).toString();
}
void BookModel::setTitle(QString title)
{
setAttribute(g_property_title, title);
}

QString BookModel::isbn() const
{
return getAttribute(g_property_isbn).toString();
}
void BookModel::setIsbn(QString isbn)
{
setAttribute(g_property_isbn, isbn);
}

QDate BookModel::releaseDate() const
{
return getAttribute(g_property_releaseDate).toDate();
}
void BookModel::setReleaseDate(QDate released)
{
setAttribute(g_property_releaseDate, released);
}
72 changes: 72 additions & 0 deletions BookModel.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
#pragma once

#include <orm/tiny/model.hpp>

#include "PublisherModel.hpp"

class PublisherModel;

class BookModel final : public Orm::Tiny::Model<BookModel, PublisherModel>
{
friend Model;
using Model::Model;

public:
BookModel(QString author, QString title, QDate releaseDate, QString isbn);
BookModel();
~BookModel();
BookModel(const BookModel &other);
BookModel(Orm::Tiny::DontFillDefaultAttributes);
BookModel(const QVector<AttributeItem> &attributes);
BookModel(std::initializer_list<AttributeItem> attributes);
BookModel &operator=(const BookModel &other);
Model &operator=(Model &&) noexcept = delete;

static void createTable();
static void dropTable();

qlonglong id() const;
void setId(qlonglong id);

QString author() const;
void setAuthor(QString author);

QString title() const;
void setTitle(QString title);

QDate releaseDate() const;
void setReleaseDate(QDate released);

QString isbn() const;
void setIsbn(QString isbn);

public:
std::unique_ptr<BelongsTo<BookModel, PublisherModel>> publisher();

private:
using RelationStore = Orm::Tiny::Support::Stores::BaseRelationStore<BookModel, PublisherModel>;

static QStringList u_fillable;
static const bool u_snakeAttributes;

QHash<QString, RelationVisitor> u_relations;
QString u_table;

protected:
static void createTableCallback(Orm::SchemaNs::Blueprint& table);
QHash<QString, RelationVisitor> relations() const;
QString table() const;

public:
static const QString g_property_id;
static const QString g_property_publisher;
static const QString g_property_author;
static const QString g_property_title;
static const QString g_property_releaseDate;
static const QString g_property_isbn;
static const QString g_table;
};




2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ set_target_properties(QSqlQueryTest PROPERTIES MSVC_RUNTIME_LIBRARY ${MSVC_RUNTI

find_package(TinyOrm CONFIG REQUIRED)
add_executable(TinyOrmQueryTest)
target_sources(TinyOrmQueryTest PRIVATE TinyOrmQueryTest.cpp)
target_sources(TinyOrmQueryTest PRIVATE BookModel.cpp PublisherModel.cpp TinyOrmQueryTest.cpp)
target_link_libraries(TinyOrmQueryTest PRIVATE Qt::Core Qt::Sql Qt::Test TinyOrm::TinyOrm)
set_target_properties(TinyOrmQueryTest PROPERTIES MSVC_RUNTIME_LIBRARY ${MSVC_RUNTIME_LIBRARY})

Expand Down
155 changes: 155 additions & 0 deletions PublisherModel.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
#include <orm/schema.hpp>

#include "BookModel.hpp"
#include "PublisherModel.hpp"

const QString PublisherModel::g_property_id("id");
const QString PublisherModel::g_property_name("name");
const QString PublisherModel::g_property_founded("founded");
const QString PublisherModel::g_property_website("website");
const QString PublisherModel::g_table("publishers");

const QVector<QString> PublisherModel::u_with {
BookModel::g_table
};

const QStringList PublisherModel::u_fillable {
g_property_id,
g_property_name,
g_property_founded,
g_property_website
};

const QVector<PublisherModel::AttributeItem> PublisherModel::u_attributes {
{ g_property_name, "<null>" },
{ g_property_founded, QDate(1970,1,1).toString(Qt::ISODate) },
{ g_property_website, "<null>" }
};

const bool PublisherModel::u_snakeAttributes(false);

PublisherModel::PublisherModel()
: Model()
, u_relations(relations())
, u_table(table())
{
}
PublisherModel::~PublisherModel()
{
}

PublisherModel::PublisherModel(const PublisherModel &other)
: Model(other)
, u_relations(relations())
, u_table(table())
{
}

PublisherModel::PublisherModel(QString name, QDate founded, QUrl website)
: PublisherModel()
{
setName(name);
setFounded(founded);
setWebsite(website);
}

PublisherModel::PublisherModel(Orm::Tiny::DontFillDefaultAttributes attributes)
: Model(attributes)
, u_relations(relations())
, u_table(table())
{
}

PublisherModel::PublisherModel(const QVector<AttributeItem> &attributes)
: Model(attributes)
, u_relations(relations())
, u_table(table())
{
}

PublisherModel::PublisherModel(std::initializer_list<AttributeItem> attributes)
: Model(attributes)
, u_relations(relations())
, u_table(table())
{
}

PublisherModel &PublisherModel::operator=(const PublisherModel &other) {
Model::operator=(other);
// do not overwrite u_relations or u_table
return *this;
}

void PublisherModel::createTableCallback(Orm::SchemaNs::Blueprint& table)
{
table.id();
table.string(g_property_name);
table.date(g_property_founded);
table.string(g_property_website);
table.timestamps();
}

void PublisherModel::createTable()
{
Orm::Schema::create(g_table, PublisherModel::createTableCallback);
}

void PublisherModel::dropTable()
{
Orm::Schema::drop(g_table);
}


qlonglong PublisherModel::id() const
{
return getAttribute(g_property_id).toLongLong();
}
void PublisherModel::setId(qlonglong id)
{
setAttribute(g_property_id, id);
}

QString PublisherModel::name() const
{
return getAttribute(g_property_name).toString();
}
void PublisherModel::setName(QString name)
{
setAttribute(g_property_name, name);
}

QDate PublisherModel::founded() const
{
return getAttribute(g_property_founded).toDate();
}
void PublisherModel::setFounded(QDate founded)
{
setAttribute(g_property_founded, founded);
}

QUrl PublisherModel::website() const
{
return getAttribute(g_property_website).toUrl();
}
void PublisherModel::setWebsite(QUrl website)
{
setAttribute(g_property_website, website.toString());
}

std::unique_ptr<PublisherModel::HasMany<PublisherModel, BookModel>>
PublisherModel::books()
{
return hasMany<BookModel>(BookModel::g_property_publisher);
}

QHash<QString, PublisherModel::RelationVisitor> PublisherModel::relations() const
{
return {
{ BookModel::g_table, [](RelationStore& visitor) { visitor(&PublisherModel::books); } }
};
}

QString PublisherModel::table() const
{
return g_table;
}
Loading

0 comments on commit 3ca86c7

Please sign in to comment.