Nut/tests/auto/tst_phrases/tst_phrases.cpp

221 lines
6.5 KiB
C++
Raw Normal View History

2019-02-26 00:16:18 +08:00
#include <QtTest>
#include <QDate>
#include <qtestcase.h>
2019-02-26 00:16:18 +08:00
#include "tst_phrases.h"
#include "phrase.h"
#include "generator.h"
2019-02-26 00:16:18 +08:00
using namespace Nut;
#define COMPARE_WHERE(w, sql) QCOMPARE(g.where(w), sql);
#define COMPARE_ORDER(o, sql) QCOMPARE(g.order(o), sql);
#define COMPARE_SELECT(s, sql) QCOMPARE(g.select(s), sql);
QT_WARNING_PUSH
QT_WARNING_DISABLE_CLANG("-Wdeprecated-declarations")
QT_WARNING_DISABLE_GCC("-Wdeprecated-declarations")
2019-07-21 23:33:06 +08:00
PhrasesTest::PhrasesTest(QObject *parent) : QObject(parent)
2019-02-26 00:16:18 +08:00
{
2019-02-26 00:16:18 +08:00
}
2019-07-21 23:33:06 +08:00
void PhrasesTest::initTestCase()
2019-02-26 00:16:18 +08:00
{
}
2019-07-21 23:33:06 +08:00
void PhrasesTest::no1()
2019-02-26 00:16:18 +08:00
{
2020-08-12 20:32:06 +08:00
{
FieldPhrase<int> id("main", "id");
FieldPhrase<QString> name("main", "name");
FieldPhrase<QString> last_name("main", "last_name");
FieldPhrase<QDate> date("main", "date");
auto w = (id == 4 && name == QStringLiteral("hi"));
Generator g;
COMPARE_WHERE(id == 10 || id.in({1, 2, 3, 4}), "([main].id = '10' OR [main].id IN ('1', '2', '3', '4'))");
2020-08-12 20:32:06 +08:00
}
2019-02-26 00:16:18 +08:00
}
void PhrasesTest::condition_numeric_sqlite()
2019-02-26 00:16:18 +08:00
{
Generator g;
2019-02-26 00:16:18 +08:00
FieldPhrase<int> n("main", "int");
2019-02-26 22:53:57 +08:00
FieldPhrase<float> f("main", "float");
2019-02-26 00:16:18 +08:00
COMPARE_WHERE(n < 1, "[main].int < '1'");
COMPARE_WHERE(n > 1, "[main].int > '1'");
COMPARE_WHERE(n <= 1, "[main].int <= '1'");
COMPARE_WHERE(n >= 1, "[main].int >= '1'");
COMPARE_WHERE(n != 1, "[main].int <> '1'");
COMPARE_WHERE(n == 1, "[main].int = '1'");
COMPARE_WHERE(n++, "[main].int + '1'");
COMPARE_WHERE(++n, "[main].int + '1'");
COMPARE_WHERE(n.between(10, 20), "[main].int BETWEEN '10' AND '20'");
COMPARE_WHERE(n + 1 < n + 4, "[main].int + '1' < [main].int + '4'");
2019-02-26 00:16:18 +08:00
auto p1 = n == 1;
auto p2 = n <= 4;
auto p3 = n >= 5;
auto p4 = n < 7;
COMPARE_WHERE(p1 && p2, "([main].int = '1' AND [main].int <= '4')");
COMPARE_WHERE(p3 == p4, "[main].int >= '5' = [main].int < '7'");
COMPARE_WHERE(f == n + 1, "[main].float = [main].int + '1'");
COMPARE_WHERE(f == 1.4 || (n == n + 1 && n < 100),
"([main].float = '1.4' OR ([main].int = [main].int + '1' AND [main].int < '100'))");
2019-02-26 22:53:57 +08:00
2019-02-27 00:26:48 +08:00
auto p24 = n = 4;
auto p26 = (n = 4) & (n = 5);
auto p27 = n | f;
2019-02-26 00:16:18 +08:00
}
void PhrasesTest::condition_string_sqlite()
2019-02-26 00:16:18 +08:00
{
Generator g;
2019-02-26 00:16:18 +08:00
FieldPhrase<QString> str("main", "string");
COMPARE_WHERE(str == "Hi", "[main].string = 'Hi'");
COMPARE_WHERE(str.like("%hi%"), "[main].string LIKE '%hi%'");
COMPARE_WHERE(str.isNull(), "[main].string IS NULL");
COMPARE_WHERE(!str.isNull(), "[main].string IS NOT NULL");
COMPARE_WHERE(str.in(QStringList() << "one"
<< "two"
<< "three"),
"[main].string IN ('one', 'two', 'three')");
COMPARE_WHERE(!str.in(QStringList() << "one"
<< "two"
<< "three"),
"[main].string NOT IN ('one', 'two', 'three')");
COMPARE_WHERE(str != "hi" && str.like("%s"),
"([main].string <> 'hi' AND [main].string LIKE '%s')");
2019-02-26 00:16:18 +08:00
}
void PhrasesTest::condition_bool_sqlite()
2019-02-26 00:16:18 +08:00
{
Generator g;
2019-02-26 00:16:18 +08:00
FieldPhrase<bool> b("main", "bool");
COMPARE_WHERE(b, "[main].bool = 'true'");
COMPARE_WHERE(!b, "[main].bool = 'false'");
COMPARE_WHERE(b == true, "[main].bool = 'true'");
COMPARE_WHERE(b == false, "[main].bool = 'false'");
2019-02-26 00:16:18 +08:00
}
void PhrasesTest::condition_datetime_sqlite()
2019-02-26 00:16:18 +08:00
{
Generator g;
2019-02-26 00:16:18 +08:00
FieldPhrase<QTime> time("main", "time");
FieldPhrase<QDate> date("main", "date");
FieldPhrase<QDateTime> datetime("main", "datetime");
QDate d(2020, 2, 20);
QTime t(12, 34, 56);
QDateTime dt(d, t);
COMPARE_WHERE(time.hour() == 1, "CAST(strftime('%H', [main].time) AS INT) = '1'");
COMPARE_WHERE(time.minute() == 2, "CAST(strftime('%M', [main].time) AS INT) = '2'");
COMPARE_WHERE(time.second() == 3, "CAST(strftime('%S', [main].time) AS INT) = '3'");
COMPARE_WHERE(date.year() == 1, "CAST(strftime('%Y', [main].date) AS INT) = '1'");
COMPARE_WHERE(date.month() == 2, "CAST(strftime('%m', [main].date) AS INT) = '2'");
COMPARE_WHERE(date.day() == 3, "CAST(strftime('%d', [main].date) AS INT) = '3'");
COMPARE_WHERE(time.isNull(), "[main].time IS NULL");
COMPARE_WHERE(!time.isNull(), "[main].time IS NOT NULL");
COMPARE_WHERE(time == t, "[main].time = '12:34:56'");
COMPARE_WHERE(time.between(t.addSecs(-10),
t),
"[main].time BETWEEN '12:34:46' AND '12:34:56'");
COMPARE_WHERE(date.addDays(2) == d, "DATE([main].date,'+2 DAY') = '2020-02-20'");
COMPARE_WHERE(time.addMinutes(-3) == t, "TIME([main].time,'-3 MINUTE') = '12:34:56'");
COMPARE_WHERE(datetime.addMinutes(1) == dt, "DATETIME([main].datetime,'+1 MINUTE') = '2020-02-20 12:34:56'");
}
void PhrasesTest::order_sqlite()
{
Generator g;
2019-02-26 17:47:08 +08:00
FieldPhrase<int> id("main", "id");
FieldPhrase<QString> name("main", "name");
FieldPhrase<QString> last_name("main", "last_name");
2019-02-26 17:47:08 +08:00
COMPARE_ORDER(id, "[main].id");
COMPARE_ORDER(id | name, "[main].id, [main].name");
COMPARE_ORDER(id | !name | last_name, "[main].id, [main].name DESC, [main].last_name");
}
void PhrasesTest::select_sqlite()
{
Generator g;
FieldPhrase<int> id("main", "id");
FieldPhrase<QString> name("main", "name");
FieldPhrase<QString> last_name("main", "last_name");
COMPARE_ORDER(id, "[main].id");
COMPARE_ORDER(id | name, "[main].id, [main].name");
COMPARE_ORDER(id | name | last_name, "[main].id, [main].name, [main].last_name");
2019-02-26 00:16:18 +08:00
}
2019-07-21 23:33:06 +08:00
void PhrasesTest::extra()
2019-02-26 00:16:18 +08:00
{
Generator g;
2019-02-26 00:16:18 +08:00
FieldPhrase<QUrl> url("main", "url");
COMPARE_WHERE(url == QUrl("http://google.com"), "[main].url = 'http://google.com'");
2019-02-26 00:16:18 +08:00
}
2019-07-21 23:33:06 +08:00
void PhrasesTest::mix()
2019-02-27 03:51:29 +08:00
{
FieldPhrase<int> id("", "");
FieldPhrase<QString> name("", "");
FieldPhrase<QString> lastName("", "");
FieldPhrase<QDate> birthDate("", "");
2019-02-27 04:08:31 +08:00
select(id);
2019-02-27 03:51:29 +08:00
select(id | name | lastName);
2019-02-27 04:08:31 +08:00
update((name = "john") & (lastName = "snow"));
insert(id = 0);
insert((id = 4) & (name = "john"));
order_by(id);
2019-02-28 16:10:41 +08:00
order_by(id | !name);
2019-02-27 03:51:29 +08:00
}
2019-07-21 23:33:06 +08:00
void PhrasesTest::select(const PhraseList &ph)
2019-02-27 00:26:48 +08:00
{
2019-02-27 04:08:31 +08:00
QTEST_ASSERT(ph.data.count());
2019-02-27 00:26:48 +08:00
}
2019-07-21 23:33:06 +08:00
void PhrasesTest::where(const ConditionalPhrase &ph)
2019-02-27 00:26:48 +08:00
{
2019-02-27 04:08:31 +08:00
QTEST_ASSERT(ph.data);
2019-02-27 00:26:48 +08:00
}
2019-07-21 23:33:06 +08:00
void PhrasesTest::update(const AssignmentPhraseList &p)
2019-02-27 00:26:48 +08:00
{
2019-02-27 04:08:31 +08:00
QTEST_ASSERT(p.data.count());
2019-02-27 00:26:48 +08:00
}
2019-07-21 23:33:06 +08:00
void PhrasesTest::insert(const AssignmentPhraseList &p)
2019-02-27 00:26:48 +08:00
{
2019-02-27 04:08:31 +08:00
QTEST_ASSERT(p.data.count());
2019-02-27 00:26:48 +08:00
}
2019-07-21 23:33:06 +08:00
void PhrasesTest::order_by(const PhraseList &ph)
2019-02-27 00:26:48 +08:00
{
2019-02-28 16:10:41 +08:00
QTEST_ASSERT(ph.data.count());
2019-02-27 00:26:48 +08:00
}
2019-07-21 23:33:06 +08:00
QTEST_MAIN(PhrasesTest)
QT_WARNING_POP