From 4b0e3e43715b59476c4e6299fd0e9d2fab3d13fa Mon Sep 17 00:00:00 2001 From: Hamed Masafi Date: Sun, 14 Jan 2018 17:33:24 +0330 Subject: [PATCH] join test created --- src/databasemodel.cpp | 9 ++-- src/query.cpp | 2 +- src/query.h | 31 ++++++++------ src/query_p.h | 1 - test/basic/tst_basic.pro | 6 ++- test/common/post.cpp | 5 ++- test/common/post.h | 2 + test/common/score.cpp | 6 +++ test/common/score.h | 24 +++++++++++ test/common/user.cpp | 8 ++-- test/common/user.h | 2 + test/common/weblogdatabase.cpp | 4 +- test/common/weblogdatabase.h | 2 + test/join/jointest.cpp | 75 ++++++++++++++++++++++++++++++++++ test/join/jointest.h | 26 ++++++++++++ test/join/tst_join.pro | 26 ++++++++++++ 16 files changed, 204 insertions(+), 25 deletions(-) create mode 100644 test/common/score.cpp create mode 100644 test/common/score.h create mode 100644 test/join/jointest.cpp create mode 100644 test/join/jointest.h create mode 100644 test/join/tst_join.pro diff --git a/src/databasemodel.cpp b/src/databasemodel.cpp index ea49a1d..e5adc6d 100644 --- a/src/databasemodel.cpp +++ b/src/databasemodel.cpp @@ -30,17 +30,20 @@ QMap DatabaseModel::_models; #define NODE_VERSION "version" #define NODE_TABLES "tables" -DatabaseModel::DatabaseModel(const QString &name) : QList(), _databaseClassName(name), _version(QString::null) +DatabaseModel::DatabaseModel(const QString &name) : + QList(), _databaseClassName(name), _version(QString::null) { _models.insert(name, this); } -DatabaseModel::DatabaseModel(const DatabaseModel &other) : QList(other), _version(QString::null) +DatabaseModel::DatabaseModel(const DatabaseModel &other) : + QList(other), _version(QString::null) { } -DatabaseModel::DatabaseModel(const QJsonObject &json) : QList() +DatabaseModel::DatabaseModel(const QJsonObject &json) : + QList() { setVersion(json.value(NODE_VERSION).toString()); diff --git a/src/query.cpp b/src/query.cpp index 9fc86ca..eacf1ba 100644 --- a/src/query.cpp +++ b/src/query.cpp @@ -23,7 +23,7 @@ NUT_BEGIN_NAMESPACE QueryPrivate::QueryPrivate(QueryBase *parent) : q_ptr(parent), - joinClassName(QString::null), skip(-1), take(-1) + skip(-1), take(-1) { } diff --git a/src/query.h b/src/query.h index a891d36..86b4ff7 100644 --- a/src/query.h +++ b/src/query.h @@ -128,7 +128,7 @@ Q_OUTOFLINE_TEMPLATE QList Query::toList(int count) { Q_UNUSED(count); Q_D(Query); - QList result; + QList returnList; d->select = "*"; QElapsedTimer t; t.start(); @@ -140,22 +140,29 @@ Q_OUTOFLINE_TEMPLATE QList Query::toList(int count) d->wheres, d->orderPhrases, d->relations, d->skip, d->take); QSqlQuery q = d->database->exec(d->sql); - if (q.lastError().isValid()) + if (q.lastError().isValid()) { qDebug() << q.lastError().text(); - + return returnList; + } QSet relatedTables; foreach (RelationModel *rel, d->relations) relatedTables << rel->slaveTable->name() << rel->masterTable->name(); - QStringList childTables, masterTables; + QSet childTables, masterTables; QMap lastClassRow; foreach (RelationModel *rel, d->relations) { - childTables.append(rel->slaveTable->name()); - masterTables.append(rel->masterTable->name()); + childTables.insert(rel->slaveTable->name()); + masterTables.insert(rel->masterTable->name()); } + foreach (QString ch, childTables) { + if (masterTables.contains(ch)) + childTables.remove(ch); + } + + qDebug() << "TABLES:"; qDebug() << childTables; qDebug() << masterTables; @@ -173,7 +180,6 @@ qDebug() << masterTables; Table *lastRow; }; QVector levels; - QList returnList; QList::iterator i; for (int i = 0; i < d->relations.count(); ++i) { LevelData data; @@ -378,12 +384,13 @@ Q_OUTOFLINE_TEMPLATE Query *Query::join(const QString &className) if (!rel) rel = d->database->model().relationByClassNames(className, d->className); - if (!rel) - qFatal("No relation beyween %s and %s", - qPrintable(d->className), qPrintable(className)); + if (!rel) { + qInfo("No relation between %s and %s", + qPrintable(d->className), qPrintable(className)); + return this; + } d->relations.append(rel); - d->joinClassName = className; d->joins.append(className); return this; } @@ -440,7 +447,6 @@ template Q_OUTOFLINE_TEMPLATE Query *Query::include(TableSetBase *t) { Q_D(Query); - d->joinClassName = t->childClassName(); return this; } @@ -448,7 +454,6 @@ template Q_OUTOFLINE_TEMPLATE Query *Query::include(Table *t) { Q_D(Query); - d->joinClassName = t->metaObject()->className(); return this; } diff --git a/src/query_p.h b/src/query_p.h index 54ff7c6..4b48859 100644 --- a/src/query_p.h +++ b/src/query_p.h @@ -46,7 +46,6 @@ public: QString select; Database *database; TableSetBase *tableSet; - QString joinClassName; QStringList joins; QList relations; QList wheres; diff --git a/test/basic/tst_basic.pro b/test/basic/tst_basic.pro index 2a11746..a274bdc 100644 --- a/test/basic/tst_basic.pro +++ b/test/basic/tst_basic.pro @@ -13,7 +13,8 @@ SOURCES += \ ../common/comment.cpp \ ../common/post.cpp \ ../common/user.cpp \ - ../common/weblogdatabase.cpp + ../common/weblogdatabase.cpp \ + ../common/score.cpp HEADERS += \ maintest.h \ @@ -21,4 +22,5 @@ HEADERS += \ ../common/comment.h \ ../common/post.h \ ../common/user.h \ - ../common/weblogdatabase.h + ../common/weblogdatabase.h \ + ../common/score.h diff --git a/test/common/post.cpp b/test/common/post.cpp index bf980f6..46df0f6 100644 --- a/test/common/post.cpp +++ b/test/common/post.cpp @@ -1,9 +1,12 @@ #include "post.h" #include "comment.h" +#include "score.h" #include "tableset.h" Post::Post(QObject *parent) : Table(parent), - m_id(0), m_title(""), m_comments(new TableSet(this)) + m_id(0), m_title(""), + m_comments(new TableSet(this)), + m_scores(new TableSet(this)) { } diff --git a/test/common/post.h b/test/common/post.h index 5b25e23..b020804 100644 --- a/test/common/post.h +++ b/test/common/post.h @@ -11,6 +11,7 @@ using namespace NUT_NAMESPACE; #endif class Comment; +class Score; class Post : public Table { Q_OBJECT @@ -27,6 +28,7 @@ class Post : public Table NUT_DECLARE_FIELD(QString, body, body, setBody) NUT_DECLARE_CHILD_TABLE(Comment, comments) + NUT_DECLARE_CHILD_TABLE(Score, scores) public: Q_INVOKABLE Post(QObject *tableSet = 0); diff --git a/test/common/score.cpp b/test/common/score.cpp new file mode 100644 index 0000000..08466a2 --- /dev/null +++ b/test/common/score.cpp @@ -0,0 +1,6 @@ +#include "score.h" + +Score::Score(QObject *parent) : Nut::Table(parent) +{ + +} diff --git a/test/common/score.h b/test/common/score.h new file mode 100644 index 0000000..02ce240 --- /dev/null +++ b/test/common/score.h @@ -0,0 +1,24 @@ +#ifndef SCORE_H +#define SCORE_H + +#include "table.h" + +class User; +class Post; +class Score : public Nut::Table +{ + Q_OBJECT + + NUT_PRIMARY_AUTO_INCREMENT(id) + NUT_DECLARE_FIELD(int, id, id, setId) + + NUT_DECLARE_FIELD(int, score, score, setScore) + + NUT_FOREGION_KEY(Post, int, post, post, setPost) + NUT_FOREGION_KEY(User, int, user, user, setUser) + +public: + Q_INVOKABLE Score(QObject *parent = Q_NULLPTR); +}; + +#endif // SCORE_H diff --git a/test/common/user.cpp b/test/common/user.cpp index 90abc0c..b2ee974 100644 --- a/test/common/user.cpp +++ b/test/common/user.cpp @@ -1,9 +1,11 @@ +#include "comment.h" +#include "score.h" + #include "user.h" -#include "comment.h" - User::User(QObject *tableSet) : Table(tableSet), - m_comments(new TableSet(this)) + m_comments(new TableSet(this)), + m_scores(new TableSet(this)) { } diff --git a/test/common/user.h b/test/common/user.h index dd3330c..667efd6 100644 --- a/test/common/user.h +++ b/test/common/user.h @@ -12,6 +12,7 @@ using namespace NUT_NAMESPACE; #endif class Comment; +class Score; class User : public Nut::Table { Q_OBJECT @@ -28,6 +29,7 @@ class User : public Nut::Table NUT_DECLARE_FIELD(QString, password, password, setPassword) NUT_DECLARE_CHILD_TABLE(Comment, comments) + NUT_DECLARE_CHILD_TABLE(Score, scores) public: Q_INVOKABLE User(QObject *tableSet = 0); diff --git a/test/common/weblogdatabase.cpp b/test/common/weblogdatabase.cpp index 40cba75..cf459b9 100644 --- a/test/common/weblogdatabase.cpp +++ b/test/common/weblogdatabase.cpp @@ -4,11 +4,13 @@ #include "post.h" #include "comment.h" #include "user.h" +#include "score.h" #include "weblogdatabase.h" WeblogDatabase::WeblogDatabase() : Database(), m_posts(new TableSet(this)), m_comments(new TableSet(this)), - m_users(new TableSet(this)) + m_users(new TableSet(this)), + m_scores(new TableSet(this)) { } diff --git a/test/common/weblogdatabase.h b/test/common/weblogdatabase.h index b7ece47..af72442 100644 --- a/test/common/weblogdatabase.h +++ b/test/common/weblogdatabase.h @@ -10,6 +10,7 @@ using namespace NUT_NAMESPACE; class Post; class Comment; class User; +class Score; class WeblogDatabase : public Database { Q_OBJECT @@ -19,6 +20,7 @@ class WeblogDatabase : public Database NUT_DECLARE_TABLE(Post, post) NUT_DECLARE_TABLE(Comment, comment) NUT_DECLARE_TABLE(User, user) + NUT_DECLARE_TABLE(Score, score) public: WeblogDatabase(); diff --git a/test/join/jointest.cpp b/test/join/jointest.cpp new file mode 100644 index 0000000..7a189c2 --- /dev/null +++ b/test/join/jointest.cpp @@ -0,0 +1,75 @@ +#include +#include +#include + +#include "consts.h" + +#include "jointest.h" +#include "query.h" +#include "tableset.h" +#include "tablemodel.h" +#include "databasemodel.h" + +#include "user.h" +#include "post.h" +#include "comment.h" +#include "score.h" + +#define PRINT(x) qDebug() << #x "=" << x; +JoinTest::JoinTest(QObject *parent) : QObject(parent) +{ +} + +void JoinTest::initTestCase() +{ + qDebug() << "User type id:" << qRegisterMetaType(); + qDebug() << "Post type id:" << qRegisterMetaType(); + qDebug() << "Comment type id:" << qRegisterMetaType(); + qDebug() << "Score type id:" << qRegisterMetaType(); + qDebug() << "DB type id:" << qRegisterMetaType(); + + db.setDriver(DRIVER); + db.setHostName(HOST); + db.setDatabaseName("nut_tst_join.db"); + db.setUserName(USERNAME); + db.setPassword(PASSWORD); + + bool ok = db.open(); + +// db.comments()->query()->remove(); +// db.posts()->query()->remove(); + + QTEST_ASSERT(ok); +} + +void JoinTest::join() +{ + auto q = db.comments()->query() + ->join() + ->join(); + +// Comment *comment = q->first(); + auto comments = q->toList(); +// Comment *comment = q->toList().first(); + qDebug() << q->sqlCommand(); + QTEST_ASSERT(comments.length()); + QTEST_ASSERT(comments[0]->author()); + QTEST_ASSERT(comments[0]->author()->username() == "admin"); +} + +void JoinTest::join2() +{ + auto q = db.users()->query() + ->join() + ->join(); + +// Comment *comment = q->first(); + auto comments = q->toList(); +// Comment *comment = q->toList().first(); +// qDebug() << q->sqlCommand(); +// QTEST_ASSERT(comments.length()); +// QTEST_ASSERT(comments[0]->author()); +// QTEST_ASSERT(comments[0]->author()->username() == "admin"); +} + +QTEST_MAIN(JoinTest) diff --git a/test/join/jointest.h b/test/join/jointest.h new file mode 100644 index 0000000..34bac28 --- /dev/null +++ b/test/join/jointest.h @@ -0,0 +1,26 @@ +#ifndef JOINTEST_H +#define JOINTEST_H + +#include +#include + +#include "weblogdatabase.h" + +class JoinTest : public QObject +{ + Q_OBJECT + WeblogDatabase db; + +public: + explicit JoinTest(QObject *parent = 0); + +signals: + +private slots: + void initTestCase(); + + void join2(); + void join(); +}; + +#endif // JOINTEST_H diff --git a/test/join/tst_join.pro b/test/join/tst_join.pro new file mode 100644 index 0000000..78533c7 --- /dev/null +++ b/test/join/tst_join.pro @@ -0,0 +1,26 @@ +QT += qml quick testlib sql +QT -= gui + +TARGET = tst_nut +TEMPLATE = app + +CONFIG += warn_on qmltestcase c++11 +INCLUDEPATH += $$PWD/../../src $$PWD/../common +include(../../nut.pri) +IMPORTPATH += $$OUT_PWD/../src/imports +SOURCES += \ + jointest.cpp \ + ../common/comment.cpp \ + ../common/post.cpp \ + ../common/user.cpp \ + ../common/weblogdatabase.cpp \ + ../common/score.cpp + +HEADERS += \ + jointest.h \ + ../common/consts.h \ + ../common/comment.h \ + ../common/post.h \ + ../common/user.h \ + ../common/weblogdatabase.h \ + ../common/score.h