Skip to content

Commit

Permalink
Merge pull request #34 from jnsebgosselin/add_tests_for_search_weathe…
Browse files Browse the repository at this point in the history
…r_data

PR: Add tests for search weather data
  • Loading branch information
jnsebgosselin authored Sep 11, 2017
2 parents 229690f + bfa9f20 commit 9a1f412
Show file tree
Hide file tree
Showing 2 changed files with 159 additions and 30 deletions.
65 changes: 35 additions & 30 deletions WHAT/meteo/search_weather_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@

import xlsxwriter
import numpy as np
from PyQt5.QtCore import QObject, pyqtSignal, Qt, QPoint, QEvent, QThread
from PyQt5.QtCore import pyqtSignal as QSignal
from PyQt5.QtCore import QObject, Qt, QPoint, QEvent, QThread
from PyQt5.QtWidgets import (QWidget, QLabel, QDoubleSpinBox, QComboBox,
QFrame, QGridLayout, QTableWidget, QCheckBox,
QTabWidget, QSpinBox, QPushButton, QDesktopWidget,
Expand All @@ -55,22 +56,23 @@

class StationFinder(QObject):

searchFinished = pyqtSignal(list)
newStationFound = pyqtSignal(list)
ConsoleSignal = pyqtSignal(str)
searchFinished = QSignal(list)
newStationFound = QSignal(list)
ConsoleSignal = QSignal(str)

def __init__(self, parent=None):
super(StationFinder, self).__init__(parent)

self.prov = None
self.lat = None
self.lon = None
self.lat = 45.40
self.lon = 73.15
self.rad = 25
self.year_min = 1960
self.year_max = 2015
self.nbr_of_years = 5
self.search_by = 'proximity'
# options are: 'proximity' or 'province'
self.stationlist = []

self.stop_searching = False
self.isOffline = False
Expand All @@ -96,7 +98,7 @@ def search_envirocan(self):

Nmax = 100 # Number of results per page (maximu m possible is 100)

staList = []
self.stationlist = []
# [station_name, station_id, start_year,
# end_year, province, climate_id, station_proxim]

Expand Down Expand Up @@ -162,7 +164,7 @@ def search_envirocan(self):
msg = 'No weather station found.'
self.ConsoleSignal.emit('<font color=red>%s</font>' % msg)
print(msg)
self.searchFinished.emit(staList)
self.searchFinished.emit(self.stationlist)
return

# Go backward from indx_e and find where the number starts to
Expand All @@ -185,7 +187,7 @@ def search_envirocan(self):
staCount = 0 # global station counter
for page in range(Npage):
if self.stop_searching:
self.searchFinished.emit(staList)
self.searchFinished.emit(self.stationlist)
return

print('Page :', page)
Expand Down Expand Up @@ -280,7 +282,7 @@ def search_envirocan(self):
# ---- Climate ID ----

if self.stop_searching:
self.searchFinished.emit(staList)
self.searchFinished.emit(self.stationlist)
return

staInfo = self.get_staInfo(province, station_id)
Expand All @@ -291,12 +293,11 @@ def search_envirocan(self):
start_year, end_year, province,
climate_id, station_proxim]

staList.append(new_station)

if self.stop_searching:
self.searchFinished.emit(staList)
return staList
self.searchFinished.emit(self.stationlist)
return self.stationlist
else:
self.stationlist.append(new_station)
self.newStationFound.emit(new_station)
else:
print("Not adding %s (not enough data)"
Expand All @@ -309,7 +310,7 @@ def search_envirocan(self):

msg = ('%d weather stations with daily data for at least %d years'
' between %d and %d'
) % (len(staList), self.nbr_of_years,
) % (len(self.stationlist), self.nbr_of_years,
self.year_min, self.year_max)
self.ConsoleSignal.emit('<font color=green>%s</font>' % msg)
print(msg)
Expand All @@ -332,9 +333,9 @@ def search_envirocan(self):
print()

print('Searching for weather station is finished.')
self.searchFinished.emit(staList)
self.searchFinished.emit(self.stationlist)

return staList
return self.stationlist

def get_staInfo(self, Prov, StationID):
"""
Expand All @@ -353,7 +354,7 @@ def get_staInfo(self, Prov, StationID):
if self.isOffline:
with open('urlsinglestation.txt', 'r') as f:
urlread = f.read()
time.sleep(1)
time.sleep(0.25)
else:
f = urlopen(url)
urlread = f.read().decode('utf-8')
Expand Down Expand Up @@ -421,8 +422,8 @@ class Search4Stations(QWidget):
Government of Canada website.
'''

ConsoleSignal = pyqtSignal(str)
staListSignal = pyqtSignal(list)
ConsoleSignal = QSignal(str)
staListSignal = QSignal(list)

def __init__(self, parent=None):
super(Search4Stations, self).__init__()
Expand Down Expand Up @@ -805,8 +806,6 @@ def btn_addSta_isClicked(self):
msg = 'No station currently selected'
print(msg)

# -------------------------------------------------------------------------

def btn_search_isClicked(self):
"""
Initiate the seach for weather stations. It grabs the info from the
Expand All @@ -823,12 +822,13 @@ def btn_search_isClicked(self):
self.btn_search.setEnabled(False)
return

# Update UI state :
self.year_widg.setEnabled(False)
self.tab_widg.setEnabled(False)
self.btn_search.setIcon(IconDB().stop)
self.station_table.clearContents()

msg = 'Searching for weather stations. Please wait...'
self.ConsoleSignal.emit('<font color=black>%s</font>' % msg)

# Set the attributes of the finder:
self.finder.prov = self.prov
self.finder.lat = self.lat
self.finder.lon = self.lon
Expand All @@ -838,12 +838,13 @@ def btn_search_isClicked(self):
self.finder.nbr_of_years = self.nbr_of_years
self.finder.search_by = self.search_by

self.btn_search.setIcon(IconDB().stop)

self.station_table.clearContents()
# Start searching for weather station :
self.thread.started.connect(self.finder.search_envirocan)
self.thread.start()

msg = 'Searching for weather stations. Please wait...'
self.ConsoleSignal.emit('<font color=black>%s</font>' % msg)

def search_is_finished(self, station_list):
self.thread.quit()
waittime = 0
Expand All @@ -857,6 +858,7 @@ def search_is_finished(self, station_list):
print(msg)
self.ConsoleSignal.emit('<font color=red>%s</font>' % msg)
return
self.thread.started.disconnect(self.finder.search_envirocan)

# ---- Reset the UI ----

Expand Down Expand Up @@ -1226,8 +1228,11 @@ def dms2decdeg(deg, mnt, sec):

search4sta = Search4Stations()

search4sta.lat_spinBox.setValue(45.4)
search4sta.lon_spinBox.setValue(73.13)
search4sta.lat_spinBox.setValue(45.40)
search4sta.lon_spinBox.setValue(73.15)
search4sta.minYear.setValue(1980)
search4sta.maxYear.setValue(2015)
search4sta.nbrYear.setValue(20)
search4sta.finder.isOffline = False

search4sta.show()
Expand Down
124 changes: 124 additions & 0 deletions WHAT/tests/test_search_weather_data.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
# -*- coding: utf-8 -*-
"""
Created on Fri Aug 4 01:50:50 2017
@author: jsgosselin
"""

import pytest

import sys
import os.path
sys.path.append(os.path.dirname(os.path.dirname(os.path.realpath(__file__))))

# Local imports
from meteo.search_weather_data import Search4Stations

# Qt Test Fixtures
# --------------------------------


@pytest.fixture
def station_finder_bot(qtbot):
station_finder_widget = Search4Stations()
station_finder_widget.lat_spinBox.setValue(45.40)
station_finder_widget.lon_spinBox.setValue(73.15)
station_finder_widget.minYear.setValue(1960)
station_finder_widget.maxYear.setValue(2015)
station_finder_widget.nbrYear.setValue(10)

qtbot.addWidget(station_finder_widget)

return station_finder_widget, qtbot

# Tests
# -------------------------------


def test_station_finder(station_finder_bot):
station_finder_widget, qtbot = station_finder_bot
assert station_finder_widget


def test_search_weather_station(station_finder_bot):
station_finder_widget, qtbot = station_finder_bot
station_finder_widget.show()

expected_results = [
["MARIEVILLE", "5406", "1960", "2017", "QC", "7024627", "1.32"],
["ROUGEMONT", "5442", "1956", "1985", "QC", "7026700", "5.43"],
["IBERVILLE", "5376", "1963", "2016", "QC", "7023270", "10.86"],
["MONT ST HILAIRE", "5423", "1960", "1969", "QC", "7025330", "17.49"],
["L'ACADIE", "10843", "1994", "2017", "QC", "702LED4", "19.73"],
["SABREVOIS", "5444", "1975", "2017", "QC", "7026734", "20.76"],
["LAPRAIRIE", "5389", "1963", "2017", "QC", "7024100", "22.57"],
["FARNHAM", "5358", "1917", "2017", "QC", "7022320", "22.73"],
["STE MADELEINE", "5501", "1979", "2016", "QC", "7027517", "24.12"],
["MONTREAL/ST-HUBERT A", "5490", "1928", "2015", "QC", "7027320",
"24.85"]
]

# Search for stations and assert the results.
searchFinished = station_finder_widget.finder.searchFinished
with qtbot.waitSignal(searchFinished, raising=True, timeout=60000):
station_finder_widget.btn_search_isClicked()
results = station_finder_widget.finder.stationlist

assert results == expected_results

# Assert that the results are displayed correctly in the UI
assert (station_finder_widget.station_table.get_staList() ==
station_finder_widget.finder.stationlist)


def test_stop_search(station_finder_bot):
station_finder_widget, qtbot = station_finder_bot
station_finder_widget.show()

expected_results = [
["MARIEVILLE", "5406", "1960", "2017", "QC", "7024627", "1.32"],
["ROUGEMONT", "5442", "1956", "1985", "QC", "7026700", "5.43"],
["IBERVILLE", "5376", "1963", "2016", "QC", "7023270", "10.86"],
["MONT ST HILAIRE", "5423", "1960", "1969", "QC", "7025330", "17.49"],
["L'ACADIE", "10843", "1994", "2017", "QC", "702LED4", "19.73"],
["SABREVOIS", "5444", "1975", "2017", "QC", "7026734", "20.76"],
["LAPRAIRIE", "5389", "1963", "2017", "QC", "7024100", "22.57"],
["FARNHAM", "5358", "1917", "2017", "QC", "7022320", "22.73"],
["STE MADELEINE", "5501", "1979", "2016", "QC", "7027517", "24.12"],
["MONTREAL/ST-HUBERT A", "5490", "1928", "2015", "QC", "7027320",
"24.85"]
]

# Start the search.
newStationFound = station_finder_widget.finder.newStationFound
with qtbot.waitSignal(newStationFound, raising=True, timeout=60000):
station_finder_widget.btn_search_isClicked()

# Stop the search as soon as we received a result and assert the results.
searchFinished = station_finder_widget.finder.searchFinished
with qtbot.waitSignal(searchFinished, raising=True, timeout=60000):
station_finder_widget.btn_search_isClicked()
results = station_finder_widget.finder.stationlist

assert len(results) < len(expected_results)
assert results == expected_results[:len(results)]

# Assert that the results are displayed correctly in the UI
assert (station_finder_widget.station_table.get_staList() ==
station_finder_widget.finder.stationlist)

# Restart the search and let it fihish completely and assert the results.
searchFinished = station_finder_widget.finder.searchFinished
with qtbot.waitSignal(searchFinished, raising=True, timeout=60000):
station_finder_widget.btn_search_isClicked()
results = station_finder_widget.finder.stationlist

assert results == expected_results

# Assert that the results are displayed correctly in the UI
assert (station_finder_widget.station_table.get_staList() ==
station_finder_widget.finder.stationlist)


if __name__ == "__main__":
import os
pytest.main([os.path.basename(__file__)])

0 comments on commit 9a1f412

Please sign in to comment.