Nut/test/basic/maintest.cpp

297 lines
6.9 KiB
C++
Raw Normal View History

2016-05-12 14:08:58 +08:00
#include <QtTest>
#include <QJsonDocument>
#include <QSqlError>
2018-01-15 22:18:49 +08:00
#include <QElapsedTimer>
2016-05-12 14:08:58 +08:00
2016-05-24 14:53:40 +08:00
#include "consts.h"
2016-05-12 14:08:58 +08:00
#include "maintest.h"
#include "query.h"
#include "tableset.h"
2016-05-21 16:09:03 +08:00
#include "tablemodel.h"
2016-05-12 14:08:58 +08:00
#include "databasemodel.h"
2018-01-09 17:33:54 +08:00
#include "user.h"
2016-05-12 14:08:58 +08:00
#include "post.h"
#include "comment.h"
2018-01-16 04:04:42 +08:00
#include "score.h"
2016-05-12 14:08:58 +08:00
2018-01-13 23:59:55 +08:00
#define PRINT(x) qDebug() << #x "=" << x;
2018-01-15 22:18:49 +08:00
#define TIC() QElapsedTimer timer; timer.start()
#define TOC() qDebug() << QString("Elapsed time: %1ms for %2") \
.arg(timer.elapsed() / 1000.) \
.arg(__func__)
2018-10-15 22:34:50 +08:00
#define REGISTER(x) qDebug() << #x << "type id:" << qRegisterMetaType<x*>()
2016-05-12 14:08:58 +08:00
MainTest::MainTest(QObject *parent) : QObject(parent)
{
}
void MainTest::initTestCase()
{
2018-10-15 22:34:50 +08:00
//register all entities with Qt-MetaType mechanism
REGISTER(User);
REGISTER(Post);
REGISTER(Score);
REGISTER(Comment);
REGISTER(WeblogDatabase);
2016-05-12 14:08:58 +08:00
2016-05-24 14:53:40 +08:00
db.setDriver(DRIVER);
db.setHostName(HOST);
2017-09-10 22:18:20 +08:00
db.setDatabaseName("nut_tst_basic");
2016-05-24 14:53:40 +08:00
db.setUserName(USERNAME);
db.setPassword(PASSWORD);
2016-05-21 16:09:03 +08:00
2016-05-12 14:08:58 +08:00
bool ok = db.open();
2018-10-15 22:34:50 +08:00
QTEST_ASSERT(ok);
2016-05-12 14:08:58 +08:00
2018-09-05 13:18:59 +08:00
db.comments()->query()->remove();
db.posts()->query()->remove();
2018-10-15 22:34:50 +08:00
db.users()->query()->remove();
db.scores()->query()->remove();
2016-05-12 14:08:58 +08:00
}
void MainTest::dataScheema()
{
2018-01-09 21:04:21 +08:00
// auto json = db.model().toJson();
// auto model = DatabaseModel::fromJson(json);
2016-05-12 14:08:58 +08:00
// qDebug() << model.toJson();
// qDebug() << db.model().toJson();
2018-01-11 15:13:01 +08:00
// QTEST_ASSERT(model == db.model());
}
void MainTest::createUser()
{
user = new User;
2018-10-15 22:34:50 +08:00
user->setId(QUuid::createUuid());
2018-01-11 15:13:01 +08:00
user->setUsername("admin");
user->setPassword("123456");
2018-09-05 13:18:59 +08:00
db.users()->append(user);
2018-01-11 15:13:01 +08:00
db.saveChanges();
2016-05-12 14:08:58 +08:00
}
void MainTest::createPost()
{
2018-02-26 18:14:36 +08:00
TIC();
2016-05-12 14:08:58 +08:00
Post *newPost = new Post;
newPost->setTitle("post title");
2016-05-21 16:09:03 +08:00
newPost->setSaveDate(QDateTime::currentDateTime());
2018-10-15 22:34:50 +08:00
newPost->setPublic(false);
2016-05-12 14:08:58 +08:00
2018-09-05 13:18:59 +08:00
db.posts()->append(newPost);
2016-05-12 14:08:58 +08:00
for(int i = 0 ; i < 3; i++){
Comment *comment = new Comment;
2018-10-15 22:34:50 +08:00
comment->setId(QUuid::createUuid());
2016-05-12 14:08:58 +08:00
comment->setMessage("comment #" + QString::number(i));
2016-05-21 16:09:03 +08:00
comment->setSaveDate(QDateTime::currentDateTime());
2018-01-12 00:14:29 +08:00
comment->setAuthorId(user->id());
2016-05-12 14:08:58 +08:00
newPost->comments()->append(comment);
}
2018-01-16 04:04:42 +08:00
for (int i = 0; i < 10; ++i) {
Score *score = new Score;
score->setScore(i % 5);
newPost->scores()->append(score);
}
2016-05-12 14:08:58 +08:00
db.saveChanges();
postId = newPost->id();
QTEST_ASSERT(newPost->id() != 0);
2018-02-26 18:14:36 +08:00
TOC();
2016-05-12 14:08:58 +08:00
qDebug() << "New post inserted with id:" << newPost->id();
}
2016-06-05 20:22:26 +08:00
void MainTest::createPost2()
{
2018-10-15 22:34:50 +08:00
//create post on the fly
QVariant postIdVar = db.posts()->query()->insert(
(Post::titleField() = "This is a sample")
& (Post::isPublicField() = true));
2016-06-05 20:22:26 +08:00
2018-10-15 22:34:50 +08:00
QTEST_ASSERT(postIdVar.type() == QVariant::LongLong);
int postId = postIdVar.toInt();
2016-06-05 20:22:26 +08:00
for(int i = 0 ; i < 3; i++){
Comment *comment = new Comment;
2018-10-15 22:34:50 +08:00
comment->setId(QUuid::createUuid());
2018-01-16 04:04:42 +08:00
comment->setMessage("comment #" + QString::number(i + 2));
2016-06-05 20:22:26 +08:00
comment->setSaveDate(QDateTime::currentDateTime());
2018-01-11 15:13:01 +08:00
comment->setAuthor(user);
2018-10-15 22:34:50 +08:00
//join child to master by id
comment->setPostId(postId);
2018-09-05 13:18:59 +08:00
db.comments()->append(comment);
2016-06-05 20:22:26 +08:00
}
db.saveChanges();
2018-10-15 22:34:50 +08:00
QTEST_ASSERT(postId != 0);
2016-06-05 20:22:26 +08:00
}
2018-02-26 18:14:36 +08:00
void MainTest::updatePostOnTheFly()
{
2018-09-05 13:18:59 +08:00
auto c = db.posts()->query()
2018-02-26 18:14:36 +08:00
->where(Post::idField() == postId)
->update(Post::titleField() = "New title");
QTEST_ASSERT(c == 1);
}
2018-02-26 14:47:05 +08:00
void MainTest::selectPublicts()
{
2018-09-05 13:18:59 +08:00
auto q = db.posts()->query()
2018-02-26 14:47:05 +08:00
->where(Post::isPublicField())
2018-10-15 22:34:50 +08:00
->count();
2018-02-26 14:47:05 +08:00
2018-09-05 13:18:59 +08:00
auto q2 = db.posts()->query()
2018-02-26 18:14:36 +08:00
->where(!Post::isPublicField())
2018-10-15 22:34:50 +08:00
->count();
QTEST_ASSERT(q == 1);
QTEST_ASSERT(q2 == 1);
2018-02-26 14:47:05 +08:00
}
2016-05-12 14:08:58 +08:00
void MainTest::selectPosts()
{
2018-09-05 13:18:59 +08:00
auto q = db.posts()->query()
2018-10-15 22:34:50 +08:00
->join<Comment>()
2018-02-17 23:44:39 +08:00
->orderBy(!Post::saveDateField() | Post::bodyField())
2018-01-09 17:33:54 +08:00
->setWhere(Post::idField() == postId);
2016-05-21 16:09:03 +08:00
2016-05-12 14:08:58 +08:00
auto posts = q->toList();
2016-05-21 16:09:03 +08:00
post = posts.at(0);
post->setBody("");
2018-01-12 00:14:29 +08:00
PRINT(posts.length());
PRINT(posts.at(0)->comments()->length());
2018-01-15 21:50:40 +08:00
QTEST_ASSERT(posts.length() == 1);
2018-10-15 22:34:50 +08:00
qDebug() << posts.at(0)->comments()->length();
2018-01-15 21:50:40 +08:00
QTEST_ASSERT(posts.at(0)->comments()->length() == 3);
QTEST_ASSERT(posts.at(0)->title() == "post title");
2016-05-12 14:08:58 +08:00
2018-01-15 21:50:40 +08:00
QTEST_ASSERT(posts.at(0)->comments()->at(0)->message() == "comment #0");
QTEST_ASSERT(posts.at(0)->comments()->at(1)->message() == "comment #1");
QTEST_ASSERT(posts.at(0)->comments()->at(2)->message() == "comment #2");
2016-05-21 16:09:03 +08:00
db.cleanUp();
2016-05-12 14:08:58 +08:00
}
2018-01-16 04:04:42 +08:00
void MainTest::selectScoreAverage()
{
2018-09-05 13:18:59 +08:00
auto a = db.scores()->query()
2018-01-16 04:04:42 +08:00
->join<Post>()
->setWhere(Post::idField() == 1)
->average(Score::scoreField());
qDebug() << a;
}
2018-01-12 00:14:29 +08:00
void MainTest::selectFirst()
{
2018-09-05 13:18:59 +08:00
auto posts = db.posts()->query()
2018-01-15 22:18:49 +08:00
->first();
2018-01-15 21:50:40 +08:00
2018-01-12 00:14:29 +08:00
QTEST_ASSERT(posts != Q_NULLPTR);
}
2016-06-05 20:22:26 +08:00
void MainTest::selectPostsWithoutTitle()
{
2018-09-05 13:18:59 +08:00
auto q = db.posts()->query();
2016-06-05 20:22:26 +08:00
q->setWhere(Post::titleField().isNull());
auto count = q->count();
QTEST_ASSERT(count == 0);
}
2017-09-10 22:18:20 +08:00
void MainTest::selectPostIds()
{
2018-09-05 13:18:59 +08:00
auto q = db.posts()->query();
2018-02-17 23:44:39 +08:00
auto ids = q->select(Post::idField());
2018-10-15 22:34:50 +08:00
qDebug() << ids.count();
2017-09-10 22:18:20 +08:00
QTEST_ASSERT(ids.count() == 2);
}
2016-05-21 16:09:03 +08:00
void MainTest::testDate()
{
QDateTime d = QDateTime::currentDateTime();
QTime t = QTime(d.time().hour(), d.time().minute(), d.time().second());
d.setTime(t);
Post *newPost = new Post;
newPost->setTitle("post title");
newPost->setSaveDate(d);
2018-09-05 13:18:59 +08:00
db.posts()->append(newPost);
2016-05-21 16:09:03 +08:00
2018-03-13 15:59:11 +08:00
db.saveChanges(true);
2016-05-21 16:09:03 +08:00
2018-09-05 13:18:59 +08:00
auto q = db.posts()->query()
2017-08-09 21:19:35 +08:00
->setWhere(Post::idField() == newPost->id())
->first();
2016-05-21 16:09:03 +08:00
QTEST_ASSERT(q->saveDate() == d);
}
2018-01-09 00:59:16 +08:00
void MainTest::join()
{
2018-01-15 22:18:49 +08:00
TIC();
2018-09-05 13:18:59 +08:00
auto q = db.comments()->query()
->join<User>()
2018-01-12 00:14:29 +08:00
->join<Post>();
2018-01-09 00:59:16 +08:00
auto comments = q->toList();
2018-01-15 22:18:49 +08:00
TOC();
QTEST_ASSERT(comments.length());
QTEST_ASSERT(comments[0]->author());
QTEST_ASSERT(comments[0]->author()->username() == "admin");
2018-01-09 00:59:16 +08:00
}
2016-05-21 16:09:03 +08:00
2016-05-12 14:08:58 +08:00
void MainTest::selectWithInvalidRelation()
{
2018-09-05 13:18:59 +08:00
auto q = db.posts()->query();
2016-06-05 20:22:26 +08:00
q->join("Invalid_Class_Name");
q->toList();
}
2016-05-12 14:08:58 +08:00
void MainTest::modifyPost()
{
2018-09-05 13:18:59 +08:00
auto q = db.posts()->query();
2016-06-05 20:22:26 +08:00
q->setWhere(Post::idField() == postId);
2016-05-12 14:08:58 +08:00
Post *post = q->first();
2018-10-15 22:34:50 +08:00
QTEST_ASSERT(post != nullptr);
2016-05-12 14:08:58 +08:00
post->setTitle("new name");
db.saveChanges();
2018-09-05 13:18:59 +08:00
q = db.posts()->query()
2017-08-09 21:19:35 +08:00
->setWhere(Post::idField() == postId);
2016-05-21 16:09:03 +08:00
2016-05-12 14:08:58 +08:00
post = q->first();
2018-01-12 00:14:29 +08:00
PRINT(post->title());
2016-05-12 14:08:58 +08:00
QTEST_ASSERT(post->title() == "new name");
}
2017-08-09 21:19:35 +08:00
void MainTest::emptyDatabase()
2016-05-12 14:08:58 +08:00
{
2018-10-15 22:34:50 +08:00
// auto commentsCount = db.comments()->query()->remove();
// auto postsCount = db.posts()->query()->remove();
// QTEST_ASSERT(postsCount == 3);
// QTEST_ASSERT(commentsCount == 6);
2016-05-12 14:08:58 +08:00
}
2018-03-13 15:59:11 +08:00
void MainTest::cleanupTestCase()
{
post->deleteLater();
user->deleteLater();
2018-10-15 22:34:50 +08:00
//release models before exiting
2018-03-13 15:59:11 +08:00
qDeleteAll(TableModel::allModels());
2018-10-15 22:34:50 +08:00
PRINT_FORM(db);
2018-03-13 15:59:11 +08:00
}
2016-05-12 14:08:58 +08:00
QTEST_MAIN(MainTest)