Nut/test/basic/maintest.cpp

195 lines
4.5 KiB
C++
Raw Normal View History

2016-05-12 14:08:58 +08:00
#include <QtTest>
#include <QJsonDocument>
#include <QSqlError>
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"
#include "post.h"
#include "comment.h"
MainTest::MainTest(QObject *parent) : QObject(parent)
{
}
void MainTest::initTestCase()
{
qDebug() << "User type id:" << qRegisterMetaType<Post*>();
qDebug() << "Comment type id:" << qRegisterMetaType<Comment*>();
qDebug() << "DB type id:" << qRegisterMetaType<WeblogDatabase*>();
2016-05-24 14:53:40 +08:00
db.setDriver(DRIVER);
db.setHostName(HOST);
db.setDatabaseName(DATABASE);
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();
QTEST_ASSERT(ok);
}
void MainTest::dataScheema()
{
auto json = db.model().toJson();
auto model = DatabaseModel::fromJson(json);
// qDebug() << model.toJson();
// qDebug() << db.model().toJson();
QTEST_ASSERT(model == db.model());
}
void MainTest::createPost()
{
Post *newPost = new Post;
newPost->setTitle("post title");
2016-05-21 16:09:03 +08:00
newPost->setSaveDate(QDateTime::currentDateTime());
2016-05-12 14:08:58 +08:00
db.posts()->append(newPost);
for(int i = 0 ; i < 3; i++){
Comment *comment = new Comment;
comment->setMessage("comment #" + QString::number(i));
2016-05-21 16:09:03 +08:00
comment->setSaveDate(QDateTime::currentDateTime());
2016-05-12 14:08:58 +08:00
newPost->comments()->append(comment);
}
db.saveChanges();
postId = newPost->id();
QTEST_ASSERT(newPost->id() != 0);
qDebug() << "New post inserted with id:" << newPost->id();
}
2016-06-05 20:22:26 +08:00
void MainTest::createPost2()
{
Post *newPost = new Post;
newPost->setTitle("post title");
newPost->setSaveDate(QDateTime::currentDateTime());
db.posts()->append(newPost);
db.saveChanges();
for(int i = 0 ; i < 3; i++){
Comment *comment = new Comment;
comment->setMessage("comment #" + QString::number(i));
comment->setSaveDate(QDateTime::currentDateTime());
comment->setPostId(newPost->id());
db.comments()->append(comment);
}
db.saveChanges();
QTEST_ASSERT(newPost->id() != 0);
qDebug() << "New post2 inserted with id:" << newPost->id();
}
2016-05-12 14:08:58 +08:00
void MainTest::selectPosts()
{
2017-08-09 21:19:35 +08:00
auto q = db.posts()->query();
2016-06-05 20:22:26 +08:00
q->join(Post::commentsTable());
q->orderBy(!Post::saveDateField() & Post::bodyField());
2016-05-24 14:47:37 +08:00
q->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("");
2016-05-12 14:08:58 +08:00
QTEST_ASSERT(posts.length() == 1);
QTEST_ASSERT(posts.at(0)->comments()->length() == 3);
QTEST_ASSERT(posts.at(0)->title() == "post title");
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
}
2016-06-05 20:22:26 +08:00
void MainTest::selectPostsWithoutTitle()
{
2017-08-09 21:19:35 +08:00
auto q = db.posts()->query();
2016-06-05 20:22:26 +08:00
q->setWhere(Post::titleField().isNull());
auto count = q->count();
qDebug() << "selectPostsWithoutTitle, count=" << count;
QTEST_ASSERT(count == 0);
}
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);
db.posts()->append(newPost);
db.saveChanges();
2017-08-09 21:19:35 +08:00
auto q = db.posts()->query()
->setWhere(Post::idField() == newPost->id())
->first();
2016-05-21 16:09:03 +08:00
QTEST_ASSERT(q->saveDate() == d);
}
2016-05-12 14:08:58 +08:00
void MainTest::selectWithInvalidRelation()
{
2017-08-09 21:19:35 +08:00
auto q = db.posts()->query();
2016-06-05 20:22:26 +08:00
q->join("Invalid_Class_Name");
q->toList();
}
void MainTest::select10NewstPosts()
{
2017-08-09 21:19:35 +08:00
auto q = db.posts()->query();
2016-06-05 20:22:26 +08:00
q->orderBy(!Post::saveDateField());
q->toList(10);
2016-05-12 14:08:58 +08:00
}
void MainTest::modifyPost()
{
2017-08-09 21:19:35 +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();
QTEST_ASSERT(post != 0);
post->setTitle("new name");
db.saveChanges();
2017-08-09 21:19:35 +08:00
q = db.posts()->query()
->setWhere(Post::idField() == postId);
2016-05-21 16:09:03 +08:00
2016-05-12 14:08:58 +08:00
post = q->first();
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
{
2017-08-09 21:19:35 +08:00
auto count = db.posts()->query()
->setWhere(Post::idField() == postId)
->remove();
2016-05-12 14:08:58 +08:00
QTEST_ASSERT(count == 1);
2017-08-09 21:19:35 +08:00
count = db.posts()->query()
->setWhere(Post::idField() == postId)
->count();
2016-05-12 14:08:58 +08:00
QTEST_ASSERT(count == 0);
}
2016-05-24 14:47:37 +08:00
2016-05-12 14:08:58 +08:00
QTEST_MAIN(MainTest)