Nut/tests/auto/tst_join/jointest.cpp

78 lines
1.7 KiB
C++
Raw Normal View History

2018-01-14 22:03:24 +08:00
#include <QtTest>
#include <QJsonDocument>
#include <QSqlError>
#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()
{
2019-02-16 21:36:38 +08:00
REGISTER(User);
REGISTER(Post);
REGISTER(Comment);
REGISTER(Score);
REGISTER(WeblogDatabase);
2018-01-14 22:03:24 +08:00
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<User>()
->join<Post>();
// Comment *comment = q->first();
auto comments = q->toList();
// Comment *comment = q->toList().first();
2018-01-15 06:12:46 +08:00
// qDebug() << q->sqlCommand();
PRINT(comments.length());
// QTEST_ASSERT(comments.length());
// QTEST_ASSERT(comments[0]->author());
// QTEST_ASSERT(comments[0]->author()->username() == "admin");
2018-01-14 22:03:24 +08:00
}
void JoinTest::join2()
{
auto q = db.users()->query()
2018-01-15 06:12:46 +08:00
->join<Comment>()
->join<Score>();
2018-01-14 22:03:24 +08:00
// 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)