API: activities/records fix and tests update

This commit is contained in:
Sam 2018-05-16 14:02:54 +02:00
parent b0d6c50c76
commit 3047655e1f
5 changed files with 611 additions and 10 deletions

View File

@ -52,7 +52,8 @@ def update_records(activity, sport_id, connection, session):
record_table.c.id == record.id,
).values(
value=value,
activity_id=record_data['activity'].id
activity_id=record_data['activity'].id,
activity_date=record_data['activity'].activity_date,
))
else:
new_record = Record(

View File

@ -8,10 +8,13 @@ records_blueprint = Blueprint('records', __name__)
@records_blueprint.route('/records', methods=['GET'])
@authenticate
def get_sports(auth_user_id):
def get_records(auth_user_id):
"""Get all records for authenticated user"""
records = Record.query.filter_by(user_id=auth_user_id)\
.order_by(Record.record_type).all()
.order_by(
Record.sport_id,
Record.record_type,
).all()
records_list = []
for record in records:
records_list.append(record.serialize())

View File

@ -56,8 +56,10 @@ def create_activity(
update_activity_data(new_activity, gpx_data)
else:
new_activity.moving = duration
new_activity.ave_speed = new_activity.distance / (
duration.seconds / 3600)
new_activity.ave_speed = (None
if duration.seconds == 0
else float(new_activity.distance) /
(duration.seconds / 3600))
new_activity.max_speed = new_activity.ave_speed
return new_activity
@ -74,6 +76,7 @@ def create_segment(activity_id, segment_data):
def edit_activity(activity, activity_data):
if activity_data.get('sport_id'):
activity.sport_id = activity_data.get('sport_id')
if activity_data.get('title'):
activity.title = activity_data.get('title')
@ -87,8 +90,9 @@ def edit_activity(activity, activity_data):
activity.moving = activity.duration
if activity_data.get('distance'):
activity.distance = activity_data.get('distance')
activity.ave_speed = activity.distance / (
activity.duration.seconds / 3600)
activity.ave_speed = (None if activity.duration.seconds == 0
else float(activity.distance) /
(activity.duration.seconds / 3600))
activity.max_speed = activity.ave_speed
return activity

View File

@ -502,3 +502,57 @@ def test_add_an_activity_no_gpx_error(app, user_1, sport_1_cycling):
assert response.status_code == 500
assert 'fail' in data['status']
assert 'Error during activity save.' in data['message']
def test_add_activity_zero_value(
app, user_1, sport_1_cycling, sport_2_running
):
client = app.test_client()
resp_login = client.post(
'/api/auth/login',
data=json.dumps(dict(
email='test@test.com',
password='12345678'
)),
content_type='application/json'
)
response = client.post(
'/api/activities/no_gpx',
content_type='application/json',
data=json.dumps(dict(
sport_id=1,
duration=0,
activity_date='2018-05-14 14:05',
distance=0,
title='Activity test'
)),
headers=dict(
Authorization='Bearer ' + json.loads(
resp_login.data.decode()
)['auth_token']
)
)
data = json.loads(response.data.decode())
assert response.status_code == 201
assert 'created' in data['status']
assert len(data['data']['activities']) == 1
assert 'creation_date' in data['data']['activities'][0]
assert data['data']['activities'][0]['activity_date'] == 'Mon, 14 May 2018 14:05:00 GMT' # noqa
assert data['data']['activities'][0]['user_id'] == 1
assert data['data']['activities'][0]['sport_id'] == 1
assert data['data']['activities'][0]['duration'] is None
assert data['data']['activities'][0]['title'] == 'Activity test' # noqa
assert data['data']['activities'][0]['ascent'] is None
assert data['data']['activities'][0]['ave_speed'] is None
assert data['data']['activities'][0]['descent'] is None
assert data['data']['activities'][0]['distance'] is None
assert data['data']['activities'][0]['max_alt'] is None
assert data['data']['activities'][0]['max_speed'] is None
assert data['data']['activities'][0]['min_alt'] is None
assert data['data']['activities'][0]['moving'] is None
assert data['data']['activities'][0]['pauses'] is None
assert data['data']['activities'][0]['with_gpx'] is False
assert len(data['data']['activities'][0]['segments']) == 0
assert len(data['data']['activities'][0]['records']) == 0

View File

@ -3,7 +3,7 @@ import json
def test_get_records_for_authenticated_user(
app, user_1, user_2, sport_1_cycling, sport_2_running,
activity_cycling_user_1
activity_cycling_user_1, activity_cycling_user_2
):
client = app.test_client()
resp_login = client.post(
@ -55,3 +55,542 @@ def test_get_records_for_authenticated_user(
assert 1 == data['data']['records'][3]['activity_id']
assert 'MS' == data['data']['records'][3]['record_type']
assert 'value' in data['data']['records'][3]
def test_get_records_no_activities_user_1(
app, user_1, user_2, sport_1_cycling, sport_2_running,
activity_cycling_user_2
):
client = app.test_client()
resp_login = client.post(
'/api/auth/login',
data=json.dumps(dict(
email='test@test.com',
password='12345678'
)),
content_type='application/json'
)
response = client.get(
'/api/records',
headers=dict(
Authorization='Bearer ' + json.loads(
resp_login.data.decode()
)['auth_token']
)
)
data = json.loads(response.data.decode())
assert response.status_code == 200
assert 'success' in data['status']
assert len(data['data']['records']) == 0
def test_add_activity_zero_value(
app, user_1, sport_1_cycling, sport_2_running
):
client = app.test_client()
resp_login = client.post(
'/api/auth/login',
data=json.dumps(dict(
email='test@test.com',
password='12345678'
)),
content_type='application/json'
)
client.post(
'/api/activities/no_gpx',
content_type='application/json',
data=json.dumps(dict(
sport_id=1,
duration=0,
activity_date='2018-05-14 14:05',
distance=0,
title='Activity test'
)),
headers=dict(
Authorization='Bearer ' + json.loads(
resp_login.data.decode()
)['auth_token']
)
)
response = client.get(
'/api/records',
headers=dict(
Authorization='Bearer ' + json.loads(
resp_login.data.decode()
)['auth_token']
)
)
data = json.loads(response.data.decode())
assert response.status_code == 200
assert 'success' in data['status']
assert len(data['data']['records']) == 0
def test_get_records_after_activities_post_and_patch(
app, user_1, sport_1_cycling, sport_2_running
):
client = app.test_client()
resp_login = client.post(
'/api/auth/login',
data=json.dumps(dict(
email='test@test.com',
password='12345678'
)),
content_type='application/json'
)
client.post(
'/api/activities/no_gpx',
content_type='application/json',
data=json.dumps(dict(
sport_id=1,
duration=3600,
activity_date='2018-05-14 14:05',
distance=7,
title='Activity test 1'
)),
headers=dict(
Authorization='Bearer ' + json.loads(
resp_login.data.decode()
)['auth_token']
)
)
response = client.get(
'/api/records',
headers=dict(
Authorization='Bearer ' + json.loads(
resp_login.data.decode()
)['auth_token']
)
)
data = json.loads(response.data.decode())
assert response.status_code == 200
assert 'success' in data['status']
assert len(data['data']['records']) == 4
assert 'Mon, 14 May 2018 14:05:00 GMT' == data['data']['records'][0]['activity_date'] # noqa
assert 1 == data['data']['records'][0]['user_id']
assert 1 == data['data']['records'][0]['sport_id']
assert 1 == data['data']['records'][0]['activity_id']
assert 'AS' == data['data']['records'][0]['record_type']
assert 7.0 == data['data']['records'][0]['value']
assert 'Mon, 14 May 2018 14:05:00 GMT' == data['data']['records'][1]['activity_date'] # noqa
assert 1 == data['data']['records'][1]['user_id']
assert 1 == data['data']['records'][1]['sport_id']
assert 1 == data['data']['records'][1]['activity_id']
assert 'FD' == data['data']['records'][1]['record_type']
assert 7.0 == data['data']['records'][1]['value']
assert 'Mon, 14 May 2018 14:05:00 GMT' == data['data']['records'][2]['activity_date'] # noqa
assert 1 == data['data']['records'][2]['user_id']
assert 1 == data['data']['records'][2]['sport_id']
assert 1 == data['data']['records'][2]['activity_id']
assert 'LD' == data['data']['records'][2]['record_type']
assert '1:00:00' == data['data']['records'][2]['value']
assert 'Mon, 14 May 2018 14:05:00 GMT' == data['data']['records'][3]['activity_date'] # noqa
assert 1 == data['data']['records'][3]['user_id']
assert 1 == data['data']['records'][3]['sport_id']
assert 1 == data['data']['records'][3]['activity_id']
assert 'MS' == data['data']['records'][3]['record_type']
assert 7.0 == data['data']['records'][3]['value']
# Post activity with lower duration (same sport)
# => 2 new records: Average speed and Max speed
client.post(
'/api/activities/no_gpx',
content_type='application/json',
data=json.dumps(dict(
sport_id=1,
duration=3000,
activity_date='2018-05-15 14:05',
distance=7,
title='Activity test 2'
)),
headers=dict(
Authorization='Bearer ' + json.loads(
resp_login.data.decode()
)['auth_token']
)
)
response = client.get(
'/api/records',
headers=dict(
Authorization='Bearer ' + json.loads(
resp_login.data.decode()
)['auth_token']
)
)
data = json.loads(response.data.decode())
assert response.status_code == 200
assert 'success' in data['status']
assert len(data['data']['records']) == 4
assert 'Tue, 15 May 2018 14:05:00 GMT' == data['data']['records'][0]['activity_date'] # noqa
assert 1 == data['data']['records'][0]['user_id']
assert 1 == data['data']['records'][0]['sport_id']
assert 2 == data['data']['records'][0]['activity_id']
assert 'AS' == data['data']['records'][0]['record_type']
assert 8.4 == data['data']['records'][0]['value']
assert 'Mon, 14 May 2018 14:05:00 GMT' == data['data']['records'][1]['activity_date'] # noqa
assert 1 == data['data']['records'][1]['user_id']
assert 1 == data['data']['records'][1]['sport_id']
assert 1 == data['data']['records'][1]['activity_id']
assert 'FD' == data['data']['records'][1]['record_type']
assert 7.0 == data['data']['records'][1]['value']
assert 'Mon, 14 May 2018 14:05:00 GMT' == data['data']['records'][2]['activity_date'] # noqa
assert 1 == data['data']['records'][2]['user_id']
assert 1 == data['data']['records'][2]['sport_id']
assert 1 == data['data']['records'][2]['activity_id']
assert 'LD' == data['data']['records'][2]['record_type']
assert '1:00:00' == data['data']['records'][2]['value']
assert 'Tue, 15 May 2018 14:05:00 GMT' == data['data']['records'][0]['activity_date'] # noqa
assert 1 == data['data']['records'][0]['user_id']
assert 1 == data['data']['records'][0]['sport_id']
assert 2 == data['data']['records'][0]['activity_id']
assert 'MS' == data['data']['records'][3]['record_type']
assert 8.4 == data['data']['records'][3]['value']
# Post activity with no new records
client.post(
'/api/activities/no_gpx',
content_type='application/json',
data=json.dumps(dict(
sport_id=1,
duration=3500,
activity_date='2018-05-16 14:05',
distance=6.5,
title='Activity test 3'
)),
headers=dict(
Authorization='Bearer ' + json.loads(
resp_login.data.decode()
)['auth_token']
)
)
response = client.get(
'/api/records',
headers=dict(
Authorization='Bearer ' + json.loads(
resp_login.data.decode()
)['auth_token']
)
)
data = json.loads(response.data.decode())
assert response.status_code == 200
assert 'success' in data['status']
assert len(data['data']['records']) == 4
assert 'Tue, 15 May 2018 14:05:00 GMT' == data['data']['records'][0]['activity_date'] # noqa
assert 1 == data['data']['records'][0]['user_id']
assert 1 == data['data']['records'][0]['sport_id']
assert 2 == data['data']['records'][0]['activity_id']
assert 'AS' == data['data']['records'][0]['record_type']
assert 8.4 == data['data']['records'][0]['value']
assert 'Mon, 14 May 2018 14:05:00 GMT' == data['data']['records'][1]['activity_date'] # noqa
assert 1 == data['data']['records'][1]['user_id']
assert 1 == data['data']['records'][1]['sport_id']
assert 1 == data['data']['records'][1]['activity_id']
assert 'FD' == data['data']['records'][1]['record_type']
assert 7.0 == data['data']['records'][1]['value']
assert 'Mon, 14 May 2018 14:05:00 GMT' == data['data']['records'][2]['activity_date'] # noqa
assert 1 == data['data']['records'][2]['user_id']
assert 1 == data['data']['records'][2]['sport_id']
assert 1 == data['data']['records'][2]['activity_id']
assert 'LD' == data['data']['records'][2]['record_type']
assert '1:00:00' == data['data']['records'][2]['value']
assert 'Tue, 15 May 2018 14:05:00 GMT' == data['data']['records'][0]['activity_date'] # noqa
assert 1 == data['data']['records'][0]['user_id']
assert 1 == data['data']['records'][0]['sport_id']
assert 2 == data['data']['records'][0]['activity_id']
assert 'MS' == data['data']['records'][3]['record_type']
assert 8.4 == data['data']['records'][3]['value']
# Edit last activity
# 1 new record: Longest duration
client.patch(
'/api/activities/3',
content_type='application/json',
data=json.dumps(dict(
duration=4000,
)),
headers=dict(
Authorization='Bearer ' + json.loads(
resp_login.data.decode()
)['auth_token']
)
)
response = client.get(
'/api/records',
headers=dict(
Authorization='Bearer ' + json.loads(
resp_login.data.decode()
)['auth_token']
)
)
data = json.loads(response.data.decode())
assert response.status_code == 200
assert 'success' in data['status']
assert len(data['data']['records']) == 4
assert 'Tue, 15 May 2018 14:05:00 GMT' == data['data']['records'][0]['activity_date'] # noqa
assert 1 == data['data']['records'][0]['user_id']
assert 1 == data['data']['records'][0]['sport_id']
assert 2 == data['data']['records'][0]['activity_id']
assert 'AS' == data['data']['records'][0]['record_type']
assert 8.4 == data['data']['records'][0]['value']
assert 'Mon, 14 May 2018 14:05:00 GMT' == data['data']['records'][1]['activity_date'] # noqa
assert 1 == data['data']['records'][1]['user_id']
assert 1 == data['data']['records'][1]['sport_id']
assert 1 == data['data']['records'][1]['activity_id']
assert 'FD' == data['data']['records'][1]['record_type']
assert 7.0 == data['data']['records'][1]['value']
assert 'Wed, 16 May 2018 14:05:00 GMT' == data['data']['records'][2]['activity_date'] # noqa
assert 1 == data['data']['records'][2]['user_id']
assert 1 == data['data']['records'][2]['sport_id']
assert 3 == data['data']['records'][2]['activity_id']
assert 'LD' == data['data']['records'][2]['record_type']
assert '1:06:40' == data['data']['records'][2]['value']
assert 'Tue, 15 May 2018 14:05:00 GMT' == data['data']['records'][0]['activity_date'] # noqa
assert 1 == data['data']['records'][0]['user_id']
assert 1 == data['data']['records'][0]['sport_id']
assert 2 == data['data']['records'][0]['activity_id']
assert 'MS' == data['data']['records'][3]['record_type']
assert 8.4 == data['data']['records'][3]['value']
# delete activity 2 => AS and MS record update
client.delete(
'/api/activities/2',
headers=dict(
Authorization='Bearer ' + json.loads(
resp_login.data.decode()
)['auth_token']
)
)
response = client.get(
'/api/records',
headers=dict(
Authorization='Bearer ' + json.loads(
resp_login.data.decode()
)['auth_token']
)
)
data = json.loads(response.data.decode())
assert response.status_code == 200
assert 'success' in data['status']
assert len(data['data']['records']) == 4
assert 'Mon, 14 May 2018 14:05:00 GMT' == data['data']['records'][0]['activity_date'] # noqa
assert 1 == data['data']['records'][0]['user_id']
assert 1 == data['data']['records'][0]['sport_id']
assert 1 == data['data']['records'][0]['activity_id']
assert 'AS' == data['data']['records'][0]['record_type']
assert 7.0 == data['data']['records'][0]['value']
assert 'Mon, 14 May 2018 14:05:00 GMT' == data['data']['records'][1]['activity_date'] # noqa
assert 1 == data['data']['records'][1]['user_id']
assert 1 == data['data']['records'][1]['sport_id']
assert 1 == data['data']['records'][1]['activity_id']
assert 'FD' == data['data']['records'][1]['record_type']
assert 7.0 == data['data']['records'][1]['value']
assert 'Wed, 16 May 2018 14:05:00 GMT' == data['data']['records'][2]['activity_date'] # noqa
assert 1 == data['data']['records'][2]['user_id']
assert 1 == data['data']['records'][2]['sport_id']
assert 3 == data['data']['records'][2]['activity_id']
assert 'LD' == data['data']['records'][2]['record_type']
assert '1:06:40' == data['data']['records'][2]['value']
assert 'Mon, 14 May 2018 14:05:00 GMT' == data['data']['records'][3]['activity_date'] # noqa
assert 1 == data['data']['records'][3]['user_id']
assert 1 == data['data']['records'][3]['sport_id']
assert 1 == data['data']['records'][3]['activity_id']
assert 'MS' == data['data']['records'][3]['record_type']
assert 7.0 == data['data']['records'][3]['value']
# add an activity with the same data as activity 1 except with a later date
# => no change in record
client.post(
'/api/activities/no_gpx',
content_type='application/json',
data=json.dumps(dict(
sport_id=1,
duration=3600,
activity_date='2018-05-20 14:05',
distance=7,
title='Activity test 4'
)),
headers=dict(
Authorization='Bearer ' + json.loads(
resp_login.data.decode()
)['auth_token']
)
)
response = client.get(
'/api/records',
headers=dict(
Authorization='Bearer ' + json.loads(
resp_login.data.decode()
)['auth_token']
)
)
data = json.loads(response.data.decode())
assert response.status_code == 200
assert 'success' in data['status']
assert len(data['data']['records']) == 4
assert 'Mon, 14 May 2018 14:05:00 GMT' == data['data']['records'][0]['activity_date'] # noqa
assert 1 == data['data']['records'][0]['user_id']
assert 1 == data['data']['records'][0]['sport_id']
assert 1 == data['data']['records'][0]['activity_id']
assert 'AS' == data['data']['records'][0]['record_type']
assert 7.0 == data['data']['records'][0]['value']
assert 'Mon, 14 May 2018 14:05:00 GMT' == data['data']['records'][1]['activity_date'] # noqa
assert 1 == data['data']['records'][1]['user_id']
assert 1 == data['data']['records'][1]['sport_id']
assert 1 == data['data']['records'][1]['activity_id']
assert 'FD' == data['data']['records'][1]['record_type']
assert 7.0 == data['data']['records'][1]['value']
assert 'Wed, 16 May 2018 14:05:00 GMT' == data['data']['records'][2]['activity_date'] # noqa
assert 1 == data['data']['records'][2]['user_id']
assert 1 == data['data']['records'][2]['sport_id']
assert 3 == data['data']['records'][2]['activity_id']
assert 'LD' == data['data']['records'][2]['record_type']
assert '1:06:40' == data['data']['records'][2]['value']
assert 'Mon, 14 May 2018 14:05:00 GMT' == data['data']['records'][3]['activity_date'] # noqa
assert 1 == data['data']['records'][3]['user_id']
assert 1 == data['data']['records'][3]['sport_id']
assert 1 == data['data']['records'][3]['activity_id']
assert 'MS' == data['data']['records'][3]['record_type']
assert 7.0 == data['data']['records'][3]['value']
# add an activity with the same data as activity 1 except with
# an earlier date
# => record update (activity 5 replace activity 1)
client.post(
'/api/activities/no_gpx',
content_type='application/json',
data=json.dumps(dict(
sport_id=1,
duration=3600,
activity_date='2018-05-14 08:05',
distance=7,
title='Activity test 5'
)),
headers=dict(
Authorization='Bearer ' + json.loads(
resp_login.data.decode()
)['auth_token']
)
)
response = client.get(
'/api/records',
headers=dict(
Authorization='Bearer ' + json.loads(
resp_login.data.decode()
)['auth_token']
)
)
data = json.loads(response.data.decode())
assert response.status_code == 200
assert 'success' in data['status']
assert len(data['data']['records']) == 4
assert 'Mon, 14 May 2018 08:05:00 GMT' == data['data']['records'][0]['activity_date'] # noqa
assert 1 == data['data']['records'][0]['user_id']
assert 1 == data['data']['records'][0]['sport_id']
assert 5 == data['data']['records'][0]['activity_id']
assert 'AS' == data['data']['records'][0]['record_type']
assert 7.0 == data['data']['records'][0]['value']
assert 'Mon, 14 May 2018 08:05:00 GMT' == data['data']['records'][1]['activity_date'] # noqa
assert 1 == data['data']['records'][1]['user_id']
assert 1 == data['data']['records'][1]['sport_id']
assert 5 == data['data']['records'][1]['activity_id']
assert 'FD' == data['data']['records'][1]['record_type']
assert 7.0 == data['data']['records'][1]['value']
assert 'Wed, 16 May 2018 14:05:00 GMT' == data['data']['records'][2]['activity_date'] # noqa
assert 1 == data['data']['records'][2]['user_id']
assert 1 == data['data']['records'][2]['sport_id']
assert 3 == data['data']['records'][2]['activity_id']
assert 'LD' == data['data']['records'][2]['record_type']
assert '1:06:40' == data['data']['records'][2]['value']
assert 'Mon, 14 May 2018 08:05:00 GMT' == data['data']['records'][3]['activity_date'] # noqa
assert 1 == data['data']['records'][3]['user_id']
assert 1 == data['data']['records'][3]['sport_id']
assert 5 == data['data']['records'][3]['activity_id']
assert 'MS' == data['data']['records'][3]['record_type']
assert 7.0 == data['data']['records'][3]['value']
# delete all activities - no more records
client.delete(
'/api/activities/1',
headers=dict(
Authorization='Bearer ' + json.loads(
resp_login.data.decode()
)['auth_token']
)
)
client.delete(
'/api/activities/3',
headers=dict(
Authorization='Bearer ' + json.loads(
resp_login.data.decode()
)['auth_token']
)
)
client.delete(
'/api/activities/4',
headers=dict(
Authorization='Bearer ' + json.loads(
resp_login.data.decode()
)['auth_token']
)
)
client.delete(
'/api/activities/5',
headers=dict(
Authorization='Bearer ' + json.loads(
resp_login.data.decode()
)['auth_token']
)
)
response = client.get(
'/api/records',
headers=dict(
Authorization='Bearer ' + json.loads(
resp_login.data.decode()
)['auth_token']
)
)
data = json.loads(response.data.decode())
assert response.status_code == 200
assert 'success' in data['status']
assert len(data['data']['records']) == 0