Nut/tests/auto/tst_basic/tst_basic.cpp

367 lines
8.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"
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
qDebug().noquote() << "Connecting to" << DATABASE;
2016-05-12 14:08:58 +08:00
bool ok = db.open();
2020-08-09 22:30:55 +08:00
QVERIFY(ok);
2016-05-12 14:08:58 +08:00
db.comments()->query().remove();
db.posts()->query().remove();
db.users()->query().remove();
db.scores()->query().remove();
2016-05-12 14:08:58 +08:00
}
2020-04-28 21:47:25 +08:00
void BasicTest::dataSchema()
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>();
2020-08-09 22:30:55 +08:00
user->setUsername(QStringLiteral("admin"));
user->setPassword(QStringLiteral("123456"));
2018-09-05 13:18:59 +08:00
db.users()->append(user);
QTEST_ASSERT(db.saveChanges() != 0);
QTEST_ASSERT(user->id() != 0);
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>();
newPost->setTitle(QStringLiteral("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>();
2020-08-09 22:30:55 +08:00
comment->setMessage(QStringLiteral("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);
score->setCondition(1); // test keyword on mysql
2019-06-26 23:06:44 +08:00
newPost->scores()->append(score);
2016-05-12 14:08:58 +08:00
}
2018-01-16 04:04:42 +08:00
QTEST_ASSERT(db.saveChanges() != 0);
2016-05-12 14:08:58 +08:00
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
}
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() = QStringLiteral("This is a sample"))
2018-10-15 22:34:50 +08:00
& (Post::isPublicField() = true));
2016-06-05 20:22:26 +08:00
2020-08-09 22:30:55 +08:00
QVERIFY(postIdVar.type() == QVariant::LongLong
2019-07-22 22:04:13 +08:00
|| postIdVar.type() == QVariant::ULongLong
|| postIdVar.type() == QVariant::Double);
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>();
comment->setMessage(QStringLiteral("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
}
QTEST_ASSERT(db.saveChanges() != 0);
2016-06-05 20:22:26 +08:00
QVERIFY(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()
.where(Post::idField() == postId)
.update(Post::titleField() = QStringLiteral("New title"));
2018-02-26 18:14:36 +08:00
QCOMPARE(c, 1);
auto titles = db.posts()
->query()
.where(Post::idField() == postId)
.select(Post::titleField());
QCOMPARE(titles.count(), 1);
QCOMPARE(titles.at(0), QStringLiteral("New title"));
2018-02-26 18:14:36 +08:00
}
2019-02-10 22:27:29 +08:00
void BasicTest::selectPublicts()
2018-02-26 14:47:05 +08:00
{
auto publinPostsCount = db.posts()->query()
.where(Post::isPublicField() == true)
.count();
2018-02-26 14:47:05 +08:00
auto nonPublicPostsCount = db.posts()->query()
.where(Post::isPublicField() == false)
.count();
2018-10-15 22:34:50 +08:00
QCOMPARE(publinPostsCount, 1);
QCOMPARE(nonPublicPostsCount, 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()
.join<Comment>()
.orderBy((!Post::saveDateField()) | Post::bodyField())
.where(Post::idField() == postId);
2016-05-21 16:09:03 +08:00
auto posts = q.toList();
2016-05-21 16:09:03 +08:00
post = posts.at(0);
post->setBody(QLatin1String());
2016-05-21 16:09:03 +08:00
2018-01-12 00:14:29 +08:00
PRINT(posts.length());
PRINT(posts.at(0)->comments()->length());
QCOMPARE(posts.length(), 1);
QCOMPARE(posts.at(0)->comments()->length(), 3);
2020-08-09 22:30:55 +08:00
QCOMPARE(posts.at(0)->title(), QStringLiteral("post title"));
2020-08-09 22:30:55 +08:00
QCOMPARE(posts.at(0)->comments()->at(0)->message(), QStringLiteral("comment #0"));
QCOMPARE(posts.at(0)->comments()->at(1)->message(), QStringLiteral("comment #1"));
QCOMPARE(posts.at(0)->comments()->at(2)->message(), QStringLiteral("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
{
2020-08-01 16:57:18 +08:00
bool ok;
auto avg = db.scores()
->query()
.join<Post>()
.where(Post::idField() == postId)
.average(Score::scoreField())
2020-08-01 16:57:18 +08:00
.toInt(&ok);
2020-08-09 22:30:55 +08:00
QVERIFY(ok);
QCOMPARE(avg, 2);
}
void BasicTest::selectScoreSum()
{
auto sum = db.scores()->query().sum(Score::scoreField());
QCOMPARE(sum, 20);
}
void BasicTest::selectScoreCount()
{
auto count = db.scores()->query().count();
QCOMPARE(count, 10);
2018-01-16 04:04:42 +08:00
}
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()
.orderBy(Post::idField())
.first();
2018-01-15 21:50:40 +08:00
2020-08-09 22:30:55 +08:00
QVERIFY(posts != Q_NULLPTR);
2018-01-12 00:14:29 +08:00
}
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();
q.where(Post::titleField().isNull());
auto count = q.count();
QCOMPARE(count, 0);
2016-06-05 20:22:26 +08:00
}
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();
auto ids = q.select(Post::idField());
2020-08-01 16:57:18 +08:00
QCOMPARE(ids.count(), 2);
2017-09-10 22:18:20 +08:00
}
void BasicTest::selectPostsWithComments()
{
auto posts = db.posts()->query().join<Comment>().toList();
QCOMPARE(posts.first()->comments()->length(), 3);
}
void BasicTest::selectCommantsWithPost()
{
auto comments = db.comments()->query().join<Post>().toList();
QCOMPARE(comments.length(), 6);
QVERIFY(!comments.first()->post().isNull());
}
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>();
newPost->setTitle(QStringLiteral("post title"));
2016-05-21 16:09:03 +08:00
newPost->setSaveDate(d);
newPost->setPublic(true);
2016-05-21 16:09:03 +08:00
2018-09-05 13:18:59 +08:00
db.posts()->append(newPost);
2016-05-21 16:09:03 +08:00
db.saveChanges();
2016-05-21 16:09:03 +08:00
2018-09-05 13:18:59 +08:00
auto q = db.posts()->query()
.where(Post::idField() == newPost->id())
.orderBy(Post::idField())
.first();
2016-05-21 16:09:03 +08:00
2020-08-09 22:30:55 +08:00
QCOMPARE(q->saveDate(), d);
2016-05-21 16:09:03 +08:00
}
void BasicTest::testLimitedQuery()
{
auto comments = db.comments()->query()
.toList(2);
2020-08-09 22:30:55 +08:00
QCOMPARE(comments.length(), 2);
}
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();
q.join(QStringLiteral("Invalid_Class_Name"));
q.toList();
2016-06-05 20:22:26 +08:00
}
2019-02-10 22:27:29 +08:00
void BasicTest::modifyPost()
2016-05-12 14:08:58 +08:00
{
2019-07-22 22:04:13 +08:00
auto q = db.posts()->query()
.where(Post::idField() == postId)
.orderBy(Post::idField());
2016-05-12 14:08:58 +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
2020-08-09 22:30:55 +08:00
post->setTitle(QStringLiteral("new name"));
2016-05-12 14:08:58 +08:00
db.saveChanges();
2018-09-05 13:18:59 +08:00
q = db.posts()->query()
.where(Post::idField() == postId)
.orderBy(Post::idField());
2016-05-21 16:09:03 +08:00
post = q.first();
2018-01-12 00:14:29 +08:00
PRINT(post->title());
2020-08-09 22:30:55 +08:00
QCOMPARE(post->title(), "new name");
2016-05-12 14:08:58 +08:00
}
2019-02-10 22:27:29 +08:00
void BasicTest::emptyDatabase()
2016-05-12 14:08:58 +08:00
{
// auto commentsCount = db.comments()->query().remove();
// auto postsCount = db.posts()->query().remove();
2018-10-15 22:34:50 +08:00
// QTEST_ASSERT(postsCount == 3);
2023-02-20 22:54:33 +08:00
// QTEST_ASSERT(commentsCount == 6);
}
void BasicTest::multipleOpen()
{
for (int i = 0; i < 10; ++i) {
WeblogDatabase database;
database.setDriver(DRIVER);
database.setHostName(HOST);
database.setDatabaseName(DATABASE);
database.setUserName(USERNAME);
database.setPassword(PASSWORD);
bool ok = database.open();
QVERIFY(ok);
auto q = database.posts()->query()
.where(Post::idField() == postId)
.orderBy(Post::idField())
.toList();
QTEST_ASSERT(q.count() > 0);
}
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)