mirror of
https://github.com/arcan1s/awesome-widgets.git
synced 2025-04-24 15:37:23 +00:00
add tests for extweather
This commit is contained in:
parent
6f09737f0f
commit
2a257de1e6
@ -134,6 +134,7 @@ void ExtWeather::setCity(const QString _city)
|
||||
qCDebug(LOG_LIB) << "City" << _city;
|
||||
|
||||
m_city = _city;
|
||||
initUrl();
|
||||
}
|
||||
|
||||
|
||||
@ -142,6 +143,7 @@ void ExtWeather::setCountry(const QString _country)
|
||||
qCDebug(LOG_LIB) << "Country" << _country;
|
||||
|
||||
m_country = _country;
|
||||
initUrl();
|
||||
}
|
||||
|
||||
|
||||
@ -185,16 +187,6 @@ void ExtWeather::readConfiguration()
|
||||
}
|
||||
|
||||
bumpApi(AWEWAPI);
|
||||
|
||||
// init query
|
||||
m_url = QUrl(YAHOO_WEATHER_URL);
|
||||
QUrlQuery params;
|
||||
params.addQueryItem(QString("format"), QString("json"));
|
||||
params.addQueryItem(QString("env"),
|
||||
QString("store://datatables.org/alltableswithkeys"));
|
||||
params.addQueryItem(QString("q"),
|
||||
QString(YAHOO_WEATHER_QUERY).arg(m_city, m_country));
|
||||
m_url.setQuery(params);
|
||||
}
|
||||
|
||||
|
||||
@ -359,6 +351,22 @@ void ExtWeather::weatherReplyReceived(QNetworkReply *reply)
|
||||
values[tag(QString("humidity"))] = 0;
|
||||
values[tag(QString("pressure"))] = 0.0;
|
||||
}
|
||||
|
||||
emit(dataReceived(values));
|
||||
}
|
||||
|
||||
|
||||
void ExtWeather::initUrl()
|
||||
{
|
||||
// init query
|
||||
m_url = QUrl(YAHOO_WEATHER_URL);
|
||||
QUrlQuery params;
|
||||
params.addQueryItem(QString("format"), QString("json"));
|
||||
params.addQueryItem(QString("env"),
|
||||
QString("store://datatables.org/alltableswithkeys"));
|
||||
params.addQueryItem(QString("q"),
|
||||
QString(YAHOO_WEATHER_QUERY).arg(m_city, m_country));
|
||||
m_url.setQuery(params);
|
||||
}
|
||||
|
||||
|
||||
|
@ -73,6 +73,7 @@ private:
|
||||
QUrl m_url;
|
||||
bool isRunning = false;
|
||||
Ui::ExtWeather *ui = nullptr;
|
||||
void initUrl();
|
||||
void translate();
|
||||
// properties
|
||||
QString m_city = QString("London");
|
||||
|
@ -29,4 +29,9 @@ set(EXTUPGRADE_SOURCES testextupgrade.cpp)
|
||||
add_executable(${SUBPROJECT}-extupgrade ${EXTUPGRADE_HEADERS} ${EXTUPGRADE_SOURCES})
|
||||
target_link_libraries(${SUBPROJECT}-extupgrade ${PROJECT_LIBRARY} ${Qt_LIBRARIES} ${Qt5Test_LIBRARIES})
|
||||
add_test(NAME "ExtUpgrade" COMMAND ${CMAKE_CURRENT_BINARY_DIR}/${SUBPROJECT}-extupgrade)
|
||||
|
||||
# extweather
|
||||
set(EXTWEATHER_HEADERS testextweather.h)
|
||||
set(EXTWEATHER_SOURCES testextweather.cpp)
|
||||
add_executable(${SUBPROJECT}-extweather ${EXTWEATHER_HEADERS} ${EXTWEATHER_SOURCES})
|
||||
target_link_libraries(${SUBPROJECT}-extweather ${PROJECT_LIBRARY} ${Qt_LIBRARIES} ${Qt5Test_LIBRARIES})
|
||||
add_test(NAME "ExtWeather" COMMAND ${CMAKE_CURRENT_BINARY_DIR}/${SUBPROJECT}-extweather)
|
||||
|
140
sources/test/testextweather.cpp
Normal file
140
sources/test/testextweather.cpp
Normal file
@ -0,0 +1,140 @@
|
||||
/***************************************************************************
|
||||
* 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 "testextweather.h"
|
||||
|
||||
#include <QtTest>
|
||||
|
||||
#include "extweather.h"
|
||||
|
||||
|
||||
void TestExtWeather::initTestCase()
|
||||
{
|
||||
extWeather = new ExtWeather(nullptr);
|
||||
extWeather->setInterval(1);
|
||||
extWeather->setCity(city);
|
||||
extWeather->setCountry(country);
|
||||
extWeather->setNumber(0);
|
||||
|
||||
extWeather->run();
|
||||
}
|
||||
|
||||
|
||||
void TestExtWeather::cleanupTestCase()
|
||||
{
|
||||
delete extWeather;
|
||||
}
|
||||
|
||||
|
||||
void TestExtWeather::test_values()
|
||||
{
|
||||
QCOMPARE(extWeather->interval(), 1);
|
||||
QCOMPARE(extWeather->number(), 0);
|
||||
QCOMPARE(extWeather->city(), city);
|
||||
QCOMPARE(extWeather->country(), country);
|
||||
}
|
||||
|
||||
|
||||
void TestExtWeather::test_run()
|
||||
{
|
||||
// init spy
|
||||
QSignalSpy spy(extWeather, SIGNAL(dataReceived(const QVariantHash &)));
|
||||
QVariantHash firstValue = extWeather->run();
|
||||
|
||||
// check values
|
||||
QVERIFY(spy.wait(5000));
|
||||
QVariantHash arguments = spy.takeFirst().at(0).toHash();
|
||||
QEXPECT_FAIL("", "WeatherID should not be 0", Continue);
|
||||
QCOMPARE(arguments[extWeather->tag(QString("weatherId"))].toInt(), 0);
|
||||
QVERIFY((arguments[extWeather->tag(QString("humidity"))].toInt()
|
||||
> humidity.first)
|
||||
&& (arguments[extWeather->tag(QString("humidity"))].toInt()
|
||||
< humidity.second));
|
||||
QEXPECT_FAIL("", "https://yahoo.uservoice.com/forums/207813-us-weather/"
|
||||
"suggestions/14209233-invalid-pressure-calculation",
|
||||
Continue);
|
||||
QVERIFY((arguments[extWeather->tag(QString("pressure"))].toFloat()
|
||||
> pressure.first)
|
||||
&& (arguments[extWeather->tag(QString("pressure"))].toInt()
|
||||
< pressure.second));
|
||||
QVERIFY((arguments[extWeather->tag(QString("temperature"))].toFloat()
|
||||
> temp.first)
|
||||
&& (arguments[extWeather->tag(QString("temperature"))].toInt()
|
||||
< temp.second));
|
||||
// image should be only one symbol here
|
||||
QCOMPARE(arguments[extWeather->tag(QString("weather"))].toString().count(),
|
||||
1);
|
||||
}
|
||||
|
||||
|
||||
void TestExtWeather::test_ts()
|
||||
{
|
||||
extWeather->setTs(1);
|
||||
// init spy
|
||||
QSignalSpy spy(extWeather, SIGNAL(dataReceived(const QVariantHash &)));
|
||||
QVariantHash firstValue = extWeather->run();
|
||||
|
||||
// check values
|
||||
QVERIFY(spy.wait(5000));
|
||||
QVariantHash arguments = spy.takeFirst().at(0).toHash();
|
||||
QEXPECT_FAIL("", "WeatherID should not be 0", Continue);
|
||||
QCOMPARE(arguments[extWeather->tag(QString("weatherId"))].toInt(), 0);
|
||||
QCOMPARE(arguments[extWeather->tag(QString("humidity"))].toInt(), 0);
|
||||
QCOMPARE(arguments[extWeather->tag(QString("pressure"))].toFloat(), 0.0f);
|
||||
QVERIFY((arguments[extWeather->tag(QString("temperature"))].toFloat()
|
||||
> temp.first)
|
||||
&& (arguments[extWeather->tag(QString("temperature"))].toInt()
|
||||
< temp.second));
|
||||
// image should be only one symbol here
|
||||
QCOMPARE(arguments[extWeather->tag(QString("weather"))].toString().count(),
|
||||
1);
|
||||
}
|
||||
|
||||
|
||||
void TestExtWeather::test_image()
|
||||
{
|
||||
extWeather->setImage(true);
|
||||
// init spy
|
||||
QSignalSpy spy(extWeather, SIGNAL(dataReceived(const QVariantHash &)));
|
||||
QVariantHash firstValue = extWeather->run();
|
||||
|
||||
// check values
|
||||
QVERIFY(spy.wait(5000));
|
||||
QVariantHash arguments = spy.takeFirst().at(0).toHash();
|
||||
QVERIFY(
|
||||
arguments[extWeather->tag(QString("weather"))].toString().startsWith(
|
||||
QString("<img")));
|
||||
}
|
||||
|
||||
|
||||
void TestExtWeather::test_copy()
|
||||
{
|
||||
ExtWeather *newExtWeather = extWeather->copy(QString("/dev/null"), 1);
|
||||
|
||||
QCOMPARE(newExtWeather->interval(), extWeather->interval());
|
||||
QCOMPARE(newExtWeather->city(), extWeather->city());
|
||||
QCOMPARE(newExtWeather->country(), extWeather->country());
|
||||
QCOMPARE(newExtWeather->ts(), extWeather->ts());
|
||||
QCOMPARE(newExtWeather->image(), extWeather->image());
|
||||
QCOMPARE(newExtWeather->number(), 1);
|
||||
|
||||
delete newExtWeather;
|
||||
}
|
||||
|
||||
|
||||
QTEST_MAIN(TestExtWeather);
|
56
sources/test/testextweather.h
Normal file
56
sources/test/testextweather.h
Normal file
@ -0,0 +1,56 @@
|
||||
/***************************************************************************
|
||||
* 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 TESTEXTWEATHER_H
|
||||
#define TESTEXTWEATHER_H
|
||||
|
||||
#include <QObject>
|
||||
#include <QVariant>
|
||||
|
||||
|
||||
class ExtWeather;
|
||||
|
||||
class TestExtWeather : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
private slots:
|
||||
// initialization
|
||||
void initTestCase();
|
||||
void cleanupTestCase();
|
||||
// test
|
||||
void test_values();
|
||||
void test_run();
|
||||
void test_ts();
|
||||
void test_image();
|
||||
void test_copy();
|
||||
|
||||
private:
|
||||
ExtWeather *extWeather = nullptr;
|
||||
QString city = QString("London");
|
||||
QString country = QString("uk");
|
||||
// humidity is in percents
|
||||
QPair<int, int> humidity = QPair<int, int>(0, 100);
|
||||
// pressure should be about 1 atm
|
||||
QPair<float, float> pressure = QPair<float, float>(500.0f, 1500.0f);
|
||||
// dont know about temperature, but I suppose it will be between -40 and 40
|
||||
QPair<float, float> temp = QPair<float, float>(-40.0f, 40.0f);
|
||||
};
|
||||
|
||||
|
||||
#endif /* TESTEXTWEATHER_H */
|
Loading…
Reference in New Issue
Block a user