File indexing completed on 2024-12-08 04:35:08

0001 /*
0002     SPDX-FileCopyrightText: 2018 Ralf Habacker ralf.habacker @freenet.de
0003     SPDX-FileCopyrightText: 2020 Thomas Baumgart <tbaumgart@kde.org>
0004 
0005     This file is part of libalkimia.
0006 
0007     SPDX-License-Identifier: LGPL-2.1-or-later
0008 */
0009 
0010 #include "alkonlinequoteprivatetest.h"
0011 
0012 #include "alkonlinequote_p.h"
0013 #include "alkonlinequotesprofile.h"
0014 
0015 #include <QtTest/QtTest>
0016 
0017 QTEST_MAIN(AlkOnlineQuotePrivateTest)
0018 
0019 AlkOnlineQuotePrivateTest::AlkOnlineQuotePrivateTest()
0020     : AlkOnlineQuote(new AlkOnlineQuotesProfile("alkimia"))
0021 {
0022 }
0023 
0024 void AlkOnlineQuotePrivateTest::testParsePrice()
0025 {
0026     AlkOnlineQuote::Private &p = d_ptr();
0027 
0028     p.m_errors = AlkOnlineQuote::Errors::Success;
0029     QVERIFY(p.parsePrice("12345.675"));
0030     QCOMPARE(p.m_price, 12345.675);
0031     QVERIFY(errors() & AlkOnlineQuote::Errors::Success);
0032 
0033     p.m_errors = AlkOnlineQuote::Errors::Success;
0034     QVERIFY(p.parsePrice(" 12 345.675"));
0035     QCOMPARE(p.m_price, 12345.675);
0036     QVERIFY(errors() & AlkOnlineQuote::Errors::Success);
0037 
0038     p.m_errors = AlkOnlineQuote::Errors::Success;
0039     QVERIFY(p.parsePrice("12,345.675"));
0040     QCOMPARE(p.m_price, 12345.675);
0041     QVERIFY(errors() & AlkOnlineQuote::Errors::Success);
0042 
0043     p.m_errors = AlkOnlineQuote::Errors::Success;
0044     QVERIFY(p.parsePrice("109728364361224110982112411097810241109340924110963082411093707241109550624110955052411094404241109230324110949022411102301241110383123111056302311105629231110692823111118272311103026231110302523111005242311102323231110262223111003212311094920231109701923110927182311091017231109091623110909152311099"));
0045     QCOMPARE(p.m_price, 1.0972836436122411e+299);
0046     QVERIFY(errors() & AlkOnlineQuote::Errors::Success);
0047 
0048     p.m_errors = AlkOnlineQuote::Errors::Success;
0049     QVERIFY(p.parsePrice("1.0971 &#8364; = &#36; Print Exchange Rate Chart Euro to Dollar - EUR/USD Invert DateExchange Rate12 Jan 241 EUR = 1.0982 USD11 Jan 241 EUR = 1.0978 USD10 Jan 241 EUR = 1.0934 USD09 Jan 241 EUR = 1.0963 USD08 Jan 241 EUR = 1.0937 USD07 Jan 241 EUR = 1.0955 USD06 Jan 241 EUR = 1.0955 USD05 Jan 241 EU"));
0050     QCOMPARE(p.m_price, 1.0971);
0051     QVERIFY(errors() & AlkOnlineQuote::Errors::Success);
0052 
0053     p.m_errors = AlkOnlineQuote::Errors::Success;
0054     QVERIFY(!p.parsePrice("Print Exchange Rate Chart Euro to Dollar"));
0055     QCOMPARE(p.m_price, 0.0);
0056     QVERIFY(errors() & AlkOnlineQuote::Errors::Price);
0057 
0058     p.m_errors = AlkOnlineQuote::Errors::Success;
0059     // workaround for msvc 2048 character string limit
0060     QFile f(":/alkonlinequoteprivatetest.data");
0061     QVERIFY(f.open(QIODevice::ReadOnly));
0062     QVERIFY(!p.parsePrice(f.readAll()));
0063     QCOMPARE(p.m_price, 0.0);
0064     QVERIFY(errors() & AlkOnlineQuote::Errors::Price);
0065 }
0066 
0067 class SingleQuoteReceiver : public QObject
0068 {
0069     Q_OBJECT
0070 public:
0071     QDate _date;
0072     double _price;
0073     SingleQuoteReceiver(QDate date, double price)
0074         : _date(date)
0075         , _price(price)
0076     {}
0077 
0078 Q_SIGNALS:
0079     void finished();
0080 
0081 public Q_SLOTS:
0082     void quote(QString id, QString symbol, QDate date, double price)
0083     {
0084         qDebug() << "comparing quote" << id << symbol;
0085         QCOMPARE(date, _date);
0086         QCOMPARE(price, _price);
0087         Q_EMIT finished();
0088     }
0089 };
0090 
0091 void AlkOnlineQuotePrivateTest::testParseQuoteCSVSingleLine()
0092 {
0093     AlkOnlineQuote::Private &p = d_ptr();
0094 
0095     AlkOnlineQuoteSource source("test", "", "", AlkOnlineQuoteSource::Symbol, "#2", "#1", "%d-%m-%y", AlkOnlineQuoteSource::CSV);
0096     p.m_source = source;
0097     QString quotedata =
0098         "22-01-24,1.0906\n"
0099         ;
0100     SingleQuoteReceiver receiver(QDate::fromString("22-01-2024", "dd-MM-yyyy"), 1.0906);
0101     connect(this, SIGNAL(quote(QString,QString,QDate,double)), &receiver, SLOT(quote(QString,QString,QDate,double)));
0102     QVERIFY(p.parseQuoteCSV(quotedata));
0103 }
0104 
0105 class MultipleQuotesReceiver : public QObject
0106 {
0107     Q_OBJECT
0108 public:
0109     const AlkDatePriceMap &_prices;
0110     MultipleQuotesReceiver(const AlkDatePriceMap &prices)
0111         : _prices(prices)
0112     {}
0113 
0114 Q_SIGNALS:
0115     void finished();
0116 
0117 public Q_SLOTS:
0118     void quotes(const QString& id, const QString& symbol, const AlkDatePriceMap& prices)
0119     {
0120         qDebug() << "comparing" << prices.size() << "quotes" << id << symbol;
0121         QCOMPARE(prices.size(), _prices.size());
0122         QCOMPARE(prices.keys(), _prices.keys());
0123         for (auto &date: prices.keys()) {
0124             QCOMPARE(prices[date].toDouble(), _prices[date].toDouble());
0125         }
0126         Q_EMIT finished();
0127     }
0128 };
0129 
0130 void AlkOnlineQuotePrivateTest::testParseQuoteCSVMultipleLines()
0131 {
0132     AlkOnlineQuote::Private &p = d_ptr();
0133 
0134     QString quotedata =
0135         "22-01-24,1.0906\n"
0136         "21-01-24,1.1906\n"
0137         ;
0138     AlkDatePriceMap map;
0139     map[QDate::fromString("22-01-2024", "dd-MM-yyyy")] = 1.0906;
0140     map[QDate::fromString("21-01-2024", "dd-MM-yyyy")] = 1.1906;
0141     MultipleQuotesReceiver receiver(map);
0142     connect(this, SIGNAL(quotes(QString,QString,AlkDatePriceMap)), &receiver, SLOT(quotes(QString,QString,AlkDatePriceMap)));
0143     QVERIFY(p.parseQuoteCSV(quotedata));
0144 }
0145 
0146 void AlkOnlineQuotePrivateTest::testParseQuoteCSVDateRange()
0147 {
0148     AlkOnlineQuote::Private &p = d_ptr();
0149 
0150     QString quotedata =
0151         "22-01-24,1.0906\n"
0152         "21-01-24,1.1906\n"
0153         "20-01-24,1.4906\n"
0154         ;
0155     p.m_startDate = QDate::fromString("21-01-2024", "dd-MM-yyyy");
0156     p.m_endDate = QDate::fromString("21-01-2024", "dd-MM-yyyy");
0157 
0158     AlkDatePriceMap map;
0159     map[QDate::fromString("21-01-2024", "dd-MM-yyyy")] = 1.1906;
0160     MultipleQuotesReceiver multiReceiver(map);
0161     connect(this, SIGNAL(quotes(QString,QString,AlkDatePriceMap)), &multiReceiver, SLOT(quotes(QString,QString,AlkDatePriceMap)));
0162 
0163     SingleQuoteReceiver singleReceiver(p.m_startDate, 1.1906);
0164     connect(this, SIGNAL(quote(QString,QString,QDate,double)), &singleReceiver, SLOT(quote(QString,QString,QDate,double)));
0165 
0166     QVERIFY(p.parseQuoteCSV(quotedata));
0167 }
0168 
0169 #include "alkonlinequoteprivatetest.moc"