File indexing completed on 2024-04-21 12:15:27

0001 /* This file is part of the KDE project
0002    Copyright (C) 2018 Jarosław Staniek <staniek@kde.org>
0003 
0004    This library is free software; you can redistribute it and/or
0005    modify it under the terms of the GNU Library General Public
0006    License as published by the Free Software Foundation; either
0007    version 2 of the License, or (at your option) any later version.
0008 
0009    This library is distributed in the hope that it will be useful,
0010    but WITHOUT ANY WARRANTY; without even the implied warranty of
0011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0012    Library General Public License for more details.
0013 
0014    You should have received a copy of the GNU Library General Public License
0015    along with this library; see the file COPYING.LIB.  If not, write to
0016    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0017  * Boston, MA 02110-1301, USA.
0018 */
0019 
0020 #include "DateTimeTest.h"
0021 #include "KDbTestUtils.h"
0022 
0023 #include <QtTest>
0024 
0025 #include <KDbDateTime>
0026 #include <KDbExpression>
0027 #include "parser/generated/sqlparser.h"
0028 #include "parser/KDbParser_p.h"
0029 
0030 QTEST_GUILESS_MAIN(DateTimeTest)
0031 
0032 void DateTimeTest::initTestCase()
0033 {
0034 }
0035 
0036 void DateTimeTest::testYear()
0037 {
0038     KDbYear year("2020");
0039     QVERIFY(year.isValid());
0040     QCOMPARE(year.toString(), "2020");
0041     QCOMPARE(year.toQDateValue(), 2020);
0042     QCOMPARE(year.toIsoValue(), 2020);
0043     QVariant yearVariant = QVariant::fromValue(year);
0044     QVERIFY(yearVariant.isValid());
0045     QCOMPARE(yearVariant.toInt(), 2020);
0046     QCOMPARE(yearVariant.toString(), "2020");
0047 }
0048 
0049 void DateTimeTest::testDate()
0050 {
0051     KDbYear year("2020");
0052     KDbDate date(year, "2", "29");
0053     QVERIFY(date.isValid());
0054     QCOMPARE(date.toString(), "2020-2-29");
0055     QDate qtDate(2020, 2, 29);
0056     QCOMPARE(date.toQDate(), qtDate);
0057     QVariant dateVariant = QVariant::fromValue(date);
0058     QVERIFY(dateVariant.isValid());
0059     QCOMPARE(dateVariant.toDate(), qtDate);
0060     //! @todo more cases
0061 }
0062 
0063 void DateTimeTest::testTime()
0064 {
0065     KDbTime time("1", "15", "20", "789", KDbTime::Period::Pm);
0066     QVERIFY(time.isValid());
0067     QCOMPARE(time.toString(), "1:15:20.789 PM");
0068     QTime qtTime(13, 15, 20, 789);
0069     QCOMPARE(time.toQTime(), qtTime);
0070     QVariant timeVariant = QVariant::fromValue(time);
0071     QVERIFY(timeVariant.isValid());
0072     QCOMPARE(timeVariant.toTime(), qtTime);
0073     //! @todo more cases
0074 }
0075 
0076 void DateTimeTest::testDateTime()
0077 {
0078     KDbYear year("2020");
0079     KDbTime time("1", "15", "20", "789", KDbTime::Period::Pm);
0080     KDbDate date(year, "2", "29");
0081 
0082     KDbDateTime dateTime(date, time);
0083     QVERIFY(dateTime.isValid());
0084     QCOMPARE(dateTime.toString(), "2020-2-29 1:15:20.789 PM");
0085     QTime qtTime(13, 15, 20, 789);
0086     QDate qtDate(2020, 2, 29);
0087     QDateTime qtDateTime(qtDate, qtTime);
0088     QCOMPARE(dateTime.toQDateTime(), qtDateTime);
0089     QVariant dateTimeVariant = QVariant::fromValue(dateTime);
0090     QVERIFY(dateTimeVariant.isValid());
0091     QCOMPARE(dateTimeVariant.toDateTime(), qtDateTime);
0092     //! @todo more cases
0093 }
0094 
0095 void DateTimeTest::cleanupTestCase()
0096 {
0097 }