tests for Polar import
This commit is contained in:
@@ -34,6 +34,12 @@ from django.contrib.auth.models import User
|
|||||||
from django.contrib.auth.decorators import login_required
|
from django.contrib.auth.decorators import login_required
|
||||||
from django.urls import reverse, reverse_lazy
|
from django.urls import reverse, reverse_lazy
|
||||||
|
|
||||||
|
from rowers.utils import myqueue
|
||||||
|
import django_rq
|
||||||
|
queue = django_rq.get_queue('default')
|
||||||
|
queuelow = django_rq.get_queue('low')
|
||||||
|
queuehigh = django_rq.get_queue('high')
|
||||||
|
|
||||||
# Project
|
# Project
|
||||||
# from .models import Profile
|
# from .models import Profile
|
||||||
from rowingdata import rowingdata
|
from rowingdata import rowingdata
|
||||||
@@ -77,7 +83,6 @@ def get_token(code): # pragma: no cover
|
|||||||
|
|
||||||
try:
|
try:
|
||||||
headers = { 'Authorization': 'Basic %s' % base64.b64encode(auth_string) }
|
headers = { 'Authorization': 'Basic %s' % base64.b64encode(auth_string) }
|
||||||
print(headers,'aa')
|
|
||||||
except TypeError:
|
except TypeError:
|
||||||
headers = { 'Authorization': 'Basic %s' % base64.b64encode(
|
headers = { 'Authorization': 'Basic %s' % base64.b64encode(
|
||||||
bytes(auth_string,'utf-8')).decode('utf-8') }
|
bytes(auth_string,'utf-8')).decode('utf-8') }
|
||||||
@@ -203,27 +208,16 @@ def get_polar_workouts(user):
|
|||||||
dologging('polar.log',url)
|
dologging('polar.log',url)
|
||||||
dologging('polar.log',authorizationstring)
|
dologging('polar.log',authorizationstring)
|
||||||
dologging('polar.log',str(response.status_code))
|
dologging('polar.log',str(response.status_code))
|
||||||
dologging('polar.log',response.reason)
|
|
||||||
dologging('polar.log',response.text)
|
|
||||||
|
|
||||||
|
|
||||||
if response.status_code == 201:
|
if response.status_code == 201:
|
||||||
|
|
||||||
uploadoptions = {
|
|
||||||
'makeprivate':False,
|
|
||||||
}
|
|
||||||
|
|
||||||
bodyyaml = yaml.safe_dump(
|
|
||||||
uploadoptions,
|
|
||||||
default_flow_style=False
|
|
||||||
)
|
|
||||||
|
|
||||||
transactionid = response.json()['transaction-id']
|
transactionid = response.json()['transaction-id']
|
||||||
url = baseurl+'/users/{userid}/exercise-transactions/{transactionid}'.format(
|
url = baseurl+'/users/{userid}/exercise-transactions/{transactionid}'.format(
|
||||||
transactionid = transactionid,
|
transactionid = transactionid,
|
||||||
userid = r.polaruserid
|
userid = r.polaruserid
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
dologging('polar.log',url)
|
dologging('polar.log',url)
|
||||||
|
|
||||||
response = requests.get(url, headers=headers)
|
response = requests.get(url, headers=headers)
|
||||||
@@ -256,14 +250,14 @@ def get_polar_workouts(user):
|
|||||||
}
|
}
|
||||||
url = settings.UPLOAD_SERVICE_URL
|
url = settings.UPLOAD_SERVICE_URL
|
||||||
|
|
||||||
|
dologging('polar.log',json_data)
|
||||||
|
dologging('polar.log',url)
|
||||||
job = myqueue(queuehigh,
|
job = myqueue(queuehigh,
|
||||||
handle_request_post,
|
handle_request_post,
|
||||||
url,
|
url,
|
||||||
json_data
|
json_data
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
exercise_dict['filename'] = filename
|
exercise_dict['filename'] = filename
|
||||||
else:
|
else:
|
||||||
exercise_dict['filename'] = ''
|
exercise_dict['filename'] = ''
|
||||||
|
|||||||
@@ -757,7 +757,42 @@ def mocked_requests(*args, **kwargs):
|
|||||||
nkimpellerworkoutlist = json.load(infile)
|
nkimpellerworkoutlist = json.load(infile)
|
||||||
|
|
||||||
polar_json = {
|
polar_json = {
|
||||||
'available-user-data': []
|
"transaction-id":240522162,
|
||||||
|
"resource-uri":"https://polaraccesslink.com/v3/users/40273947/exercise-transactions/240522162"
|
||||||
|
}
|
||||||
|
|
||||||
|
polar_exercise_list = {
|
||||||
|
"exercises": [
|
||||||
|
"https://www.polaraccesslink.com/v3/users/12/exercise-transactions/34/exercises/56",
|
||||||
|
"https://www.polaraccesslink.com/v3/users/12/exercise-transactions/34/exercises/120"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
|
||||||
|
polar_exercise_dict = {
|
||||||
|
"id": 1937529874,
|
||||||
|
"upload-time": "2008-10-13T10:40:02Z",
|
||||||
|
"polar-user": "https://www.polaraccesslink/v3/users/1",
|
||||||
|
"transaction-id": 179879,
|
||||||
|
"device": "Polar M400",
|
||||||
|
"device-id": "1111AAAA",
|
||||||
|
"start-time": "2008-10-13T10:40:02Z",
|
||||||
|
"start-time-utc-offset": 180,
|
||||||
|
"duration": "PT2H44M",
|
||||||
|
"calories": 530,
|
||||||
|
"distance": 1600,
|
||||||
|
"heart-rate": {
|
||||||
|
"average": 129,
|
||||||
|
"maximum": 147
|
||||||
|
},
|
||||||
|
"training-load": 143.22,
|
||||||
|
"sport": "OTHER",
|
||||||
|
"has-route": True,
|
||||||
|
"club-id": 999,
|
||||||
|
"club-name": "Polar Club",
|
||||||
|
"detailed-sport-info": "WATERSPORTS_WATERSKI",
|
||||||
|
"fat-percentage": 60,
|
||||||
|
"carbohydrate-percentage": 38,
|
||||||
|
"protein-percentage": 2
|
||||||
}
|
}
|
||||||
|
|
||||||
with open('rowers/tests/testdata/c2workoutlist.txt') as f:
|
with open('rowers/tests/testdata/c2workoutlist.txt') as f:
|
||||||
@@ -997,6 +1032,15 @@ def mocked_requests(*args, **kwargs):
|
|||||||
garmintester = re.compile(r'.*?garmin\.com')
|
garmintester = re.compile(r'.*?garmin\.com')
|
||||||
fakturoidtester = re.compile(r'.*?fakturoid\.cz')
|
fakturoidtester = re.compile(r'.*?fakturoid\.cz')
|
||||||
|
|
||||||
|
polarlistregex = r'.*?polaraccesslink\.com\/.*\/(\d+)$'
|
||||||
|
polarlisttester = re.compile(polarlistregex)
|
||||||
|
|
||||||
|
polarexerciseregex = r'.*?polaraccesslink\.com\/.*\/(\d+)\/exercises\/(\d+)$'
|
||||||
|
polarexercisetester = re.compile(polarexerciseregex)
|
||||||
|
|
||||||
|
polartcxregex = r'.*?polaraccesslink\.com\/.*\/(\d+)\/tcx'
|
||||||
|
polartcxtester = re.compile(polartcxregex)
|
||||||
|
|
||||||
c2importregex = r'.*?concept2.com\/api\/users\/me\/results\/\d+'
|
c2importregex = r'.*?concept2.com\/api\/users\/me\/results\/\d+'
|
||||||
c2importtester = re.compile(c2importregex)
|
c2importtester = re.compile(c2importregex)
|
||||||
|
|
||||||
@@ -1091,8 +1135,20 @@ def mocked_requests(*args, **kwargs):
|
|||||||
return MockResponse(json_data,200)
|
return MockResponse(json_data,200)
|
||||||
|
|
||||||
if polartester.match(args[0]):
|
if polartester.match(args[0]):
|
||||||
|
if polartcxtester.match(args[0]):
|
||||||
|
filename = 'rowers/tests/testdata/polar_response.tcx'
|
||||||
|
return MockContentResponse(filename,200)
|
||||||
|
elif polarexercisetester.match(args[0]):
|
||||||
|
json_data = polar_exercise_dict
|
||||||
|
return MockResponse(json_data,200)
|
||||||
|
elif polarlisttester.match(args[0]):
|
||||||
|
json_data = polar_exercise_list
|
||||||
|
return MockResponse(json_data,200)
|
||||||
|
|
||||||
|
# getting user data
|
||||||
json_data = polar_json
|
json_data = polar_json
|
||||||
return MockResponse(json_data,200)
|
|
||||||
|
return MockResponse(json_data,201)
|
||||||
|
|
||||||
if tptester.match(args[0]):
|
if tptester.match(args[0]):
|
||||||
if 'token' in args[0]:
|
if 'token' in args[0]:
|
||||||
|
|||||||
@@ -16,6 +16,7 @@ from rowers import dataprep
|
|||||||
from rowers import tasks
|
from rowers import tasks
|
||||||
from rowers import c2stuff
|
from rowers import c2stuff
|
||||||
from rowers import stravastuff
|
from rowers import stravastuff
|
||||||
|
from rowers import polarstuff
|
||||||
import urllib
|
import urllib
|
||||||
import json
|
import json
|
||||||
|
|
||||||
@@ -755,6 +756,71 @@ class NKObjects(DjangoTestCase):
|
|||||||
w = Workout.objects.get(id=1)
|
w = Workout.objects.get(id=1)
|
||||||
#self.assertTrue(w.impeller)
|
#self.assertTrue(w.impeller)
|
||||||
|
|
||||||
|
|
||||||
|
@override_settings(TESTING=True)
|
||||||
|
class PolarObjects(DjangoTestCase):
|
||||||
|
def setUp(self):
|
||||||
|
self.c = Client()
|
||||||
|
self.u = User.objects.create_user('john',
|
||||||
|
'sander@ds.ds',
|
||||||
|
'koeinsloot')
|
||||||
|
|
||||||
|
self.u.first_name = 'John'
|
||||||
|
self.u.last_name = 'Sander'
|
||||||
|
self.u.save()
|
||||||
|
self.r = Rower.objects.create(user=self.u,gdproptin=True,surveydone=True,
|
||||||
|
gdproptindate=timezone.now()
|
||||||
|
)
|
||||||
|
|
||||||
|
self.r.polartoken = '12'
|
||||||
|
self.r.polarrefreshtoken = '123'
|
||||||
|
self.r.polartokenexpirydate = arrow.get(datetime.datetime.now()+datetime.timedelta(days=100)).datetime
|
||||||
|
|
||||||
|
|
||||||
|
self.r.save()
|
||||||
|
self.c.login(username='john',password='koeinsloot')
|
||||||
|
self.nu = datetime.datetime.now()
|
||||||
|
|
||||||
|
filename = 'rowers/tests/testdata/testdata.csv'
|
||||||
|
|
||||||
|
rr = rrower(hrmax=self.r.max,hrut2=self.r.ut2,
|
||||||
|
hrut1=self.r.ut1,hrat=self.r.at,
|
||||||
|
hrtr=self.r.tr,hran=self.r.an,ftp=self.r.ftp)
|
||||||
|
row = rdata(csvfile=filename,rower=rr)
|
||||||
|
totaldist = row.df['cum_dist'].max()
|
||||||
|
totaltime = row.df['TimeStamp (sec)'].max()-row.df['TimeStamp (sec)'].min()
|
||||||
|
totaltime = totaltime+row.df.loc[:,' ElapsedTime (sec)'].iloc[0]
|
||||||
|
|
||||||
|
|
||||||
|
hours = int(totaltime/3600.)
|
||||||
|
minutes = int((totaltime - 3600.*hours)/60.)
|
||||||
|
seconds = int(totaltime - 3600.*hours - 60.*minutes)
|
||||||
|
tenths = int(10*(totaltime - 3600.*hours - 60.*minutes - seconds))
|
||||||
|
|
||||||
|
duration = "%s:%s:%s.%s" % (hours,minutes,seconds,tenths)
|
||||||
|
|
||||||
|
|
||||||
|
workoutdate = row.rowdatetime.strftime('%Y-%m-%d')
|
||||||
|
workoutstarttime = row.rowdatetime.strftime('%H:%M:%S')
|
||||||
|
|
||||||
|
self.w = Workout.objects.create(
|
||||||
|
name='testworkout',workouttype='water',
|
||||||
|
user=self.r,date=self.nu.strftime('%Y-%m-%d'),
|
||||||
|
starttime=workoutstarttime,
|
||||||
|
startdatetime=row.rowdatetime,
|
||||||
|
duration=duration,distance=totaldist,
|
||||||
|
csvfilename=filename
|
||||||
|
)
|
||||||
|
|
||||||
|
@patch('rowers.polarstuff.requests.post', side_effect=mocked_requests)
|
||||||
|
@patch('rowers.polarstuff.requests.get', side_effect=mocked_requests)
|
||||||
|
def test_polar_auto_import(self, mock_get, mock_post):
|
||||||
|
self.r.polar_auto_import = True
|
||||||
|
self.r.save()
|
||||||
|
|
||||||
|
res = polarstuff.get_polar_workouts(self.r.user)
|
||||||
|
self.assertEqual(len(res),2)
|
||||||
|
|
||||||
#@pytest.mark.django_db
|
#@pytest.mark.django_db
|
||||||
@override_settings(TESTING=True)
|
@override_settings(TESTING=True)
|
||||||
class RP3Objects(DjangoTestCase):
|
class RP3Objects(DjangoTestCase):
|
||||||
|
|||||||
1
rowers/tests/testdata/polar_response.tcx
vendored
Normal file
1
rowers/tests/testdata/polar_response.tcx
vendored
Normal file
File diff suppressed because one or more lines are too long
@@ -260,7 +260,7 @@ from oauth2_provider.models import Application,Grant,AccessToken
|
|||||||
import django_rq
|
import django_rq
|
||||||
queue = django_rq.get_queue('default')
|
queue = django_rq.get_queue('default')
|
||||||
queuelow = django_rq.get_queue('low')
|
queuelow = django_rq.get_queue('low')
|
||||||
queuehigh = django_rq.get_queue('low')
|
queuehigh = django_rq.get_queue('high')
|
||||||
|
|
||||||
import redis
|
import redis
|
||||||
import threading
|
import threading
|
||||||
|
|||||||
Reference in New Issue
Block a user