Nut/test/tst_basic/tst_basic.cpp

290 lines
6.8 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"
2019-02-10 22:27:29 +08:00
#include "tst_basic.h"
2016-05-12 14:08:58 +08:00
#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
2019-02-10 22:27:29 +08:00
BasicTest::BasicTest(QObject *parent) : QObject(parent)
2016-05-12 14:08:58 +08:00
{
}
2019-02-10 22:27:29 +08:00
void BasicTest::initTestCase()
2016-05-12 14:08:58 +08:00
{
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);
2019-03-02 23:51:43 +08:00
db.setDatabaseName(DATABASE);
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
}
2019-02-10 22:27:29 +08:00
void BasicTest::dataScheema()
2016-05-12 14:08:58 +08:00
{
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());
}
2019-02-10 22:27:29 +08:00
void BasicTest::createUser()
2018-01-11 15:13:01 +08:00
{
2019-06-18 23:37:03 +08:00
user = Nut::create<User>();
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
}
2019-02-10 22:27:29 +08:00
void BasicTest::createPost()
2016-05-12 14:08:58 +08:00
{
2018-02-26 18:14:36 +08:00
TIC();
2019-06-18 23:37:03 +08:00
auto newPost = Nut::create<Post>();
2016-05-12 14:08:58 +08:00
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++){
2019-06-18 23:37:03 +08:00
auto comment = Nut::create<Comment>();
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());
2019-06-26 23:06:44 +08:00
newPost->comments()->append(comment);
}
for (int i = 0; i < 10; ++i) {
auto score = Nut::create<Score>();
score->setScore(i % 5);
newPost->scores()->append(score);
2016-05-12 14:08:58 +08:00
}
2018-01-16 04:04:42 +08:00
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();
}
2019-02-10 22:27:29 +08:00
void BasicTest::createPost2()
2016-06-05 20:22:26 +08:00
{
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
2019-02-11 00:33:14 +08:00
QTEST_ASSERT(postIdVar.type() == QVariant::LongLong || postIdVar.type() == QVariant::ULongLong);
2018-10-15 22:34:50 +08:00
int postId = postIdVar.toInt();
2016-06-05 20:22:26 +08:00
for(int i = 0 ; i < 3; i++){
2019-06-18 23:37:03 +08:00
auto comment = Nut::create<Comment>();
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
}
2019-02-10 22:27:29 +08:00
void BasicTest::updatePostOnTheFly()
2018-02-26 18:14:36 +08:00
{
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);
}
2019-02-10 22:27:29 +08:00
void BasicTest::selectPublicts()
2018-02-26 14:47:05 +08:00
{
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
}
2019-02-10 22:27:29 +08:00
void BasicTest::selectPosts()
2016-05-12 14:08:58 +08:00
{
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
}
2019-02-10 22:27:29 +08:00
void BasicTest::selectScoreAverage()
2018-01-16 04:04:42 +08:00
{
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;
}
2019-02-10 22:27:29 +08:00
void BasicTest::selectFirst()
2018-01-12 00:14:29 +08:00
{
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);
}
2019-02-10 22:27:29 +08:00
void BasicTest::selectPostsWithoutTitle()
2016-06-05 20:22:26 +08:00
{
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);
}
2019-02-10 22:27:29 +08:00
void BasicTest::selectPostIds()
2017-09-10 22:18:20 +08:00
{
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);
}
2019-02-10 22:27:29 +08:00
void BasicTest::testDate()
2016-05-21 16:09:03 +08:00
{
QDateTime d = QDateTime::currentDateTime();
QTime t = QTime(d.time().hour(), d.time().minute(), d.time().second());
d.setTime(t);
2019-06-18 23:37:03 +08:00
auto newPost = Nut::create<Post>();
2016-05-21 16:09:03 +08:00
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
2019-02-11 00:33:14 +08:00
qDebug() << q->saveDate() << d;
2016-05-21 16:09:03 +08:00
QTEST_ASSERT(q->saveDate() == d);
}
2019-02-10 22:27:29 +08:00
void BasicTest::join()
2018-01-09 00:59:16 +08:00
{
2019-02-10 22:27:29 +08:00
// TIC();
// auto q = db.comments()->query()
// ->join<User>()
// ->join<Post>();
2018-01-09 00:59:16 +08:00
2019-02-10 22:27:29 +08:00
// auto comments = q->toList();
2018-01-15 22:18:49 +08:00
2019-02-10 22:27:29 +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
2019-02-10 22:27:29 +08:00
void BasicTest::selectWithInvalidRelation()
2016-05-12 14:08:58 +08:00
{
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();
}
2019-02-10 22:27:29 +08:00
void BasicTest::modifyPost()
2016-05-12 14:08:58 +08:00
{
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
2019-06-18 23:37:03 +08:00
Nut::Row<Post> post = q->first();
2016-05-12 14:08:58 +08:00
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");
}
2019-02-10 22:27:29 +08:00
void BasicTest::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
}
2019-02-10 22:27:29 +08:00
void BasicTest::cleanupTestCase()
2018-03-13 15:59:11 +08:00
{
2019-06-06 19:02:02 +08:00
// post->deleteLater();
// user->deleteLater();
2018-03-13 15:59:11 +08:00
2018-10-15 22:34:50 +08:00
//release models before exiting
2019-02-28 16:10:41 +08:00
// qDeleteAll(TableModel::allModels());
2018-10-15 22:34:50 +08:00
2019-06-03 23:22:21 +08:00
// if (QFile::remove("nut_tst_basic"))
// qDebug() << "database removed";
2019-02-08 17:11:53 +08:00
2018-10-15 22:34:50 +08:00
PRINT_FORM(db);
2018-03-13 15:59:11 +08:00
}
2019-02-10 22:27:29 +08:00
QTEST_MAIN(BasicTest)