add test for extquotes

* change url generation in extquotes
* fix bug with missing derivate values
This commit is contained in:
Evgenii Alekseev 2016-05-26 16:10:23 +03:00
parent b73fb19409
commit 6f09737f0f
5 changed files with 214 additions and 24 deletions

View File

@ -105,6 +105,7 @@ void ExtQuotes::setTicker(const QString _ticker)
qCDebug(LOG_LIB) << "Ticker" << _ticker;
m_ticker = _ticker;
initUrl();
}
@ -127,16 +128,6 @@ void ExtQuotes::readConfiguration()
}
bumpApi(AWEQAPI);
// init query
m_url = QUrl(YAHOO_QUOTES_URL);
QUrlQuery params;
params.addQueryItem(QString("format"), QString("json"));
params.addQueryItem(QString("env"),
QString("store://datatables.org/alltableswithkeys"));
params.addQueryItem(QString("q"),
QString(YAHOO_QUOTES_QUERY).arg(m_ticker));
m_url.setQuery(params);
}
@ -226,35 +217,51 @@ void ExtQuotes::quotesReplyReceived(QNetworkReply *reply)
// ask
value = jsonQuotes[QString("Ask")].toString().toDouble();
values[tag(QString("askchg"))]
= values[QString("ask")].toDouble() == 0.0
= values[tag(QString("ask"))].toDouble() == 0.0
? 0.0
: value - values[QString("ask")].toDouble();
values[tag(QString("percaskchg"))] = 100.0
* values[QString("askchg")].toDouble()
/ values[QString("ask")].toDouble();
: value - values[tag(QString("ask"))].toDouble();
values[tag(QString("percaskchg"))]
= 100.0 * values[tag(QString("askchg"))].toDouble()
/ values[tag(QString("ask"))].toDouble();
values[tag(QString("ask"))] = value;
// bid
value = jsonQuotes[QString("Bid")].toString().toDouble();
values[tag(QString("bidchg"))]
= values[QString("bid")].toDouble() == 0.0
= values[tag(QString("bid"))].toDouble() == 0.0
? 0.0
: value - values[QString("bid")].toDouble();
values[tag(QString("percbidchg"))] = 100.0
* values[QString("bidchg")].toDouble()
/ values[QString("bid")].toDouble();
: value - values[tag(QString("bid"))].toDouble();
values[tag(QString("percbidchg"))]
= 100.0 * values[tag(QString("bidchg"))].toDouble()
/ values[tag(QString("bid"))].toDouble();
values[tag(QString("bid"))] = value;
// last trade
value = jsonQuotes[QString("LastTradePriceOnly")].toString().toDouble();
values[tag(QString("pricechg"))]
= values[QString("price")].toDouble() == 0.0
= values[tag(QString("price"))].toDouble() == 0.0
? 0.0
: value - values[QString("price")].toDouble();
: value - values[tag(QString("price"))].toDouble();
values[tag(QString("percpricechg"))]
= 100.0 * values[QString("pricechg")].toDouble()
/ values[QString("price")].toDouble();
= 100.0 * values[tag(QString("pricechg"))].toDouble()
/ values[tag(QString("price"))].toDouble();
values[tag(QString("price"))] = value;
emit(dataReceived(values));
}
void ExtQuotes::initUrl()
{
// init query
m_url = QUrl(YAHOO_QUOTES_URL);
QUrlQuery params;
params.addQueryItem(QString("format"), QString("json"));
params.addQueryItem(QString("env"),
QString("store://datatables.org/alltableswithkeys"));
params.addQueryItem(QString("q"),
QString(YAHOO_QUOTES_QUERY).arg(m_ticker));
m_url.setQuery(params);
}

View File

@ -61,6 +61,7 @@ private:
QUrl m_url;
bool isRunning = false;
Ui::ExtQuotes *ui = nullptr;
void initUrl();
void translate();
// properties
QString m_ticker = QString("EURUSD=X");

View File

@ -17,6 +17,12 @@ set(EXTSCRIPT_SOURCES testextscript.cpp)
add_executable(${SUBPROJECT}-extscript ${EXTSCRIPT_HEADERS} ${EXTSCRIPT_SOURCES})
target_link_libraries(${SUBPROJECT}-extscript ${PROJECT_LIBRARY} ${Qt_LIBRARIES} ${Qt5Test_LIBRARIES})
add_test(NAME "ExtScript" COMMAND ${CMAKE_CURRENT_BINARY_DIR}/${SUBPROJECT}-extscript)
# extquotes
set(EXTQUOTES_HEADERS testextquotes.h)
set(EXTQUOTES_SOURCES testextquotes.cpp)
add_executable(${SUBPROJECT}-extquotes ${EXTQUOTES_HEADERS} ${EXTQUOTES_SOURCES})
target_link_libraries(${SUBPROJECT}-extquotes ${PROJECT_LIBRARY} ${Qt_LIBRARIES} ${Qt5Test_LIBRARIES})
add_test(NAME "ExtQuotes" COMMAND ${CMAKE_CURRENT_BINARY_DIR}/${SUBPROJECT}-extquotes)
# extupgrade
set(EXTUPGRADE_HEADERS testextupgrade.h)
set(EXTUPGRADE_SOURCES testextupgrade.cpp)

View File

@ -0,0 +1,123 @@
/***************************************************************************
* This file is part of awesome-widgets *
* *
* awesome-widgets is free software: you can redistribute it and/or *
* modify it under the terms of the GNU General Public License as *
* published by the Free Software Foundation, either version 3 of the *
* License, or (at your option) any later version. *
* *
* awesome-widgets is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with awesome-widgets. If not, see http://www.gnu.org/licenses/ *
***************************************************************************/
#include "testextquotes.h"
#include <QtTest>
#include "extquotes.h"
void TestExtQuotes::initTestCase()
{
extQuotes = new ExtQuotes(nullptr);
extQuotes->setInterval(1);
extQuotes->setTicker(ticker);
extQuotes->setNumber(0);
extQuotes->run();
}
void TestExtQuotes::cleanupTestCase()
{
delete extQuotes;
}
void TestExtQuotes::test_values()
{
QCOMPARE(extQuotes->interval(), 1);
QCOMPARE(extQuotes->number(), 0);
QCOMPARE(extQuotes->ticker(), ticker);
}
void TestExtQuotes::test_run()
{
// init spy
QSignalSpy spy(extQuotes, SIGNAL(dataReceived(const QVariantHash &)));
QVariantHash firstValue = extQuotes->run();
QCOMPARE(firstValue[extQuotes->tag(QString("ask"))].toDouble(), 0.0);
QCOMPARE(firstValue[extQuotes->tag(QString("bid"))].toDouble(), 0.0);
QCOMPARE(firstValue[extQuotes->tag(QString("price"))].toDouble(), 0.0);
// check values
QVERIFY(spy.wait(5000));
QList<QVariant> arguments = spy.takeFirst();
cache[QString("ask")]
= arguments.at(0).toHash()[extQuotes->tag(QString("ask"))];
cache[QString("bid")]
= arguments.at(0).toHash()[extQuotes->tag(QString("bid"))];
cache[QString("price")]
= arguments.at(0).toHash()[extQuotes->tag(QString("price"))];
for (auto type : types) {
qDebug() << "Test type" << type;
QVERIFY((cache[type].toDouble() > price.first)
&& (cache[type].toDouble() < price.second));
}
}
void TestExtQuotes::test_derivatives()
{
// init spy
QSignalSpy spy(extQuotes, SIGNAL(dataReceived(const QVariantHash &)));
QVariantHash firstValue = extQuotes->run();
// check values
QVERIFY(spy.wait(5000));
QList<QVariant> arguments = spy.takeFirst();
QVariantHash values;
values[QString("ask")]
= arguments.at(0).toHash()[extQuotes->tag(QString("ask"))];
values[QString("bid")]
= arguments.at(0).toHash()[extQuotes->tag(QString("bid"))];
values[QString("price")]
= arguments.at(0).toHash()[extQuotes->tag(QString("price"))];
for (auto type : types) {
qDebug() << "Test type" << type;
QCOMPARE(arguments.at(0)
.toHash()[extQuotes->tag(QString("%1chg").arg(type))]
.toDouble(),
(values[type].toDouble() - cache[type].toDouble()));
QWARN("Possible round error");
QCOMPARE(arguments.at(0)
.toHash()[extQuotes->tag(QString("perc%1chg").arg(type))]
.toDouble(),
100.0 * (values[type].toDouble() - cache[type].toDouble())
/ values[type].toDouble());
}
}
void TestExtQuotes::test_copy()
{
ExtQuotes *newExtQuotes = extQuotes->copy(QString("/dev/null"), 1);
QCOMPARE(newExtQuotes->interval(), extQuotes->interval());
QCOMPARE(newExtQuotes->ticker(), extQuotes->ticker());
QCOMPARE(newExtQuotes->number(), 1);
delete newExtQuotes;
}
QTEST_MAIN(TestExtQuotes);

View File

@ -0,0 +1,53 @@
/***************************************************************************
* This file is part of awesome-widgets *
* *
* awesome-widgets is free software: you can redistribute it and/or *
* modify it under the terms of the GNU General Public License as *
* published by the Free Software Foundation, either version 3 of the *
* License, or (at your option) any later version. *
* *
* awesome-widgets is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with awesome-widgets. If not, see http://www.gnu.org/licenses/ *
***************************************************************************/
#ifndef TESTEXTQUOTES_H
#define TESTEXTQUOTES_H
#include <QObject>
#include <QVariant>
class ExtQuotes;
class TestExtQuotes : public QObject
{
Q_OBJECT
private slots:
// initialization
void initTestCase();
void cleanupTestCase();
// test
void test_values();
void test_run();
void test_derivatives();
void test_copy();
private:
ExtQuotes *extQuotes = nullptr;
QVariantHash cache;
QString ticker = QString("EURUSD=X");
QStringList types = QStringList() << QString("ask") << QString("bid")
<< QString("price");
// we assume that price will not be differ more than in 2 times
QPair<double, double> price = QPair<double, double>(0.5, 2.0);
};
#endif /* TESTEXTQUOTES_H */