mocking request to weather API
This commit is contained in:
@@ -610,6 +610,7 @@ def mocked_requests(*args, **kwargs):
|
||||
def __init__(self, json_data, status_code):
|
||||
self.json_data = json_data
|
||||
self.status_code = status_code
|
||||
self.ok = True
|
||||
|
||||
def json(self):
|
||||
return self.json_data
|
||||
|
||||
@@ -242,9 +242,9 @@ class WorkoutViewTest(TestCase):
|
||||
|
||||
@patch('rowers.dataprep.create_engine')
|
||||
@patch('rowers.dataprep.getsmallrowdata_db')
|
||||
# @patch('rowers.weather.requests.get', side_effect=mocked_requests)
|
||||
@patch('rowers.weather.requests.get', side_effect=mocked_requests)
|
||||
def test_waterworkout_view(self,
|
||||
mocked_sqlalchemy, mocked_getsmallrowdata_db):
|
||||
mocked_sqlalchemy, mocked_getsmallrowdata_db, mocked_requests):
|
||||
login = self.c.login(username=self.u.username, password=self.password)
|
||||
self.assertTrue(login)
|
||||
|
||||
|
||||
@@ -777,7 +777,7 @@ class PermissionsViewTests(TestCase):
|
||||
login = self.c.login(username=self.ucoach.username, password=self.ucoachpassword)
|
||||
self.assertTrue(login)
|
||||
|
||||
url = reverse('rower_favoritecharts_view',kwargs={'id':self.ubasic.id})
|
||||
url = reverse('rower_favoritecharts_view',kwargs={'userid':self.ubasic.id})
|
||||
|
||||
response = self.c.get(url)
|
||||
self.assertEqual(response.status_code,200)
|
||||
@@ -788,7 +788,7 @@ class PermissionsViewTests(TestCase):
|
||||
login = self.c.login(username=self.ucoach.username, password=self.ucoachpassword)
|
||||
self.assertTrue(login)
|
||||
|
||||
url = reverse('rower_favoritecharts_view',kwargs={'id':self.ubasic.id})
|
||||
url = reverse('rower_favoritecharts_view',kwargs={'userid':self.ubasic.id})
|
||||
|
||||
response = self.c.get(url)
|
||||
self.assertEqual(response.status_code,403)
|
||||
|
||||
@@ -2214,8 +2214,21 @@ def workout_smoothenpace_view(request,id=0,message="",successmessage=""):
|
||||
if row == 0:
|
||||
return HttpResponse("Error: CSV Data File Not Found")
|
||||
|
||||
pace = row.df[' Stroke500mPace (sec/500m)'].values
|
||||
velo = 500./pace
|
||||
try:
|
||||
pace = row.df[' Stroke500mPace (sec/500m)'].values
|
||||
velo = 500./pace
|
||||
except KeyError:
|
||||
messages.error(request,'Unable to find the data')
|
||||
if previousurl:
|
||||
url = previousurl
|
||||
else:
|
||||
url = reverse(r.defaultlandingpage,
|
||||
kwargs = {
|
||||
'id':id,
|
||||
}
|
||||
)
|
||||
|
||||
return HttpResponseRedirect(url)
|
||||
|
||||
if not 'originalvelo' in row.df:
|
||||
row.df['originalvelo'] = velo
|
||||
|
||||
@@ -4,6 +4,7 @@ from __future__ import print_function
|
||||
from __future__ import unicode_literals
|
||||
from __future__ import unicode_literals, absolute_import
|
||||
import requests
|
||||
from requests.exceptions import ConnectionError
|
||||
import json
|
||||
from lxml import objectify,etree
|
||||
import xml.etree.ElementTree as ET
|
||||
@@ -26,13 +27,16 @@ def get_airport_code(lat,lon):
|
||||
newlon = a.iloc[0]['lon']
|
||||
distance = geo_distance(lat,lon,newlat,newlon)
|
||||
return airport_code,newlat,newlon,distance
|
||||
|
||||
|
||||
# Get weather data from the DarkSky API
|
||||
def get_weather_data(long,lat,unixtime):
|
||||
url = "https://api.darksky.net/forecast/"+FORECAST_IO_KEY+"/"
|
||||
url += str(long)+","+str(lat)+","+str(unixtime)
|
||||
|
||||
s = requests.get(url)
|
||||
try:
|
||||
s = requests.get(url)
|
||||
except ConnectionError:
|
||||
return 0
|
||||
|
||||
if s.ok:
|
||||
return s.json()
|
||||
@@ -55,10 +59,14 @@ def get_metar_data(airportcode,unixtime):
|
||||
except:
|
||||
message = 'Failed to download METAR data'
|
||||
return [0,0,message,'','']
|
||||
|
||||
|
||||
|
||||
if s.ok:
|
||||
doc = etree.fromstring(s.content)
|
||||
try:
|
||||
doc = etree.fromstring(s.content)
|
||||
except AttributeError:
|
||||
message = 'METAR data content error'
|
||||
return [0,0,message,'',0]
|
||||
lengte = len(doc.xpath('data/METAR/station_id'))
|
||||
idnr = int(lengte/2)
|
||||
try:
|
||||
@@ -77,7 +85,7 @@ def get_metar_data(airportcode,unixtime):
|
||||
wind_knots = float(wind_speed)
|
||||
wind_ms = 0.514444*wind_knots
|
||||
wind_ms = int(10*wind_ms)/10.
|
||||
|
||||
|
||||
temperaturec = float(temp_c)
|
||||
temp_f = 32.+(9./5.)*temperaturec
|
||||
temp_f = str(int(10*temp_f)/10.)
|
||||
@@ -87,17 +95,19 @@ def get_metar_data(airportcode,unixtime):
|
||||
message += '. Wind: '+str(wind_ms)+' m/s ('+str(wind_knots)+' kt)'
|
||||
message +='. Wind Bearing: '+str(windbearing)+' degrees'
|
||||
# message +='\n'+rawtext
|
||||
|
||||
|
||||
return [wind_ms,windbearing,message,rawtext,timestamp]
|
||||
|
||||
|
||||
message = 'Failed to download METAR data'
|
||||
return [0,0,message,'',timestamp]
|
||||
|
||||
|
||||
|
||||
# Get wind data (and translate from knots to m/s)
|
||||
def get_wind_data(lat,long,unixtime):
|
||||
data = get_weather_data(lat,long,unixtime)
|
||||
summary = ''
|
||||
temperature = 20
|
||||
if data:
|
||||
try:
|
||||
# we are getting wind in mph
|
||||
@@ -107,11 +117,13 @@ def get_wind_data(lat,long,unixtime):
|
||||
windspeed = 0
|
||||
windbearing = 0
|
||||
|
||||
|
||||
try:
|
||||
airports = data['flags']['madis-stations']
|
||||
except KeyError:
|
||||
airports = ['unknown']
|
||||
|
||||
|
||||
try:
|
||||
temperature = data['currently']['temperature']
|
||||
# Temp is given in Fahrenheit, so convert to Celsius for Europeans
|
||||
@@ -121,6 +133,7 @@ def get_wind_data(lat,long,unixtime):
|
||||
temperature = 'unknown'
|
||||
temperaturec = 'unknown'
|
||||
|
||||
|
||||
try:
|
||||
summary = data['currently']['summary']
|
||||
except KeyError:
|
||||
@@ -128,15 +141,19 @@ def get_wind_data(lat,long,unixtime):
|
||||
else:
|
||||
windspeed = 0
|
||||
windbearing = 0
|
||||
summary = 'unknown'
|
||||
airports = ['unknown']
|
||||
temparature = 'unknown'
|
||||
temperaturec = 'unknown'
|
||||
message = 'Not able to get weather data'
|
||||
|
||||
# apply Hellman's coefficient for neutral air above human
|
||||
# inhabitated areas
|
||||
windspeed = windspeed*(0.1)**0.34
|
||||
windspeed = 0.01*int(100*windspeed)
|
||||
|
||||
|
||||
timestamp = arrow.get(unixtime).isoformat()
|
||||
|
||||
|
||||
message = 'Summary for your location at '+timestamp+': '+summary
|
||||
message += '. Temperature '+str(temperature)+'F/'+str(temperaturec)+'C'
|
||||
|
||||
|
||||
Reference in New Issue
Block a user